// copyright (c) 2002-2011 AquaMinds Software Corporation. All rights reserved.
var sectionPageNumber   = 1;
var pageNumber          = 0;
var selectedEntryId     = -1;
var selectedText        = null;
var notebookPageCount   = 1;
var notebookTitle       = "";
var notebookTitleHeight = 55;
var sectionName         = "";	           // Section name of current page
var pageName            = "";		       // Name of current page
var hasCoverPage        = 0;
var contentsValid       = 0;
var showControls        = 1;
var showCoverTitle      = 1;
var showCoverRevision   = 0;
var showCoverBadge      = 0;
var revision            = "";
var bgImageType         = -2;
var bgImageName         = "";
var bgImageWidth        = 0;
var bgImageHeight       = 0;
var bgColor             = "#C0C8D0";
var fontScaling         = 1.0;
var currentPageIdent    = -1;
   
function getCookieValue(name) {
    var i1 = document.cookie.indexOf(name + "=");
    if (i1 >= 0) {
        i1 += name.length + 1;
        var i2 = document.cookie.indexOf(";", i1);
        if (i2 < 0) i2 = document.cookie.length;
        var s = document.cookie.substring(i1, i2);
	return s;
    }
    return null;
}

// font scaling used in iPad app only
var scaling = getCookieValue("fontScaling");
if (scaling && (scaling > 0)) {
    fontScaling = scaling;
}
function setFontScaling(newScale) {
    fontScaling = newScale;
    document.cookie = "fontScaling=" + fontScaling;
}

if (location.search.indexOf("?") == 0) {
    pageNumber = parseInt(location.search.substring(1, location.search.length));
}

var hash;

if (location.hash.length > 1) {
    hash = location.hash.substring(1, location.hash.length);
    var i = hash.indexOf("_");
    if (i < 0)
		selectedEntryId = hash;
    else {
		selectedEntryId = hash.substr(0, i);
		selectedText    = decodeURIComponent(hash.substr(i + 1));
    }
}

var locURL = location.href;
if (hash != null)
    locURL = locURL.substring(0, locURL.length - hash.length - 1);
var baseURL  = locURL.substring(0, locURL.lastIndexOf("/")+1);
var imageURL = baseURL + "images/";


var appName   = window.navigator.appName.toUpperCase();
var vendor    = window.navigator.vendor ? window.navigator.vendor.toUpperCase() : "";
var isSafari  = window.navigator.userAgent.indexOf("Safari") >= 0;
var isIE      = (appName.indexOf("EXPLORER") >= 0);
var isMozilla = (!isSafari && !isIE && (appName.indexOf("NETSCAPE") >= 0) && 
				 ((vendor == "") || (vendor.indexOf("NETSCAPE") >= 0) || (vendor.indexOf("CAMINO") >= 0) || (vendor.indexOf("FIREFOX") >= 0)));
var isIPhone  = (window.navigator.platform.indexOf("iPhone") >= 0) || (window.navigator.platform.indexOf("iPod") >= 0);
var isIPad    = (window.navigator.platform.indexOf("iPad") >= 0);
var useTitleFrame = !isIPhone;
var useTouch = (isIPhone || isIPad) && isSafari;

// Following functions emulate the new encodeURI(), for use in older browsers
function utf8(wide) {
    var c, s;
    var enc = "";
    var i = 0;
    while(i<wide.length) {
		c= wide.charCodeAt(i++);
		// handle UTF-16 surrogates
		if (c>=0xDC00 && c<0xE000) continue;
		if (c>=0xD800 && c<0xDC00) {
			if (i>=wide.length) continue;
			s= wide.charCodeAt(i++);
			if (s<0xDC00 || c>=0xDE00) continue;
			c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
		}
		// output value
		if (c<0x80) enc += String.fromCharCode(c);
		else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
		else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
		else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    }
    return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
    return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~*'();/?:@&=+$,%";

function encodeURINew(s) {
    var s = utf8(s);
    var c;
    var enc = "";
    for (var i= 0; i<s.length; i++) {
		if (okURIchars.indexOf(s.charAt(i))==-1)
			enc += "%"+toHex(s.charCodeAt(i));
		else
			enc += s.charAt(i);
    }
    return enc;
}

function ntEncodeURI(s)
{
    if ((typeof encodeURI == "function") && !isIE) {
		// Use JavaScript built-in function
		// IE 5.5+ and Netscape 6+ and Mozilla
		if (encodeURI("a%20b").length != 5)
			// cannot use the builtin function because it is re-encoding
			return encodeURINew(s);
		else
			return encodeURI(s);
    }
    else {
		// Need to mimic the JavaScript version
		// Netscape 4 and IE 4 and IE 5.0
		return encodeURINew(s);
    }
}

function validatePageNumber(num) {
    // Make sure a new page number is within bounds
    if (num < 0) {
        num = hasCoverPage ? 0 : 1;
    }
    else if ((num == 0) && !hasCoverPage) {
        num = 1;
    }
    else if (num > notebookPageCount) {
        num = notebookPageCount;
    }
    return num;
}

function gotoPage(pageNum, entryId, text) {
    var i;
    var doPageReload;
    // Changed to enable iframe embedding
    //if (parent.validatePageNumber)
    //		pageNum = parent.validatePageNumber(pageNum);
    //else 
    if (validatePageNumber)
        pageNum = validatePageNumber(pageNum);
    
    // Set vars to new page and selected entry id
    doPageReload = (pageNumber == pageNum) && (selectedEntryId != entryId) && (entryId > 0);
    pageNumber = pageNum;
    selectedEntryId = entryId;
    selectedText = text;
    
    // Change the window location to the new page
    i = location.href.indexOf("?");
    var newLoc;
    
    if (!isMozilla) {
        newLoc = ntEncodeURI((i >= 0) ? location.href.substring(0, i) : location.href)  + "?" + pageNum + ((entryId < 0) ? "" : ("#" + entryId));
    }
    else {
        newLoc = ((i >= 0) ? location.href.substring(0, i) : location.href)  + "?" + pageNum + ((entryId < 0) ? "" : ("#" + entryId));
    }
    if (text)
		newLoc += "_" + text;
    
    location.href = newLoc;
    
    if (doPageReload)
        // Force page to refresh so that selection rectangle changes
		frames[0].location.reload();
    window.scrollTo(0, 1);
	
}

function prevPage() {
    gotoPage((pageNumber == (hasCoverPage ? 0 : 1)) ? notebookPageCount : (pageNumber - 1), -1);
}

function nextPage() {
    gotoPage((pageNumber == notebookPageCount) ? 1 : (pageNumber + 1), -1);
}

function go(pn, en, text) {
    gotoPage(pn, en, text);
}

function prevDown(doc, isDown) {
    var big = isIPhone || isIPad;
    doc.images["Prev"].src = imageURL + ((isDown == true) ? (big ? "prev2_pressed.png" : "prev_pressed.png") : (big ? "prev2.png" : "prev.png"));
}

function nextDown(doc, isDown) {
    var big = isIPhone || isIPad;
    doc.images["Next"].src = imageURL + ((isDown == true) ? (big ? "next2_pressed.png" : "next_pressed.png") : (big ? "next2.png" : "next.png"));
}

function loadCSS(doc, url) {
    var headID = doc.getElementsByTagName("head")[0];
    var newScript = doc.createElement('link');
    newScript.rel = 'stylesheet';
    newScript.type = 'text/css';
    newScript.href = url;
    headID.appendChild(newScript);
}

// First set all the tabs to their lowered image.  Then, if the current page has a tab, set its
// image to the raised image. If the current page has no tab image, use the current section's tab.
function updateTabs() {
    var doc = frames["tabFrame"].document;
    var i;
    var w;
    var images = doc.images;
    var n = images.length;
    var image = null;
    
    for (i = 2; i < n; i++) {
        images[i].src = images[i].name + "/tabL.png";
        images[i].border=0;
		w = images[i].width;
    }
    if (images["pages/" + pageNumber]) {
		image = images["pages/" + pageNumber];
        image.src = "pages/" +  pageNumber + "/tabH.png";
    }
    else if (images["pages/" + sectionPageNumber]) {
		image = images["pages/" + sectionPageNumber];
        image.src = "pages/" +  sectionPageNumber + "/tabH.png";
    }
    if (image) {
		image.width = w - 1;
    }
}


