var p = getQueryVariable("print");

if ( (typeof host == "undefined") || host == "" ) {  host = "http://htmldev.scholastic.net"; }

if(p == 1) {
	document.write('<link rel="stylesheet" type="text/css" href="' + host + '/content/presentation/www/content.css" />');
	document.write('<link rel="stylesheet" type="text/css" href="' + host + '/content/presentation/www/contentprint.css" />');
	addLoadEvent(printIt);
}
else if(p == 2) {
	document.write('<link rel="stylesheet" type="text/css" href="' + host + '/content/presentation/www/content.css" />');
	document.write('<link rel="stylesheet" type="text/css" href="' + host + '/content/presentation/www/contentprintimg.css" />');
	addLoadEvent(printIt);
}
else {
	document.write('<link rel="stylesheet" type="text/css" href="' + host + '/content/presentation/www/content.css" />');
}

var openCallout = "";

function printPage(hostName) {
	if( !document.getElementById || !document.getElementsByTagName ) return;
	if(openCallout != "" && openCallout != "printCallout") {
		document.getElementById(openCallout).style.display = "none";
	}
	if ( !hostName ) hostName = host; 
	
	if(!document.getElementById("printCallout")) {
		var body = document.getElementsByTagName("body")[0];
		var d = document.createElement('div');	
		body.appendChild(d);
		d.setAttribute("id", "printCallout");
	}
	
	
	var topImg = "callout_top.gif";
	obj = document.getElementById("printCallout");
	obj2 = document.getElementById("print");
	temp = getposOffset(obj2, "top") + 16;
	temp2 = getposOffset(obj2, "left");
	temp2 = temp2 - 55;
	if( temp2 < 0 ) {
		temp2 += (-temp2) + 10;
		topImg = "callout_topA.gif";
	}
	
	cContent = '<img src="' + hostName + '/content/presentation/www/images/' + topImg +'" width="370" height="27" class="dBlock" />';
	cContent += '<div id="printCalloutContent">';
	cContent += '<div style="float: right;"><a href="#" onclick="hide(\'printCallout\'); return false;"><img src="' + hostName + '/content/presentation/www/images/cart_close.gif" width="20" height="18" border="0" /></a></div>';
	cContent += '<a href="http://www.scholastic.com/custsupport/">Help with printing?</a><br /><strong>PRINT THIS PAGE</strong>';
	cContent += '<p><form action="" method="post" onsubmit="return printable(this);">';
	cContent += '<table border="0" cellpadding="0" cellspacing="0" width=""><tr valign="top">';
	cContent += '<td width="25"><input type="radio" name="pImages" value="no" checked="checked" /></td>';
	cContent += '<td width="562"><strong>Printer-friendly format, no images</strong><br />Removes all graphics, other content, and advertising. Use this if you just want the body text!</td></tr>';
	cContent += '<tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr valign="top">';
	cContent += '<td><input type="radio" name="pImages" value="yes" /></td>';
	cContent += '<td><strong>Printer-friendly format, with images</strong><br />Removes other content, and advertising, but images remain. Use this if you want to print out pictures alongside the body text!</td></tr>';
	cContent += '<tr valign="top"><td>&nbsp;</td><td>&nbsp;</td></tr>';
	cContent += '<tr valign="top"><td colspan="2"><input type="image" src="' + hostName + '/content/presentation/www/images/btnPrint.gif" alt="Print" width="47" height="15" border="0" /></td></tr>';
	cContent += '</table></form></div>';
	cContent += '<div><img src="' + hostName + '/content/presentation/www/images/callout_btm.gif" width="370" height="14" class="dBlock" /></div>';
	
	obj.style.top = temp + "px";
	obj.style.left = temp2 + "px";
	obj.style.display = "block";
	
	obj.innerHTML = cContent;
	
	openCallout = "printCallout";
}


var emailText;

function emailPage() {
	if(openCallout != "" && openCallout != "emailCallout") {
		document.getElementById(openCallout).style.display = "none";
	}	
		
	obj = document.getElementById("emailCallout");
	if ( !obj ) return;
	obj2 = document.getElementById("email");
	if ( !obj2 ) return;
	temp = getposOffset(obj2, "top") + 16;
	temp2 = getposOffset(obj2, "left") - 55;
		
	obj.style.top = temp + "px";
	obj.style.left = temp2 + "px";
	obj.style.display = "block";
	
	openCallout = "emailCallout";
}

function hide(which) {
	document.getElementById(which).style.display = "none";
	openCallout = "";
}

function printable(form) {	
	hide('printCallout');			
	var choice;
	for (var counter = 0; counter < form.pImages.length; counter++) {
		if (form.pImages[counter].checked) choice = counter; 
	}
	var url = location.href;
	var print;
	if(url.indexOf('?') != -1) print = '&print=';
	else print = '?print=';
	if (form.pImages[choice].value == "no")	window.open(location.href + print + '1');
	else if (form.pImages[choice].value == "yes") window.open(location.href + print + '2');	

	return false;
}

function posRelatedProds () {
	if ( !document.getElementById ) { return; }
	var article = document.getElementById("articleContent");
	if ( !article ) { return; }
	var fix = document.getElementById("fixed");
	var temp = document.getElementById("relatedProductsTemp");
		
	if ( fix && temp ) {
		var tempFix = fix.innerHTML;
		tempFix += '<br /><br /><div id="relatedProducts">';
		tempFix += temp.innerHTML;
		tempFix += '</div>';
	
		fix.innerHTML = tempFix;
		temp.style.display = "none";
	}
	
	else if( !fix && temp ) {
		var un = document.getElementById("unav");
		var contrib = document.getElementById("contributor");	
		var collateral = document.getElementById("collateral");
		var div = document.createElement("div");	
		div.setAttribute("id", "relatedProductsFirst");
		
		if(contrib || collateral) {
			var which = contrib ? contrib : collateral;
			article.insertBefore( div, which.nextSibling );
		}
		else {		
			if ( !un ) { return; }
			article.insertBefore( div, un.nextSibling );
		}
		div.innerHTML = temp.innerHTML;
		temp.style.display = "none";
	}		
}

function printIt() {
	window.print();
}



function setImgCaptions() {
	var tbl = document.getElementsByTagName("table");
	for ( var i = 0; i < tbl.length; i++ ) {
    	if ( tbl[i].id.indexOf("img_table_") > -1 ) {
        	var imgs = tbl[i].getElementsByTagName("img");
        	for ( var j = 0; j < imgs.length; j++) {
            	if ( imgs[j].id.indexOf("img_table") > -1 ) {
                	tbl[i].style.width = imgs[j].width + "px";
            	}
        	}
    	}
	}	
}

addLoadEvent(posRelatedProds);
addLoadEvent(setImgCaptions);