$(document).ready(function(){

	var audioElement = "";
	
	// Fallback option if the template url has not been set via the Thesis custom_functions.php 
	// function custom_head_add_header_animation
	if(typeof(template_url) == "undefined"){ template_url = "/wp-content/themes/thesis_18"; }
	
	// If the browser is not IE, we need to alleviate any errors by the variable not be initialized
	if(typeof(ie) == "undefined"){ ie = false; }
	if(ie){
		// Use SoundManager2 to create the dinging sound
		$("body").append("<div id=\"ding\" ></div>");
		soundManager.onload = function() {
			soundManager.createSound({
				id:'ding',
				url:template_url+'/lib/sounds/ding.mp3'
			});
			soundManager.load("ding");
			mainAnimation(0);
		}
	}else{
		// Use HTML5 <audio> tag to create the dinging sound
		$("body").append("<audio id=\"ding\" ></audio>");
		var audioElement = document.getElementById('ding');
		audioElement.setAttribute('src', template_url+'/lib/sounds/ding.mp3');
		audioElement.load();
		mainAnimation(audioElement);
	}

});

/*
	mainAnimation is responsible for the header & logo animations
	- sliding into the header area
	- hammering animation
	- loading full logo
*/
function mainAnimation(audioElement){
	var hammerImage = new Image();
	hammerImage.src = template_url+'/lib/images/logo-strip.png';
	var logoImage = new Image();
	logoImage.src = template_url+'/lib/images/logo.png';
	hammerImage.onload = function(){
		// Slide the top navigation, call today & customer care into place
		PlayHeaderAnimations();
		// Play the logo animation
		PlayLogoAnimation(hammerImage, logoImage, audioElement);
	};
}	
	
function PlayHeaderAnimations(){
	setTimeout(function(){$("#call-today").animate({width: "427px"}, 1000);}, 0); //	First
	setTimeout(function(){
		$("#header-right-nav ul").animate({top: "0px"}, 1000);
		$("#customer-care").animate({right: "0px"}, 1000);
	}, 500);	
}

function PlayLogoAnimation(hammer, logofinish, audio){
	var logo = $("#header-logo");
	logo.css("background-image", "url('"+hammer.src+"')");
	logo.animate({width: "208px"});
	logo.animate({left: "35px"});
	setTimeout(function(){
		if(ie){ soundManager.play('ding'); }
		else{ audio.play(); }
		var x = 0;
		var hammerdown = true;
		var hammered = 0;
		var hammer = setInterval(function(){
			if(hammerdown){
				logo.css("background-position", "-"+x+"px 0px");
				x+=209;
				if(x == 627) { hammerdown = false; }
			}
			else{
				logo.css("background-position", "-"+x+"px 0px");
				x-=209;
				if(x == 0){
					hammered++;
					hammerdown = true;
					if(hammered == 2){
						clearInterval(hammer);
					}
				}
			}
		},100);
		setTimeout(function(){
			logo.css("background-image", "url('"+logofinish.src+"')");
			logo.css("background-position", "0px 5px");
			logo.css("width", "281px");
		}, 2000);
	},1000);
}
