	$(function(){
		$started = false;
		$ended = false;
		$rand = parseInt( Math.random() * 2);
		if($.browser.msie){
			$rand = 0;
		}
		$layers = ["/img/header/bg.jpg", "/img/header/layer1.png", "/img/header/layer2.png", "/img/header/layer3.png", "/img/header/layer4.png", "/img/header/layer1.png"];
		$c = 0;
		
		$.preload($layers, {"complete":function(){
			$c++;
			if($c == $layers.length){
				initAnimation();
			}
		}});
	});

	function initAnimation(){
		if($.browser.msie && navigator.appVersion.indexOf("MSIE 7") < 0 && navigator.appVersion.indexOf("MSIE 8") < 0){
			$("div#head").append("<img src='/img/header/layer1.png'/>");
			$("div#head img").fadeIn(500);
			return;
		}
	
		$("div#head").append("<img src='/img/header/layer1.png' l='1'/>");
		$("div#head img").fadeIn(500, function(){
			$(this).mouseover(function(){
				startAnimation();
			});
		});
	}
	
	function startAnimation(){
		if($started !== false) return;
		$started = true;
		insertNextLayer(2);
	}
	
	function endAnimation(){
		if($ended == true)return;
	
		if($rand == 0){
			$("div#head img").wait(2000).animate({
				marginLeft:"30px"
			}, 350, function(){
				$("div#head img[l!=last]").animate({
					marginLeft:"-1200px"
				}, 400, function(){
				
					if($(this).attr("l")*1 > 1){
						$(this).remove();
					} else {
						if($ended == true)return;
						$("div#head").append("<img src='/img/header/layer1.png' l='last'/>");
						$("div#head img[l='last']").fadeIn(1800);
						$ended = true;
					}
				
				});
			});

		} else {
			$("div#head img").wait(2000).fadeOut(1000, function(){
				if($ended == true)return;
				$("div#head").append("<img src='/img/header/layer1.png' l='last'/>");
				$("div#head img[l='last']").fadeIn(1800);
				$ended = true;
			});
		
		}
	}
	
	function insertNextLayer(i){
		if(i > 4){
			endAnimation();
			return;
		}
		if($rand == 0){
			slideIn(i);
		}
		if($rand == 1){
			opacFadeIn(i);
		}

		if(i == 4){
			if($rand == 0){
				shake();
			}
			if($rand == 1){
				pulsate();
			}
		}
	}
	
	
	function opacFadeIn(i){
		$("div#head").append("<img src='/img/header/layer"+i+".png' l='"+i+"' style='z-index:"+i+";'/>");
		$("div#head img[l='"+i+"']").fadeIn((600-(40*i)), function(){
			insertNextLayer(i+1);
		});
	}
	
	function slideIn(i){
		$("div#head").append("<img src='/img/header/layer"+i+".png' l='"+i+"' style='margin-left:-1000px;z-index:"+i+";'/>");
		$("div#head img[l='"+i+"']").show().animate({
			marginLeft:"0px"
		}, (700-(100*i)), function(){
			insertNextLayer(i+1);
		});
	}
	
	
	
	function pulsate(){
		$("div#head img:last").effect("pulsate", {
				times:2
		}, 250);		
	}
	
	function shake(){
		$("div#head img:last").effect("shake", {
				times:2,
				distance:20
		}, 100);
	}
