	var URL_IMAGE_BASE = 'http://covers.ebooks.com';
	var URL_WIDGET_JR_BASE = 'http://widgets.eb20.com/jr/';
	var WINDOW_HEIGHT_DEFAULT = 553;
	var WINDOW_WIDTH_DEFAULT = 775;
	var WINDOW_BAR_HEIGHT = 53;

	function addToSite(bookobj, ItemId) { 
		if (document.getElementById){
			var overlayobj = document.getElementById('hiddenoverlay' + ItemId);
			if (overlayobj.style.display != 'block') {
				overlayobj.style.display = 'block';
				overlayobj.style.left=bookobj.offsetLeft+"px";
				overlayobj.style.top=bookobj.offsetTop+"px";
			} else {
				hideoverlay('hiddenoverlay' + ItemId);
			}
		}
	}

	function launchPreview(ItemId, siteid) {
		launchPreview(ItemId, siteid, '', WINDOW_HEIGHT_DEFAULT, WINDOW_WIDTH_DEFAULT);
	}

	function launchPreview(ItemId, siteid, eformat) {
		launchPreview(ItemId, siteid, eformat, WINDOW_HEIGHT_DEFAULT, WINDOW_WIDTH_DEFAULT);
	}
	
	function launchPreview(ItemId, siteid, eformat, height, width) {
		var fParameter='';
		if (eformat!='') fParameter='&f='+eformat;
		if (height=='') height=WINDOW_HEIGHT_DEFAULT;
		if (width=='') width=WINDOW_WIDTH_DEFAULT;
		
		popup=window.open(URL_WIDGET_JR_BASE + 'shell.asp?IID='+ItemId+'&siteid=' + siteid + fParameter + '&h=' + height + '&w=' + width + '&uuid='+Math.round(Math.random()*10000000),'preview','height='+height+',width='+width);
		popup.focus();
	}

	function hideoverlay(overlaydiv){
		if (document.getElementById){
			var overlayobj=document.getElementById(overlaydiv);
			if (overlayobj.style.display=='block') {
				overlayobj.style.display = 'none';
			}
		}
	}

	function selectCode(selSize, ItemId, siteid, eformat, height, width) {
		var htmlform = document.getElementById('form' + ItemId);
		htmlform.jscriptcode.value = getSelectCode(selSize, ItemId, siteid, eformat, height, width);
	}
	
	function getSelectCode(selSize, ItemId, siteid, eformat, height, width) {
		if (selSize == 'blogsite') {
			return '<div style="width:114px;margin:0px;border:0px;"><div style="padding:0;color:white;clear:left;"><a href="' + URL_WIDGET_JR_BASE + 'mpop.asp?IID=' + ItemId + '&s=' + siteid + '&f=' + eformat + '" target="eb20jr"><img src="' + URL_WIDGET_JR_BASE + 'thumbheader.gif" style="border: none;"></a></div><div style="padding:0;color:white;clear:left;background-color:#9CCE63;padding:7.5px;"><a href="' + URL_WIDGET_JR_BASE + 'mpop.asp?IID=' + ItemId + '&s=' + siteid + '&f=' + eformat + '" target="eb20jr"><img src="' + GetThumbnailPath(ItemId) + '" style="border: none;"></a></div><div style="padding:0;color:white;clear:left;"><a href="' + URL_WIDGET_JR_BASE + 'addtosite.asp?IID=' + ItemId + '" title="Add to your site/blog"><img src="' + URL_WIDGET_JR_BASE + 'thumbfooter1.gif" style="border: none;"></a><a href="' + URL_WIDGET_JR_BASE + 'mpop.asp?IID=' + ItemId + '&s=' + siteid + '&f=' + eformat + '" target="eb20jr"><img src="' + URL_WIDGET_JR_BASE + 'thumbfooter2.gif" style="border: none;"></a></div></div>';

		} else {
			if ((selSize != 'large') && (selSize != 'small')) selSize = 'small';
			if (selSize == 'large')
						{
							height = WINDOW_HEIGHT_DEFAULT;
							width = WINDOW_WIDTH_DEFAULT;
			}
			return '<script type="text/javascript" src="' + URL_WIDGET_JR_BASE + 'eb20junior.js"></script>\n<script type="text/javascript">new eb20junior(' + ItemId + ', "' + selSize + '",' + siteid + ',"' + eformat + '","' + eval(parseInt(height)+parseInt(WINDOW_BAR_HEIGHT)) + '","' + width + '");</script>';
		}
	}
	
	function eb20overlay(ItemId, mode, siteid, eformat, height, width) {
			document.write('<div id="hiddenoverlay' + ItemId + '" style="position:absolute; display:none; border: 1px solid black; background-color: #9CCE63; width: 300px; height: 176px; padding: 8px; font-family: Arial, Helvetica; font-size: 11px;">');
			document.write('Select the size of the widget you\'d like to embed on your site and then copy the HTML code below onto your website.<br /><br />');
			document.write('<form id="form' + ItemId + '">');
			document.write('<select name="widgetsize" onchange="selectCode(this.options[this.selectedIndex].value, ' + ItemId + ', \'' + siteid + '\', \'' + eformat + '\', \'' + height + '\', \'' + width + '\');" style="background-color: #E7E7E7; font-weight: bold; font-size: 11px;  width:290px;">');
			document.write('<option value="small"')
			if (mode == 'small') document.write (' selected');
			document.write('>Small (114 x 219)</option>');
			document.write('<option value="large"');
			if (mode == 'large') document.write (' selected');
			document.write('>Large (' + eval(parseInt(height)+parseInt(WINDOW_BAR_HEIGHT)) + ' x ' + width + ')</option>');
			document.write('<option value="blogsite">Blog site (No JavaScript)</option>');
			document.write('</select><br />');
			document.write('<textarea name="jscriptcode" class="textarea" style="font-family: Arial, Helvetica; background-color: #E7E7E7; display: block; font-size: 11px; width:290px; height:80px;">' + getSelectCode(mode, ItemId, siteid, eformat, height, width) + '</textarea>');
			document.write('</form>');
			document.write('<p align="right"><a href="javascript:hideoverlay(\'hiddenoverlay' + ItemId + '\');" style="color:black;">Hide</a></p>');
			document.write('</div>');
	}
	
	function eb20junior(ItemId, mode, siteid) {
		eb20junior(ItemId, mode, siteid, '',WINDOW_HEIGHT_DEFAULT, WINDOW_WIDTH_DEFAULT);
	}

	function eb20junior(ItemId, mode, siteid, eformat) {
		eb20junior(ItemId,mode,siteid,eformat,WINDOW_HEIGHT_DEFAULT,WINDOW_WIDTH_DEFAULT);
	}
	
	function eb20junior(ItemId, mode, siteid, eformat, height, width) {
		var fParameter='';
		var fJSParameter='';
		if (eformat!='') {
			fParameter='&f='+eformat;
			fJSParameter=',\'' + eformat + '\'';
		} else {
			fJSParameter=',\'\'';
		}
		if (height=='') height=WINDOW_HEIGHT_DEFAULT;
		if (width=='') width=WINDOW_WIDTH_DEFAULT;
		
		if (mode == 'large') {
			document.write('<iframe style="width: ' + WINDOW_WIDTH_DEFAULT + 'px; height: ' + WINDOW_HEIGHT_DEFAULT + 'px;" name="ipage" id="ipage" src="' + URL_WIDGET_JR_BASE + 'shell.asp?e=1&IID=' + ItemId + '&siteid=' + siteid + fParameter + '&h=' + WINDOW_HEIGHT_DEFAULT + '&w=' + WINDOW_WIDTH_DEFAULT + '" scrolling="no" frameborder="0"></iframe>');
		} else {
			mode = 'small';
			new eb20overlay(ItemId, mode, siteid, eformat,height,width);
			document.write('<div style="width:114px;margin:0px;border:0px;">');
			document.write('<div style="padding:0;color:white;clear:left;"><a href="javascript:launchPreview(' + ItemId + ',' + siteid + fJSParameter + ','+height+','+width+');"><img src="' + URL_WIDGET_JR_BASE + 'thumbheader.gif" style="border: none;"></a></div>');
			document.write('<div style="padding:0;color:white;clear:left;background-color:#9CCE63;padding:7.5px;"><a href="javascript:launchPreview(' + ItemId + ',' + siteid + fJSParameter + ','+height+','+width+');"><img src="' + GetThumbnailPath(ItemId) + '" style="border: none;"></a></div>');
			document.write('<div style="padding:0;color:white;clear:left;"><a href="javascript:addToSite(this,' + ItemId + ');" title="Add to your site/blog"><img src="' + URL_WIDGET_JR_BASE + 'thumbfooter1.gif" style="border: none;"></a><a href="javascript:launchPreview(' + ItemId + ',' + siteid + fJSParameter + ','+height+','+width+');"><img src="' + URL_WIDGET_JR_BASE + 'thumbfooter2.gif" style="border: none;"></a></div>');
			document.write('</div>');
		}
	}
	
	function GetThumbnailPath(ItemId) {
		var strBufferedItemID = pad(ItemId.toString(), 9, "0", 0);

		return URL_IMAGE_BASE + "/" + Left(strBufferedItemID, 3) + "/" + Left(strBufferedItemID, 4) + "/" + Left(strBufferedItemID, 6) + "/" + strBufferedItemID + "/" + strBufferedItemID + "-sml-1.jpg"
	}
	
	function Left(str, n){
		if (n <= 0)
		    return "";
		else if (n > String(str).length)
		    return str;
		else
		    return String(str).substring(0,n);
	}
	function Right(str, n){
	    if (n <= 0)
	       return "";
	    else if (n > String(str).length)
	       return str;
	    else {
	       var iLen = String(str).length;
	       return String(str).substring(iLen, iLen - n);
	    }
	}
	
	function pad(pString, l, s, t) {
		return s || (s = " "), (l -= pString.length) > 0 ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2)) + pString + s.substr(0, l - t) : pString;
	}

	var detect = navigator.userAgent.toLowerCase();
	checkIt=function(string) {return detect.indexOf(string) + 1;}
	
	var newOriginalWindow;
	function originalWindow(url) {
		if (window.opener && !window.opener.closed) {
			window.opener.focus();
			if (url!='') window.opener.location.href=url;
		} else if (newOriginalWindow && !newOriginalWindow.closed) {
			newOriginalWindow.focus();
			if (url!='') newOriginalWindow.location.href=url;
		} else {
			if ((screen.availHeight >= 0) && (screen.availWidth >= 0)) {
				var height = screen.availHeight - 50;
				var width = screen.availWidth - 10;
			}
			else if ((screen.Height >= 0) && (screen.Width >= 0)) {
				var height = screen.Height - 90;
				var width = screen.Width - 10;
			}
			if (checkIt('aol')) {
				width = width - 10;
				height = height - 130;
			}
			newOriginalWindow=window.open(url,"orig","toolbar=yes,status=yes,location=yes,menubar=yes,scrollbars=yes,resizable=yes,screenX=0,screenY=0,left=0,top=0,width=" + width + ",height=" + height);
			if (window.focus) {newOriginalWindow.focus()}
		}
	}
