$(document).ready(function (){

	var current_site="glowna.html"	
	var recurrent = 0;
	$('.content .scroll').css({zIndex:"5"});
	$("a").not("input").focus( function(){
		$(this).blur()
	});
	
	//alert($(".logo").offset().top)
	
	function setSite(){
		$(".contentBg").css("width", $(window).width()-$(".contentBg").offset().left-201 +"px");
		$(".grayBoxContainer").css("width", $(window).width()-$(".grayBoxContainer").offset().left +"px");
	}
	$( window ).resize( setSite );

	setSite();
	$('.displayLightBox a').lightBox();	
	$.preloadCssImages();
	showMain("glowna.html");
	navigation();
	function showMain(siteUrl){
		$(".content").animate({opacity:"0"},function(){
			//$(".menu > li a").animate({"paddingRight":527,"marginRight":-527},800).children("a").css({"marginRight":362});
			$(".grayBoxContainer").animate({marginLeft: 160, width: $(window).width()-$(".grayBoxContainer").offset().left},800,function (){
			$(".menu > li a").not(".submenu a").animate({"marginRight":-380, paddingRight:380},800);
						
			$.ajax({
			url: siteUrl,
			cache: false,
			success: function(html){
				//alert(html);
				if(siteUrl=="glowna.html"){
					$(".smallContent").html(html);
					$(".grayInfo").animate({width: 550, opacity: "0.95"},800, function() {
						$(".smallContent").animate({opacity: "0.95"},800);
					});
				}
				if(siteUrl=="kontakt.html"){
					$(".smallContent").animate({opacity: "0"},800, function() {
						$(".smallContent").html("");
						$(".grayInfo").animate({width: 249}, 800);
					});
					$(".content").html(html);
					$(".content").animate({opacity: "0.9"},800);
				}
				
				if(siteUrl=="firma.html"){
					$(".smallContent").animate({opacity: "0"},800, function() {
						$(".smallContent").html("");
						$(".grayInfo").animate({width: 249}, 800);
					});
					$(".content").html(html);
					$(".content").animate({opacity: "0.9"},800);
				}
				
				if(siteUrl=="realizacje.html"){
					$(".smallContent").animate({opacity: "0"},800, function() {
						$(".smallContent").html("");
						$(".grayInfo").animate({width: 249}, 800);
					});
					$(".content").html(html);
					$(".content").animate({opacity: "0.9"},800);
				}
				
				$('.scroll').jScrollPane();
				current_site = siteUrl;
			},
			error: function(){
				alert('nie istnieje taka strona');
			}
			});});
		});		
	}
	
	function navigation(){
		
	// wyl md	$(".menu a").click(function () {
			
			// $(this).parent().children(".submenu").css({display:"block"});
			// $(".jScrollPaneContainer").css({"zIndex":"-1"});
			
			// wyl md
			// $(".menu > li > a").not(this).mouseover(function () {
			// $(".submenu").css({display:"none"});
			//	 $(".jScrollPaneContainer").css({"zIndex":"1"});
			//});
		
			// wyl md
			// $(".submenu *").mouseover(function(){
			//	//alert($(this).parent("").parent("li").html())
			//	$(this).css({display:"block"});
			//	$(this).not(".submenu").parent("").parent("li").children("a").css({backgroundColor:"#fff"});
			//	$(".jScrollPaneContainer").css({"zIndex":"-1"});
			//});
			
			// wyl md
			//$(".submenu").mouseout(function(){
			//	//$(this).css({"display":"none"});
			//	$(this).parent("li").children("a").css({backgroundColor:"transparent"});
			//	$(this).parent("li").children("a").hover(function(){
			//		$(this).css({backgroundColor:"#fff"});
			//	});
			//	$(this).parent("li").children("a").mouseout(function(){
			//		$(this).css({backgroundColor:"transparent"});
			//	});
			//});
		//});
	
	
		$(".menu a, .logo a, .path a, .wstecz a, .content .scroll a, .realizacje a").live("click", function () {//click(function(){//
			
			var site = $(this).attr("href");
			
			if(site=="#" || site == current_site ){
				return false;
			}
			
			if(site=="glowna.html"){
				showMain("glowna.html");
				return false;
			}
			
			if(site=="kontakt.html"){
				showMain("kontakt.html");
				//
				return false;
			}
			if(site=="firma.html"){
				showMain("firma.html");
				//
				return false;
			}
			if(site=="realizacje.html"){
				showMain("realizacje.html");
				//
				return false;
			}
			if (site.indexOf(".pdf")!=-1){
			
			}		
			else if (site.indexOf(".jpg")==-1 && $(this).attr("target")!="_blank"){
				$(".submenu").css({"display":"none"});	
				
				$(".content").animate({"opacity": "0"},800,function(){
					$(".content").html('<img src="design_el/site_preloader.gif" class="preloader" />').css({opacity:"1"});
					//$(".grayBox").animate({marginLeft:"160"},800,function(){
					//	interVal = setInterval(animateBg, 7500 );
						//$(".menu > li > a").not(".submenu a").animate({"marginRight":-380, paddingRight:380});
						
						$(".smallContent").animate({opacity: "0"},800, function() {
							$(".smallContent").html("");
							$(".grayInfo").animate({width: 249}, 800);
						});
						$(".menu > li > a").not(".submenu a").animate({"marginRight":0, paddingRight:19},800);
						$(".content").css({"marginLeft":0});
						$(".content").css({"width":656});
						//var pause = setTimeout(function(){
							$.ajax({
								url: site,
								cache: false,
								success: function(html){
								//$(".content").html('<img src="design_el/lightbox-ico-loading.gif" style="background:#fff;">');
									$(".content").html("");
									
										$(".grayBoxContainer").animate({marginLeft: "-201px", width: 727},800,"swing",function(){	
											//clearInterval(interVal);
											//clearInterval(pause);
											$(".content").html(html);
											$(".content").animate({opacity: "1"},800);
											$('.scroll').jScrollPane({scrollbarWidth: 13, showArrows:true, arrowSize: 13, animateTo:0});
											$(".jScrollPaneContainer").css({"zIndex":"1"});
											//$('.jScrollPaneDrag').css({height:31});										
     										$('.displayLightBox a').not(".description a").lightBox();											
											//scrollImg();
										});
								},
								error: function(){
									alert('nie istnieje taka strona');
								}
							});
						//}, 2500);
					//});
				});
				current_site = site;
				return false;
			}
		});
	}
	
	function scrollImg(){
		$(".cover").hover(
			function(){
				
				if($(this).children("img").css('margin-top')!=($(".cover img").height()-$(".cover").height())){
					$(this).children("img").animate({marginTop:0},2500);
				}
				else{
					$(this).children("img").animate({marginTop:-($(".cover img").height()-$(".cover").height())},2500);
				}
			},function(){
				$(this).children("img").stop();
				$(".cover img").animate({marginTop:0},2500);
				scrollImg();
		});
	}
	
	function animateBg(){
		if(recurrent==13){
			$(".contentBg").css({backgroundPosition: "0 0"});
			recurrent=1;
		}
		var moveToPx = -recurrent*363;
		$(".contentBg").animate({backgroundPosition: moveToPx+"px 0"},800,"swing");
		return recurrent++;
	}	
	
	interVal = setInterval(animateBg, 7500 );
});
