/* catalogue homepage image changer
	author mike foskett - websemantics.co.uk
	24/02/2010 - version 1
*/

var T=null;

/* author: Simon Willisons - http://simonwillison.net/2004/May/26/addLoadEvent/ */
function addLoadEvent(f){var o=window.onload;if(typeof window.onload!='function'){window.onload=f;}else{window.onload=function(){if(o){o();}f();};}}

/* mike foskett - http://websemantics.co.uk/resources/useful_javascript_functions/ */
function $id(id){return(document.getElementById(id)?document.getElementById(id):false);}
function setOpacity(id,op){var o=$id(id).style;o.opacity=op/100;o.MozOpacity=op/100;o.KhtmlOpacity=op/100;o.filter="alpha(opacity="+op+")";}

function endFade(fromId,toId){
	$id(fromId).className=$id(fromId).className.replace('current','');
	$id(toId).style.zIndex='0';
	T=null;
}

function fadeHeroIn(fromObj,toObj){
	if (fromObj.id===toObj.id){
		return;
	}
	if (T===null){
		setOpacity(toObj.id,0);
		toObj.style.zIndex="3";
		toObj.className=toObj.className+" current";
		// add / remove border to activating link
		var As=$id('newproducts').getElementsByTagName('a');
		for (var k=0;k<As.length;k++){
			if (As[k].className.match(toObj.id)){
				if (!As[k].className.match(' borderOn')){
					As[k].className=As[k].className+' borderOn';
				}
			}else{
				As[k].className=As[k].className.replace(' borderOn','');
			}
		}
		// cross fade images
		/*
		setOpacity(toObj.id,33);
		T=setTimeout("setOpacity('"+toObj.id+"',66)",50);
		T=setTimeout("setOpacity('"+toObj.id+"',100)",100);
		// reset fade ready for next image change over
		T=setTimeout("endFade('"+fromObj.id+"','"+toObj.id+"')",100);
		*/
		
		setOpacity(toObj.id,25);
		T=setTimeout("setOpacity('"+toObj.id+"',50)",50);
		T=setTimeout("setOpacity('"+toObj.id+"',75)",100);
		T=setTimeout("setOpacity('"+toObj.id+"',100)",150);
		// reset fade ready for next image change over
		T=setTimeout("endFade('"+fromObj.id+"','"+toObj.id+"')",150);
	}
}

function changeHero(){
	stopAuto(); // pause rotation while link hovered / focused
	if (T===null){
		var heroList=$id('mainHeroList');
		if (heroList){
			var lis=heroList.getElementsByTagName('li');
			if (lis){
				for (var j=0;j<lis.length;j++){
					if (this.className.match(lis[j].id)){
						var nextObj=lis[j];
					}
					if (lis[j].className.match('current')){
						var currentObj=lis[j];
					}
				}
				fadeHeroIn(currentObj,nextObj);
			}
		}
	}
}

var R=null;
function autoRotate(){
	if (T===null){
		// get current
		var heroList=$id('mainHeroList');
		if (heroList){
			var lis=heroList.getElementsByTagName('li');
			if (lis){
				for (var j=0;j<lis.length;j++){
					if (lis[j].className.match('current')){
						currentObj=lis[j];
						break;
					}
				}
				// get next
				var next=currentObj.id.replace('h','');
				if (next>=lis.length-1){
					next=1;
				}else{
					next++;
				}
				var nextId="h"+next;
				//alert(nextId);
				var nextObj=$id(nextId);
				// change
				fadeHeroIn(currentObj,nextObj);
			}
		}
	}
}

function stopAuto(){
	clearInterval(R);
	R=null;
}

function resumeAuto(){
	if (R===null){
		R=setInterval('autoRotate();',5000);
	}
}

addLoadEvent(function() {
	var newProducts=$id('newproducts');
	if (newProducts){
		var As=newProducts.getElementsByTagName('a');
		if (As){
			// preload large images
			var preload1=new Image(); 
			preload1.src="/p/inc/specials/catalogue2010/i/home/hero02.jpg";
			var preload2=new Image(); 
			preload1.src="/p/inc/specials/catalogue2010/i/home/hero03.jpg";
			var preload3=new Image(); 
			preload1.src="/p/inc/specials/catalogue2010/i/home/hero04.jpg";
			var preload4=new Image(); 
			preload1.src="/p/inc/specials/catalogue2010/i/home/hero05.jpg";
			var preload5=new Image(); 
			preload1.src="/p/inc/specials/catalogue2010/i/home/hero06.jpg";
			// apply onhover change hero image to links
			for (var i=0;i<As.length;i++){
				As[i].onmouseover=changeHero;
				As[i].onfocus=changeHero;
				// resume rotation after link hovered / focused
				As[i].onmouseout=resumeAuto;
				As[i].onblur=resumeAuto;
			}
			// auto rotate
			R=setInterval('autoRotate();',5000);
			// pause rotate on hero image hover
			var heroList=$id('mainHeroList');
			if (heroList){
				var As=heroList.getElementsByTagName('a');
				if (As){
					for (var j=0;j<As.length;j++){
						As[j].onmouseover=stopAuto;
						As[j].onmouseout=resumeAuto;
						As[j].onfocus=stopAuto;
						As[j].onblur=resumeAuto;
					}
				}
			}

		}
	}
});

