function $(id) {
	return document.getElementById(id);
}

var mouseouts = new Array();
function nukemouseout(ent) {
	if (!mouseouts[ent]) {
		mouseouts[ent] = $(ent+'div').getAttribute('onmouseout');
	}
	$(ent+'div').setAttribute('onmouseout', null);
}

function getmouseback(ent) {
	$(ent+'div').setAttribute('onmouseout', mouseouts[ent]);
	if (!mouseouts[ent]) {
		alert('oh shit');
	}
}

function showmenu(ent) {
	var opac = 1;//0.35;
	var thediv = 'm'+ent+'div';
	$(thediv).style.display = 'block';
	$(thediv).style.left = ($(ent+'anchor').offsetLeft+4)+'px';
	hidemenu(ent);
	$('themaindiv').style.opacity = opac;
	$('thenavdiv').style.opacity = opac;
	$(thediv).focus();
}

function hidemenu(skipent) {
	for (var i in breadcrumbs) {
		var otherent = breadcrumbs[i];
		if (otherent == skipent) continue;
		if (!$('m'+otherent+'div')) continue; //alert(otherent+'div');
		$('m'+otherent+'div').style.display = 'none';
	}
	if (skipent == 'x') $('themaindiv').style.opacity = 1;
	if (skipent == 'x') $('thenavdiv').style.opacity = 1;
}

function showmenu2(ent) {
	var opac = 1;//0.35;
	var thediv = ent+'div';
	var anchors = $(thediv).getElementsByTagName('a');
	for ( var i=0; i < anchors.length ; ++i) {
		anchors[i].style.display = 'block';
	}
	//$(thediv).style.left = ($(ent+'anchor').offsetLeft-4)+'px';
}

function hidemenu2(skipent) {
	var divs = $('middiv').getElementsByTagName('div');
	for ( var i=0; i < divs.length ; ++i ) {
		var anchors = divs[i].getElementsByTagName('a');
		for ( var j=0; j < anchors.length ; ++j ) {
			anchors[j].style.display = 'none';
		}
	}
}

function setOpacity(ent,opac) {
	
}

//Pre-Load Images
function preload(imgObj,imgSrc) {
	if (document.images) {
		eval(imgObj+' = new Image()')
		eval(imgObj+'.src = "'+imgSrc+'"')
	}
}	

//Image Roll-Overs
function imageSwap(imgName,imgObj) {
	if (document.images) {
		if (document.layers && layer!=null) eval('document.'+layer+'.document.images["'+imgName+'"].src = '+imgObj+'.src')
		else document.images[imgName].src = eval(imgObj+".src")
	}
}

function resizebody(maxwidth) {
	var mydiv = $("maincontent");
	var curr_pad = mydiv.style.marginRight;
	curr_pad = curr_pad.replace(/px/i, '');
	if (document.body.clientWidth > maxwidth) {
		new_pad = document.body.clientWidth - maxwidth;
		mydiv.style.marginRight = "25%"; // new_pad+"px";
	}
}

function resizebodyx(width) {
	var mydiv = $("maincontent");
	mydiv.style.marginRight = width;
}

function getWindowHeight() {
  var windowHeight = 0;
	
  if (typeof(window.innerHeight) == 'number')
    windowHeight = window.innerHeight;
	
  else {
		
    if (document.documentElement && document.documentElement.clientHeight)
      windowHeight = document.documentElement.clientHeight;
		
    else {
      if (document.body && document.body.clientHeight)
        windowHeight = document.body.clientHeight; }; };
				
  return windowHeight;
};

function heightfix () {
	var nd = $("thenavdiv").offsetHeight;
	var md = $("themaindiv").offsetHeight;
	var qd = $("themaindivbottom").offsetHeight;
	// var v = document.thenavdiv.offsetHeight;
	if (nd > md) {
		// alert(nd+' > '+md);
		$("themaindiv").style.height = (nd-(qd+22))+"px";
		// alert($("themaindiv").style.height);
	} else {
		$("bottomone").style.marginLeft = (196-24)+"px";
	}
}

function showDiv() {
	if (document.getElementById) { // DOM3 = IE5, NS6
		$('hideShow').style.visibility = 'visible';
	} else {
		if (document.layers) { // Netscape 4
			document.hideShow.visibility = 'visible';
		} else { // IE 4
			document.all.hideShow.style.visibility = 'visible';
		}
	}
}

function switchad() {
	if (document.getElementById) { // DOM3 = IE5, NS6
		$('hideShow').style.visibility = 'visible';
	} else {
		if (document.layers) { // Netscape 4
			document.hideShow.visibility = 'visible';
		} else { // IE 4
			document.all.hideShow.style.visibility = 'visible';
		}
	}
}

function greyout(theform,thesubmit) {
	theform.thesubmit.value="Wait...";
	theform.thesubmit.disabled=true;
	theform.thesubmit.className='disabled';
}

function expandmain(expandpic) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		$('wrapper').style.width = 'auto';
		$('expandpic').style.visibility = 'hidden';
	} else {
		alert('Your browser is too old for this feature.');
	}
}

function dobreadnav(navurl,text) {
	$('middiv').style.paddingLeft = '300px';
	$('middiv').innerHTML = '<b>Please Wait...</b>';
	$('maincontent').innerHTML = '<p class="big grn">Loading <i>'+text+'</i></p>'; // <br><img src="/site-icons/rev.gif">
	$('maincontent').style.height = '800px';
	window.location.href = navurl;
}

function killtach() {
	alert('ok');
}

function gonuts(numnuts,thisid,light,waitelement,nutelement,nutspec,nutrate,nuthalf,nutzero,nutnorm) {
	numnuts *= 20;
	var voteurl = '/pages/_/rate/'+thisid+'/'+numnuts+'?nc='+Math.random()/10;
	//alert(voteurl);
	var req;
	try	{
		// Firefox, Opera, Safari
		req = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
			}
		}
	}
	req.open("GET", voteurl, true);
	req.send(null);
	req.onreadystatechange = function () {
		if (req.readyState == 4 && req.status == 200) {
			shownuts(req.responseText,nutelement,light,waitelement,nutspec,nutrate,nuthalf,nutzero,nutnorm);
		} else if (req.readyState == 4 && req.status != 200) {
			$(nutelement).innerHTML = 'Error showing rating. Sorry.';
			return true;
		} else {
			waitnuts(waitelement);
		}
	}

	return true;
}

function waitnuts(waitelement) {
	$(waitelement).innerHTML = 'Please wait...';
}

function shownuts(input,nutelement,light,waitelement,nutspec,nutrate,nuthalf,nutzero,nutnorm) {
	var inputdata = input.split(',');
	var rating = inputdata[0];
	var numvotes = inputdata[1];
	var element = $(nutelement);
	if (rating < 0) {
		element.innerHTML = 'Error showing rating.';
		return true;
	}
	rating /= 20;
	var flr = Math.floor(rating);
	var ratingdelta = rating - flr;
	for (var i = 0; i <= 5; i++) {
		var thisnut = $(nutspec+i);
		thisnut.setAttribute('onclick', null);
		thisnut.setAttribute('onmouseover', null);
		thisnut.setAttribute('onmouseout', null);
		if (i == 0 && rating == 0) { // article is rated 0, show 0 nut
			thisnut.setAttribute('src', nutzero);
		} else if (i == 0 && rating != 0) { // on the 0th nut, higher than 0 rate, hide 0 nut
			thisnut.style.display = 'none';
		} else if (ratingdelta > 0.74 && i == flr+1) {
			thisnut.setAttribute('src', nutrate);
		} else if (ratingdelta > 0.35 && i == flr+1) {
			thisnut.setAttribute('src', nuthalf);
		} else if (i > flr) {
			//thisnut.style.display = 'none';
			thisnut.setAttribute('src', nutnorm); 
		} else {
			thisnut.setAttribute('src', nutrate);
		}
	}
	var vt = (numvotes == 1) ? ' vote.' : ' votes.';
	$(light).innerHTML = 'Thanks for rating.';
	$(waitelement).innerHTML = 'This page is currently rated '+rating+' out of 5 nuts from '+numvotes+vt;
}

function lightnuts(numnuts,light,nutspec,nutrate) {
	for (var i = 1; i <= numnuts; i++) {
		$(nutspec+i).setAttribute("src", nutrate);
		//alert(nutspec+i);
	}
	$(light).innerHTML = numnuts+' out of 5 nuts.';
	return true;
}

function offnuts(numnuts,light,nutspec,nutnorm) {
	for (var i = 1; i <= 5; i++) {
		$(nutspec+i).setAttribute("src", nutnorm);
	}
	$(light).innerHTML = 'Mouse over the nuts to rate.';
	return true;
}

function nonuts(light) {
	$(light).innerHTML = 'ZERO out of 5 nuts.';
	return true;
}