/*
	home.js for hellosweetbird.com writen by www.featurecreep.com
	You are free to use any code you find in this file as long as you credit www.featurecreep.com
	comments or questions to info@featurecreep.com
*/

/*************************************************************************************
	INITIALISE SCREEN
*************************************************************************************/
var windowLoaded = false;
window.onload = function(){windowLoaded = true}

function initialiseScreen(){

	//alert(getCurrentStyle(document.getElementById("css-media-marker"), "visibility", "visibility"));
	
	
	//if this element has a current style of visible the screen stylesheet has loaded, and so go ahead with screen JavaScript
	//if(getCurrentStyle(document.getElementById("css-media-marker"),"visibility","visibility") == "hidden")
	
	//JAVASCRIPT CSS RULES//
	appendToClassName(document.getElementsByTagName("HTML")[0], "JavaScript");
	
	//DIAGNOSTICS//
	//initialiseDiagnostics();

	//GLOBALS//
	window.platform = sniffPlatform();
	window.browser = sniffBrowser();
	
	var bodyClass = document.body.className;
	window.nodeId = parseInt(bodyClass.substr(bodyClass.indexOf("node") + 4));
	

	//ELEMENTS BY CLASS//
	window.classNameHash = new Array();
	createClassNameHash();
	
	//LINK FOCUS//
	var links = document.body.getElementsByTagName("A");
	for(var i = 0 ; i < links.length ; i++) links[i].onfocus = function(){this.blur()};
	
	//GLOBAL ROLLOVERS//
	globalRollovers();
	
	//NUTRITIONAL INFORMATION
	addNutritionalInformationHandlers();
	
	//RECIPECOLLECTION//
	if(document.body.className.indexOf("RecipeCollection") > -1) RecipeCollection();
	
	//SYRUPS FLAVOURS
	if(document.body.className.indexOf("flavours") > -1) flavours();
	
	//IMAGE ROTATIONS
	rotatingImages();
	
}


/*************************************************************************************
	ROTATING IMAGES
*************************************************************************************/
function rotatingImages(){
	if(browser == "safari1") return;

	var contentDiv = document.getElementById("content");
	
	if(document.body.className.indexOf("smoothies ") > -1 && document.body.className.indexOf("RecipeCollection") < 1) {
		var rand = Math.floor((2-0) * Math.random()) + 1;
		contentDiv.style.backgroundImage = "url(/images/backgrounds/smoothies" + rand + ".jpg)";
		
	} else if(document.body.className.indexOf("iceBlendedSmoothies") > -1) {
		var rand = Math.floor((3-0) * Math.random()) + 1;
		contentDiv.style.backgroundImage = "url(/images/backgrounds/ice-blended-smoothies" + rand + ".jpg)";
	}
}

/*************************************************************************************
	GLOBAL RLLOVERS
*************************************************************************************/
function globalRollovers(){	
	if(browser == "safari1") return;
	var globalNavLinks = document.getElementById("globalNavigation").getElementsByTagName("LI");
	for(var i = 0 ; i < globalNavLinks.length ; i++){
		//Swap the background images from Safari1 safe to flipable versions
		globalNavLinks[i].style.backgroundImage = getCurrentStyle(globalNavLinks[i],"background-image", "backgroundImage").replace("basic","full");
		
		if(globalNavLinks[i].className.indexOf("selected")>-1) globalNavLinks[i].style.backgroundPosition = "0px -34px";
		var h = globalNavLinks[i].offsetHeight;
		var a = globalNavLinks[i].getElementsByTagName("A")[0];
		addHandlers(globalNavLinks[i], a, h);
	}
	
	//Product pages
	var flavoursLinkDiv = classNameHash.flavoursLink;
	if(flavoursLinkDiv){
		var flavoursLink = flavoursLinkDiv[0].getElementsByTagName("SPAN")[0];
		var h = flavoursLink.offsetHeight;
		var a = flavoursLink.getElementsByTagName("A")[0];
		addHandlers(flavoursLinkDiv[0], a, h);
	}
	
	// Smoothies-specific links
	var iceBlendedSmoothiesLinkDiv = classNameHash.iceBlendedSmoothiesLink;
	if(iceBlendedSmoothiesLinkDiv) {
		var iceBlendedSmoothiesLink = iceBlendedSmoothiesLinkDiv[0].getElementsByTagName("SPAN")[0];		
		var h = iceBlendedSmoothiesLink.offsetHeight;
		var a = iceBlendedSmoothiesLink.getElementsByTagName("A")[0];
		addHandlers(iceBlendedSmoothiesLinkDiv[0], a, h);
	}
	
	var fruitSmoothiesLinkDiv = classNameHash.fruitSmoothiesLink;
	if(fruitSmoothiesLinkDiv) {
		var fruitSmoothiesLink = fruitSmoothiesLinkDiv[0].getElementsByTagName("SPAN")[0];		
		var h = fruitSmoothiesLink.offsetHeight;
		var a = fruitSmoothiesLink.getElementsByTagName("A")[0];
		addHandlers(fruitSmoothiesLinkDiv[0], a, h);
	}
	
}
function addHandlers(container, a, offset){
	a.onmouseover = function(){
		if(container.className != "selected"){
			container.style.backgroundPosition = ("0px -" + offset + "px");
		}
	};
	a.onmouseout = function(){
		if(container.className != "selected") container.style.backgroundPosition = "0px 0px";
	};
}



/*************************************************************************************
NUTRITIONAL INFORMATION
*************************************************************************************/
function addNutritionalInformationHandlers(){
	var divs = classNameHash.uls;

	if (divs) {
		for(var j = 0 ; divs[j] ; j++){
			var anchors = divs[j].getElementsByTagName("A");
			
			for (var i = 0 ; i < anchors.length ; i++){
				anchors[i].onmouseover = function(){
					this.parentNode.style.backgroundPosition = ("0px -17px");
					this.style.color = "#ffffff";
				};
				anchors[i].onmouseout = function(){
					this.parentNode.style.backgroundPosition = "0px 0px";
					this.style.color = "#666666";
				};
			}
		}
	}
}




/*************************************************************************************
RECIPES
*************************************************************************************/

function RecipeCollection(){

	//Add print button
	var div = document.createElement("DIV");
	div.id = "printButton";
	div.innerHTML = '<a href="javascript:print();"><span>print</span></a>';
	document.getElementById("content").appendChild(div);
	div.onmouseover = function(){this.style.backgroundPosition = "0px -17px"};
	div.onmouseout = function(){this.style.backgroundPosition = "0px 0px"};
	
	
	
	var recipeDiv = classNameHash.recipes;
	var h2s = recipeDiv[0].getElementsByTagName("H2");
	for(var i = 0 ; i < h2s.length ; i++){
			
		h2s[i].onmouseover = function(){
			this.style.backgroundPosition = this.className == "expanded" ? "0 -51px" : "0 -17px";
			this.style.color = "white";
		}
		h2s[i].onmouseout = function(){
			this.style.backgroundPosition = this.className == "expanded" ? "0 -34px" : "0 0";
			this.style.color = this.className == "expanded" ? "white" : "#666666";
		}
	
		h2s[i].onmousedown = function(){
			this.className = "expanded";
			var div = this.parentNode.getElementsByTagName("DIV")[0];
			if (div.className.indexOf("boxout") > -1 && div.className.indexOf("boxoutExpanded") == -1){
				div.className = div.className.replace("boxout","boxoutExpanded");
			}
			else{
				this.className = "over";
				div.className = div.className.replace("boxoutExpanded", "boxout");
			}
			this.onmouseover();
		}
	}
	
	var recipe = document.getElementById(document.location.hash.substr(1));
	if(recipe && browser != "msie5mac"){
		window.locationRecipe = recipe;
		setTimeout("locationRecipe.getElementsByTagName('H2')[0].onmousedown()",500);
		setTimeout("locationRecipe.getElementsByTagName('H2')[0].onmouseout()",700);
		setTimeout("locationRecipe.getElementsByTagName('H2')[0].onmouseover()",900);
		setTimeout("locationRecipe.getElementsByTagName('H2')[0].onmouseout()",1100);
		if(browser.indexOf("safari")>-1) eval("setTimeout('window.scrollTo(0,locationRecipe.offsetTop)', 500)");
	}
}




/*************************************************************************************
FLAVOURS
*************************************************************************************/
function flavours(){
window.loading;
var bodyClass = document.body.className;
window.flavoursFor = bodyClass.indexOf("syrupsFlavours")>-1 ? "syrup" : bodyClass.indexOf("saucesFlavours")>-1 ? "sauce" : bodyClass.indexOf("fruitSmoothieFlavours")>-1 ? "fruit-smoothie" : "smoothie";
window.selectedFlavour = -1;
window.flavours = new Array();
window.h3s = document.getElementsByTagName("H3");
window.loadedPNGs = 0;
window.PNGCache = new Array();
window.furniturePNGCache = new Array();
cacheImages();
}

function cacheImages(){
	var furniturePNGs = ["/images/boxout/shadow-t.png","/images/boxout/shadow-tr.png","/images/boxout/shadow-r.png","/images/boxout/shadow-b.png","/images/boxout/shadow-l.png","/images/boxout/shadow-l.png","/images/boxout/shadow-tl.png"]
	for(var i = 0 ; i < furniturePNGs.length ; i++){
		if(browser != "msie5win"){
			var img = furniturePNGCache[furniturePNGCache.length] = new Image();
			img.src = furniturePNGs[i];
		}
	}
	for(var i = 0 ; i < h3s.length ; i++){
		var name = h3s[i].parentNode.className.split(" ")[1];
		var img = PNGCache[PNGCache.length] = new Image();
	
	
		img.onload = img.onerror = img.onabort = addToLoadedPNGs;
		img.src = "/images/system/" + flavoursFor+ "/bar-" + name + (browser == "msie5win" ? ".gif" : ".png");
	}
}

function addToLoadedPNGs(){
	loadedPNGs += 1;
	if(loadedPNGs == h3s.length) createRollovers();
}

function createRollovers(){

	for(var i = 0 ; i < h3s.length ; i++){
		//Abs position - make screen only and avoid print trouble
		var div= h3s[i].parentNode.getElementsByTagName("DIV")[0];
		var divTop = browser == "msie5mac" ? div.offsetTop + 49 : div.offsetTop - 17;
		div.style.top = divTop + "px";
		
		var furnitureDiv = document.createElement("DIV");
		furnitureDiv.style.width = furnitureDiv.style.height = "10px";
		furnitureDiv.style.position = "absolute";
		furnitureDiv.style.top = divTop + "px";
		furnitureDiv.style.left = "30px";
		furnitureDiv.style.visibility = "hidden";
		furnitureDiv.style.zIndex = 101;
		h3s[i].parentNode.appendChild(furnitureDiv);
		
		var index = flavours.length;
		var flavour = flavours[index] = new Flavour(h3s[i], div, furnitureDiv, index);
	
	
		addKeyHandlers(h3s[i], flavour);
		addKeyHandlers(div, flavour);
	}
	setTimeout("displayPage()",500);
}

function displayPage(){
	var hiddenH2s = document.getElementsByTagName("H2");
	var hiddenH3s = document.getElementsByTagName("H3");
	
	for(var i = 0 ; i < hiddenH2s.length ; i++){
		hiddenH2s[i].style.visibility = "visible";
	}
	for(var i = 0 ; i < hiddenH3s.length ; i++){
		hiddenH3s[i].style.visibility = "visible";
	}
	var bg = flavoursFor == "smoothie" ? "smoothies-flavours" : flavoursFor == "sauce" ? "sauces-flavours" : "flavours";
	if(flavoursFor == "fruit-smoothie") bg = "fruit-smoothie-flavours";
	document.getElementById("content").style.backgroundImage = "url(/images/backgrounds/" +bg+ ".jpg)"
}
/*FLAVOUR CLASS*/
var Flavour = function(h3, div, furnitureDiv, index){
	
	this.timer;
	this.h3 = h3;
	this.div = div;
	this.furnitureDiv = furnitureDiv;
	this.index = index;
	this.selected = false;
	
	//Add furniture
	function FurniturePiece(params,object){
		if(!params.gifAlt && browser == "msie5win") return;
		var piece = new TransparentPNG(params.src, params.width, params.height);
		piece.style.position = "absolute";
		piece.style.left = params.left + "px";
		piece.style.top = params.top + "px";
		furnitureDiv.appendChild(piece);
		if(!params.hides)addKeyHandlers(piece,object)
	}
	
	var divHeight = div.offsetHeight;
	var syrupName = div.parentNode.className.substr(div.parentNode.className.indexOf(" ")+1);
	
	var furniturePieces = [
		{src:"/images/system/" +flavoursFor + "/bar-" + syrupName,width:219,height:23,left:0,top:-23,hides:false, gifAlt:true}
		,{src:"/images/boxout/box-bottom",width:247,height:8,left:-10,top:divHeight,hides:false, gifAlt:false}
		
		/*The shaddow clockwise from the top*/
		
		,{src:"/images/boxout/shadow-t", width:247, height:11, left:-10, top:-34, hides:true, gifAlt:false}
		,{src:"/images/boxout/shadow-tr", width:18, height:23, left:219, top:-23, hides:true, gifAlt:false}
		,{src:"/images/boxout/shadow-r", width:18, height:divHeight, left:219, top:0, hides:true, gifAlt:false}
		,{src:"/images/boxout/shadow-b", width:247,height:19, left:-10, top:	divHeight + 8, hides:true, gifAlt:false}
		,{src:"/images/boxout/shadow-l", width:10,height:divHeight - 17, left:-10, top:17, hides:true, gifAlt:false}
		,{src:"/images/boxout/shadow-l", width:10,height:17, left:-10, top:0, hides:false, gifAlt:false}//this doesn't hide as it's over the key
		,{src:"/images/boxout/shadow-tl", width:10, height:23, left:-10, top:-23, hides:true, gifAlt:false}
	]
	
	for(var i = 0 ; i < furniturePieces.length ; i++){
		new FurniturePiece(furniturePieces[i],this);
	}
}
//FLAVOUR PROTOTYPE METHODS//
Flavour.prototype.show = function(){
	if(!windowLoaded)return;
	clearTimeout(this.object.timer);
	if(this.object.selected)return;
	this.object.selected = true;
	if(selectedFlavour > -1 && selectedFlavour != this.object.index)flavours[selectedFlavour]._hide();
	selectedFlavour = this.object.index;
	this.object.h3.className = "selected";
	this.object.div.style.visibility = "visible";
	this.object.furnitureDiv.style.visibility = "visible";
	/*Stacking in msie for smoothies*/
	var wrapper = this.object.div.parentNode.parentNode;
	if(flavoursFor == "smoothie") wrapper.style.zIndex = 200;
}
Flavour.prototype.hide = function(){
	eval("this.object.timer = setTimeout(\"flavours[" + this.object.index + "]._hide()\",300)");
}
Flavour.prototype._hide = function(){
	this.h3.className = "";
	this.selected = false;
	this.div.style.visibility = "hidden";
	this.furnitureDiv.style.visibility = "hidden";
	/*Stacking for msie smoothies*/
	var wrapper = this.div.parentNode.parentNode;
	if(flavoursFor == "smoothie") wrapper.style.zIndex = 1;
}
function addKeyHandlers(element, object){
	element.object = object;
	element.onmouseover = object.show;
	element.onmouseout = object.hide;
}
function addHideHandler(element, object){
	element.object = object;
	element.onmouseover = object.hide;
}



initialiseScreen();


