/* Books JS used for:
		Make a link display a content block, on hover or focus.
		Stripe table data and add onclick to tr.
		Test url foranchor parameter and set content accordingly.
		Add onclick action to links With rel="section".
		Tick through block content ifthe mouse is not on the content.

	Some of this is hard coded, sorry.

	mike foskett - http://websemantics.co.uk/ - 09/10/2009
*/
	


/* author: Simon Willisons - http://simonwillison.net/2004/May/26/SWaddLoadEvent/ */
function SWaddLoadEvent(f){var o=window.onload;if(typeof window.onload!='function'){window.onload=f;}else{window.onload=function(){if(o){o();}f();};}}



/*  author: mike foskett - version 1.1 - 09/10/2009 - http://websemantics.co.uk/
		requires:	#hasCSS {display:none} in the CSS
		returns: boolean.
*/
var cssOn;
function hasCSS(){var d=document.createElement('div');d.id="hasCSS";document.body.appendChild(d);var o=document.getElementById("hasCSS"),v=false;if(window.getComputedStyle){v=(window.getComputedStyle(o,null).getPropertyValue('display')==='none');}else{if(o.currentStyle){v=(o.currentStyle.display==='none');}}document.body.removeChild(d);return v;}
// run first on page load.
SWaddLoadEvent(function(){cssOn=hasCSS();});



/*  Make a link display a content block, on hover or focus, with an activation delay and a block crossfade.
		author: mike foskett - http://websemantics.co.uk/
		version 1 - 11/08/2009
		parameters:
			init:	navigation block id,
						hover class name,
						delay before activation of hover.
			requires var cssOn to be true.
*/
var displayBookSection=function(){

	var t=null,hoverClass,delay,bookNav;

	function setOpacity(obj,op){obj.style.opacity=op/100;obj.style.MozOpacity=op/100;obj.style.KhtmlOpacity=op/100;obj.style.filter="alpha(opacity="+op+")";}
	function getNextSibling(o){var n=o.nextSibling;while(n.nodeType!=1){n=n.nextSibling;}return n;}

	function addHoverClass(obj){obj.className=obj.className+' '+hoverClass;}
	function isHoverClass(obj){return obj.className.match(hoverClass);}

	function fadeCurrent(){
		function remove(){
			a.className=a.className.replace(' '+hoverClass,"").replace(hoverClass,"");
			bs.className=bs.className.replace(' '+hoverClass,"").replace(hoverClass,"");
		}
		var h2s=bookNav.getElementsByTagName('h2');
		for(var i=h2s.length-1;i>-1;i--){
			var a=h2s[i].getElementsByTagName('a')[0];
			if(isHoverClass(a)){
				var bs=document.getElementById(a.bookSectionID);
				bs.style.zIndex="1";
				setTimeout(remove,140);
				return;
	}	}	}

	function bookSectionOn(){
		clearTimeout(t);
		if(!isHoverClass(this)){
			var that=this;
			t=setTimeout(function(){
				var obj=document.getElementById(that.bookSectionID);
				obj.style.zIndex="2";
				setOpacity(obj,33);
				addHoverClass(obj);
				setTimeout(function(){setOpacity(obj,66);},70);
				fadeCurrent();
				setTimeout(function(){setOpacity(obj,100);},140);
				addHoverClass(that);
			},delay);
	}	}

	function init(navID,overClass,activationDelay){
		SWaddLoadEvent(function(){
			if(cssOn){
				bookNav=document.getElementById(navID);
				if(bookNav){
					hoverClass=overClass;
					delay=activationDelay;
					if(hoverClass&&delay){
						var h2s=bookNav.getElementsByTagName('h2');
						for(var i=h2s.length-1;i>-1;i--){
							var next=getNextSibling(h2s[i]);
							if(next){
								next.id="b"+i;
								var a=h2s[i].getElementsByTagName('a')[0];
								if(a){
									a.bookSectionID="b"+i;
									if(document.getElementById(a.bookSectionID)){
										a.onmouseover=bookSectionOn;
										a.onfocus=bookSectionOn;
			}	} }	}	}	} }
		});
	}


	return{
		init:init
	};

}();

// paramenters: nav id, hover class, activation delay
displayBookSection.init('bookNav','show','300');



/*  Stripe table data and add onclick to tr.
		author: mike foskett - http://websemantics.co.uk/
		version 1 - 18/08/2009
		parameters:
			initClass:	block id
									table class name,
									hover class name,
									odd row class name,
									even row class name,
									table width property value (used to test CSS available).
		requires var cssOn to be true.
*/
var stripeTableAddClick=function(){

	var blockID,tbodyClass,hoverClass,oddClass,evenClass;

	function setup(){
		function goToLink(){
			window.location=this.getElementsByTagName('a')[0].href;
		}
		function mOver(){this.className=this.className+" "+hoverClass;}
		function mOut(){this.className=this.className.replace(hoverClass,'');}
		var tbodys=blockID.getElementsByTagName('tbody');
		for(var i=tbodys.length-1;i>-1;i--){
			if(tbodys[i].className.match(tbodyClass)){
				var counter=1;
				var trs=tbodys[i].getElementsByTagName('tr');
				for(var j=trs.length-1;j>-1;j--){
					trs[j].onclick=goToLink;
					if(counter==1){
						if(!trs[j].className.match(oddClass)){
							trs[j].className=trs[j].className+' '+oddClass;
						}
						counter=0;
					}else{
						if(!trs[j].className.match(evenClass)){
							trs[j].className=trs[j].className+' '+evenClass;
						}
						counter=1;
					}

					trs[j].onmouseover=mOver;
					trs[j].onmouseout=mOut;
	}	}	}	}

	function init(bid,tc,hc,or,er){
		SWaddLoadEvent(function(){
			if(cssOn){
				blockID=document.getElementById(bid);
				if(blockID){
					tbodyClass=tc;
					hoverClass=hc;
					oddClass=or;
					evenClass=er;
					if(tbodyClass && hoverClass && oddClass && evenClass){
						setup();
			}	}	}
		});
	}

	return{
		init:init
	};

}();

// paramenters: block id, tbody class, hover class, odd row class, even row class.
stripeTableAddClick.init('charts','stripe','over','odd','even');
stripeTableAddClick.init('preorders','stripe','over','odd','even');



/*  Test url for anchor parameter and set book charts content accordingly.
		author: mike foskett - http://websemantics.co.uk/
		version 1 - 20/08/2009
		requires:			#hasCSS {display:none} in the CSS
		parameters:		none
		returns:			boolean
		requires var cssOn to be true.
*/
var setInitialBookChartsContent=function(){

	function addHideSectionClass(id){
		var block1=document.getElementById(id);
		if(block1){
			if(!block1.className.match('hideSection')){
				block1.className=block1.className?block1.className+' hideSection':'hideSection';
	}	}	}

	function removeHideSectionClass(id){
		var block1=document.getElementById(id);
		if(block1){
			if(block1.className.match('hideSection')){
				block1.className=block1.className.replace(' hideSection','').replace('hideSection','');
	}	}	}

	function setSection(liClass){
		var lis=document.getElementById('bookNav').getElementsByTagName('li'), a, d;
		for(var i=0;i<lis.length;i++){
			if(lis[i].className.match('paperback')){
				d=lis[i].getElementsByTagName('div')[0];
				d.className=d.className.replace('show','');
				a=lis[i].getElementsByTagName('a')[0];
				a.className=a.className.replace('show','');
				d.style.zIndex="-1";
			}
			if(lis[i].className.match(liClass)){
				d=lis[i].getElementsByTagName('div')[0];
				d.className=d.className+' show';
				a=lis[i].getElementsByTagName('a')[0];
				a.className=a.className+' show';
				d.style.zIndex="0";
	}	}	}

	function init(pageID){
		SWaddLoadEvent(function(){
			if(cssOn){
				var page=document.getElementById(pageID);
				if(page){
					var anchor=unescape(self.document.location.hash.substring(1));
					switch (anchor){
						case 'hardbackSection':
							setSection('hardback');
							addHideSectionClass('paperbackSection');
							removeHideSectionClass('hardbackSection');
							window.location="#bookcharts";
							break;

						case 'childrensSection':
							setSection('childrens');
							addHideSectionClass('paperbackSection');
							removeHideSectionClass('childrensSection');
							window.location="#bookcharts";
							break;
					
						default : break;
		}	}	}
		});
	}

	return{
		init:init
	};

}();

// bookcharts page only - get and set active section from url anchor
setInitialBookChartsContent.init('bookcharts');



/*  Test url for anchor parameter and set book pre-orders content accordingly.
		author: mike foskett - http://websemantics.co.uk/
		version 1 - 20/08/2009
		requires:			#hasCSS {display:none} in the CSS
		parameters:		none
		returns:			boolean
		requires var cssOn to be true.
*/
var setInitialBookPreorderContent=function(){

	function addHideSectionClass(id){
		var block1=document.getElementById(id);
		if(block1){
			if(!block1.className.match('hideSection')){
				block1.className=block1.className?block1.className+' hideSection':'hideSection';
	}	}	}

	function removeHideSectionClass(id){
		var block1=document.getElementById(id);
		if(block1){
			if(block1.className.match('hideSection')){
				block1.className=block1.className.replace(' hideSection','').replace('hideSection','');
	}	}	}

	function setSection(liClass){
		var lis=document.getElementById('bookNav').getElementsByTagName('li'), d, a;
		for(var i=0;i<lis.length;i++){
			if(lis[i].className.match('paperback')){
				d=lis[i].getElementsByTagName('div')[0];
				d.className=d.className.replace('show','');
				a=lis[i].getElementsByTagName('a')[0];
				a.className=a.className.replace('show','');
				d.style.zIndex="-1";
			}
			if(lis[i].className.match(liClass)){
				//alert(liClass)
				d=lis[i].getElementsByTagName('div')[0];
				d.className=d.className+' show';
				a=lis[i].getElementsByTagName('a')[0];
				a.className=a.className+' show';
				d.style.zIndex="0";
	}	}	}

	function init(pageID){
		SWaddLoadEvent(function(){
			if(cssOn){
				var page=document.getElementById(pageID);
				if(page){
					var anchor=unescape(self.document.location.hash.substring(1));
					switch (anchor){
						case 'hardbackSection':
							setSection('hardback');
							addHideSectionClass('paperbackSection');
							removeHideSectionClass('hardbackSection');
							window.location="#preorder";
							break;

						case 'nonfictionSection':
							setSection('non-fiction');
							addHideSectionClass('paperbackSection');
							removeHideSectionClass('nonfictionSection');
							window.location="#preorder";
							break;

						case 'kidsSection':
							setSection('kids');
							addHideSectionClass('paperbackSection');
							removeHideSectionClass('kidsSection');
							window.location="#preorder";
							break;
					
						default : break;
		}	}	}
		});
	}

	return{
		init:init
	};

}();

// pre-orders page only - get and set active section from url anchor
setInitialBookPreorderContent.init('preorder');



/*  add Onclick To Links With Rel attribute = section.
		Sorry - Hard-coded due to functionality creep and lack of time.
		author: mike foskett - http://websemantics.co.uk/
		version 1 - 21/08/2009
		parameters: page id, the rest are all hard coded
		requires var cssOn to be true.
*/
var addOnClickToLinksWithRel=function(){

	function relClickedBookcharts(){
			var block1=document.getElementById('paperbackSection'),
					block2=document.getElementById('hardbackSection'),
					block3=document.getElementById('childrensSection');
			if(block1){
				if(!block1.className.match('hideSection')){
					block1.className=block1.className?block1.className+' hideSection':'hideSection';
				}
			}
			if(block2){
				if(!block2.className.match('hideSection')){
					block2.className=block2.className?block2.className+' hideSection':'hideSection';
				}
			}
			if(block3){
				if(!block3.className.match('hideSection')){
					block3.className=block3.className?block3.className+' hideSection':'hideSection';
				}
			}
			var newBlock=(this.rel)?document.getElementById(this.rel):null;
			if(newBlock){
				if(newBlock.className.match('hideSection')){
					newBlock.className=newBlock.className.replace(' hideSection','').replace('hideSection','');
					return false;
				}
			}
			return true;
	}

	function relClickedPreorder(){
		var block1=document.getElementById('paperbackSection'),
				block2=document.getElementById('hardbackSection'),
				block3=document.getElementById('nonfictionSection');
				block4=document.getElementById('kidsSection');
		if(block1){
			if(!block1.className.match('hideSection')){
				block1.className=block1.className?block1.className+' hideSection':'hideSection';
			}
		}
		if(block2){
			if(!block2.className.match('hideSection')){
				block2.className=block2.className?block2.className+' hideSection':'hideSection';
			}
		}
		if(block3){
			if(!block3.className.match('hideSection')){
				block3.className=block3.className?block3.className+' hideSection':'hideSection';
			}
		}
		if(block4){
			if(!block4.className.match('hideSection')){
				block4.className=block4.className?block3.className+' hideSection':'hideSection';
			}
		}
		var newBlock=(this.rel)?document.getElementById(this.rel):null;
		if(newBlock){
			if(newBlock.className.match('hideSection')){
				newBlock.className=newBlock.className.replace(' hideSection','').replace('hideSection','');
				return false;
			}
		}
		return true;
	}

	function init(pageID){
		SWaddLoadEvent(function(){
			if(cssOn){
				if(document.getElementById(pageID)){
					var page=pageID;
					var as=document.getElementById('bookNav').getElementsByTagName('a');
					for(var i=as.length-1;i>-1;i--){
						if(as[i].rel){
							if(page==="bookcharts"){
								as[i].onclick=relClickedBookcharts;
							}
							if(page==="preorder"){
								as[i].onclick=relClickedPreorder;
			} }	}	} }
		});
	}

	return{
		init:init
	};

}();

addOnClickToLinksWithRel.init('bookcharts');
addOnClickToLinksWithRel.init('preorder');



/*  Tick through block content ifthe mouse is not on the content.
		author: mike foskett - http://websemantics.co.uk/
		version 1 - 09/10/2009
		requires:			cssOn boolean set true.
		parameters:		nav block id
*/
var tickbookNavContent=function(){

	var delay=5000,navID,t,hoverClass="show";

	function setOpacity(obj,op){obj.style.opacity=op/100;obj.style.MozOpacity=op/100;obj.style.KhtmlOpacity=op/100;obj.style.filter="alpha(opacity="+op+")";}
	function getNextSibling(o){var n=o.nextSibling;while(n.nodeType!=1){n=n.nextSibling;}return n;}

	function fadeInNext(){
		clearInterval(t);
		// find current
		var noSections,currentOn,nextOn;
		t=setInterval(function(){

			var h2s=document.getElementById(navID).getElementsByTagName('h2'), objC, objN;
			if(h2s){
				noSections=h2s.length;
				for(var i=0;i<noSections;i++){
					var divObj=getNextSibling(h2s[i]);
					if(divObj.className.match(hoverClass)){
						currentOn=i;
						objC=divObj;
						// calc next on section
						if(currentOn==noSections-1){
							nextOn=0;
						}else{
							nextOn=i+1;
						}
						//nextOn=(currentOn==noSections)?0:i+1;
						objN=getNextSibling(h2s[nextOn]);
						break;
					}
				} // current and next tested and set

				// place next over current via zIndex setting
				objC.style.zIndex="1";
				objN.style.zIndex="2";

				// nextOn at reduced opacity
				setOpacity(objN,33);

				// display nextOn by adding "show" to h2 a, and div
				objN.className=objN.className+" "+hoverClass;
				h2s[nextOn].getElementsByTagName('a')[0].className=h2s[nextOn].getElementsByTagName('a')[0].className+" "+hoverClass;

				setTimeout(function(){setOpacity(objN,66);},100);

				setTimeout(function(){
					setOpacity(objN,100);
					// remove currentOn "show" from h2 a, and div
					objC.className=objC.className.replace(' '+hoverClass,'').replace(hoverClass,'');
					h2s[currentOn].getElementsByTagName('a')[0].className=h2s[currentOn].getElementsByTagName('a')[0].className.replace(' '+hoverClass,'').replace(hoverClass,'');
				},200);

			}

		},delay);
	}

	function init(nID){
		navID=nID;
		SWaddLoadEvent(function(){
			if(cssOn){
				// add on mouseover / on focus clearTimeOuts
				var bookNav=document.getElementById(navID);
				if(bookNav){
					bookNav.onmouseout=fadeInNext;
					bookNav.onmouseover=function(){clearInterval(t);};
					//bookNav.onblur=fadeInNext;
					//bookNav.onfocus=function(){clearInterval(t);};
					fadeInNext();
				}
			}
		});
	}

	return{
		init:init
	};

}();

// tick through book nav content
tickbookNavContent.init('bookNav');