/*
* Author:		Lukáš Hurych
* Mail:			lukas.hurych@gmail.com
* Date: 		08-11-2009
* Version:		2.5
*/

//-- Nadpisy - u vícestránkových sekcí zůstávají, mění se pouze content
//-- Opakovat animaci létajících objektů
//-- Nová verze scriptu
    //-- neřešila by rozdíl mezi normálním contentem a novinkami - na php odeslán $.get, který by vrátil zpátky obsah -> novinky, normální content.. -> menší zatíženost klienta
    //-- univerzálnost #hash adres - IE + others

//-- after loading
$(document).ready(function()
{
	if ($.browser.msie && $.browser.version === "6.0")
	{
		$("div#container").css("position", "absolute");
		$("div#slideShow").hide();
	}
});

var divA = "";

//-- CORE
var Core =
{
	init: function()
	{
		Resize.logo();
		Resize.page();
		this.load();
		Content.preload(Animation.init());
		//Animation.init();
		Content.init();
	},
	
	load: function()
	{
		if ($.browser.msie && $.browser.version === "7.0")
		{
			$("div#menu ul").load("./pages/menuIE.php");
		}
		$("div#mapPaper").hide();
		$("div#logo").click(function()
		{
			$("div#mapPaper").fadeOut(500);
			window.location.hash = "#";
			divA = "";
		});
		$("div#logo").css("cursor", "pointer");
		$(window).resize(function()
		{
			Resize.logo();
			Resize.page();
			//$("div#container").css("zoom", );
		});
		
	}
}

//-- RESIZE
var Resize =
{
	vyska: $(window).height(), //-- globálně použitelné, změna při window.resize
	sirka: $(window).width(),
	
	logo: function()
	{
		/*var vyska = $(window).height();
		var logoTop = (-vyska+850);
		if (logoTop <= 0)
		{
			logoTop = 0;
		}
		else if (logoTop >= 75)
		{
			logoTop = 75;
		}
		$("div#logo").css("top", logoTop);*/
	},
	
	page: function()
	{
		var vyska = $(window).height();
		var sirka = $(window).width();
		
		$("div#container").css("margin-left", "-620px");
		$("div#container").css("left", "50%");
		$("div#container").css("position", "absolute");
		
		if (sirka <= 985)
		{
			$("body").css("overflow-x", "auto");
		}
		else
		{
			$("body").css("overflow-x", "hidden");
		}
		
		/*var zoom = vyska/1000+0.15;
		
		if (zoom >= 1 )
		{
			zoom = 1;
		}
		
		$("div#container").css("zoom", zoom);*/
		
		/*var containerTop = -1*(-vyska+850);
		if (containerTop >= 0)
		{
			containerTop = 0;
		}
		else if (containerTop <= -70)
		{
			containerTop = -70;
		}
		//containerTop = 0; //!!!TEST
		$("div#container").css("top", containerTop);*/
	}
}

//-- CONTENT
var Content =
{
	init: function()
	{
		Content.change();
		Content.slideshow();
		//Content.title();
		//Content.tooltip();
		Content.newsBox();
		Content.newsBox();
	},
	
	change: function()
	{
		//var divA = "";
		
		//var options = "div#menu a, div#contentJidelniListekSekce a, div#contentJidelniListek a, div.contentFotogaleriePage a, div#contentDenniListek a, div.pagination a, ";
        var options = "div#menu a, div#contentJidelniListekSekce a, div#contentJidelniListek a, div.contentFotogaleriePage a, div.pagination a, ";
		var optionsNews = "div#newsBox a";
        
		$(options + optionsNews).livequery('click',function()
		{
            if ($(this).attr("class") === "newsBoxPost")
            {
                var verNews = true;
                //-- facebook verification
                if ($(this).attr("title") === "facebook")
                {
                    window.location = "http://www.facebook.com/pages/Praha-Czech-Republic/Letem-Svetem-Panoramaticky-Restaurant/182994460847?ref=ts";
                }
            }
            else
            {
                var verNews = false;
            }
            
			var toLoad = $(this).attr('href');
			toLoad = toLoad.substr(1);
			if ($.browser.msie && $.browser.version === "7.0")
			{
				toLoad = toLoad.substr(27); //28
			}
			
			//$("div#container, div#menu a").click($.fn.fancybox.close);
			
			window.location.hash = "#" + toLoad;
			
			if (divA === "")
			{
				if (verNews === false)
                {
                    //alert("nic není otevřeno - načtení");
				    //$("div#logo").fadeOut(500);
				    $("div#contentContainer").html("");
				    $("div#contentContainer").load("./pages/content/" + toLoad + ".php", function()
				    {
    					Content.paginationHide();
    					Content.pagination();
    					Content.title();
    					//Content.tooltip();
    				});
    			
    				$("div#mapPaper").fadeIn(500);
    				divA = toLoad;
                }
                else
                {
                    //alert("novinka - první content");
                    if ($.browser.msie && $.browser.version === "7.0")
                    {
                        var toLoadTemp = toLoad.substr(8);
                    }
                    else
                    {
                        var toLoadTemp = toLoad.substr(9);
                    }
                    //var newsTemp = "index.php?c=novinky&s=" + toLoadTemp + "&news=post";
                    
                    $.get('index.php', {c: 'novinky', s: toLoadTemp, news: 'post'}, function(news)
                    {
                        $("div#contentContainer").html(news);
                        Content.paginationHide();
                        Content.pagination();
                        $("div#mapPaper").fadeIn(500);
                    });
                    
                    $(document).attr("title", "Novinky | Letem-Světem.com");
                    
                    divA = toLoad;
                }
			}
			else if (divA === toLoad)
			{
                //alert("je otevřená stejná položka - zavření");
				$("div#mapPaper").fadeOut(500);
				//$("div#logo").fadeIn(500);
				divA = "";
			}
			else
			{
				if (verNews === false)
                {
                //alert("je otevřená jiná položka - změna");
				$("div#contentContainer").fadeOut(500, function()
				{
					$("div#contentContainer").load("./pages/content/" + toLoad + ".php", function()
					{
						Content.paginationHide();
						Content.pagination();
						Content.title();
						//Content.tooltip();
						$("div#contentContainer").fadeIn(500);
					});
				});
				divA = toLoad;
                }
                else
                {
                    if ($.browser.msie && $.browser.version === "7.0")
                    {
                        var toLoadTemp = toLoad.substr(8);
                    }
                    else
                    {
                        var toLoadTemp = toLoad.substr(9);
                    }
                    
                    //var newsTemp = "index.php?c=novinky&s=" + toLoadTemp + "&news=post";
                    
                    $.get('index.php', {c: 'novinky', s: toLoadTemp, news: 'post'}, function(news)
                    {
                        //$("div#mapPaper").fadeIn(500);
                        $("div#contentContainer").fadeOut(500, function()
                        {
                            $("div#contentContainer").html(news);
                            Content.paginationHide();
                            Content.pagination();
                            $(document).attr("title", "Novinky | Letem-Světem.com");
                            $("div#contentContainer").fadeIn(500);
                        });
                    });
                    
                    divA = toLoad;
                }
			}
			
			return false;
		});
		
		var hash = window.location.hash.substr(1);
		if (hash !== "")
		{
            $("div#mapPaper").show();
			//$("div#logo").hide();
            
            //hashTemp = hash.substr(1);
            
            if ($.browser.msie && $.browser.version === "7.0")
			{
				hashTemp = hash;
			}
            else
            {
                hashTemp = hash.substr(1);
            }
            
            if (hashTemp.substr(0, 7) === 'novinky')
            {
                if ($.browser.msie && $.browser.version === "7.0")
                {
				    var toLoadTemp = hash.substr(8);
                }
                else
                {
                    var toLoadTemp = hash.substr(9);
                }
                
                //var newsTemp = "index.php?c=novinky&s=" + toLoadTemp + "&news=post";
                
                $.get('index.php', {c: 'novinky', s: toLoadTemp, news: 'post'}, function(news)
                {
                    $("div#contentContainer").html(news);
                    Content.paginationHide();
                    Content.pagination();
                    $("div#mapPaper").fadeIn(500);
                    $(document).attr("title", "Novinky | Letem-Světem.com");
                });
                
                divA = hashTemp;
            }
            else
            {
                $("div#contentContainer").load("./pages/content/" + hash + ".php", function()
                {
    				Content.paginationHide();
    				Content.pagination();
    				Content.title();
    				//Content.tooltip();
    			});
    			divA = hash;
            }
		}
	},
	
	pagination: function()
	{
		var divB = divA.substr(1)
		if ($.browser.msie && $.browser.version === "7.0")
		{
			divB = divA
		}
		switch (divB)
		{
			//-- fotogalerie
			case "fotogalerie":
			$("div.pagination").html(' \
			<a href="./fotogalerie"><img src="./images/galerieA.png" /></a> \
			<a href="./fotogalerie2"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "fotogalerie2":
			$("div.pagination").html(' \
			<a href="./fotogalerie"><img src="./images/galerieI.png" /></a> \
			<a href="./fotogalerie2"><img src="./images/galerieA.png" /></a> \
			');
			break;
			
			//-- přílohy
			case "jidelni-listek/prilohy":
			$("div.pagination").html(' \
			<a href="./jidelni-listek/prilohy"><img src="./images/galerieA.png" /></a> \
			<a href="./jidelni-listek/prilohy2"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "jidelni-listek/prilohy2":
			$("div.pagination").html(' \
			<a href="./jidelni-listek/prilohy"><img src="./images/galerieI.png" /></a> \
			<a href="./jidelni-listek/prilohy2"><img src="./images/galerieA.png" /></a> \
			');
			break;
			
			//-- těstoviny a risotta
			case "jidelni-listek/testoviny-a-risotta":
			$("div.pagination").html(' \
			<a href="./jidelni-listek/testoviny-a-risotta"><img src="./images/galerieA.png" /></a> \
			<a href="./jidelni-listek/testoviny-a-risotta2"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "jidelni-listek/testoviny-a-risotta2":
			$("div.pagination").html(' \
			<a href="./jidelni-listek/testoviny-a-risotta"><img src="./images/galerieI.png" /></a> \
			<a href="./jidelni-listek/testoviny-a-risotta2"><img src="./images/galerieA.png" /></a> \
			');
			break;
			
			//-- z domova i ze světa
			case "jidelni-listek/z-domova-i-ze-sveta":
			$("div.pagination").html(' \
			<a href="./jidelni-listek/z-domova-i-ze-sveta"><img src="./images/galerieA.png" /></a> \
			<a href="./jidelni-listek/z-domova-i-ze-sveta2"><img src="./images/galerieI.png" /></a> \
			<a href="./jidelni-listek/z-domova-i-ze-sveta3"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "jidelni-listek/z-domova-i-ze-sveta2":
			$("div.pagination").html(' \
			<a href="./jidelni-listek/z-domova-i-ze-sveta"><img src="./images/galerieI.png" /></a> \
			<a href="./jidelni-listek/z-domova-i-ze-sveta2"><img src="./images/galerieA.png" /></a> \
			<a href="./jidelni-listek/z-domova-i-ze-sveta3"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "jidelni-listek/z-domova-i-ze-sveta3":
			$("div.pagination").html(' \
			<a href="./jidelni-listek/z-domova-i-ze-sveta"><img src="./images/galerieI.png" /></a> \
			<a href="./jidelni-listek/z-domova-i-ze-sveta2"><img src="./images/galerieI.png" /></a> \
			<a href="./jidelni-listek/z-domova-i-ze-sveta3"><img src="./images/galerieA.png" /></a> \
			');
			break;
			
			//-- side dishes
			case "menu/side-dishes":
			$("div.pagination").html(' \
			<a href="./menu/side-dishes"><img src="./images/galerieA.png" /></a> \
			<a href="./menu/side-dishes2"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "menu/side-dishes2":
			$("div.pagination").html(' \
			<a href="./menu/side-dishes"><img src="./images/galerieI.png" /></a> \
			<a href="./menu/side-dishes2"><img src="./images/galerieA.png" /></a> \
			');
			break;
			
			//-- pasta and risotto
			case "menu/pasta-and-risotto":
			$("div.pagination").html(' \
			<a href="./menu/pasta-and-risotto"><img src="./images/galerieA.png" /></a> \
			<a href="./menu/pasta-and-risotto2"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "menu/pasta-and-risotto2":
			$("div.pagination").html(' \
			<a href="./menu/pasta-and-risotto"><img src="./images/galerieI.png" /></a> \
			<a href="./menu/pasta-and-risotto2"><img src="./images/galerieA.png" /></a> \
			');
			break;
			
			//-- from home and abroad
			case "menu/from-home-and-abroad":
			$("div.pagination").html(' \
			<a href="./menu/from-home-and-abroad"><img src="./images/galerieA.png" /></a> \
			<a href="./menu/from-home-and-abroad2"><img src="./images/galerieI.png" /></a> \
			<a href="./menu/from-home-and-abroad3"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "menu/from-home-and-abroad2":
			$("div.pagination").html(' \
			<a href="./menu/from-home-and-abroad"><img src="./images/galerieI.png" /></a> \
			<a href="./menu/from-home-and-abroad2"><img src="./images/galerieA.png" /></a> \
			<a href="./menu/from-home-and-abroad3"><img src="./images/galerieI.png" /></a> \
			');
			break;
			
			case "menu/from-home-and-abroad3":
			$("div.pagination").html(' \
			<a href="./menu/from-home-and-abroad"><img src="./images/galerieI.png" /></a> \
			<a href="./menu/from-home-and-abroad2"><img src="./images/galerieI.png" /></a> \
			<a href="./menu/from-home-and-abroad3"><img src="./images/galerieA.png" /></a> \
			');
			break;
			
			default:
			$("div.pagination").html("");
		}
	},
	
	paginationHide: function()
	{
		$("div.contentFotogaleriePage").html("");
		$("div.contentJidelak").html("");
	},
	
	slideshow: function()
	{
		$('#slideShowItem1').crossSlide({
			sleep: 2,
			fade: 1
		}, [
		{ src: './images/slide/1.png' },
		{ src: './images/slide/2.png' },
		{ src: './images/slide/3.png' },
		{ src: './images/slide/4.png' },
		{ src: './images/slide/5.png' },
		{ src: './images/slide/6.png' },
		{ src: './images/slide/7.png' },
		{ src: './images/slide/8.png' },
		{ src: './images/slide/9.png' },
		{ src: './images/slide/10.png' },
		{ src: './images/slide/11.png' },
		{ src: './images/slide/12.png' },
		{ src: './images/slide/13.png' }
		]);
	},
	
	title: function()
	{
		if ($.browser.msie && $.browser.version === "7.0")
		{
			var titleTemp = divA.split('/');
            
			if (titleTemp === "")
			{
				var titleTempT = "Restaurace Praha | Italské jídlo | Francouzské jídlo | Letem-světem.com";
			}
			if (titleTemp[1] === 'jidelni-listek')
			{
				titleTemp = "index.php?c=" + titleTemp[0] + "&s=" + titleTemp[1] + "&title=1";
			}
			else if (titleTemp[1] === 'novinky')
			{
				titleTemp = "index.php?c=" + titleTemp[0] + "&title=1";
			}
			else
			{
				titleTemp = "index.php?c=" + titleTemp[0] + "&title=1";
			}
			
			$.get(titleTemp, { }, function(title)
			{
				//title = title.split("<");
				$(document).attr("title", title);
			});
		}
		else
		{
    		var titleTemp = divA.split('/');
            
            if (titleTemp === '')
			{
                var titleTempT = "Restaurace Praha | Italské jídlo | Francouzské jídlo | Letem-světem.com";
			}
			else if (titleTemp[2] === 'jidelni-listek')
			{
				titleTemp = "index.php?c=" + titleTemp[1] + "&s=" + titleTemp[2] + "&title=1";
			}
			else if (titleTemp[2] === 'novinky')
			{
				titleTemp = "index.php?c=" + titleTemp[1] + "&title=1";
			}
			else
			{
				titleTemp = "index.php?c=" + titleTemp[1] + "&title=1";
			}
			
            $.get(titleTemp, { }, function(title)
			{
				//title = title.split("<");
				$(document).attr("title", title);//title[0]
				if (titleTempT !== "")
				{
					$(document).attr("title", titleTempT);
				}
			});
		}
	},
	
	preload: function()
	{
		//$("div#container").append('<img src="./images/gallery/1s.png" class="testImg" />');
		//$("img.testImg").hide();
		//var image1 = $('<img />').attr('src', './images/gallery/1s.png');
		
		$("div#container").append('<div id="pre1"></div>');
		$("div#container").append('<div id="pre2"></div>');
		$("div#container").append('<div id="pre3"></div>');
		$("div#container").append('<div id="pre4"></div>');
		$("div#container").append('<div id="pre5"></div>');
		
		$("div#pre1").load("./pages/content/fotogalerie.php", function()
		{
			$("div#pre1").hide();
		});
		
		$("div#pre2").load("./pages/content/fotogalerie2.php", function()
		{
			$("div#pre2").hide();
		});
		
		$("div#pre3").load("./pages/content/kontakt.php", function()
		{
			$("div#pre3").hide();
		});
		
		$("div#pre4").load("./pages/content/jidelni-listek.php", function()
		{
			$("div#pre4").hide();
		});
		
		$("div#pre5").load("./pages/content/menu.php", function()
		{
			$("div#pre5").hide();
		});
	},
	
	tooltip: function()
	{
		var optionsT = "div#menu a, div#contentJidelniListekSekce a, div#contentJidelniListek a, div#contentDenniListek a, div.pagination a";
		var optionsA = "div#contentFotogalerie a, div#contentFotogalerie2 a";
		// if this.attribute('alt') then optionsA else optionsT
		$(optionsA).qtip({
			content: $(this).attr('alt'),
			position: {target: 'mouse'},
			style:
			{
				width: 200,
				//padding: 5,
				name: 'cream',
				border: { width: 4, radius: 5 },
				tip: 'topLeft'
			}
		});
	},        
	
	newsBox: function()
	{
        // ziskám JSON objekt, z kterého už se pak bude vše vypisovat..
        
        //var newsTemp = "index.php?c=novinky&news=json";
        $.get('index.php', {c: 'novinky', news: 'json' }, function(news)
        {
            var newsArr = news.split(" _-_ ");
            
            $("div#newsBox").html('<div id="newsBoxPosts"></div>');
            $("div#newsBoxPosts").html(newsArr[0]);
            
            var i = 0;
            
            setInterval(function()
            {
                $("div#newsBoxPosts").fadeOut(1000, function()
                {
                    $("div#newsBoxPosts").html(newsArr[i]);
                    $("div#newsBoxPosts").fadeIn(700);
                });
                i++;
                if (i >= newsArr.length-1)
                {
                    i = 0;
                }
            }, 5000);
        });
	}
}

//-- ANIMATION
var Animation =
{
	init: function()
	{
		this.intro();
		this.menu();
		this.object2_1();
		//setInterval("Animation.object2_1()", 21000);
		this.object2_2();
		//setInterval("Animation.object2_2()", 21000);
	},
	
	intro: function()
	{
		/*var containerTop = -1*(-Resize.vyska+850);
		if (containerTop >= 0)
		{
			containerTop = 0;
		}
		else if (containerTop <= -70)
		{
			containerTop = -70;
		}*/
		//containerTop = 0; //!!!TEST
		$("div#container").css("top", "-900px");
		$("div#container").animate({"top": "0px"}, 1500); //containerTop
	},
	
	menu: function()
	{
		var menuVerification = true;
		
		$("div#menuContainer").css("bottom", "105px");
		
		function showMenu()
		{
			$("div#menuContainer").animate({ "bottom": "0px" }, 1000, function()
			{
				menuVerification = true;
			});
		}
		
		function hideMenu()
		{
			$("div#menuContainer").animate({ "bottom": "105px" }, 1000, function()
			{
				menuVerification = false;
			});
		}
		
		setTimeout('$("div#menuContainer").animate({ "bottom": "0px" }, 1000)', 1000);
		//{ direction: "up", easing: "easeOutBounce" }
		
		
		$("div#menuHolder").click(function()
		{
			if (menuVerification === true)
			{
				hideMenu();
			}
			else
			{
				showMenu();
			}
		});
		
		$("div#menuHolder2").click(function()
		{
			if (menuVerification === true)
			{
				hideMenu();
			}
			else
			{
				showMenu();
			}
		});
	},
	
	object2_1: function()
	{
		var vyska = $(window).height();
		var sirka = $(window).width();
		
		$("img#object2_1_img").show();
		$("div#object2_1").show();
		$("div#object2_1").css("left", "0px");	
		$("div#object2_1").css("top", "0px");
		$("img#object2_1_img").css("left", "0px");
		$("img#object2_1_img").css("top", "0px");
		$("img#object2_1_img").width(130);
		
		setInterval('$("div#object2_1").animate({"top": (Math.round(Math.random()*50))+10})', 1000/10); //*100))+50
		$("img#object2_1_img").animate({"left": sirka+200, "top": 600}, sirka*16, function() //1400; 600
		{
			$("img#object2_1_img").fadeOut(500);
		});
	},
	
	object2_2: function()
	{
		var vyska = $(window).height();
		var sirka = $(window).width();
		
		$("img#object2_2_img").show();
		$("div#object2_2").show();
		$("div#object2_2").css("right", "0px");	
		$("div#object2_2").css("top", "0px");
		$("img#object2_2_img").css("right", "0px");
		$("img#object2_2_img").css("top", "100px");
		$("img#object2_2_img").width(170);
		
		setInterval('$("div#object2_2").animate({"top": (Math.round(Math.random()*50))+10})', 1000/10);
		$("img#object2_2_img").animate({"right": sirka+200,"top": 700 }, sirka*16, function() //1400; 700
		{
			//$("div#object2_2").hide();
			$("img#object2_2_img").fadeOut(500);
		});
	}
	
}
