
var panorama_speed = 10; //ms
var panorama_chmury_speed = 1; //px
var panorama_1plan_speed = 2; //px

var scrolling_l = null;
var scrolling_p = null;
var panorama_chmury_x = 0;
var panorama_1plan_x = 0;

var area_dzien = ''+
					  '<area shape="poly" class="bud1" coords="59,348,255,384,272,411,309,411,309,429,295,451,240,458,42,394,52,389,52,358" href="#budynekC3" />'
					  +'<area shape="poly" class="bud2" coords="52,344,112,334,337,368,322,376,314,406,272,408,257,381" href="#centrumMed" />'
					  +'<area shape="poly" class="bud3" coords="340,369,414,383,412,416,389,419,350,408,320,408,322,381" href="#szkolnictwo" />'
					  +'<area shape="poly" class="bud4" coords="242,304,309,303,309,361,222,348,217,318,242,316" href="#budynekB2" />'
					  +'<area shape="poly" class="bud5" coords="312,284,464,286,465,383,439,386,434,404,417,401,417,381,312,361" href="#budynekC2" />'
					  +'<area shape="poly" class="bud6" coords="437,268,467,276,584,275,585,303,467,306,467,283,437,283" href="#" />'
					  +'<area shape="poly" class="bud7" coords="472,310,472,390,512,393,514,386,559,388,592,376,592,91,557,76,554,43,540,35,520,31,467,25,462,50,432,46,437,268,467,273,589,273,589,303" href="#budynekC1" />'
					  +'<area shape="poly" class="bud8" coords="515,390,557,395,594,378,647,377,652,405,630,417,595,430,582,440,568,442,562,415,530,405,512,422" href="#budynekUsl" />'
					  +'<area shape="poly" class="bud9" coords="595,280,694,288,699,378,655,400,652,375,595,375" href="#budynekB1" />'
					  +'<area shape="poly" class="bud10" coords="669,200,754,198,752,362,745,365,740,377,717,378,715,285,667,283" href="#budynekA" />';

var area_noc = ''+
					  '<area shape="poly" class="bud1" coords="42,378,210,409,215,419,217,435,217,447,207,481,195,483,55,439,59,416,59,400" href="#budynekC3" />'
					  +'<area shape="poly" class="bud2" coords="43,376,96,368,295,395,268,400,268,431,222,433,215,408" href="#centrumMed" />'
					  +'<area shape="poly" class="bud3" coords="292,394,357,403,357,434,328,439,302,433,272,433,270,403" href="#szkolnictwo" />'
					  +'<area shape="poly" class="bud4" coords="225,330,277,331,277,391,200,379,199,345,225,344" href="#budynekB2" />'
					  +'<area shape="poly" class="bud5" coords="284,301,436,303,436,413,389,406,385,431,360,428,359,401,284,389" href="#budynekC2" />'
					  +'<area shape="poly" class="bud6" coords="409,281,439,289,556,288,557,316,439,319,439,296,409,296" href="#" />'
					  +'<area shape="poly" class="bud7" coords="441,322,441,415,461,420,466,410,528,420,557,403,554,78,530,66,521,30,500,17,479,13,433,5,403,39,401,58,406,280,436,285,558,285,558,315" href="#budynekC1" />'
					  +'<area shape="poly" class="bud8" coords="506,424,533,424,576,416,617,410,620,456,600,463,565,476,552,486,538,488,506,478,505,463,502,448" href="#budynekUsl" />'
					  +'<area shape="poly" class="bud9" coords="563,312,694,313,690,417,623,434,620,407,563,407" href="#budynekB1" />'
					  +'<area shape="poly" class="bud10" coords="672,219,758,218,753,398,751,410,747,421,715,425,716,312,670,310" href="#budynekA" />';

$(function() {
	//alert($("#panorama_chmury").css("background-position-x"));
	
	if($.browser.mozilla)
	{
		p_tmp = $("#panorama_chmury").css("background-position");
		p_tmp_1 = p_tmp.split(" ");
		panorama_chmury_x = p_tmp_1[0].replace("px", "");
	}
	else
	{
		p_tmp = $("#panorama_chmury").css("background-position-x");
		panorama_chmury_x = p_tmp.replace("px", "");
	}
	
	panorama_chmury_x = parseInt(panorama_chmury_x, 10);
	
	p_tmp = $("#panorama_1plan").css("left");
	panorama_1plan_x = p_tmp.replace("px", "");
	panorama_1plan_x = parseInt(panorama_1plan_x, 10);
	
	// przesuniecie w prawo, lewo panoramy
	$("#lewo").mouseover(function() {
		/*clearTimeout(scrolling_l);
		clearTimeout(scrolling_p);
		setTimeout("przewinLewo()",panorama_speed);*/
		przewinLewo();
	}).mouseout(function() {
		$("#panorama_1plan").stop(true);
		$("#panorama_chmury").stop(true);
		//clearTimeout(scrolling_l);
	});
	
	$("#prawo").mouseover(function() {
		/*clearTimeout(scrolling_l);
		clearTimeout(scrolling_p);
		setTimeout("przewinPrawo()",panorama_speed);*/
		przewinPrawo();
	}).mouseout(function() {
		$("#panorama_1plan").stop(true);
		$("#panorama_chmury").stop(true);
		//clearTimeout(scrolling_p);
	});
	
	// pokaz ukry obczar budynku
	$("#budynki area").live("mouseover",function() {
		$("#capital_towers").addClass($(this).attr("class")+"_obszar").animate({opacity: 1}, 500);
	});
	$("#budynki area").live("mouseout",function() {
		$("#capital_towers").removeAttr("class").css("opacity", 0.1);
	});
	
	/*$("#budynki area").mouseover(function() {
		$("#capital_towers").addClass($(this).attr("class")+"_obszar").animate({opacity: 1}, 500);
	}).mouseout(function() {
		$("#capital_towers").removeAttr("class").css("opacity", 0.1);
	});*/
	
	// zmiana dzien/noc
	$("#noc_dzien").toggle(
		function(){
			// noc
			$(this).removeClass("noc").addClass("dzien").text("dzień");
			
			
			$("#panorama_okno").animate({opacity: 0.2}, 500, function(){
				// ukryj busy
				//$(".bus1").hide().clearQueue().stop();
				$(".bus1").hide().stop(true);
				
				//$(".bus2").hide().clearQueue().stop();
				$(".bus2").hide().stop(true);
				
				$("#panorama_chmury").css("opacity", 0.0);
				$("#panorama_1plan").addClass("noc");
				$("#budynki").html(area_noc);
				$(this).animate({opacity: 1}, 500);
			});
		}, 
		function(){
			// dzien
			$(this).removeClass("dzien").addClass("noc").text("noc");
			
			$("#panorama_okno").animate({opacity: 0.2}, 500, function(){
				// busy jezdza w dzien
				animateBus1();
				animateBus2();
	
				$("#panorama_1plan").removeClass("noc");
				$("#panorama_chmury").css("opacity", 1);
				$("#budynki").html(area_dzien);
				$(this).animate({opacity: 1}, 500);
			});
		}
	);
	
	// bus animate
	animateBus1();
	animateBus2();
	
	// strzalki animuj
	animateLs();
	animatePs();
	
	// pokaz info
	$(".menu a:not(.noajax)").live("click", pokazdInfo);
	$("#budynki area").live("click", pokazdInfo);
	$("#mapka area").live("click", pokazdInfo);
	//zamknij info
	$("#info .close").live("click", zamknijInfo);
	$(window).keyup(escHandler);
	
	// menu
	$(".menu > li").mouseenter(function(){
			if($(this).find("ul:not(:animated)").length)
			{
				$(this).find("ul:not(:animated)").show("blind", {},300);
			}
		}).mouseleave(function(){
			$(this).find("ul").stop(true).fadeOut();
	});
	
	$(window).resize(sprawdzPolorzenie);
	$(window).scroll(przesunDolneNavi);
	
	//ukryj loading screen
	/*var htmlnew = '<div class="hide_img"><img src="images/panorama_1plan.png" /></div>';
	var jakistamheihjt = $('.hide_img img').length;
	if(jakistamheihjt) 
	{
    $(body).append(htmlnew);
    $('.hide_img img').load(function() {
      console.log('dd2');
        $(".loading_screen").delay(500).fadeOut();
    });
	}*/
	
	$(".loading_screen").delay(500).fadeOut();
	
	/*var jakistamheihjt = $('.hide_img img').height();
	console.log(jakistamheihjt);
	if(jakistamheihjt > 0)
	{
    $(".loading_screen").delay(500).fadeOut();
	}
	else
	{
    console.log('dd');
      $('.hide_img img').load(function() {
        console.log('dd2');
          $(".loading_screen").delay(500).fadeOut();
      });

	}*/

	
});

function animateBus1() {
	$(".bus1").animate({bottom: '84px', left: '1593px', width: '56px'}, 5000, 'easeOutQuart')
	.delay(400)
	.animate({bottom: '215px', left: '1113px', width: '30px'}, 6000, 'linear')
	.animate({bottom: '228px', left: '1078px', width: '20px'}, 600, 'linear')
	.animate({bottom: '323px', left: '870px', width: '15px'}, 2700, 'linear')
	.animate({bottom: '331px', left: '808px', width: '8px'}, 1200, 'linear')
	.hide("fast")
	.delay(4000)
	.css({'width': '58px', 'bottom':'21px', 'left':'1895px'})
	.fadeIn("fast", animateBus1);
}

function animateBus2() {
	$(".bus2").animate({bottom: '329px', left: '890px', width: '8px'}, 600, 'linear')
	.animate({bottom: '314px', left: '874px', width: '10px'}, 600, 'linear')
	.animate({bottom: '248px', left: '1002px', width: '16px'}, 1200, 'linear')
	.animate({bottom: '178px', left: '1103px', width: '28px'}, 800, 'linear')
	.animate({bottom: '91px', left: '1367px', width: '33px'}, 6000, 'easeOutExpo')
	.delay(200)
	.animate({bottom: '-30px', left: '1828px', width: '34px'}, 2000, 'easeInQuad')
	.delay(4000)
	.css({'width': '8px', 'bottom':'329px', 'left':'947px'})
	.fadeIn("fast", animateBus2);
}
function animateLs() {
	$("#lewo img").animate({ left: '10px', width: '39px'}, 1000, 'swing')
	.animate({ left: '50px', width: '30px'}, 1000, 'swing', animateLs);
}
function animatePs() {
	$("#prawo img").animate({ left: '50px', width: '39px'}, 1000, 'swing')
	.animate({ left: '10px', width: '30px'}, 1000, 'swing', animatePs);
}

function przewinLewo()
{
	// przekroczyło zakres
	/*if(panorama_1plan_x >= 0)
	{
		clearTimeout(scrolling_l);
	}
	else
	{
		panorama_chmury_x = panorama_chmury_x + panorama_chmury_speed;
		$("#panorama_chmury").css("background-position", (panorama_chmury_x) + "px 0px");
		
		panorama_1plan_x = panorama_1plan_x + panorama_1plan_speed;
		$("#panorama_1plan").css("left", (panorama_1plan_x) + "px");
		
		scrolling_l = setTimeout("przewinLewo()",panorama_speed);
	}*/
	
	if($.browser.mozilla)
	{
		$("#panorama_chmury").animate(
			{
				backgroundPosition: "(-1400px 0px)"
			},
			10000,
			"easeOutExpo"
		);
	}
	else
	{
		$("#panorama_chmury").animate(
			{
				backgroundPositionX: "-1400px"
			},
			10000,
			"easeOutExpo"
		);
	}

	
	$("#panorama_1plan").animate(
		{
			left: 0
		},
		10000,
		"easeOutExpo"
	);
}

function przewinPrawo()
{
/*
	var prawa_granica =  $("#panorama_okno").width() - $("#panorama_1plan").width();
	
	// przekroczyło zakres
	//if(panorama_1plan_x <= -1230)
	if(panorama_1plan_x <= prawa_granica)
	{
		clearTimeout(scrolling_p);
	}
	else
	{
		panorama_chmury_x = panorama_chmury_x - panorama_chmury_speed;
		$("#panorama_chmury").css("background-position", (panorama_chmury_x) + "px 0px");
	
		panorama_1plan_x = panorama_1plan_x - panorama_1plan_speed;
		$("#panorama_1plan").css("left", (panorama_1plan_x) + "px");
	
		scrolling_p = setTimeout("przewinPrawo()",panorama_speed);
	}
	*/
	
	var prawa_granica =  $("#panorama_okno").width() - $("#panorama_1plan").width();
	
	
	if($.browser.mozilla)
	{
		//TO DO
		// te wartości po prwej trzeb policzyć dynamicznie
		$("#panorama_chmury").animate(
			{
				backgroundPosition: "(-1827px 0px)"
				//backgroundPosition: "("+panorama_chmury_x+"px 0px)"
			},
			10000,
			"easeOutExpo"
		);
	}
	else
	{
		//TO DO
		// te wartości po prwej trzeb policzyć dynamicznie
		$("#panorama_chmury").animate(
			{
				backgroundPositionX: "-1827px"
				//backgroundPosition: "(-1827px 0px)"
				//backgroundPosition: "("+panorama_chmury_x+"px 0px)"
			},
			10000,
			"easeOutExpo"
		);
	}

	
	$("#panorama_1plan").animate(
		{
			left: prawa_granica
		},
		10000,
		"easeOutExpo"
	);
}

function pokazdInfo()
{
	var temp1 = $(this).attr('href');
	temp1 = temp1.replace("#", "");	
	var url = kategorie[temp1];
	
	if(url)
	{
		$('.loading_info_screen').show();
		
		$.ajax({
			url: url,
			success: function(msg){
				$('.loading_info_screen').hide();
				
				$('#info').show("fast", function(){
					$('#info').html(msg)
						wyrownajElemnt($('#info .box'));
						$('#info .box').show("clip");
					
				});			
			}/*,
			error: function(jqXHR, textStatus, errorThrown) {
        console.log('error');
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
			},
			complete: function(jqXHR, textStatus ) {
        console.log('complete');
			}*/
		});
	}
	
	return false;
}

function wyrownajElemnt(ele)
{
	var ele_width = $(ele).width();
	var body_width = $("#panorama_okno").width();
	$(ele).css('left',(body_width / 2) - (ele_width / 2) + "px");
}

function zamknijInfo()
{
	$(this).parent().closest('.box').hide("fold", function(){
		if($("#info .box:visible").length == 0 )
		{
			$('#info').hide("fast");
		}
		
	});
	/*$('#info .box').hide("fold", function(){
		$('#info').hide("fast");
	});*/
}
function escHandler(e) 
{
	if(e.which == 27 )
	{
		zamknijInfo();
	}
}

function sprawdzPolorzenie()
{
	// przesuniecie x
	p_tmp = $("#panorama_1plan").css("left");
	panorama_1plan_x = p_tmp.replace("px", "");
	panorama_1plan_x = parseInt(panorama_1plan_x, 10);
	
	// pobranie danych i ustawienie nowego css dla 1plan
	var prawa_granica =  $("#panorama_okno").width() - $("#panorama_1plan").width();
	
	if(panorama_1plan_x <= prawa_granica)
	{
		var wielkosc_przesuniecia = panorama_1plan_x - prawa_granica;
		$("#panorama_1plan").css("left", panorama_1plan_x - wielkosc_przesuniecia +"px");
		
		//p_tmp = $("#panorama_chmury").css("background-position");
		p_tmp = $("#panorama_chmury").css("background-position-x");
		//p_tmp_1 = p_tmp.split(" ");
		panorama_chmury_x = p_tmp.replace("px", "");
		panorama_chmury_x = parseInt(panorama_chmury_x, 10);
		//panorama_chmury_x = panorama_chmury_x - wielkosc_przesuniecia;
		
		/*prawa_granica = $("#panorama_okno").width() - 3548;
		wielkosc_przesuniecia = panorama_chmury_x - prawa_granica;*/
		$("#panorama_chmury").css("background-position", panorama_chmury_x + "px 0px");
	}
	
	przesunDolneNavi({});
}

function przesunDolneNavi(e)
{
	if(e.currentTarget)
	{
		var przesuniecie = e.currentTarget.pageYOffset * -1;
	}
	else
	{
		
		var przesuniecie = $(window).pageYOffset;
	}
	
	//alert(e.currentTarget.scrollY);
	//console.log('---------');
	// przesuniecie, mapy i noc/dzien
	$("#mala_mapa").css('bottom', (przesuniecie + 10) + "px");
	$("#noc_dzien").css('bottom', (przesuniecie + 10) + "px");
}
