// Requires jQuery
var ltIE7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
var IE7 = $.browser.msie && /MSIE\s(7\.)/.test(navigator.userAgent);

Array.prototype.maxVal = function () {
    if (this.length == 0) return undefined;
    var n = Number(this[0]);
    for (var i=1; i<this.length; i++) {n = Math.max(n, this[i])};
    return n;
}

// Class: targetObject
function targetObject() {
    this.targetTop = null;
    this.targetBottom = null;
    this.targetMiddle = null;
    this.targetCenter = null;
    this.targetRight = null;
    this.targetLeft = null;
}

function targetCoordsLayout(targetNode, recurseOff) {
    if (!targetNode) return false;

    if (!recurseOff) recurseOff =  false;

    //var targetNode = object;
    var targetResults = new targetObject();

    // Top
    tempTop = targetNode.offsetTop;

    // Left
    tempLeft = targetNode.offsetLeft;

    // Check to see if offsetParent is the BODY
    // otherwise start looping and adding parent's offset until we get there
    var offsetParentCheck = targetNode.offsetParent;

    if (!recurseOff) {
        while ((offsetParentCheck.nodeName != "BODY") && (offsetParentCheck.nodeName != "HTML")) {
            tempTop += offsetParentCheck.offsetTop;
            tempLeft += offsetParentCheck.offsetLeft;
		    offsetParentCheck = offsetParentCheck.offsetParent;
	    }
    }

    targetResults.targetTop = tempTop;
    targetResults.targetLeft = tempLeft;

    // Right
    tempWidth = targetNode.offsetWidth;
    targetResults.targetRight = tempLeft + tempWidth;

    // Bottom
    tempHeight = targetNode.offsetHeight;
    targetResults.targetBottom = tempTop + tempHeight;

    // Center
    var tempCenter = parseInt(tempWidth / 2, 10);
    targetResults.targetCenter = tempLeft + tempCenter;

    // Middle
    var tempMiddle = parseInt(tempHeight /2, 10);
    targetResults.targetMiddle = tempTop + tempMiddle;

    /*
    alert("Top: " + targetResults.targetTop + "\n" +
      "Right: " + targetResults.targetRight + "\n" +
      "Bottom: " + targetResults.targetBottom + "\n" +
      "Left: " + targetResults.targetLeft + "\n" +
      "Center: " + targetResults.targetCenter + "\n" +
      "Middle: " + targetResults.targetMiddle + "\n"
    );
    */

    return targetResults;
}

function pathParts() {
    this.Path = "";
    this.FullName = "";
    this.Base = "";
    this.Ext = "";
}

function filePath(uriSrc) {
    var tempParts = new pathParts();

    // Get position data to extract name
    firstpos = uriSrc.lastIndexOf('/')+1;
    lastpos = uriSrc.length;
    thisPath = uriSrc.substring(0, firstpos-1);
    thisFullName = uriSrc.substring(firstpos, lastpos);

    // Strip extension
    tmpSplit = thisFullName.split('.');
    thisBase = tmpSplit[0];
    thisExt = tmpSplit[1];

    tempParts.Path     = thisPath;
    tempParts.FullName = thisFullName;
    tempParts.Base     = thisBase;
    tempParts.Ext      = thisExt;

    return tempParts;
}

function imageSwap(item, state) {
    // Get image source
    var imgSrc = item.src;

    // Check whether '/' exists.
    if (imgSrc.lastIndexOf('/') !=-1) {
        var currItem = filePath(imgSrc);

        // Build new image name
        switch(state) {
            case 's': {
                if (currItem.Base.substring(currItem.Base.length-2, currItem.Base.length) != "_s") {
                    newBase = currItem.Base + "_s";
                } else {
                    newBase = currItem.Base;
                }
                break;
            }
            default: {
                if (currItem.Base.substring(currItem.Base.length-2, currItem.Base.length) == "_s") {
                    newBase = currItem.Base.substring(0, currItem.Base.length-2);
                } else {
                    newBase = currItem.Base;
                }
                break;
            }
        }

        // Build new source path
        newSrc = currItem.Path + "/" + newBase + "." + currItem.Ext;

        // Show new image
        item.src= newSrc;
    }
}

function pageJump(loc,maxPage) {
    maxPage = parseInt(maxPage);
    // Get new page value
    var newPage = document.getElementById(loc+'_new').value;
    // Check for integer value for new page
    var integer = /^\d+$/;
    if (!integer.test(newPage)) {
        // Non-integer value - do nothing
        return false;
    }
    // Determine jump page value
    if (newPage > maxPage) {
        jumpPage = maxPage;
    } else if (newPage < 1) {
        jumpPage = 1;
    } else {
        jumpPage = newPage;
    }
    // Get URI
    var uri = location.href;
    var staticPages = false;
    if( uri.indexOf( '.html' ) > -1 ) {
        staticPages = true;
        uri = uri.substring( 0, uri.indexOf( '.html' ));
    }
    if( uri.indexOf( 'INTRO' ) > -1 ) {
        uri = uri.substring( 0, uri.indexOf( 'INTRO' )) 
              + 'CONTENT' + uri.substring( uri.indexOf( 'INTRO' ) +5 )
    } else if( uri.indexOf( 'CONTENT' ) > -1 ) {
        uri = uri.substring( 0, uri.lastIndexOf(','));
    }
    uri = uri + "," + jumpPage;
    if( staticPages ) uri = uri + ".html";
    // Jump to new page
    location.href = uri;
    if (window.event) window.event.cancelBubble = true;
    return false;
}