var Gallery = {
	
	active_img_index : 0,

	first_id: 0,
	next_id: 0,
	prev_id: 0,
	last_id: 0,

	gid: 0,

	gallery_align : {},

	enableOnSelect : function()
	{
		Common.CSSClass.remove(document.body,  'body_no-select');
		Common.Event.remove(document, 'selectstart', return_false);
	},

	disableOnSelect : function()
	{
		document.body.focus();

		Common.CSSClass.set(document.body,  'body_no-select');
		Common.Event.add(document, 'selectstart', return_false);
	},

	showViewer : function(gid, index)
	{
	
		var args = 'mode=return_gallery_pager&gid=' + gid + '&id=' + index;
		var answ = Common.AJAX.getContent('POST', '/ajax.php', args);

		if (!answ)
		{
			return true;
		}

		eval(answ);

		var oScrollPos = Common.getScrollXY();
		var oPicture = $('gallery_viewer_pic');

		if (!oPicture)
		{
			return false;
		}

		this.gid = gid;

		oPicture.src = '/i/blank.gif';
		oPicture.width = pager.curr.width;
		oPicture.height = pager.curr.height;

		$('gallery_viewer_name').innerHTML = pager.curr.name;

		var oContainer = $('gallery_viewer_block');
		
		oContainer.style.display = 'block';
		oContainer.style.left = ( ( document.body.offsetWidth - oContainer.offsetWidth ) / 2 ) + 'px';
		oContainer.style.top = ( 50 + oScrollPos.y ) + 'px';
		
		this.first_id = pager.first.id;
		this.next_id = pager.next.id;
		this.prev_id = pager.prev.id;
		this.last_id = pager.last.id;

		oPicture.src = pager.curr.url;
		blockScreen(oContainer);

		return false;
	},

	showNextViewer : function()
	{	

		if (!this.gid || !this.first_id)
		{
			return false;
		}
		unBlockScreen();
		if (!this.next_id)
		{
			return this.showViewer(this.gid, this.first_id);
		}

		return this.showViewer(this.gid, this.next_id);
	},

	closeViewer : function()
	{
		$('gallery_viewer_block').style.display = 'none';
		$('gallery_viewer_pic').src = '/i/blank.gif';
   
		Common.Event.remove(document.body, 'mouseup', this.closeViewer);
		unBlockScreen();
		return false;
	},

	fGalleryMouseDownHandler : function(oEvent)
	{
		var oContainer = $('gallery_viewer_block');

		if (!oContainer)
		{
			return false;
		}

		var oContainerPosition = Common.getAbsolutePos(oContainer);

		gallery_align = {x : oContainerPosition.x - oEvent.clientX, y : oContainerPosition.y - oEvent.clientY};

		this.disableOnSelect();

		Common.Event.add(document, 'mouseup', this.fGalleryMouseUpHandler);
		Common.Event.add(document, 'mousemove', this.fGalleryMouseMoveHandler);
	},

	fGalleryMouseUpHandler : function(oEvent)
	{
		this.enableOnSelect();

		Common.Event.remove(document, 'mousemove', this.fGalleryMouseMoveHandler);
	},

	fGalleryMouseMoveHandler : function(oEvent)
	{
		var oContainer = $('gallery_viewer_block');

		if (!oContainer)
		{
			return false;
		}

		var x = oEvent.clientX + gallery_align.x,
			y = oEvent.clientY + gallery_align.y;

		if ((x + oContainer.offsetWidth) >= document.body.offsetWidth)
		{
			x = document.body.offsetWidth - oContainer.offsetWidth;
		}
		else if (x < 0)
		{
			x = 0;
		}

		if ((y + oContainer.offsetHeight) >= document.body.offsetHeight)
		{
			y = document.body.offsetHeight - oContainer.offsetHeight;
		}
		else if (y < 0)
		{
			y = 0;
		}

		oContainer.style.left = (x) + 'px';
		oContainer.style.top = (y) + 'px';
	},
	
	resize: function() {
		var oContainer = $('gallery_viewer_block');
		if (oContainer && oContainer.style.display != "none") {
			oContainer.style.left = ( ( document.body.offsetWidth - oContainer.offsetWidth ) / 2 ) + 'px';
		}
	}

};