var histNum = 1; 		
function updateLayout(){ setTimeout(function(){window.scrollTo(0, 1);}, 10); };
addEventListener("load", function() { setTimeout(updateLayout, 0); }, false);

$(document).ready(function(){

	var i = 1;
	var max = $("#watch-nav-max").text();
	var cur = $("#watch-nav-num").text();
	var itlast = cur;
	
	$('#thumbs li a').removeClass("nojs");
	$('.prev').removeClass("nojs");
	$('.next').removeClass("nojs");
	
	$('#thumbs li a')
		.mouseover(function(){  $('.thumbimg', this).stop().animate({marginTop:"0px"}, {duration:250});  } )
		.mouseout(function(){  $('.thumbimg', this).stop().animate({marginTop:"-42px"}, {duration:250}); return false; });
	
	$('#thumbs li a').click(function(){
		if (id !="no") {
			var xwhere = $(this).attr("id");
			xwhere= xwhere.replace("tm-","")
			_gaq.push(['_trackEvent', 'Site', 'Thumbs', id+'/'+xwhere]);
			xgo (xwhere);
			return false;
		} 
		return true;
	}); 
		
	var id = "no";
	
	$(".prev")
			.mouseover(function(){ 
			if ($('#watch-presentation').hasClass('black') || $('#watch-presentation').hasClass('presentation-bg1') || $('#watch-presentation').hasClass('presentation-kabuki') || $('#watch-presentation').hasClass('presentation-bg2') || $('#watch-presentation').hasClass('presentation-shirt') || $('#watch-presentation').hasClass('presentation-kim'))
				$(this).stop().animate({backgroundPosition:"(0 -35px)"}, {duration:250}); 
				else $(this).stop().animate({backgroundPosition:"(-27px -35px)"}, {duration:250});  
			})
			.mouseout(function(){  
			if ($('#watch-presentation').hasClass('black') || $('#watch-presentation').hasClass('presentation-bg1') || $('#watch-presentation').hasClass('presentation-kabuki') || $('#watch-presentation').hasClass('presentation-bg2') || $('#watch-presentation').hasClass('presentation-shirt') || $('#watch-presentation').hasClass('presentation-kim'))
				$(this).stop().animate({backgroundPosition:"(0 4px)"}, {duration:250}); 
				else $(this).stop().animate({backgroundPosition:"(-27px 4px)"}, {duration:250}); 
			});

		
	$(".next")
		.mouseover(function(){ 

		if ($('#watch-presentation').hasClass('black') || $('#watch-presentation').hasClass('presentation-bg1') || $('#watch-presentation').hasClass('presentation-kabuki') || $('#watch-presentation').hasClass('presentation-bg2') || $('#watch-presentation').hasClass('presentation-shirt') || $('#watch-presentation').hasClass('presentation-kim'))
			$(this).stop().animate({backgroundPosition:"(-81px -35px)"}, {duration:250}); 
			else $(this).stop().animate({backgroundPosition:"(-54px -35px)"}, {duration:250});  
		
		})
		.mouseout(function(){  
		
		if ($('#watch-presentation').hasClass('black') || $('#watch-presentation').hasClass('presentation-bg1') || $('#watch-presentation').hasClass('presentation-kabuki') || $('#watch-presentation').hasClass('presentation-bg2') || $('#watch-presentation').hasClass('presentation-shirt') || $('#watch-presentation').hasClass('presentation-kim'))
			$(this).stop().animate({backgroundPosition:"(-81px 4px)"}, {duration:250}); 
			else $(this).stop().animate({backgroundPosition:"(-54px 4px)"}, {duration:250}); 
		
		
		});
		
	$('#navbar a')
		.mouseover(function(){ if (!$(this).hasClass('sel')) $(this).stop().animate({backgroundPosition:"(0 100%)"}, {duration:350})  })
		.mouseout(function(){  if (!$(this).hasClass('sel')) $(this).stop().animate({backgroundPosition:"(-200px 100%)"}, {duration:350}) });
		
		
	if ( $('#interface').hasClass('sel')  ) id = "interface"; 
	else if ( $('#ilustracje').hasClass('sel')  ) id = "ilustracje"; 
	else if ( $('#identyfikacja').hasClass('sel')  ) id = "identyfikacja"; 
	 
	//var History = window.History;
	//var State = History.getState();			
	var newok = $(".new"); 			
	$(".new").animate({top:"-10px"}, 300).animate({top:"4px"}, 470);	 
	$(".new").everyTime(770, function(){						 
    $(".new").animate({top:"-10px"}, 300).animate({top:"4px"}, 470);	
	  });
	  
function start() {
		
	    if (id!="no") {
	  	$.get("/ajax/"+id+"/"+cur,
	  		function(data){ 
	  		 //itlast = data.nazwa;
	   			$.loadImages( [ '/works/'+data.nextimg,'/works/'+data.previmg,'/img/presentation-bg-1.jpg','/img/presentation-bg-2.jpg','/img/kim.gif' ]  );
	  		}
	  	,'json');
		}
  
  }

function showmore() {
	_gaq.push(['_trackEvent', 'Site', 'Showmore', 'Showmore']);
	$("#thumbs").animate( {marginTop: '-90px'} , {duration:150}, 'linear' );	
	if (!$("#more-nav-more").hasClass("hidden")) $("#more-nav-more").addClass("hidden");
	$("#more-nav-less").removeClass("hidden");	
	
}

function showless() {
	_gaq.push(['_trackEvent', 'Site', 'Showmore', 'Showless']);
	$("#thumbs").animate( {marginTop: '0px'} , {duration:150}, 'linear' );	
	if (!$("#more-nav-less").hasClass("hidden")) $("#more-nav-less").addClass("hidden");
	$("#more-nav-more").removeClass("hidden");	
	
}



$("#more-nav-more").click(function(){
		showmore();
		return false;
	});
	

$("#more-nav-less").click(function(){
	showless();
	return false;
});

$(".prev")
	.click(function(){
		prev();
		return false;
	}); 
    
	$(".next")
		.click(function(){
			next();
	  	return false;
	    }); 

start();

 function xgo(where) {

	
	
	if (id!="no" && cur!=where) {
	  	
	    cur = where; 
	    $("#watch-nav-num").text(cur);
	   
	    
		$('#it-pres')
		    .animate( {left: '-=30px', opacity: 0} , {duration:150}, 'linear' )
		    .animate( {left: '+=60px' }, {duration:1} );
		 
		$('#watch').stop().animate({ opacity: 0}, {duration:150}, 'linear' );
	
	    histNum++;
	    
		$.get("/ajax/"+id+"/"+cur,
			function(data){

				
				if (cur<=15) {
					$("#thumbs").animate( {marginTop: '0px'} , {duration:150}, 'linear' );	
					if (!$("#more-nav-less").hasClass("hidden")) $("#more-nav-less").addClass("hidden");
					$("#more-nav-more").removeClass("hidden");
						
				} else { 
					$("#thumbs").animate( {marginTop: '-90px'} , {duration:150}, 'linear' ); 
					if (!$("#more-nav-more").hasClass("hidden")) $("#more-nav-more").addClass("hidden");
					$("#more-nav-less").removeClass("hidden");
				}
				
			    $("#it-head").removeClass("newitem");
			    $("#it-head").text(data.head2);
			  	$("#it-desc").html(data.opis2);
			  	$("#tm-"+itlast).removeClass("selected");
			  	$("#tm-"+cur).addClass("selected");
			    itlast = cur;
			    document.title = data.title;
			    History.pushState({state:histNum}, data.title , "/"+ id + "/" + data.nazwa );
			    var vnew = data.vnew;
			  	if (data.xnew =="new") {
			   	 
			   	 	$("#it-head").addClass("newitem"); 
			   	
			   	
			   	 	if ($.cookie(data.nazwa)==null) {
			   	 		 $.cookie(data.nazwa, 'new', { expires: 20, path: '/',domain:'.kwedo.com' });
			   	  		 vnew++;
			   		 }
			   	 	if (vnew >= data.bnew) newok.fadeOut();
			   	 
			   }
			   
			   if (data.pres == 'black' || data.pres == 'presentation-bg1' || data.pres =='presentation-kabuki' || data.pres =='presentation-bg2' || data.pres =='presentation-shirt' || data.pres =='presentation-kim') {
			   		$('.next').stop().animate({backgroundPosition:"(-81px 4px)"}, {duration:250});
			   		$('.prev').stop().animate({backgroundPosition:"(0 4px)"}, {duration:250}); 
			   		
			   	} else {
			   	
			   		$('.next').stop().animate({backgroundPosition:"(-54px 4px)"}, {duration:250});
			   		$('.prev').stop().animate({backgroundPosition:"(-27px 4px)"}, {duration:250});
			   	
			   	}
			
			   
			  
			   $('#content').delay(400).css( 'background-position',"50% 220px"); 
			  if (data.typ!="flash") $.loadImages( '/works/'+data.img, function() {  
			  			
			  			$("#watch-presentation").removeClass();
			  			if ($("#it-pres").hasClass('screen-pad')) $("#it-pres").removeClass("screen-pad");
			  			if (data.typ=="screen") $("#it-pres").addClass('screen-pad');
			  			$("#watch-presentation").addClass(data.pres);
			  			$("#it-pres").html('<img src="/works/' + data.img +'" alt="' + data.head2 + '" />');
			  					  			$('#content').stop().css( 'background-position',"50% -30px");
			  			$('#watch').stop().animate({ opacity: 1}, {duration:350}, 'linear'	);
			    	    $('#it-pres').animate({left: 0,  opacity: 1}, {duration:150});
			 		 	$.loadImages( [ '/works/'+data.nextimg,'/works/'+data.previmg ]  );
			 		 	
			 		 	$('#it-pres img').swipe(  {
			 		 		 
			 		 	     swipeLeft: function() { next(); },
			 		 	     swipeRight: function() { prev(); },
			 		 	});
			 		 	
			
			  	});
			  	if (data.typ=="flash") {
			  		$("#watch-presentation").removeClass();
			  		if ($("#it-pres").hasClass('screen-pad')) $("#it-pres").removeClass("screen-pad");
			  		$("#watch-presentation").addClass(data.pres);
			  		$('#content').stop().css( 'background-position',"50% -30px");
			  		$('#watch').stop().animate({ opacity: 1}, {duration:350}, 'linear'	);
			  		$.loadImages( [ '/works/'+data.nextimg,'/works/'+data.previmg ]  );
			  		$('#it-pres').animate({left: 0,  opacity: 1}, {duration:150});
			  		$("#it-pres").html('<iframe src="/works'+data.img+'" width="960px" height="477px"><img src="/works/motion/flash.png" alt="flash" class="'+data.pres+'"/></iframe>');
			  		$('#it-pres img').swipe(  {
			  			 
			  		     swipeLeft: function() { next(); },
			  		     swipeRight: function() { prev(); },
			  		});
			  	}
		
		},'json');
 	}
 }
	
	
	
 function next() 
  {
  
  	if (id!="no") {
  	
    cur ++; if (cur > max) cur = 1; 
    $("#watch-nav-num").text(cur);
    
	$('#it-pres')
	    .animate( {left: '-=30px', opacity: 0} , {duration:150}, 'linear' )
	    .animate( {left: '+=60px' }, {duration:1} );
	 
	$('#watch').stop().animate({ opacity: 0}, {duration:150}, 'linear' );


    histNum++;
    
    	

	$.get("/ajax/"+id+"/"+cur,
		function(data){
		    _gaq.push(['_trackEvent', 'Site', 'Next', id+'/'+data.nazwa]);
		     if (cur<=15) {
		     	$("#thumbs").animate( {marginTop: '0px'} , {duration:150}, 'linear' );	
		     	if (!$("#more-nav-less").hasClass("hidden")) $("#more-nav-less").addClass("hidden");
		     	$("#more-nav-more").removeClass("hidden");
		     		
		     } else { 
		     	$("#thumbs").animate( {marginTop: '-90px'} , {duration:150}, 'linear' ); 
		     	if (!$("#more-nav-more").hasClass("hidden")) $("#more-nav-more").addClass("hidden");
		     	$("#more-nav-less").removeClass("hidden");
		     }
		    
		    $("#it-head").removeClass("newitem");
		    $("#it-head").text(data.head2);
		  	$("#it-desc").html(data.opis2);
		  	$("#tm-"+itlast).removeClass("selected");
		  	$("#tm-"+cur).addClass("selected");
		  	itlast = cur;
		    document.title = data.title;
		    History.pushState({state:histNum}, data.title , "/"+ id + "/" + data.nazwa );
		    var vnew = data.vnew;
		  	 if (data.xnew =="new") {
		   	 
		   	 $("#it-head").addClass("newitem"); 
		   	
		   	 
		   	 if ($.cookie(data.nazwa)==null) {
		   	  $.cookie(data.nazwa, 'new', { expires: 20, path: '/',domain:'.kwedo.com' });
		   	  vnew++;
		   	 }
		   	 if (vnew >= data.bnew) {
		   	 	newok.fadeOut();
		   	 }
		   }
		  
		    
		if (data.pres == 'black' || data.pres == 'presentation-bg1' || data.pres =='presentation-kabuki' || data.pres =='presentation-bg2' || data.pres =='presentation-shirt' || data.pres =='presentation-kim') {
				$('.next').stop().animate({backgroundPosition:"(-81px 4px)"}, {duration:250});
				$('.prev').stop().animate({backgroundPosition:"(0 4px)"}, {duration:250}); 
				
			} else {
			
				$('.next').stop().animate({backgroundPosition:"(-54px 4px)"}, {duration:250});
				$('.prev').stop().animate({backgroundPosition:"(-27px 4px)"}, {duration:250});
			
			}
			
					    
		    $('#content').delay(400).css( 'background-position',"50% 220px"); 
		  	if (data.typ!="flash") $.loadImages( '/works/'+data.img, function() {  
		  			
		  			
		  			$("#watch-presentation").removeClass();
		  			if ($("#it-pres").hasClass('screen-pad')) $("#it-pres").removeClass("screen-pad");
		  			if (data.typ=="screen") $("#it-pres").addClass('screen-pad');
		  			$("#watch-presentation").addClass(data.pres);
		  			$("#it-pres").html('<img src="/works/' + data.img +'" alt="' + data.head2 + '" />');
		  			
		  			$('#content').stop().css( 'background-position',"50% -30px");
		  			$('#watch').stop().animate({ opacity: 1}, {duration:350}, 'linear'	);
		    	    $('#it-pres').animate({left: 0,  opacity: 1}, {duration:150});
		 		 	$.loadImages( [ '/works/'+data.nextimg,'/works/'+data.previmg ]  );
		 		 	
		 		 	$('#it-pres img').swipe(  {
		 		 		 
		 		 	     swipeLeft: function() { next(); },
		 		 	     swipeRight: function() { prev(); },
		 		 	});
		 		 	
		 		 	
		  			
		  	});
		  	if (data.typ=="flash") {
		  		$("#watch-presentation").removeClass();
		  		if ($("#it-pres").hasClass('screen-pad')) $("#it-pres").removeClass("screen-pad");
		  		$("#watch-presentation").addClass(data.pres);
		  			
		  		$('#content').stop().css( 'background-position',"50% -30px");
		  		$('#watch').stop().animate({ opacity: 1}, {duration:350}, 'linear'	);
		  		$.loadImages( [ '/works/'+data.nextimg,'/works/'+data.previmg ]  );
		  		$('#it-pres').animate({left: 0,  opacity: 1}, {duration:150});
		  		$("#it-pres").html('<iframe src="/works'+data.img+'" width="960px" height="477px"><img src="/works/motion/flash.png" alt="flash" class="'+data.pres+'"/></iframe>');
		  		$('#it-pres img').swipe(  {
		  			 
		  		     swipeLeft: function() { next(); },
		  		     swipeRight: function() { prev(); },
		  		});
		  		
		  		  			
		  	}
		  	/*	
		  		
		  		
		  	
		  	}*/
	
	},'json');
}}

function prev() 
  {
  	if (id!="no") {
	cur --;  if (cur < 1) cur = max; 
  	$("#watch-nav-num").text(cur);
  	
	$('#it-pres')
		    .animate( {left: '+=30px', opacity: 0} , {duration:150}, 'linear' )
		    .animate( {left: '-=60px' }, {duration:1} );
		
		$('#watch').stop().animate({ opacity: 0}, {duration:150}, 'linear' );
	
	
	    histNum++;
	    
	    	
	  	
		$.get("/ajax/"+id+"/"+cur,
			function(data){
			 _gaq.push(['_trackEvent', 'Site', 'Prev', id+'/'+data.nazwa]);
			if (cur<=15) {
				$("#thumbs").animate( {marginTop: '0px'} , {duration:150}, 'linear' );	
				if (!$("#more-nav-less").hasClass("hidden")) $("#more-nav-less").addClass("hidden");
				$("#more-nav-more").removeClass("hidden");
					
			} else { 
				$("#thumbs").animate( {marginTop: '-90px'} , {duration:150}, 'linear' ); 
				if (!$("#more-nav-more").hasClass("hidden")) $("#more-nav-more").addClass("hidden");
				$("#more-nav-less").removeClass("hidden");
			}
			
			    $("#it-head").removeClass("newitem");
			    $("#it-head").text(data.head2);
			  	$("#it-desc").html(data.opis2);
			   
			   $("#tm-"+itlast).removeClass("selected");
			   itlast = cur;
			   
			    document.title = data.title;
			    History.pushState({state:histNum}, data.title , "/"+ id + "/" + data.nazwa );
			    
			    var vnew = data.vnew;
			    if (data.xnew =="new") {
			    	 
			    	 $("#it-head").addClass("newitem"); 
			    	
			    	 if ($.cookie(data.nazwa)==null) {
			    	  $.cookie(data.nazwa, 'new', { expires: 20, path: '/',domain:'.kwedo.com' });
			    	  vnew++;
			    	 }
			    	 if (vnew >= data.bnew) {
			    	 	newok.fadeOut();
			    	 }
			    }
			    
			    
			    if (data.pres == 'black' || data.pres == 'presentation-bg1' || data.pres =='presentation-kabuki' || data.pres =='presentation-bg2' || data.pres =='presentation-shirt' || data.pres =='presentation-kim') {
			    		$('.next').stop().animate({backgroundPosition:"(-81px 4px)"}, {duration:250});
			    		$('.prev').stop().animate({backgroundPosition:"(0 4px)"}, {duration:250}); 
			    		
			    	} else {
			    	
			    		$('.next').stop().animate({backgroundPosition:"(-54px 4px)"}, {duration:250});
			    		$('.prev').stop().animate({backgroundPosition:"(-27px 4px)"}, {duration:250});
			    	
			    	}
			    
			    $('#content').delay(400).css( 'background-position',"50% 220px"); 
			  	if (data.typ!="flash") $.loadImages( '/works/'+data.img, function() {  
			  			$("#tm-"+cur).addClass("selected");
			  			
			  			$("#watch-presentation").removeClass();
			  			if ($("#it-pres").hasClass('screen-pad')) $("#it-pres").removeClass("screen-pad");
			  			if (data.typ=="screen") $("#it-pres").addClass('screen-pad');
			  			$("#watch-presentation").addClass(data.pres);
			  			$("#it-pres").html('<img src="/works/' + data.img +'" alt="' + data.head2 + '" />');
			  	
			  			$('#content').stop().css( 'background-position',"50% -30px");
			  			$('#watch').stop().animate({ opacity: 1}, {duration:350}, 'linear'	);
			  		    $('#it-pres').stop().animate({left: 0,  opacity: 1}, {duration:150});
			  		 	$.loadImages( [ '/works/'+data.nextimg,'/works/'+data.previmg ]  );
			  		 	
			  		 	$('#it-pres img').swipe(  {
			  		 		 
			  		 	     swipeLeft: function() { next(); },
			  		 	     swipeRight: function() { prev(); },
			  		 	})
			  			
			  	});
			  	if (data.typ=="flash") {
			  		$("#tm-"+cur).addClass("selected");
			  		$("#watch-presentation").removeClass();
			  		if ($("#it-pres").hasClass('screen-pad')) $("#it-pres").removeClass("screen-pad");
			  		$("#watch-presentation").addClass(data.pres);
			  		
			  		$('#content').stop().css( 'background-position',"50% -30px");
			  		$('#watch').stop().animate({ opacity: 1}, {duration:350}, 'linear'	);
			  		$.loadImages( [ '/works/'+data.nextimg,'/works/'+data.previmg ]  );
			  		$('#it-pres').stop().animate({left: 0,  opacity: 1}, {duration:150});
			  		$("#it-pres").html('<iframe src="/works'+data.img+'" width="960px" height="477px"><img src="/works/motion/flash.png" alt="flash"  class="'+data.pres+'"/></iframe>');
			  		$('#it-pres img').swipe(  {
			  			 
			  		     swipeLeft: function() { next(); },
			  		     swipeRight: function() { prev(); },
			  		})
			  	}
		},'json');
}}


$('#it-pres img').swipe(  {
	 
     swipeLeft: function() { next(); },
     swipeRight: function() { prev(); },
})

$(document).jkey('left, right',function(key){
	  if(key == 'right') next();
	  if(key == 'left') prev();
});






});

(function(window,undefined){




	    var History = window.History;
	    if ( !History.enabled ) { // History.js is disabled for this browser.
	        return false;
	    }
		History.Adapter.bind(window,'statechange',function(){ 
	       var State = History.getState();
	       // alert(State.data.state+ " " + State.data.data.head2)
	       if (histNum != State.data.state) {
	         window.location.reload();
	       
	       }
	        // History.log(State.data, State.title, State.url);
	        //alert(State.data);
	    });	
  
	   	    
})(window); 



