var active_img_index = 0;

var gallery_align = {};

function enableOnSelect()
{
	Common.CSSClass.remove(document.body,  'body_no-select');
	Common.Event.remove(document, 'selectstart', return_false);
}

function disableOnSelect()
{
	document.body.focus();

	Common.CSSClass.set(document.body,  'body_no-select');
	Common.Event.add(document, 'selectstart', return_false);
}

function show_viewer(index){

	active_img_index = index;

	var active_img = all_imgs[active_img_index];

	if (!active_img)
		return false;

	var oScrollPos = Common.getScrollXY();

	var oContainer = $('id_viewer_block');

	oContainer.style.display = '';
	$('id_viewer_pic').src = active_img.big_img;
	$('id_viewer_name').innerHTML = active_img.title;

	oContainer.style.left = ((document.body.offsetWidth / 2) - (oContainer.offsetWidth / 2)) + 'px';
	oContainer.style.top = (oScrollPos.y + 200) + 'px';

	return false;
}

function show_next_viewer(){

	active_img_index++;

	if (active_img_index >= all_imgs.length)
		active_img_index = 0;

	return show_viewer(active_img_index);
}

function close_viewer(){

	$('id_viewer_block').style.display = 'none';
	$('id_viewer_pic').src = '/i/blank.gif';

	return false;
}

function fGalleryMouseDownHandler(oEvent)
{
	var oContainer = $('id_viewer_block');

	if (!oContainer)
	{
		return false;
	}

	var oContainerPosition = Common.getAbsolutePos(oContainer);

	gallery_align = {x : oContainerPosition.x - oEvent.clientX, y : oContainerPosition.y - oEvent.clientY};

	disableOnSelect();

	Common.Event.add(document, 'mouseup', fGalleryMouseUpHandler);

	Common.Event.add(document, 'mousemove', fGalleryMouseMoveHandler);
}

function fGalleryMouseUpHandler(oEvent)
{
	enableOnSelect();

	Common.Event.remove(document, 'mousemove', fGalleryMouseMoveHandler);
}

function fGalleryMouseMoveHandler(oEvent)
{
	var oContainer = $('id_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';
}