
var HeaderEffect = {
	kiwis: [],
	strings: [],
	plasters: [],
	loaded: 0,
	topPos: [85, 130, 180, 220],
	leftPos: [22, 115, 31, 164],
	leftPosKiwi: [22, 115, 31, 164],
	leftPosPlaster: [31, 203, 374],
	slideDuration: 500,
	kiwisDuration: 1000,
	stringsDuration: 10000,
	linksSpeed: 200,
	
	initLinks: function() {
		$("a.headerLinks").each(function() {
			$(this).bind("mouseover", this, function(event) {
				$(event.data).find("span").stop().animate({ top: -38 }, HeaderEffect.linksSpeed);
			}).bind("mouseout", this, function(event) {
				$(event.data).find("span").stop().animate({ top: -50 }, HeaderEffect.linksSpeed);
			});
		});	
		
		$("img.headerLinks").each(function() {
			$(this).bind("mouseover", this, function(event) {
				$(event.data).attr("src", "/images/header/facebook2.png");
				$(event.data).addClass("headerLinks");
			}).bind("mouseout", this, function(event) {
				$(event.data).attr("src", "/images/header/facebook.png");
			});
		});	
		
	},
	
	init: function() {
		for(i = 1; i <= 4; i++) {
			k = new Image();
			s = new Image();			
			$(k).css("position", "absolute").css("left", -92).css("top", this.topPos[i - 1]).appendTo($("body"));
			$(s).css("position", "absolute").css("left", -241).css("top", this.topPos[i - 1]).appendTo($("body"));;
			k.src = "/images/header/letters/kiwi" + i + ".png";
			s.src = "/images/header/letters/string" + i + ".png";
			this.kiwis[i - 1] = k;
			this.strings[i - 1] = s;
		}
		for(i = 1; i <= 3; i++) {
			k = new Image();			
			$(k).css("position", "absolute").css("left", -168).css("top", 100).appendTo($("body"));
		
			k.src = "/images/header/letters/plaster.png";
			this.plasters[i - 1] = k;
		}
		
			HeaderEffect.slideInPlaster(2);
	},
	
	submitLogin: function() {
		$("form#loginForm").get(0).submit();
	}, 
	
	
	slideInPlaster: function(number) {
		var l = ($(document).width() - 1013) / 2 + this.leftPosPlaster[number];
		
		$(this.plasters[number]).animate(
  			{left: l}, 
  			{ duration: this.slideDuration, easing: 'easeOutExpo', complete: function() {
	  			
	  			var ind	= HeaderEffect.plasters.indexOf(this);
	  			
	  			if(ind > 0)
	  				HeaderEffect.slideInPlaster(ind - 1);
	  				else setTimeout('HeaderEffect.slideOutPlaster(0)', HeaderEffect.stringsDuration);
	  	}});
	},
	
	slideOutPlaster: function(number) {
		
		$(this.plasters[number]).animate(
  			{left: -168}, 
  			{ duration: this.slideDuration, easing: 'easeOutExpo', complete: function() {
	  			
	  			var ind	= HeaderEffect.plasters.indexOf(this);
	  			
	  			if(ind == 0)
	  				HeaderEffect.slideOutPlaster(1);
	  			else if(ind == 1)
	  				setTimeout('HeaderEffect.slideInKiwis(0)', HeaderEffect.kiwisDuration);
	  			else setTimeout('HeaderEffect.slideInStrings(0)', HeaderEffect.kiwisDuration);
	  	}});
	},
	
	slideInKiwis: function(number) {
		var l = ($(document).width() - 1013) / 2 + this.leftPosKiwi[number];
		
		$(this.kiwis[number]).animate(
  			{left: l}, 
  			{ duration: this.slideDuration, easing: 'easeOutExpo', complete: function() {
	  			
	  			var ind	= HeaderEffect.kiwis.indexOf(this);
	  			
	  			if(ind < 3)
	  				HeaderEffect.slideInKiwis(ind + 1);
	  				else setTimeout('HeaderEffect.slideOutKiwis(0)', HeaderEffect.kiwisDuration);
	  	}});
	},
	
	slideOutKiwis: function(number) {
		
		$(this.kiwis[number]).animate(
  			{left: -92}, 
  			{ duration: this.slideDuration, easing: 'easeOutExpo', complete: function() {
	  			
	  			var ind	= HeaderEffect.kiwis.indexOf(this);
	  			
	  			if(ind < 3)
	  				HeaderEffect.slideOutKiwis(ind + 1);
	  			else HeaderEffect.slideOutPlaster(2);
	  	}});
	},
	
	slideInStrings: function(number) {
		var l = ($(document).width() - 1013) / 2 + this.leftPos[number];
		
		$(this.strings[number]).animate(
  			{left: l}, 
  			{ duration: this.slideDuration, easing: 'easeOutExpo', complete: function() {
	  			
	  			var ind	= HeaderEffect.strings.indexOf(this);
	  			
	  			if(ind < 3)
	  				HeaderEffect.slideInStrings(ind + 1);
	  			else setTimeout('HeaderEffect.slideOutStrings(0)', HeaderEffect.stringsDuration);
	  	}});
	},
	
	slideOutStrings: function(number) {
		var l = ($(document).width() - 1013) / 2 + this.leftPos[number];
		$(this.strings[number]).animate(
  			{left: -241}, 
  			{ duration: this.slideDuration, easing: 'easeOutExpo', complete: function() {
	  			
	  			var ind	= HeaderEffect.strings.indexOf(this);
	  			
	  			if(ind < 3)
	  				HeaderEffect.slideOutStrings(ind + 1);
	  			else {
	  				/*for(i = 0; i < 4; i ++) {
	  						var r1 = Math.floor(Math.random() * 4);
	  						var r2 = Math.floor(Math.random() * 4);
	  						 
	  						var tmpImg = HeaderEffect.strings[r1];
	  						HeaderEffect.strings[r1] = HeaderEffect.strings[r2]; 
	  						HeaderEffect.strings[r2] = tmpImg;
	  						
	  						$(HeaderEffect.strings[r1]).css("top", HeaderEffect.topPos[r1]);
	  						$(HeaderEffect.strings[r2]).css("top", HeaderEffect.topPos[r2]);
	  						
	  						var tmpLeft = HeaderEffect.leftPos[r1];
	  						HeaderEffect.leftPos[r1] = HeaderEffect.leftPos[r2]; 
	  						HeaderEffect.leftPos[r2] = tmpLeft;
	  					}*/
	  				setTimeout('HeaderEffect.slideInPlaster(2)', HeaderEffect.kiwisDuration);
	  				
	  				}
	  	}});
	},
	
	slideInKiwiFooter: function(event) {
		var l = -($(document).width() - 1013) / 2;

		if($("img.kiwiFooter2").css("right") == 100 + "px") {
			$("img.kiwiFooter2").animate(
  			{right: l}, 
  			{ duration: this.slideDuration, easing: 'easeOutExpo', complete: function() {
	  			$("img.kiwiFooter2").hide();	
	  			$("img.kiwiFooter").show().animate(
	  				{right: 0}, 
  					{ duration: this.slideDuration, easing: 'easeOutExpo'});
	  			
	  		}});
		} else if($("img.kiwiFooter").css("right") == 0 + "px") {
			$("img.kiwiFooter").animate(
  			{right: l}, 
  			{ duration: this.slideDuration, easing: 'easeOutExpo', complete: function() {
	  			$("img.kiwiFooter").hide();	
	  			$("img.kiwiFooter2").show().animate(
	  				{right: 100}, 
  					{ duration: this.slideDuration, easing: 'easeOutExpo'});
	  			
	  		}});
		}
	  	},
	
	
	cityChanged: function(event) {
	
	var c= event.data.city;
	var d = event.data.district;
	
	var newD = $("<select>", {
  							"name": d.name,
  							"className": d.className,
  							"id": d.id
  							})[0];
  	if(event.data.empty)	  		
  		newD.options[0] = new Option("dzielnica", "");
	
	for(var i = 0; i < $(Kiwi.cities).size(); i++)	{
  		var item = Kiwi.cities[i];
  		if(item.id != c.value) continue;
		
		for(var j = 0; j < $(item.districts).size(); j++) {
  			var tmp = item.districts[j];
  			
  			var ind = j;
  			if(event.data.empty) ind++;
  			
  			newD.options[ind] = new Option(tmp.name, tmp.id);
  			}
  		
		}
		
	$(d).parent().find("a").remove();	
	$(d).parent().append(newD);
	$(d).remove();
	$(newD).selectmenu({style: 'dropdown'});
	event.data.district = newD;
	},
	
	initArrows: function() {
		 $("a.searchHeader").each(function() {
			 	var obj = {active: 0, arrow: $(this).find("span").get(0)};
		 		$(this).bind("mouseover",obj, function(ev) {
			 			$(obj.arrow).stop().animate(
			 		  			{right: 20}, 
			 		  			{ duration: HeaderEffect.arrowDuration });
		 				});
		 		$(this).bind("mouseout",obj, function(ev) {
		 			$(obj.arrow).stop().animate(
		 		  			{right: 60}, 
		 		  			 {duration: HeaderEffect.arrowDuration });
		 				});
		 		HeaderEffect.arrows.push(obj);
		 		});
	},
	
	arrows: [],
	arrowDuration: 400
} 


$(document).ready(function() {
	HeaderEffect.initLinks();
	HeaderEffect.initArrows();
	
	
	$("span.footerImageDim").hide();
	$("div.patronageFooter").each(function() {
		$(this).bind("mouseover", $(this).find("span.footerImageDim").get(0), function(event) {
				$(event.data).show();
			})
			.bind("mouseout", $(this).find("span.footerImageDim").get(0), function(event) {
			$(event.data).hide();
			});
	});
	 $('select#citySearch').bind("change", { 
	 		city: $('select#citySearch').get(0), 
	 		district: $('select#districtSearch').get(0),
	 		empty: true
	 		}, 
	 					HeaderEffect.cityChanged);
	 					
	 $('select.search').selectmenu({style: 'dropdown'});
	 $('input.search').datepicker({ dateFormat: 'dd/mm/y', changeYear: true, 
	 		changeMonth: true, yearRange: '2011:2014',
	 		beforeShow: function(input, inst) {$($(input).parent()).addClass("ui-state-active");},
	 		onClose: function(dateText, input) { $($(this).parent()).removeClass("ui-state-active");} });
	 $("input.search").each(function() {
	 		$($(this).parent()).bind("mouseover",$(this).parent(), function(ev) {$(ev.data).addClass("ui-state-hover");});
	 		$($(this).parent()).bind("mouseout",$(this).parent(), function(ev) {$(ev.data).removeClass("ui-state-hover");});
	 		$(this).bind("mouseover", this, function(ev) {$(ev.data).css("color", "white");});
	 		$(this).bind("mouseout", this, function(ev) {$(ev.data).css("color", "black");});
	 		
	 		});
});
