//********* Start of standard code *************

var thumbNailStartPage = 1;

var searchString = "";

function setSearchString(search)
{
    searchString = search;
}

var clickthrough = "adclick.ashx";
var publication = "";
function setClickThroughParams(url, pub)
{
    clickthrough = url;
    publication = pub;
}

function setFacingPages(f) {
    if (f == null || f == "") {
        _viewerParams.facingPages = getUserFacingPageCookie();
    }
    else {
        if (f == "0")
            _viewerParams.facingPages = false;
        else
            _viewerParams.facingPages = true;
    }
}

function op() 
{ //This function is used with folders that do not open pages themselves. See online docs.
}

function ShowArchives() {
   var myWindow = parent.window.open("archives.aspx", "Archives", "status=0, toolbar=0, location=0, menubar=0, resizable=0, scrollbars=0, titlebar=0, height=590, width=300, top=200, left=400");
}

function InviteFriend()
{
    var vp = _viewerParams;
    var url = "inviteFriend.aspx?d=" + vp.docName;
    var page1;
    var page2;
    if(vp.facingPages)
    {
        if(vp.page > 1)
        {
            if(vp.page < vp.lastPage)
            {
                page1 = new Number(vp.page) - 1;
                page2 = page1 + 1;
            }
            else
            {
                page1 = 0;
                page2 = vp.lastPage;
            }
        }
        else
        {
            page1 = 0;
            page2 = 1;
        }
            
    }
    else
    {
        page1 = 0;
        page2 = new Number(vp.page);
    }
   url = url + "&page1=" + page1 + "&page2=" + page2 + "&f=";
   if(vp.facingPages)
     url = url + "1";
   else
     url = url + "0";
   var myWindow = parent.window.open(url, "Share", "status=0, toolbar=0, location=0, menubar=0, resizable=1, scrollbars=0, height=590, width=360, top=200, left=400");
}

function AddToBookmarks (url,title) 
{ 
    parent.window.external.AddFavorite(url,title); 
}  

function AddCurrentPageToBookMarks()
{
    var pgnum = (_viewerParams.facingPages) ? _viewerParams.page - 1 : _viewerParams.page;
    var title = publication + " - " + _viewerParams.docName + " - Page - " + pgnum;
    var urlparts = parent.location.href.split("?");
    
    var url = urlparts[0] + "?d=" + _viewerParams.docName + "&pagenum=" + pgnum;
   
    AddToBookmarks(url, title);
}

function getUserZoomCookie() {
    var zoomCookie = getCookie("userzoom");
    var zoomIndex = -1;
    if (zoomCookie == null || zoomCookie.length == 0) {
        zoomIndex = -1;
    }
    else {
        zoomIndex = new Number(zoomCookie);
        if (zoomIndex > _viewerParams.resolutions.length - 1)
            zoomIndex = -1;
    }
    return zoomIndex;
}

function getUserFacingPageCookie() {
    var pagesCookie = getCookie("facingpages");
    if (pagesCookie == null || pagesCookie == 1) {
        return true;
    }
    return false;
}

function getUserZoom() {
    //check if there is cookie for user zoom settings
    if (frmToolbar.isFitPages())
        return -1;
    var zoomIndex = getUserZoomCookie();
    //User saved with FitPages - but now has unchecked fit pages
    if (zoomIndex == -1) 
        return frmToolbar.getZoomSliderValue();
    else
        return zoomIndex;
}

function InitializeDocument(pageNum)
{
	var href = document.location.href;
	if(href.substring(0, 4).toLowerCase() == "http")
		_viewerParams.IsWeb = true;
	else
		_viewerParams.IsWeb = false;

	_viewerParams.Thumbnails = false;

	_viewerParams.userZoom = 0;
	_viewerParams.zooming = false;

	initializeToolbar();
}

function initializeToolbar() {
    if (!frmToolbar.isToolbarLoaded())
        setTimeout("initializeToolbar()", 100);
    else
        frmToolbar.InitializeToolbar(_viewerParams);
}

function isNetscape() {
	return (window.navigator.userAgent.indexOf("IE") == -1)
} 


function NextThumbnails()
{
	if((thumbNailStartPage + 16 ) < _viewerParams.lastPage)
	{
		thumbNailStartPage += 16;
		DisplayThumbnails(thumbNailStartPage);
	}
}

function PrevThumbnails()
{
	if(thumbNailStartPage > 1)
	{
		thumbNailStartPage -= 16;
		if(thumbNailStartPage < 1)
			thumbNailStartPage = 1;
		DisplayThumbnails(thumbNailStartPage);
	}
}

function DisplayThumbnails(startPage)
{
    parent._viewerParams.Thumbnails = true;
    basefrm.window.location.href = "thumbs.aspx?d=" + _viewerParams.docName;
}



function Page()
{
	this.width = 0;
	this.height = 0;
	this.pgnum = -1;
//	this.mapName = "";
//	this.mapString = "";
	this.src = "";
	this.linkNodes = null;
	
	this.InitializePage = InitializePageFromParams;
}

function linkOut()
{
    this.className = "maglink";
}

function linkOver()
{
    this.className = "maglinkrollover";
}

function InitializePageFromParams(params)
{
    var w,h, factor;
    
    //get zoom index
    var i;
    for(i = 0; i < params.resolutions.length; i++)
    {
        if(params.zoom == params.resolutions[i])
            break;
    }
    var zoomIndex = i;
    
	if(this.pgnum != -1)
	{
	    this.height = params.pageHeight  * params.zoom / 72;
	    this.width = params.pageWidth * params.zoom / 72;
		if(params.pageDimensions[this.pgnum] != null)
		{
			this.height = params.pageDimensions[this.pgnum].height * params.zoom / 72;
			this.width = params.pageDimensions[this.pgnum].width * params.zoom / 72;
		}
		var factor = params.zoom / 72.0;
		var s = "";
		
	    var imageWidth;
	    var imageHeight;
        if (params.zoom == params.resolutions[0]) //lowest resolution
        {
            var windowHeight = getWindowHeight(basefrm);
            var windowWidth = getWindowWidth(basefrm);
            if(params.facingPages)
                windowWidth = windowWidth / 2;
            //we need to fit to window
            if (!(params.widths[0] < windowWidth && params.heights[0] < windowHeight)) {
                if (params.widths[0] / params.heights[0] > windowWidth / windowHeight) {
                    //width is restricted
                    imageWidth = windowWidth;
                    imageHeight = imageWidth * params.heights[0] / params.widths[0];
                }
                else {
                    //height is restricted
                    imageHeight = windowHeight;
                    imageWidth = imageHeight * params.widths[0] / params.heights[0];
                }
                factor = factor * imageHeight / this.height;
                
                this.width = imageWidth;
                this.height = imageHeight;
            }
        }
			
		//page needs imagemap
		
		pageLinks = params.links[this.pgnum];
	
		this.mapString = "";
		
		var links = new Array();

		if(pageLinks != null)
		{
			for(i = 0; i < pageLinks.length; i++)
			{
				var link = pageLinks[i];
				var imgNode = basefrm.document.createElement("img");
				var linkNode = basefrm.document.createElement("a");
				
				linkNode.appendChild(imgNode);
				
				imgNode.style.position = "absolute";
                imgNode.onmouseout = linkOut;
                imgNode.onmouseover = linkOver;
				imgNode.setAttribute("src", "images/transparent.gif");
				imgNode.setAttribute("border" , "0");
				
				var x1, y1, x2, y2;
				
				x1 = Math.round(new Number(link.x1) * factor);
				y1 = Math.round(new Number(link.y1) * factor);
				x2 = Math.round(new Number (link.x2) * factor);
				y2 = Math.round(new Number(link.y2) * factor);
				
				imgNode.style.left = x1 + "px";
				imgNode.style.top = y2 + "px";
				imgNode.style.width = (x2 - x1) + "px";
				imgNode.style.height = (y1 - y2) + "px";
                imgNode.className = "maglink";
				
				if(link.uri.toLowerCase().substr(0, 7) == "mailto:")
				{
				    linkNode.setAttribute("href", link.uri);
				}
				else if(link.uri.toLowerCase().substr(0, 11) == "javascript:")
				{
				    linkNode.setAttribute("href", link.uri);
				}
				else if(link.type == "external")
				{
				    //do clickthrough
				    var href;
				    if(clickthrough != null && clickthrough.length != 0)
				        href = clickthrough + "?url=" + escape(link.uri) + "&pub=" + publication + "&edition=" + params.docName + "&pagenum=" + this.pgnum; 
				    else
				        href = link.uri;
				    
				    linkNode.setAttribute("href", href);
				    linkNode.setAttribute("target", "_new");
				}
				else
				{
				    linkNode.setAttribute("href", "javascript:linkClick(" + link.page + ")");
				}
				links.push(linkNode);
			}
		}	
		this.linkNodes = links;

		this.src='docs/' + params.docName + '/' + 'images/' + params.zoom + "/" + params.docName + "_" + getPageNumberString(this.pgnum) + ".page";
		if(searchString != "")
		    this.src += "?s=" + searchString;
	}
}

function OnPageImageLoaded()
{
}

function addPageToDiv(pg, div, id)
{
    while(div.hasChildNodes())
    {
        div.removeChild(div.firstChild);
    }

    var newImage = basefrm.document.createElement("img");
    newImage.onload = OnPageImageLoaded;
    newImage.setAttribute("id", id);
    newImage.setAttribute("border", "0");
    newImage.setAttribute("src", pg.src);
//    basefrm.trace('initiated download of page ' + pg.src);
    newImage.setAttribute("width", pg.width);
    newImage.setAttribute("height", pg.height);
    newImage.style.left = "0";
    newImage.style.top = "0";
    div.appendChild(newImage);
    for(i = 0; i < pg.linkNodes.length; i++)
    {
        div.appendChild(pg.linkNodes[i]);
    }
}

function getWindowHeight(w) {
    var functionReturn = 0;
    var d = w.document;
    if ((d.documentElement) && (d.documentElement.clientHeight))
        functionReturn = d.documentElement.clientHeight;
    else if ((d.body) && (d.body.clientHeight))
        functionReturn = d.body.clientHeight;
    else if ((d.body) && (d.body.offsetHeight))
        functionReturn = d.body.offsetHeight;
    else if (w.innerHeight)
        functionReturn = w.innerHeight - 2;

    functionReturn = parseInt(functionReturn);
    if ((isNaN(functionReturn) == true) || (functionReturn < 0))
        functionReturn = 0;
    return functionReturn;
};

function getWindowWidth(w) {
    var functionReturn = 0;
    var d = w.document;
    if ((d.documentElement) && (d.documentElement.clientWidth))
        functionReturn = d.documentElement.clientWidth;
    else if ((d.body) && (d.body.clientWidth))
        functionReturn = d.body.clientWidth;
    else if ((d.body) && (d.body.offsetWidth))
        functionReturn = d.body.offsetWidth;
    else if (w.innerWidth)
        functionReturn = window.innerWidth - 18;

    functionReturn = parseInt(functionReturn);
    if ((isNaN(functionReturn) == true) || (functionReturn < 0))
        functionReturn = 0;
    return functionReturn;
};

function getPageZoom(params)
{
    var imageWidth;
    var imageHeight;
    var windowHeight = getWindowHeight(basefrm);
    var windowWidth = getWindowWidth(basefrm);
    if(params.facingPages)
        windowWidth = windowWidth / 2;
    
    var i;
    for(i = params.widths.length - 1; i > 0; i--)
    {
        if(params.widths[i] < windowWidth)
            break;
    }
    
    for(; i > 0; i--)
    {
        if(params.heights[i] < windowHeight)
            break;
    }
    return params.resolutions[i];
}

function pageResize()
{
    if (getUserZoom() == -1 && !_viewerParams.zooming) {
        _viewerParams.isZoomed = false;
        DisplayPage(_viewerParams, false);
        _viewerParams.zooming = false;
        
    }
}

function isPageZoomed(params)
{
    var zoom = getPageZoom(params);
    if(params.zoom == zoom)
        return false;
    return true;
      
}

var reentrant = 0;

function DisplayPage(params, fSyncToc)
{
	var width, height;
	var cellPadding = 10;
	params.Thumbnails = false;
	
	//Two pages for facing pages
	//Page1 is on left Page 2 is on right
	//Page objects will contain the following
	//Page.Width
	//Page.Height
	//Page.src
	//Page.pgnum
	//
	var Page1 = new Page();
	var Page2 = new Page();
	
	if(params.facingPages == true)
	{
		if(params.page % 2 == 0)
		{
			//Even page - will be on the left
			Page1.pgnum = params.page;
		
		    if (params.page == params.lastPage)
			{
			    Page2.pgnum = -1;
		    }
		    else	
			    Page2.pgnum = (params.page < params.lastPage) ? params.page + 1 : params.lastPage;
		}
		else
		{
            if (params.page == params.firstPage)
            {
			            Page1.pgnum = params.firstPage;
			            Page2.pgnum = -1;
            }			
		    else	
		    {
			    Page1.pgnum = (params.page > params.firstPage) ? params.page - 1: "";
			    Page2.pgnum = params.page;
            }
	    }
	}
	else
	{
		//Everything happens on page1
		Page1.pgnum = params.page;
		Page2.pgnum = -1;
	}
	
	//Here - we need to set the page zoom; - before initializing the pages
	
	//if zoomed in - use the zoom resolution - otherwise figure out resolution based on screen size
	if(!params.isZoomed)
	    params.zoom = getPageZoom(params);
	Page1.InitializePage(params);
	Page2.InitializePage(params);

	
    var leftDiv = basefrm.document.getElementById("_leftDiv");
    var rightDiv = basefrm.document.getElementById("_rightDiv");
    


   addPageToDiv(Page1, leftDiv, "_leftImg");
    var helpDiv = basefrm.document.getElementById("customDiv");
    _rightImg = basefrm.document.getElementById("_rightImg");

    var lPageNav = basefrm.document.getElementById("lPageNav");
    var mPageNav = basefrm.document.getElementById("mPageNav");
    var rPageNav = basefrm.document.getElementById("rPageNav");

    if((Page1.pgnum == 1) & (Page1.height < 1000))
    {
		//remove everything from _right div except _rightImg container
		//should correct link display issue and improper scrolling on page 1    		
		while(rightDiv.childNodes[1] != null)
		{
          rightDiv.removeChild(rightDiv.childNodes[1]);
        } 
	    
	    //set properties of _rightImg
	    _rightImg.src = "images/transparent.gif";
        _rightImg.width = 0;
        _rightImg.height = 0;
    		
    		//enable newsstand
        helpDiv.style.display = "";
        
        //set pager divs
        lPageNav.style.display = "";
        mPageNav.style.display = "";
        rPageNav.style.display = "none";
    }
    else
    {
        helpDiv.style.display = "none";
        //set pager divs
        mPageNav.style.display = "none";
        lPageNav.style.display = "";
        rPageNav.style.display = "";
        
        if(Page2.pgnum != -1)
        {
              addPageToDiv(Page2, rightDiv, "_rightImg");
        }
        else
        {
            _rightImg.src = "../images/transparent.gif";
            var j;
            while(rightDiv.childNodes[1] != null)
                rightDiv.removeChild(rightDiv.childNodes[1]);
            _rightImg.width = 0;
            _rightImg.height = 0;
        } 
    }
    
    //Now set a time out - and try to load other next pages
    if(Page2.pgnum != -1)
       lastPageDownloaded = Page2.pgnum;
    else
        lastPageDownloaded = Page1.pgnum;
    if(params.pageNavigation)
        setTimeout('cacheNextPages(lastPageDownloaded, _viewerParams.zoom, false)', 2500);
    params.pageNavigation = false;
}


function ImageCacheComplete(evt)
{
}

function cacheNextPages(n, res, zoom)
{
    var params = _viewerParams;
    var src;
    n++;
    if(n <= params.lastPage)
    {
        var newImage = new Image();
        src = 'docs/' + params.docName + '/' + 'images/' + res + "/" + params.docName + "_" + getPageNumberString(n) + ".page";
        newImage.onload = ImageCacheComplete;        
        newImage.src = src;
    }
    //If facing pages is true - then download two pages - otherwise just one page
    if (params.facingPages) {
        n++;
        if (n <= params.lastPage) {
            src = 'docs/' + params.docName + '/' + 'images/' + res + "/" + params.docName + "_" + getPageNumberString(n) + ".page";
            var newImage = new Image();
            newImage.onload = ImageCacheComplete;
            newImage.src = src;
        }
    }
}



function getPageNumberString(pgnum)
{
	if(pgnum < 10)
		return "000" + pgnum;
	if(pgnum < 100)
		return "00" + pgnum;
	if(pgnum < 1000)
		return "0" + pgnum;
	return new String(pgnum);
}




// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) 
{
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) 
{
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}



function getFrameWidth() {

var frameWidth;

if (self.innerWidth)
{
	
	frameWidth = self.innerWidth;
}
else if (document.documentElement && document.documentElement.clientWidth)
{
	frameWidth = basefrm.document.documentElement.clientWidth;
}
else if (document.body)
{
	frameWidth = basefrm.document.body.clientWidth;
}

return frameWidth;

}

function getFrameHeight() {

var frameHeight;

if (self.innerWidth)
{
	
	frameHeight = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientWidth)
{
	frameHeight = basefrm.document.documentElement.clientHeight;
}
else if (document.body)
{
	//frameHeight = basefrm.document.body.offsetHeight

	frameHeight = basefrm.document.body.clientHeight;
}


	return frameHeight;

}



// -->

