$(document).ready(function(){
	var Stats = {
		code : {
			icon  : "icon",
			text  : "text",
			image : "image"
		},
		url : "http://www.journeyman.cz/stats.php",
		send : function(code){
		   $.get('/stats.php', {action : code});
		}
	};


	var globalCarousel = null;
	function slideshow_initCallback(carousel){
		jQuery('.slide-nav a').hover(
			function()
			{
				carousel.stopAuto();
			},
			function()
			{
				carousel.startAuto();
			}
		);

		jQuery('.slide-nav a').bind('click',
			function()
			{
				Stats.send(Stats.code.icon + jQuery.jcarousel.intval(jQuery(this).text()));
				carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
				return false;
			}
		);

		jQuery('#vpred').bind('click',
		function(){
			carousel.next();
			return false;
		});
		jQuery('#zpet').bind('click',
		function(){
			carousel.prev();
			return false;
		});

		jQuery("#slideshow").hover(
			function()
			{
				 carousel.stopAuto();
			},
			function()
			{
				carousel.startAuto();
			}
		);

		jQuery("#slideshow a").click(
			function()
			{
				 Stats.send(Stats.code.image + ($(this).parent("li").prevAll().length+1));
			}
		);

		globalCarousel = carousel;
	};

	function slideshow_current(carousel, item, i, state, evt){
		var $p = $(".slide-nav li").length;
		var idx = carousel.index(i, $p) - 1;
		$(".slide-nav li").eq(idx).addClass("zde");
		$(".slide-nav li").eq(idx).siblings().removeClass("zde");
	};
	var size = 0;
	var actual = 0;

	var carousel = $("#slideshow").jcarousel({
		scroll: 1,
		wrap: "circular",
		auto: 5,
		easing: "linear",
		animation: 500,
		initCallback: slideshow_initCallback,
		itemLoadCallback: trigger,
		itemVisibleInCallback: {
			onBeforeAnimation: slideshow_current
		},
		buttonNextHTML: null,
		buttonPrevHTML: null
	});

	function trigger(carousel, state)
	{
		size = carousel.size();
		actual = carousel.first;
		actual %= size;
		var act = 0;
		act = actual;
		if(act == 0)
		{
			act = size;
		}
		if(act >= 0)
		{
			if($("#texty > div").eq(act-1).children("div").children("div").text().replace(/ /g, "").length == 0)
				$("#icon").hide();
			else
				$("#icon").show();
		}
	}


/* HTML pro zobrazení pop-up*/
	var click_html = "<div id=\"popup\"><div id=\"dialog\"><div class=\"dialmenu\"><button type=\"button\" class=\"close\">&times;</button></div><div id=\"dialbody\"><div class=\"text\"><p>Lorem Ipsum dolor sit amet</p></div></div></div></div>";
	$("body").append(click_html)


/* HTML obsah oken */
	var messages = [];
	var display = [];
	display.push()
	$("#texty > div").each(function(i)
	{
		messages[i+1] = $(this).html();
		if(messages[i+1].length > 0)
		{
			display[i+1] = true;
		}
		else
		{
			display.push(false);
		}

	});
	messages[0] = messages[messages.length-1];
	display[0] = display[display.length-1];
	var clickShow = {
		icon        : $("#icon"),
		dialog      : $("#dialog"),
		close       : $("#dialog .dialmenu .close"),
		menu        : $("#dialog .dialmenu"),
		body        : $("#dialog #dialbody"),
		activated   : false
	};

	if($.layout.name == "webkit")
	{
		clickShow.dialog.css("left", ((screen.availWidth - parseInt( clickShow.dialog.css("width"))) / 2) + "px");
	}

   clickShow.dialog.draggable({ disabled: true });
   clickShow.menu.mousedown(function()
   {
		clickShow.dialog.draggable({ disabled: false });
   });

   clickShow.menu.mouseup(function()
   {
		clickShow.dialog.draggable({ disabled: true });
   });

   var speed = 300;
   clickShow.icon.click(function()
   {
		if(!clickShow.activated)
		{
			var a = actual;
			if(a == 0)
				a = messages.length-1;
			Stats.send(Stats.code.text + a);
			clickShow.dialog.css("height", "auto").show().children("#dialbody").css({height : "auto", position : "static"})
												  .children("div.text").html(messages[actual]);
			var auto = clickShow.dialog.height();
			speed = auto*1.2;
			clickShow.dialog.css({height : 0, width : 0})
							.children("#dialbody").css("position", "absolute");
			clickShow.dialog.show()
							.animate(
								{
									width: "400px",
									left: "-=200px",
									top: "-=50px",
									height: auto+"px"
								}, speed);
			clickShow.body.css(
						  {
							left: "-200px"
						  })
						  .animate(
						  {
							left: "+=200px",
							height: auto
						  }, speed);
			clickShow.close.css("right", "-200px")
						   .animate(
							{
								right: "0px"
							}, speed);
			globalCarousel.stopAuto();
			clickShow.activated = true;
		}
   });
   clickShow.close.click(function()
   {
		globalCarousel.startAuto();
		clickShow.dialog.animate(
							{
								width: "0",
								left: "+=200px",
								top: "+=50px",
								height: "0"
							}, speed, function(){$(this).hide().children("#dialbody").css("position", "static");})
		clickShow.body.animate(
					{
						left: "-=200px"
					}, speed);
		clickShow.close.animate(
							{
								right: "-200px"
							}, speed);


		clickShow.activated = false;
   });
});
