﻿//http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_result;
}

function gMarginHeight(elmID)
{    
    var retVal;
    
    if(isIE()) 
        retVal = parseInt(elmID.currentStyle.marginTop)+parseInt(elmID.currentStyle.marginBottom);
    else 
        retVal = parseInt(document.defaultView.getComputedStyle(elmID, '').getPropertyValue('margin-top'))+parseInt(document.defaultView.getComputedStyle(elmID, '').getPropertyValue('margin-bottom'));
        
    if(isNaN(retVal))
        return 0;
    else    
        return retVal;
}

// Check if an array contains a particular value
function arrayContains(array, value) {
    for (i=0; i < array.length; i++) {
        if (array[i]==value) {
            return true;
        }
    }
    
    return false;
}

// get methods
function gX(el){var curleft=0;if(el.offsetParent){while(1){curleft+=el.offsetLeft;if(!el.offsetParent)break;el=el.offsetParent;}}else if(el.x)curleft += el.x;return curleft;}
function gY(el){var curtop=0;if(el.offsetParent){while(1){curtop+=el.offsetTop;if(!el.offsetParent)break;el=el.offsetParent;}}else if(el.y)curtop+=el.y;return curtop;}
function gW(el){var w;w=el.offsetWidth;return w;}
function gH(el){var h;h=el.offsetHeight;return h;}
function gBodyW(){var w=document.body.offsetWidth;return w;}
function gBodyH(){var h=document.body.offsetHeight;return h;}
function gWinW(){var w;if(isIE()){w=document.documentElement.offsetWidth;}else{w=window.innerWidth;}return w;}
function gWinH(){ return f_clientHeight(); }
//function gWinH(){var h;if(isIE()){h=document.documentElement.offsetHeight;}else{h=window.innerHeight;}return h;};
function gST() {
                    var t;
                    t = document.documentElement.scrollTop >= document.body.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop; 
                    return t;
                }
function gSL(){var l;l=document.documentElement.scrollLeft;return l;}
function gEST(el){var t;t=gE(el).scrollTop;return t;}
function gT(el){var t;t=el.offsetTop;return t;}
function gL(el){var l;l=el.offsetLeft;return l;}
function gURLParams(name, URL)
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(URL);

    if( results == null )
        return "";
    else
        return results[1];
}

//PAGE VIEW methods
function getNumDisplayedPages()
{ 
	if (PAGE_NUMBER < 2 || PAGE_NUMBER == PAGE_COUNT || VIEW == 'FULL')
    {
		return 1;
    }
    else if( VIEW == 'FACING' )
    {
		return 2;
    }
    return 1;
}

// set methods
function sT(el,t){el.style.top=t+'px';}
function sL(el,l){el.style.left=l+'px';}
function sW(el,w){if(!isNaN(w))w+='px';el.style.width=w;}
function sH(el,h){if(h<0)h=0;if(!isNaN(h))h+='px';el.style.height=h;}
function sO(el,v){el.style.overflow=v;}

// div methods
function gE(el){var o;if(document.getElementById){o=document.getElementById(el);}else if(document.all){o=document.all[el];}return o;}
function wH(div, html){gE(div).innerHTML=html;}
function hide(el){el.className=el.className.replace('show','hide');}
function hideInline(el){el.className=el.className.replace('inline','hide');}
function showInline(el){el.className=el.className.replace('hide','inline');}
function show(el){el.className=el.className.replace('hide','show');}
function disabled(el) { el.className = el.className.replace('enabled', 'disabled'); }
function enabled(el) { el.className = el.className.replace('disabled', 'enabled'); }

// browser detection
function isIE(){if(navigator.appName.indexOf('Microsoft')!=-1)return true;else return false;}
function isV6down(){if(navigator.appVersion.indexOf('6.0')!=-1||navigator.appVersion.indexOf('5.5')!=-1)return true;else return false;}
function isV8() { if (navigator.appVersion.indexOf('8.0') != -1) return true; else return false; }
function isOp() { if (navigator.appName.indexOf('Opera') != -1) return true; else return false; }
function isNS(){if(navigator.appName.indexOf('Netscape')!=-1)return true;else return false;}
function isSaf(){if(navigator.appVersion.indexOf('Safari')!=-1)return true;else return false;}

// buttons
function bO(el){el.src=el.src.indexOf('_off')>-1?el.src.replace('_off','_on'):el.src.replace('_on','_off');} 

// hide or show the hand/pointer or default arrow
function dC(d){var c;if(d){if(isIE())c='hand';else c='pointer';}else{c='default';}document.body.style.cursor=c;}
function dC2(d,obj){var c;if(d){if(isIE())c='hand';else c='pointer';}else{c='default';}obj.style.cursor=c;}

//Text Decoration.
function ul(el, value){ (value) ? el.style.textDecoration = "underline" : el.style.textDecoration = ""; }

//Navigation
var newWindow; 
function navigateCurrent(url){ window.location.href = url; }
function navigateExternal(url){ window.open(url); }
function navigateExternalWinStats(url, winstats){ window.open(url, null, winstats); }
function navigateExternalPopUp(url, windowName)
{
    var winStats = "toolbar=yes,location=yes,directories=no,menubar=no, resizable=yes, scrollbars=yes,width=800px,height=600px";
    
    if (navigator.appName.indexOf("Microsoft") >= 0)
        winStats += ",left=0,top=0";
    else
        winStats += ",screenX=0,screenY=0";
        
    if (newWindow != null)
    {
        if (typeof(newWindow) == "undefined" || newWindow.closed)
        {
            newWindow = window.open(url, windowName, winStats);
            newWindow.focus();
        } else 
        {
            newWindow.location = url;
            newWindow.focus();
        }
    } else 
    {
        newWindow = window.open(url, windowName, winStats);
        newWindow.focus();
    }
}

function navigateExternalSameWindow(url, windowName)
{        
    if (newWindow != null)
    {
        if (typeof(newWindow) == "undefined" || newWindow.closed)
        {
            newWindow = window.open(url, windowName);
            newWindow.focus();
        } else 
        {
            newWindow.location = url;
            newWindow.focus();
        }
    } else 
    {
        newWindow = window.open(url, windowName);
        newWindow.focus();
    }
}

function NavigateOpenerExternal(URL) 
{
	var winStats ='toolbar=yes,location=yes,directories=no,menubar=yes,resizable=yes,scrollbars=yes,width=800,height=500';

	if(navigator.appName.indexOf('Microsoft') >= 0)
		winStats += ',left=200,top=250';
	else
		winStats += ',screenX=200,screenY=250';

	if(window['opener'] != null) 
	{
		try 
		{
			if(typeof(window['opener']) == 'undefined' || opener.closed) 
			{
				window.open(URL,'',winStats);
			} 
			else 
			{
				opener.location = URL;
				opener.focus();
			}
		}
		catch(e) 
		{
			window.open(URL,'',winStats);
		}
	} 
	else 
	{
		window.open(URL,'',winStats);
	}
}

//Formatting
function formatJSONString( value )
{
	if( value != null )
		return value.replace( /\r/g, '<br>' ).replace( /\n/g, '<br>' ).replace( /\"/g, '\\\"' );
	else
		return '';
}

function globalReplaceChar(string, chr)
{
    var str = string.toString();
    var newStr = '';
    
    for(var i = 0; i < str.length; ++i)
    {
        if(str.charAt(i) != chr)
            newStr += str.charAt(i);
    }
    
    return newStr;
}

function setFirstFacingPageNumber() {    
    // if its an even page subtract one as long as its not the first or last page
    if (PAGE_COUNT > 2 && PAGE_NUMBER % 2 != 0 && PAGE_NUMBER != PAGES.pages.length && PAGE_NUMBER != 1) {
        if (VERSION != 'FLASH') {
            var widthContentFrame = gW(gE('content'));
            // if the two page images are not too wide for the content frame, decrement PAGE_NUMBER
            if (!oPUBINFO.PublicationType.image.includedimensions || (FACING_IMAGE.images[PAGE_NUMBER - 1].width + FACING_IMAGE.images[PAGE_NUMBER].width) < widthContentFrame)
                PAGE_NUMBER--;
        }
        else {
            PAGE_NUMBER--;
        }
    }  
}

function TransformCoords( coords, transform )
{
    var modCoords = '';
    var stringLength;
    var start;
    var coordsArray;

    stringLength = coords.length;

    coordsArray = coords.split(',');

    for( var i = 0 ; i < coordsArray.length ; i++)
    {
        if (i > 0)
            modCoords += ",";

        modCoords += coordsArray[i] * transform;
    }

    return modCoords;
}
String.prototype.endsWith = function(str)
{ return (this.match(str + "$") == str) }