$(function () {
	
	$.trackPage('UA-402536-17', {onload: true});
	$('.meni a').track({
		category: function(element) { return element.parent().parent().prev().prev().attr("name"); },
		action: function(element) { return element.text(); },
		label : "menu",
		skip_internal: false
	});
	$('.footer-kontakt').parent().track({
		category: function(element) { return element.parent().parent().parent().find(".anchor").attr("name"); },
		action: "kontakt",
		label : "footer",
		skip_internal: false
	});
	$('.footer-domov').parent().track({
		category: function(element) { return element.parent().parent().parent().find(".anchor").attr("name"); },
		action: "domov",
		label : "footer",
		skip_internal: false
	});
	$('.footer-next1, .footer-next2').parent().track({
		category: function(element) { return element.parent().parent().parent().find(".anchor").attr("name"); },
		action: function(element) { return element.attr("href").replace("#", ""); },
		label : "footer",
		skip_internal: false
	});
	
	$("a.logo").frameAnimation({delay:30, repeat:2, mouseOut:false});
	
    var strankeDiv = $(".stranke");
	var strankeCont = $(".strankeCont", strankeDiv);
	var stranke = $(".stranka", strankeCont);
	var strankeNum = stranke.size();
	var strankaWidth = stranke.width();
	
	var t = setTimeout(function(){slideNextClient(strankeCont);}, 5000);
	
	function slideNextClient(obj){
        
        obj.animate({"left": -1*strankaWidth + "px"}, 500, function(){
						var s = $(".stranka", strankeCont).eq(0);
						s.clone().appendTo(strankeCont);
						$(this).css("left", 0);
						s.remove();
						//if ($("#slideshow").is(":not(:visible)"))
						t = setTimeout(function(){ slideNextClient(strankeCont);}, 5000);
		});
    }
	
	
	
	var projekti = $(".projekt");
	var projekt = projekti.eq(0);
	var items = $(".items", projekti.eq(0));
	var slides = $(".slide", items);
	slides.find("img.abs:not(:first)").css({"opacity":0, "z-index":0});
	slides.find(".features div:first").css({"opacity":1, "z-index":0, "display":"block"});
	slides.find(".features div:not(:first)").css({"opacity":0, "z-index":0, "display":"none"});
	
	var s = slides.eq(0);
	$("#top").show();
	var slideWidth = s.width();
	$("#top").hide();
	$(".down").hide();
	var startPIndex = 0;
	
	$("#top").css("display", "block");
	var projektHeight = projekti.eq(0).outerHeight() + parseInt(projekti.eq(0).css("marginTop"))+ parseInt(projekti.eq(0).css("marginBottom"));
	$("#top").css("display", "none");
				
	
	$(window).bind("resize", resizeWindow);
	
	function resizeWindow( e ) {
		var newWindowHeight = $(window).height();
		$("#top").height(newWindowHeight);
	}
	
	$(".lazy").lazyload({ 
	    effect : "fadeIn" 
	  });
	
	  var q = $.getUrlVar('q');
	  if(q){
		swfobject.embedSWF("swfs/sticky.swf?q=" + q, "sticky", "255", "250", "9.0.0","expressInstall.swf", null, params, null);
		$('#sticky').fadeIn(1000).fadeTo(5000, 1).fadeOut(1000);	
	  }
		var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
		
		$("#top").height(viewportHeight);
							
	
		$(".meni-projekti").click(function(e){
			e.preventDefault();
			$("#top").slideToggle("slow", function(){ 
				 if ($("#slideshow").is(":visible"))
					startRoll();
			});
			$(this).toggleClass("active");
		 });
		
		$(".stranka", strankeCont).live("click",function(){
			$("#top").slideToggle("slow", function(){ 
				 if ($("#slideshow").is(":visible"))
					startRoll();
			});
		 });
		
		$(".close").click(function(){
			$("#top").slideToggle("slow");
			$(".meni-projekti").removeClass("active");
		});
		
	var c = 0;
	
	$(".client img").each(function(){
		var rot = (Math.random()*6)-3;
		var img = $(this);
		img.data("rotation", rot);
		img.css({"-moz-transform":"rotate("+ rot +"deg)", "-webkit-transform":"rotate("+ rot +"deg)", "-o-transform":"rotate("+ rot +"deg)", "transform":"rotate("+ rot +"deg)"});
		img.parent().data("index", c);
		c++;
		
		img.parent().click(function(){
			$("#clients").hide();
			$("#slideshow").show();
			setIndex($(this).data("index"), items, slides);
		});
		
		img.parent().hover(function(){
			img.css({"-moz-transform":"rotate(0deg)", "-webkit-transform":"rotate(0deg)", "-o-transform":"rotate(0deg)", "transform":"rotate(0deg)"});
		}, 	function(){
				if (img.hasClass("active") == false)
				img.css({"-moz-transform":"rotate("+ rot +"deg)", "-webkit-transform":"rotate("+ rot +"deg)", "-o-transform":"rotate("+ rot +"deg)", "transform":"rotate("+ rot +"deg)"});
			});
	});
	
	$(".back").click(function(){
		backToClients();
	});
	
	function backToClients(){
		$(".client img").removeClass("active");
		$(".client img").each(function(){
			$(this).css({"-moz-transform":"rotate("+ $(this).data("rotation") +"deg)", "-webkit-transform":"rotate("+ $(this).data("rotation") +"deg)", "-o-transform":"rotate("+ $(this).data("rotation") +"deg)", "transform":"rotate("+ $(this).data("rotation") +"deg)"});
		});
		$(".client img").eq(items.data("index")).addClass("active").css({"-moz-transform":"rotate(0deg)", "-webkit-transform":"rotate(0deg)", "-o-transform":"rotate(0deg)", "transform":"rotate(0deg)"});
		$("#slideshow").hide();
		
		$("#clients").show();
	}
	
	
	function nextThumb(){
		var index = parseInt(items.data("index"));
		var thumbs = $(".client img");
		thumbs.eq(index).removeClass("active").css("-moz-transform", "rotate("+ thumbs.eq(index).data("rotation") +"deg)");
		if (index < thumbs.length - 1) index++;
		items.data("index", index);
		thumbs.eq(items.data("index")).addClass("active").css("-moz-transform", "rotate(0deg)");
		setIndex(index);
	}
	
	function prevThumb(){
		var index = parseInt(items.data("index"));
		var thumbs = $(".client img");
		thumbs.eq(index).removeClass("active").css("-moz-transform", "rotate("+ thumbs.eq(index).data("rotation") +"deg)");
		if (index > 0 ) index--;
		items.data("index", index);
		thumbs.eq(items.data("index")).addClass("active").css("-moz-transform", "rotate(0deg)");
		setIndex(index);
	}
	
	function downThumb(){
		var index = parseInt(items.data("index"));
		var thumbs = $(".client img");
		thumbs.eq(index).removeClass("active").css("-moz-transform", "rotate("+ thumbs.eq(index).data("rotation") +"deg)");
		if (index < thumbs.length - 5) index+=5;
		items.data("index", index);
		thumbs.eq(items.data("index")).addClass("active").css("-moz-transform", "rotate(0deg)");
		setIndex(index);
	}
	
	function upThumb(){
		var index = parseInt(items.data("index"));
		var thumbs = $(".client img");
		thumbs.eq(index).removeClass("active").css("-moz-transform", "rotate("+ thumbs.eq(index).data("rotation") +"deg)");
		if (index > 4 ) index-=5;
		items.data("index", index);
		thumbs.eq(items.data("index")).addClass("active").css("-moz-transform", "rotate(0deg)");
		setIndex(index);
	}
	
	
	function slideToIndex(index, items, slides){
		items.data("index", index);
		var s = slides.eq(0);
		
		var imageView = $(".imageView", slides.eq(index));
		var images = imageView.find("img.abs");
		
		var features = $(".features", slides.eq(index));
		
		if (isNaN(imageView.data("current"))){
			imageView.data("current", 0);
			var imgNum = images.length;
			imageView.data("imgNum", imgNum);
		}
		if (isNaN(features.data("current"))){
			features.data("current", 0);
		}
		
		images.eq(imageView.data("current")).css({"z-index": 1, "opacity":1});
	
		items.animate({"left":-1*(index * (parseInt(s.width()) + parseInt(s.css("marginLeft"))+ parseInt(s.css("marginRight")) + parseInt(s.css("paddingLeft")) + parseInt(s.css("paddingRight"))))}, speed, startRoll);
		
		var projekt = items.parents(".projekt");
		
		updatePositionCircles(projekt);
		
		if (index == 0) $(".prev", projekt).css("visibility", "hidden");
		else
			$(".prev", projekt).css("visibility", "visible");
		if (index == slides.size()-1) $(".next", projekt).css("visibility", "hidden");
		else
			$(".next", projekt).css("visibility", "visible");
	
	}
	
	function setPositionCircles(projektIndex){
			var positionCircles = $("#positionCircles");
			var positionHtml = "";
		
			var currentIndex = items.data("index");
			for (var i=0;i<slides.size(); i++){
				if (i == currentIndex)
					positionHtml += "<span class='active'>&bull;</span> ";
				else
					positionHtml += "<span>&bull;</span> ";
			} 
			positionCircles.html(positionHtml);
			
		
				var spans = positionCircles.find("span");
				spans.click(function(){
					var index = spans.index($(this));
					slideToIndex(index, items, slides);
				});
		
	}
	
	function updatePositionCircles(projekt){
			var items = $(".items", projekt);
			var positionCircles = $("#positionCircles");
			$("span.active", positionCircles).removeClass("active");
			$("span", positionCircles).eq(items.data("index")).addClass("active");
			
	}
	
	$("#slideshow").hide();
	
	$(document).keydown(keyDown);
	
	function keyDown(e) {
	  switch (e.keyCode) {
	   case 8: //BACKSPACE
	    if ($("#slideshow").is(":visible"))
	   		backToClients();
	   break;
	   case 13: //ENTER
		if ($("#slideshow").is(":visible"))
	   		$(".back").click();
		else {
			$("#clients").hide();
			$("#slideshow").show();
		}
		break;
	   case 27: // ESCAPE
		$("#top").slideUp("slow");
		$("div#topline a.active").removeClass("active");
	   break;
	   case 37: // LEFT
		if ($("#slideshow").is(":visible")){
		$(".prev").click();
		}
		else{
			prevThumb();
		}
	    break;
	   case 38: // UP
	    if (!$("#slideshow").is(":visible"))
	   		upThumb();
	    break;
	   case 39: // RIGHT
	    if ($("#slideshow").is(":visible"))
	   		$(".next").click();
		else
			nextThumb();
	    break; 
	   case 40: // DOWN
	   	if (!$("#slideshow").is(":visible"))
	   		downThumb();
	    break;
	   //default:
		//alert(e.keyCode);
	  }

	 }
	
	
	var speed = 500;
	
	function startRoll(){
	
		var index = items.data("index");
	 	s = slides.eq(index);
		imageView = $(".imageView", s);
		images = imageView.find("img.abs");
		curr = imageView.data("current");
		
		imageView.animate({"left": 0}, 8*speed, function(){
			items = $(".items", projekt);
			slides = $(".slide", items);
			if (items.data("index") == index && $("#slideshow").is(":visible")){
		 	s = slides.eq(index);
			imageView = $(".imageView", s);
			curr = imageView.data("current");
			images = imageView.find("img.abs");
			images.eq(curr).animate({"opacity":0,"z-index":1}, speed, function(){$(this).css("z-index",0); });						
			var features = $(".features", s);
			var featureDivs = $("div", features);
			
			
			var currFeature = features.data("current");
			
			featureDivs.eq(currFeature).animate({"opacity":0,"z-index":1}, speed, function(){$(this).css("display","none"); });
			
			if (curr >= images.length-1)
			curr = 0;
			else
			curr++;
			imageView.data("current", curr);
			images.eq(curr).animate({"opacity":1,"z-index":0}, speed, function(){$(this).css("z-index",1); });						//featureDivs.eq(curr).slideUp();
			
			if (currFeature >= featureDivs.length-1)
			currFeature = 0;
			else
			currFeature++;
			features.data("current", currFeature);
			
				featureDivs.eq(currFeature).animate({"opacity":1,"z-index":0}, speed, function(){$(this).css("display","block"); });	
			
			startRoll();
			}
		});
	
	}
	

	
	function setIndex(index){
		items.data("index", index);
		
		items.css({"left":-1*(index * (slideWidth + parseInt(s.css("marginLeft"))+ parseInt(s.css("marginRight")) + parseInt(s.css("paddingLeft")) + parseInt(s.css("paddingRight"))))});
		
		var projekt = items.parents(".projekt");
		
		setPositionCircles(index);
		
		if (index == 0) $(".prev", projekt).css("visibility", "hidden");
		else
			$(".prev", projekt).css("visibility", "visible");
		if (index == slides.size()-1) $(".next", projekt).css("visibility", "hidden");
		else
			$(".next", projekt).css("visibility", "visible");
			
			var imageView = $(".imageView", slides.eq(index));
			var images = imageView.find("img.abs");
			if (isNaN(imageView.data("current"))){
				imageView.data("current", 0);
				var imgNum = images.length;
				imageView.data("imgNum", imgNum);
			}
			images.eq(imageView.data("current")).css("z-index", 1);
			
			var features = $(".features", slides.eq(index));
			
			if (isNaN(features.data("current"))){
				features.data("current", 0);
			}
		
		
		startRoll();
	}
	
	function setProject(index){ 
		
		setPositionCircles(index);
		
		startPIndex = index;
		
		var offset = -1*(index*projektHeight);
		
		$("#projekti").css({"top":offset});
		
		
		if (index == 0) $(".up").css("visibility", "hidden");
		else
			$(".up").css("visibility", "visible");
		if (index == projekti.size()-1) $(".down").css("visibility", "hidden");
		else
			$(".down").css("visibility", "visible");
	
	}
	
	function changeProject(index){
		$(".nav").css("opacity", 0);
		projekti.eq(index).data("index", index);
	
		
		var s = projekti.eq(0);
		var offset = -1*(index * (parseInt(s.height()) + parseInt(s.css("marginTop"))+ parseInt(s.css("marginBottom")) + parseInt(s.css("paddingTop")) + parseInt(s.css("paddingBottom"))));
		$("#projekti").animate({"top":offset}, speed, function(){ 								  $(".nav").css("opacity", 1);
		setPositionCircles(index);
		});
		
		if (index == 0) $(".up").css("visibility", "hidden");
		else
			$(".up").css("visibility", "visible");
		if (index == projekti.size()-1) $(".down").css("visibility", "hidden");
		else
			$(".down").css("visibility", "visible");
	
	}
		
	$(".up").click(function(){
		startPIndex--;
		if (startPIndex < 0) startPIndex = 0;
		changeProject(startPIndex);
	});
	
	$(".down").click(function(){
		startPIndex++;
		if (startPIndex > projekti.size()-1) startPIndex = projekti.size()-1;
		changeProject(startPIndex);
	});
		
	
	$(".up").css("visibility", "hidden");
	
		
	$(".projekt").each(function(){	
	var projekt = $(this);
	
	var startIndex = 0;
	var perPage = 1;  
	var items = $(".items", projekt);
	var pane = items.parent();
	var slides = $(".slide", items);
	var pSize = slides.size();
	
	var flor = Math.floor(perPage*0.5);	
	
	var nextt = $(".next", projekt);
	var prevv = $(".prev", projekt);
	
	var tabs = $("#flowtabs", projekt);
	
	slideToIndex(startIndex, items, slides);
	
	
	function prevSlide(){
		
				var pIndex = projekti.index($(this).parent().parent());
				var projekt = projekti.eq(pIndex);
				var items = $('.items', projekt);
				var slides = $('.slide', items);
				
				var currentIndex = items.data("index");
				currentIndex--;

				if (currentIndex < 0) 
					currentIndex = 0; 
				else 
					slideToIndex(currentIndex, items, slides);
			return false;
	}
	
	function nextSlide(){
			
				var pIndex = projekti.index($(this).parent().parent());
				var projekt = projekti.eq(pIndex);
				var items = $('.items', projekt);
				var slides = $('.slide', items);
				var currentIndex = items.data("index");
				
				currentIndex++;

				if (currentIndex > slides.size()-1) 
					currentIndex = slides.size()-1; 
				else 
					slideToIndex(currentIndex, items, slides);

			return false;
	}
	
	$(".prev", projekt).click(prevSlide);

	$(".next", projekt).click(nextSlide);
	
	}); // EACH END
	
	
	
});	
			
			
	
