jQuery(document).ready(function () {
	
	var $ = jQuery;
	
	var topImageHeight = getCookie("topImage");
	
	//maketopImageSetShort();

	if(topImageHeight == 1) {
		maketopImageTall();
	} 
	if(topImageHeight == 0) {
		maketopImageSetShort();
	}
	// topImage rotation ---------------------------------------
	
	function rotateImages(y) {
		$.each(texts, function(key, val) {			
			if (key != y) {				
				$(val).fadeOut(2500);
			}else{		
				$(val).fadeIn(2500);
			}
		});					
		$.each(imgs, function(key, val) {			
			if (key != y) {				
				$(val).fadeOut(2500);
			}else{
				$(val).fadeIn(2500);
			}
		});	
	}	
	
	// Animations ------------------------------------------------------
		
	function maketopImageTall(){		
		$(".topImage").animate({height:"405"},980);
		$("#headerimages").animate({top:"0"},980);	
		$(".makeshort").addClass("block");
		$(".enlarge").addClass("invisible");
		$(".makeshort").removeClass("invisible");		
		setCookie("topImage","1",30);
	}	
	
	function maketopImageShort(){		
		$(".topImage").animate({height:"211"},980);
		$("#headerimages").animate({top:"-97"},980);	
		$(".enlarge").removeClass("invisible");
		$(".enlarge").addClass("block");
		$(".makeshort").addClass("invisible");		
		setCookie("topImage","0",30);
	}
	function maketopImageSetShort(){		
		$(".topImage").animate({height:"211"},0);
		$("#headerimages").animate({top:"-97"},0);

		//$(".topImage").css('height','211px');	
		//$("#headerimages").css('top','-97px');	
		
		$(".enlarge").removeClass("invisible");
		$(".enlarge").addClass("block");
		$(".makeshort").addClass("invisible");		
		setCookie("topImage","0",30);
	}	
	// Cookies ---------------------------------------------------------------
	
	function getCookie(name) {
		var nameq = name + "=";
		var cookie_array = document.cookie.split(';');
		for (var i=0; i<cookie_array.length; i++) {
			var cookie = cookie_array[i];
			while (cookie.charAt(0)===' ') {
				cookie = cookie.substring(1,cookie.length);
			}
			if (cookie.indexOf(nameq) === 0) {
				return unescape( cookie.substring(nameq.length, cookie.length) );
			}
		}
		return null;
	}
	
	function setCookie(name,value,days,path,domain,secure) {
		var expires, date;
		if (typeof days == "number") {
			date = new Date();
			date.setTime( date.getTime() + (days*24*60*60*1000) );
			expires = date.toGMTString();
		}
		document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	}		
	
	// Fun -------------------------------------------------------------------

	$(".enlarge").click(maketopImageTall);
	$(".makeshort").click(maketopImageShort);
	
	$(".imageInfo").toggle(
    	function () {
      		$(".text_wrap").css('top','0px');
			$(".toggler").animate({
				"height":88,
				"width":337
			});
      	},
      	function () {
			$(".toggler").animate({
				"height": 28,
				"width": 337
			}, 500, function() {
				$(".text_wrap").css('top','18px');	
			});		
      	}
    );
	
	var imgs = $(".headerpic").hide();
	var texts= $(".text_wrap").hide();
	var y=1;	
	rotateImages(0);
	
	$(document).everyTime('10s', 'kvTimer', function() {
		if (y >= imgs.length) {
			y = 0;
		}
		rotateImages(y);
		y++;
	});
		
		
	
});	

