$(document).ready
(
	function()
	{
		$('#logo object').click
		(
			function ()
			{
				window.location.href = '/sk/reklamne-predmety';
			}
		);
		/*
		$('#logo_swf').flash
		(
			{
				swf: $('#logo_swf span').html(),
				width: 180,
				height: 180
			}
		);
		*/
		/*
		$('#footer_swf').flash
		(
			{
				swf: $('#footer_swf span').html(),
				width: 853,
				height: 40
			}
		);
		*/
		$('#mycarousel').html('');
		
		$('#mycarousel').jcarousel
		(
			{
				size: mycarousel_itemList.length,
				itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback},
				initCallback: mycarousel_initCallback,
				auto: 2,
				scroll: 4,
				wrap: 'last'
			}
		);
		
		$('.ui-overlay, .ui-widget-overlay, , #overlay_content, .overlay_close').click
		(
			function ()
			{
				$('#overlay').css('display', 'none');
			}	
		);
		
		$('#next, #prev').click
		(
			function ()
			{
				open_image($(this).attr('rel'));
				
				return false;
			}
		);
	}
);

function mycarousel_itemLoadCallback(carousel, state)
{
	for (var i = carousel.first; i <= carousel.last; i++)
	{
		if (carousel.has(i))
		{
			continue;
		}
		
		if (i > mycarousel_itemList.length)
		{
			break;
		}
		
		carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i-1], carousel, i));
		position_of_images(i, mycarousel_itemList[i-1]);
	}
};

function mycarousel_initCallback(carousel)
{
	/**
	 * Disable autoscrolling if the user clicks the prev or next button.
	 */
	/*
	carousel.buttonNext.bind
	(
		'click',
		function()
		{
			carousel.startAuto(0);
		}
	);
	
	carousel.buttonPrev.bind
	(
		'click',
		function()
		{
			carousel.startAuto(0);
		}
	);
	*/
	
	/**
	 * Pause autoscrolling if the user moves with the cursor over the clip.
	 */
	carousel.clip.click
	(
		function()
		{
			carousel.stopAuto();
		}
	);
	
	$('.ui-overlay, .ui-widget-overlay, #overlay_content, .overlay_close').click
	(
		function ()
		{
			carousel.startAuto();
		}
	);
}

function position_of_images(i, image)
{
	if (image.width * 1 > image.height * 1)
	{
		width = '175';
		height = Math.round((175 * 100 / image.width) * image.height / 100);
	}
	else
	{
		height = '175';
		width = Math.round((175 * 100 / image.height) * image.width / 100);
	}
	
	var margin_top = '0';
	if (height < '175')
	{
		margin_top = (175 - height) / 2;
	}
	
	$('#image_' + i).css('margin-top', margin_top + 'px');
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item, carousel, i)
{
	return '<img src="' + item.url + '" alt="" onclick="open_image(\'' + item.id + '\');" width="158" id="image_' + i + '" />';
};

function open_image(theImage)
{
	var post = 'id=' + $('#page_id').html() + '&type=' + theImage;
	
	$.ajax
	(
		{
			type: 'GET',
			url: '/editor/scripts/ajax/image_loader.ajax.php',
			data: post,
			success: function(req)
			{
				var content = req.getElementsByTagName('current')[0].childNodes[0].nodeValue;
				var prev = req.getElementsByTagName('prev')[0].childNodes[0];
				var next = req.getElementsByTagName('next')[0].childNodes[0];
				
				if (next == undefined)
				{
					$('#next').css('display','none');
				}
				else
				{
					$('#next').attr('rel', next.nodeValue);
					$('#next').show();
				}
				
				if (prev == undefined)
				{
					$('#prev').css('display','none');
				}
				else
				{
					$('#prev').attr('rel', prev.nodeValue);
					$('#prev').show();
				}

				$('#overlay_content').html(content);
				
				if ($('#overlay').css('display') == 'none')
				{
					$('#overlay').show();
				}
			}
		}
	);
	
	return false;
}
