var d, dnlWidth, dnlHeight, dnlMaj, dnlMin, dnlBld, l, p, pr, path, sp, ns, dnlInstalled, viewStr, instStr, prog, speed, progTim, dispWS, controlsAbove, pid, scriptable, tbType, pvImageName, pvMode, isMac;

var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);

function GetObj(id) {
if (isDOM) return document.getElementById(id);
if (isIE4) return document.all[id];
}

function GetStyle(id) {
return GetObj(id).style;
}

function Ready(){
	var o;
	HideProgress();
	o = GetObj("dnlplyr");
	if (o){
		o.LoadBook(path + d);
	}
}

function NoReader(){
	var o;
	HideProgress();
	o = GetObj("bookspace");
	o.innerHTML = instStr;
}

function HideProgress(){
	var st;
	st = GetStyle("ProgressLayer");
	st.visibility = "hidden";
	st = GetStyle("ProgressBarLayer");
	st.visibility = "hidden";
	st = GetStyle("BookLayer");
	st.visibility = "hidden";
	speed = 5;
	prog = 1;
	if (progTim > 0) clearInterval(progTim);
}

function DisplayBook(){
	var o;
	if (!ns) DisplayProgress();
	o = GetObj("bookspace");
	o.innerHTML = viewStr;
	o = GetObj("dnlplyr");
	if (o){
		o.LoadBook(path + d);
	}
}

function Install(){
	var o;
	if (!ns) DisplayProgress();
	o = GetObj("bookspace");
	o.innerHTML = viewStr;
	if (ns){
		xpi={'Install DNL Viewer':'xpinst.xpi'};
		if (InstallTrigger.compareVersion("DNL_Viewer",dnlMaj + "." + dnlMin + ".0." + dnlBld) < 0)
			InstallTrigger.install(xpi);
	}
}

function UpdateProgress(){
var img, s;
prog += speed;
if (prog > 194) prog = 194;
else {
	s = Math.round(prog/40);
	switch(s){
	case 0: speed = 5; break;
	case 1: speed = 4; break;
	case 2: speed = 3; break;
	case 3: speed = 2; break;
	case 4: speed = 1; break;
	}
}
img = GetObj("ProgressBar");
img.width = prog;
}

function DisplayProgress(){
	var st, l, t, w, h, img;
	w = document.body.offsetWidth-20;
	h = dnlHeight + 20;
	l = (w - 250) / 2;
	t = (h - 32) / 2;
	st = GetStyle("BookLayer");
	st.left = (w - dnlWidth) / 2;
    if (controlsAbove)
    	st.top = 75;	
    else
    	st.top = 18;	
	img = GetObj("BookLyrImg");
	img.width = dnlWidth;
    if (controlsAbove)
       	img.height = dnlHeight + 15;
    else
       	img.height = dnlHeight;
	st.visibility = "visible";
	st = GetStyle("ProgressLayer");
	st.left = l;
	st.top = t;	
	img = GetObj("ProgressBar");
	img.width = prog;
	st.visibility = "visible";
	st = GetStyle("ProgressBarLayer");
	st.left = l + 46;
	st.top = t + 21;
	st.visibility = "visible";
	progTim = setInterval("UpdateProgress()",300);
}

function InitData(tb){
progTim = 0;
tbType = tb;
instStr = "<table width=" + dnlWidth + " height=" + dnlHeight + " align='center' border=0><tr>";
instStr += "<td align='center' class='instText'>This page contains a Digital Web Book.<br>";
instStr += "To view this digital web book, you must install the DNL Reader.<br><br>";
instStr += "Click <a href='javascript:Install();'>here</a> to install the DNL Reader<br><br><br>"
instStr += "For more information on Digital Web Books, visit <a href='http://digitalwebbooks.com' target='_blank'>digitalwebbooks.com</a></td>";
instStr += "</tr></table>";

speed = 5;
prog = 1;
l = new String(window.location);
p = l.lastIndexOf("/") + 1;
if (p > 0) path = l.substring(0,p);
pr = l.substring(0,4);
if (pr.toLowerCase() == "file"){
	sp = l.substring(8,l.length);
	while(sp.search("/") >= 0){
	  sp = sp.replace("/","\\");
	}
	while(sp.search("%20") >= 0){
	  sp = sp.replace("%20"," ");
	}
	p = sp.lastIndexOf("\\") + 1;
	if (p > 0) sp = sp.substring(0,p);
	path = "file://" + sp;
}

ns = (((document.layers) || (document.getElementById && !document.all)) ? true: false);
dnlInstalled = false;
scriptable = false;
if (ns){
	var agt=navigator.userAgent.toLowerCase();
	var majorVer = parseInt(navigator.appVersion);
	if ((majorVer >= 5) && (agt.indexOf("netscape6") < 0)) scriptable = true;
} else {
 	scriptable = true;
}
isMac = false;
if(navigator.platform.indexOf("Mac")>-1) isMac=true;
}

function InsertBook(){
if (isMac){
	viewStr = "<table width='500' border='0' align='center'><tr height='60'><td>This eBook must be displayed by the DNL Reader.</td></tr>";
	viewStr += "<tr><td>Step 1. <a href='http://www.dnlreader.com/reader'>Click here to download and install the Intel Mac DNL Reader</a>";
	viewStr += "<br/><br/>Step 2. <a href='" + d + "'>Click here to download the eBook</a><br/><br/>";
	viewStr += "Step 3. Double-click the downloaded DNL eBook to view the eBook</td></tr>";
	viewStr += "<tr height='40'><td class='instText'>Note: If the eBook contains multimedia, you may be prompted to install the multimedia plugins.</td></tr></table>";
	document.writeln(viewStr);
	return;
}
if (ns){
	dnlInstalled = navigator.mimeTypes && navigator.mimeTypes["application/x-dnl"] && navigator.mimeTypes["application/x-dnl"].enabledPlugin;
	viewStr = "<table align='center'><tr><td>";
	viewStr += "<embed id=\"dnlplyr\" type=\"application/x-dnl\"";
	viewStr += "SRC=\"" + path + d + "\" width=" + dnlWidth + " height=" + dnlHeight + "	codebase=\"http://www.digitalwebbooks.com/reader/xpinst.xpi\">";
	viewStr += "</embed></td></tr></table>";
} else {
	viewStr = "<table id='dnltab' align='center'><tr><td>";
	viewStr += "<object classid=\"clsid:164B406B-0FD6-4E7F-BA7E-64D227D4CA37\"";
	viewStr += "codebase=\"http://www.digitalwebbooks.com/reader/dbplugin.cab#version=" + dnlMaj + "," + dnlMin + ",0," + dnlBld + "\"";
	viewStr += "name=\"dnlplyr\" id=\"dnlplyr\" data=\"DATA:application/x-oleobject;BASE64,a0BLFtYPf066fmTSJ9TKNwADAADIMAAAUhwAAA==\"";
	viewStr += "width=" + dnlWidth + " height=" + dnlHeight + " onreadystatechange=\"Ready();\" onerror='NoReader();'>";
	viewStr += "</object></td></tr></table>";
}
if ((scriptable) && (controlsAbove)) InsertControlBar();
document.writeln("<span id=\"bookspace\" class='instText'>");
document.writeln("</span>");
if ((scriptable) && (!controlsAbove)) InsertControlBar();
if (dnlInstalled){
	DisplayBook();
} else {
	Install();
}
}

function GoForward(){
 var d = GetObj("dnlplyr");
 if (d) d.GoForward();
}

function GoBack(){
 var d = GetObj("dnlplyr");
 if (d) d.GoBack();
}

function Standalone(){
 var d = GetObj("dnlplyr");
 if (d) d.GoStandAlone();
}

function Save(){
 var d = GetObj("dnlplyr");
 if (d) d.Save();
}

function Mail(){
 var d = GetObj("dnlplyr");
 if (d) d.Mail();
}

function PrintFullSize(){
 HidePrintOptions();
 var d = GetObj("dnlplyr");
 if (d) d.PrintFullSize();
}

function PrintActualSize(){
 HidePrintOptions();
 var d = GetObj("dnlplyr");
 if (d) d.PrintActualSize();
}

function Website(){
 window.open("http://desktopauthor.com/?pid=" + pid, target="_blank");
}

function ShowPrintOptions(){
 if (ent == 0){
   	b = GetObj("buttons");
	if (dispWS == 1)
	   	x = document.body.offsetWidth / 2 - 35;
	else 
	   	x = document.body.offsetWidth / 2 - 50;
	y = b.offsetTop + b.offsetHeight - 8;
	if (ns) {
		x = x + 18;
		y = y + 4;
	}
	st = GetStyle("PrintMenu");
	st.left=x;
	st.top=y;
	st.visibility="visible";
	ent = 1;
 } else {
 	HidePrintOptions();
 }
}

function HidePrintOptions(){
 ent = 0;
 GetStyle("PrintMenu").visibility="hidden";
}

function InsertButtons(){
document.write("<table id=\"buttons\" align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
document.write("<tr><td align=\"center\"> <img  src=\"AutoElongBarNoLogo.jpg\" border=\"0\" usemap=\"#Map\"> <map name=\"Map\">");
document.write("<area shape=\"poly\" coords=\"49,4,29,5,16,7,7,10,2,13,7,18,15,20,28,22,49,24\" onmouseover=\"HidePrintOptions()\" onClick=\"GoBack()\" title=\"Go Back One Page\">");
document.write("<area shape=\"poly\" coords=\"193,4,210,5,225,7,235,9,242,13,235,19,225,22,210,23,193,24\" onmouseover=\"HidePrintOptions()\" onClick=\"GoForward()\" title=\"Go Forward One Page\">");
document.write("<area shape=\"rect\" coords=\"50,5,85,25\" onClick=\"Standalone()\" onmouseover=\"HidePrintOptions()\" title=\"Detach book from Browser\">");
document.write("<area shape=\"rect\" coords=\"85,4,121,25\" onClick=\"ShowPrintOptions()\" title=\"Print this Book\">");
document.write("<area shape=\"rect\" coords=\"120,5,151,25\" onClick=\"Save()\" onmouseover=\"HidePrintOptions()\" title=\"Save this Book\">");
document.write("<area shape=\"rect\" coords=\"151,4,192,25\" onClick=\"Mail()\" onmouseover=\"HidePrintOptions()\" title=\"Mail this Book\">");
document.write("</map></td></tr>");
document.write("</table>");
document.write("</table></div><div id=\"PrintMenu\" style=\"visibility:hidden; position:absolute; left:10px; top:35px; width:150px; height:50px; z-index:2;\">");
document.write("<table border=\"1\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"#eeeeee\">");
document.write("<tr><td><a href=\"javascript:PrintFullSize()\" title=\"Print this book Full Size\"><font face=\"Arial\" pointsize=\"8\">Print Full Size</font></a></td></tr>");
document.write("<tr><td><a href=\"javascript:PrintActualSize()\" title=\"Print this book Actual Size\"><font face=\"Arial\" pointsize=\"8\">Print Actual Size</font></a></td></tr>");
document.write("</table></div>");
if (controlsAbove) document.write("<br><br>");
}

function InsertWSButtons(){
document.write("<table id=\"buttons\" align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
document.write("<tr><td align=\"center\"> <img  src=\"AutoElongBar.jpg\" border=\"0\" usemap=\"#Map\"> <map name=\"Map\">");
document.write("<area shape=\"poly\" coords=\"41,4,21,7,10,10,4,14,9,19,21,22,41,24\" onClick=\"GoBack()\" onmouseover=\"HidePrintOptions()\" title=\"Go Back One Page\">");
document.write("<area shape=\"poly\" coords=\"200,5,214,5,225,7,234,9,242,13,240,17,233,20,225,22,213,22,200,24\" onmouseover=\"HidePrintOptions()\" onClick=\"GoForward()\" title=\"Go Forward One Page\">");
document.write("<area shape=\"poly\" coords=\"42,5,55,4,74,4,74,24,56,24,42,24\" onClick=\"Website()\" onmouseover=\"HidePrintOptions()\" title=\"Visit the Software Maker's Website\">");
document.write("<area shape=\"rect\" coords=\"74,5,103,25\" onClick=\"Standalone()\" onmouseover=\"HidePrintOptions()\" title=\"Detach book from Browser\">");
document.write("<area shape=\"rect\" coords=\"103,4,135,25\" onClick=\"ShowPrintOptions()\" title=\"Print this Book\">");
document.write("<area shape=\"rect\" coords=\"134,4,164,25\" onClick=\"Save()\" onmouseover=\"HidePrintOptions()\" title=\"Save this Book\">");
document.write("<area shape=\"rect\" coords=\"164,4,200,25\" onClick=\"Mail()\" onmouseover=\"HidePrintOptions()\" title=\"Mail this Book\">");
document.write("</map></td></tr></table>");
document.write("</table></div><div id=\"PrintMenu\" style=\"visibility:hidden; position:absolute; left:10px; top:35px; width:150px; height:50px; z-index:2;\">");
document.write("<table border=\"1\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"#eeeeee\">");
document.write("<tr><td><a href=\"javascript:PrintFullSize()\" title=\"Print this book Full Size\"><font face=\"Arial\" pointsize=\"8\">Print Full Size</font></a></td></tr>");
document.write("<tr><td><a href=\"javascript:PrintActualSize()\" title=\"Print this book Actual Size\"><font face=\"Arial\" pointsize=\"8\">Print Actual Size</font></a></td></tr>");
document.write("</table></div>");
if (controlsAbove) document.write("<br><br>");
}

function InsertMetalButtons(){
document.write("<table id=\"buttons\" align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
document.write("<tr><td><img src=\"design2_arrow_left.gif\" onClick=\"GoBack()\" onmouseover=\"HidePrintOptions()\" title=\"Go Back One Page\"></td>");
if (dispWS == 1)
	document.write("<td><img src=\"design2_blue_logo.gif\" onClick=\"Website()\" onmouseover=\"HidePrintOptions()\" title=\"Visit the Software Creator's Website\"></td>");
document.write("<td><img src=\"design2_envelope.gif\" onClick=\"Mail()\" onmouseover=\"HidePrintOptions()\" title=\"Email this Book\"></td>");
document.write("<td><img src=\"design2_pop_out.gif\" onClick=\"Standalone()\" onmouseover=\"HidePrintOptions()\" title=\"Detach book from Browser\"></td>");
document.write("<td><img src=\"design2_save.gif\" onClick=\"Save()\" onmouseover=\"HidePrintOptions()\" title=\"Save this Book\"></td>");
document.write("<td><img src=\"print_icon.gif\" onClick=\"ShowPrintOptions()\" title=\"Print this Book\"></td>");
document.write("<td><img src=\"design2_arrow_right.gif\" onClick=\"GoForward()\" onmouseover=\"HidePrintOptions()\" title=\"Go Forward One Page\"></td></tr>");
document.write("</table><div id=\"PrintMenu\" style=\"visibility:hidden; position:absolute; left:10px; top:35px; width:150px; height:50px; z-index:2;\">");
document.write("<table border=\"1\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"#eeeeee\">");
document.write("<tr><td><a href=\"javascript:PrintFullSize()\" title=\"Print this book Full Size\"><font face=\"Arial\" pointsize=\"8\">Print Full Size</font></a></td></tr>");
document.write("<tr><td><a href=\"javascript:PrintActualSize()\" title=\"Print this book Actual Size\"><font face=\"Arial\" pointsize=\"8\">Print Actual Size</font></a></td></tr>");
document.write("</table></div>");
if (controlsAbove) document.write("<br><br>");
}

function InsertControlBar(){
switch(tbType){
case 1: // ellipse
    if (dispWS == 1){
		InsertWSButtons();
	} else {
		InsertButtons();
	}
break;

case 2: // metal
    InsertMetalButtons();
break;
}
}

function LocateImage(sel){
switch (sel){

case 1:
	document.DNLPreview.width=dnlWidth;
	document.DNLPreview.height=dnlHeight;
break;
case 2:
	w = document.DNLPreview.width;
	h = document.DNLPreview.height;
	w2 = document.DNLPreview.width;
	h2 = document.DNLPreview.height;
	if (h2 != h) h = h2;
	if (w2 != w) w = w2;
	ar = w/h;
	dar = dnlWidth/dnlHeight;
	if (dar > ar){
		nw = w * dnlHeight / h;
		nh = dnlHeight;
	} else {
		nw = dnlWidth;
		nh = h * dnlWidth / w;
	}
	document.DNLPreview.width=nw;
	document.DNLPreview.height=nh;
	xofs = (dnlWidth - nw) / 2;
	if (xofs < 0) xofs = 0;
	yofs = (dnlHeight - nh) / 2;
	if (yofs < 0) yofs = 0;
	document.DNLPreview.hspace = xofs;
	document.DNLPreview.vspace = yofs;
break;

case 0:
case 3:
	w = document.DNLPreview.width;
	h = document.DNLPreview.height;
	xofs = (dnlWidth - w) / 2;
	if (xofs < 0) xofs = 0;
	yofs = (dnlHeight - h) / 2;
	if (yofs < 0) yofs = 0;
	document.DNLPreview.hspace = xofs;
	document.DNLPreview.vspace = yofs;
break;
}
}

function ShowBook(){
  if (dnlInstalled){
	DisplayBook();
  } else {
	Install();
  }
}

function ManualInsertBook(){
if (isMac){
	viewStr = "<table width='500' border='0' align='center'><tr height='60'><td>This eBook must be displayed by the DNL Reader.</td></tr>";
	viewStr += "<tr><td>Step 1. <a href='http://www.dnlreader.com/reader'>Click here to download and install the Intel Mac DNL Reader</a>";
	viewStr += "<br/><br/>Step 2. <a href='" + d + "'>Click here to download the eBook</a><br/><br/>";
	viewStr += "Step 3. Double-click the downloaded DNL eBook to view the eBook</td></tr>";
	viewStr += "<tr height='40'><td class='instText'>Note: If the eBook contains multimedia, you may be prompted to install the multimedia plugins.</td></tr></table>";
	document.writeln(viewStr);
	return;
}
if (ns){
	dnlInstalled = navigator.mimeTypes && navigator.mimeTypes["application/x-dnl"] && navigator.mimeTypes["application/x-dnl"].enabledPlugin;
	viewStr = "<table align='center'><tr><td>";
	viewStr += "<embed id=\"dnlplyr\" type=\"application/x-dnl\" ";
	viewStr += "SRC=\"" + path + d + "\" width=" + dnlWidth + " height=" + dnlHeight + "	codebase=\"http://www.digitalwebbooks.com/reader/xpinst.xpi\">";
	viewStr += "</embed></td></tr></table>";
} else {
	viewStr = "<table align='center'><tr><td>";
	viewStr += "<object classid=\"clsid:164B406B-0FD6-4E7F-BA7E-64D227D4CA37\" ";
	viewStr += "codebase=\"http://www.digitalwebbooks.com/reader/dbplugin.cab#version=" + dnlMaj + "," + dnlMin + ",0," + dnlBld + "\" ";
	viewStr += "name=\"dnlplyr\" id=\"dnlplyr\" data=\"DATA:application/x-oleobject;BASE64,a0BLFtYPf066fmTSJ9TKNwADAADIMAAAUhwAAA==\" ";
	viewStr += "width=" + dnlWidth + " height=" + dnlHeight + " onreadystatechange=\"Ready();\" onerror='NoReader();'>";
	viewStr += "</object></td></tr></table>";
}
if ((scriptable) && (controlsAbove)) InsertControlBar();
document.writeln("<span id=\"bookspace\" class='instText'>");
document.writeln("<table align='center'><tr><td>");
document.writeln("<a href='javascript:ShowBook()'><img name='DNLPreview' onload='LocateImage(" + pvMode + ")' border=0 src='" + pvImageName + "' alt='Click to Load Book'></a>");
document.writeln("</td></tr></table>");
document.writeln("</span>");
if ((scriptable) && (!controlsAbove)) InsertControlBar();
}