// Load initial javascripts.  From http://dean.edwards.name/weblog/2006/06/again/
 function init() {
     // quit if this function has already been called
     if (arguments.callee.done) return;
     // flag this function so we don't do the same thing twice
     arguments.callee.done = true;
     // RUN MY SCRIPT(S)
     setdisplay();
	 setbehaviors();
	 setorderlinkrollovers();
 };
 /* for Mozilla */
 if (document.addEventListener) {
     document.addEventListener("DOMContentLoaded", init, false);
 }
 /* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
  if (this.readyState == "complete") {
    init(); // call the onload handler
  }
};
/*@end @*/
 /* for other browsers */
 window.onload = init;

var currentcontent="";
var navnextcontent="";
var navbackcontent="";
function setdisplay() {
	var dhtml = (document.createElement('p')) ? true : false ;
	//var highres = (getwinwidth() > 760) ? true : false ;	// NEED TO CHANGE TO PROPER BROWSERWIDTH DETECTION
	if (dhtml) {
		// set page css to dhtml class
		document.getElementsByTagName('body').item(0).className="dhtml";
		// display first review
		firstreview = document.getElementById('reviews').getElementsByTagName('div')[0];
		firstreview.style.display = 'block';
		currentcontent = firstreview.id;
		// Add nav links at end of reviews div if there are at least two review divs
		if (firstreview && (node_after(firstreview))) {
			var contentnav = document.createElement("div");
			contentnav.setAttribute("id", "contentnav");
			contentnav.innerHTML = '<span id="navnext" onClick="nav(\'next\')">Next Review</span><span id="navback" onClick="nav(\'back\')">Previous Review</span>';
			document.getElementById('reviews').appendChild(contentnav);	
			// Adjust initial nav "next" link depending whether there is content following
			if (node_after(document.getElementById(currentcontent)).className.indexOf("review")==0) {
				document.getElementById('navnext').className="enabled";
				navnextcontent=node_after(document.getElementById(currentcontent)).id;
			}
		}
		// IF THERE IS A REVIEW ID IN THE URL, SHOW IT?
	} else {
		//alert('did not run setdisplay: '+dhtmlok+' '+highres);
	}
}

function setbehaviors() {
	var elements;
	// add onclick behavior to all links in #contentlinks
	if (document.getElementById('contentlinks')) {
		elements=document.getElementById('contentlinks').getElementsByTagName('a');
		for(i=0;i<elements.length;i++) {
			if(elements[i].className == 'reviewlink') {
				// send the text after the # in the link's url to showfocalcontent function
				elements[i].onclick=function(){showfocalcontent(this.href.split('#')[1]); return false;};
			}
		}
	}
	// add onclick behavior for cdsearch field
	document.getElementById('cdsearch').onclick=function(){cdsearchclear();};
}

function showfocalcontent(selectedcontent) {
	if (document.getElementById(selectedcontent)) {
		// Hide the previously shown review and show the selected one
		document.getElementById(currentcontent).style.display = "none";
		document.getElementById(selectedcontent).style.display = "block";
		currentcontent = selectedcontent;
		// If scrolled down below the top of the main content area, scroll back up to top
		scrollposition = gettopposition();
		reviewcontainer = document.getElementById('reviewsandlinks');
		if (reviewcontainer.offsetParent) {
			reviewcontainerposition = reviewcontainer.offsetTop + reviewcontainer.offsetParent.offsetTop
		} else {
			reviewcontainerposition = reviewcontainer.offsetTop;
		}
		if (scrollposition > reviewcontainerposition) {
			scroll(0,(document.getElementById('contents').offsetTop - 2));	// Scroll to just above where main content starts
		}
		// Either dim or activate the nav links and set the destinations depending on sibling content. 
		if (node_after(document.getElementById(currentcontent)).className.indexOf("review ")===0) {
			document.getElementById('navnext').className="enabled";
			navnextcontent=node_after(document.getElementById(currentcontent)).id;
		} else {
			document.getElementById('navnext').className='';
			navnextcontent='';
		}
		if (node_before(document.getElementById(currentcontent))) {	// Doesn't work when checking for "review" class as above for some reason
			document.getElementById('navback').className="enabled";
			navbackcontent=node_before(document.getElementById(currentcontent)).id;
		} else {
			document.getElementById('navback').className="";
			navbackcontent='';
		}
	} else {
		// If no dhtml then just scroll down to the selected review
		window.location.hash = selectedcontent;
	}
}

function nav(direction) {
	if (direction=='top') {
		scroll(0,0);
	}
	else if (direction=='next' && navnextcontent) {
		showfocalcontent(navnextcontent);
	}
	else if (direction=='back' && navbackcontent) {
		showfocalcontent(navbackcontent);
	}
}

function cdsearchclear() {
	if (document.getElementById('cdsearch').value == 'search/browse') {
		document.getElementById('cdsearch').value = '';
	}
	else {
		document.getElementById('cdsearch').select;
	}
}


// Get browser window width.  From http://www.thescripts.com/forum/thread146955.html
function getwinwidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (document.body.clientWidth) {
		return document.body.clientWidth;
	} else {
		return 100;
	}
}

// Get path or filename from string.  From http://lawrence.ecorp.net/inet/samples/regexp-parse.php
function parseurl(data){
	var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
	return {path: m[1], file: m[2]}
}

// From http://snipplr.com/view/1529/dom-traversal-helper-functions/
function is_all_ws(nod) { return !(/[^\t\n\r ]/.test(nod.data)); }
function is_ignorable(nod) { return (nod.nodeType == 8) || ((nod.nodeType == 3) && is_all_ws(nod)); }
function node_before(sib) {
	while ((sib = sib.previousSibling)) {
		if (!is_ignorable(sib)) return sib;
	}
	return null;
}
function node_after(sib) {
	while ((sib = sib.nextSibling)) {
		if (!is_ignorable(sib)) return sib;
	}
	return null;
}
function first_child(par) {
	var res = par.firstChild;
	while(res) {
		if(!is_ignorable(res)) return res;
		res = res.nextSibling;
	}
	return null;
}
function last_child(par) {
	var res = par.lastChild;
	while(res) {
		if(!is_ignorable(res)) return res;
		res = res.previousSibling;
	}
	return null;
}

function gettopposition() {
		// Find out how far page is scrolled down.  From http://forums.digitalpoint.com/showthread.php?t=11965
		if (document.documentElement && !document.documentElement.scrollTop)
			topposition = 0;
		else if (document.documentElement && document.documentElement.scrollTop)
			topposition = document.documentElement.scrollTop;
		else if (document.body && document.body.scrollTop)
			topposition = document.body.scrollTop;
		else
			topposition = 0;	// Who knows?
		return topposition;
}

function setorderlinkrollovers() {
	var elements;
	elements=document.getElementById('orderlinks').getElementsByTagName('img');
	for(i=0;i<elements.length;i++) {
		// set img links to same as orderlink class names (plus '-hover')
		mouseoverlink='../graphics/order-'+elements[i].className+'-hover.gif';
		mouseoutlink='../graphics/order-'+elements[i].className+'.gif';
		// pre-load images
		mouseoverpreload = new Image(); 
		mouseoverpreload.src = mouseoverlink;
		mouseoutpreload = new Image(); 
		mouseoutpreload.src = mouseoutlink;
		// set mouseover behaviors - Have to use expression and "this.className" for setting the image; won't work using previously defined mouseoverlink and mouseoutlink variables when multiple order links exist
		elements[i].onmouseover=function(){this.setAttribute('src','../graphics/order-'+this.className+'-hover.gif'); return false;};
		elements[i].onmouseout=function(){this.setAttribute('src','../graphics/order-'+this.className+'.gif'); return false;};
	}
}
