/**
/* toolbar.js
/*
/* @copyright: 2006 by Thomas M. Stambaugh & Zeetix, LLC (http://www.zeetix.com)
/* All rights reserved.
/*
/* The contents of this file may not be copied, duplicated, or used without the
/* written consent of Zeetix, LLC.
**/

/**
 * Set up a log window for brain-damaged IE debugging.
 */
var TheZeeLog = null;

function reportAgent() {
    var anAgentArray = ["msie", "firefox"];
    var agent = navigator.userAgent.toLowerCase();
    for (var i = 0; i < anAgentArray.length; i++) {
        var agentStr = anAgentArray[i];
        if (agent.indexOf(agentStr) != -1) {
            return agentStr;
            }
        }
    return "Incompatible";
}

var TheAgent = reportAgent();

function isMSIE() {return (TheAgent == 'msie')};
function isFirefox() {return (TheAgent == 'firefox')};

function initializeZeeLog() {
    TheZeeLog = window.open("","log","width=640,height=480,resizable,scrollbars");
    TheZeeLog.document.open("text/plain");
}

function log_(aString) {
   TheZeeLog.document.write(aString);
}

function logLine_(aString) {
    log_(aString + '\n');
}

var _userAgent = navigator.userAgent.toLowerCase();

/**
 *  Toolbar Button Stuff
`*
 *  A mouse-down changes to drag handlers for
 *      onmouseout
 *      onmouseover
 *      onmouseup
 *
 *  Buttons are in three visible states:
 *      on
 *      over
 *      down
 *
 *  Starting with the mouse up,
 *      over a button makes sends it to "over".
 *      down over any button has changes it to "down" and installs the drag handlers
 */

// Rollover images

// These should probably be spread across subdirectories, and generated so that they contain
// no more entries than needed
var rollover= new Array()
rollover['firstButton'] = new Image(); rollover['firstButton'].src = "/images/firstButton.gif";
rollover['firstButton_down'] = new Image(); rollover['firstButton_down'].src = "/images/firstButton_down.gif";
rollover['firstButton_over'] = new Image(); rollover['firstButton_over'].src = "/images/firstButton_over.gif";

rollover['previousButton'] = new Image(); rollover['previousButton'].src = "/images/previousButton.gif";
rollover['previousButton_down'] = new Image(); rollover['previousButton_down'].src = "/images/previousButton_down.gif";
rollover['previousButton_over'] = new Image(); rollover['previousButton_over'].src = "/images/previousButton_over.gif";

rollover['nextButton'] = new Image(); rollover['nextButton'].src = "/images/nextButton.gif";
rollover['nextButton_down'] = new Image(); rollover['nextButton_down'].src = "/images/nextButton_down.gif";
rollover['nextButton_over'] = new Image(); rollover['nextButton_over'].src = "/images/nextButton_over.gif";

rollover['lastButton'] = new Image(); rollover['lastButton'].src = "/images/lastButton.gif";
rollover['lastButton_down'] = new Image(); rollover['lastButton_down'].src = "/images/lastButton_down.gif";
rollover['lastButton_over'] = new Image(); rollover['lastButton_over'].src = "/images/lastButton_over.gif";


var TheTrackingButtonId = null;
var TheToolbarButtons = ['firstButton', 'previousButton', 'nextButton', 'lastButton'];

function isValidButtonId_(aButtonId) {
    var answer = aButtonId.slice(-6) == 'Button';
    return answer;
}

function isTracking() {return TheTrackingButtonId != null}

/**
 *  Invoked from onmousedown on a toolbar button.
 *
 *  Remember the id where the button is clicked and
 *  change the button image for the new button.
 */
function startFrobbingButton_event_(aToolbarButton, anEvent) {
    TheTrackingButtonId = aToolbarButton.id;
    var anOldImageSrc = document.getElementById(TheTrackingButtonId + 'Image').src;
    document.getElementById(TheTrackingButtonId + 'Image').src = rollover[aToolbarButton.id + "_down"].src;

    if (isFirefox())
        {// DOM Level 2 Event model
        document.addEventListener("mouseover", overHandler, true);
        document.addEventListener("mouseout", outHandler, true);
        document.addEventListener("mouseup", upHandler, true);
        anEvent.stopPropagation()
        anEvent.preventDefault()
        }
    else if (isMSIE())
        { // IE 5+ event model
        aToolbarButton.attachEvent("onmouseover", overHandler);
        aToolbarButton.attachEvent("onmouseout", outHandler);
        aToolbarButton.attachEvent("onmouseup", upHandler);
        aToolbarButton.setCapture();
        anEvent.cancelBubble = true;
        anEvent.returnValue = false;
        }

    /**
     *  Handle mouseover events while the user is changing the button.
     *
     *  If the buttonId is the same as the tracking Button Id, then turn the button image to DOWN.
     */
    function overHandler(e) {
        var aTarget;
        var anEvent
        if (isMSIE())
            { // Handle IE event.
            anEvent = window.event;
            aTarget = anEvent.srcElement;
            }
        else
            {
            anEvent = e;
            aTarget = anEvent.target;
            }

        var aButtonId = aTarget.parentNode.id;
        if (TheTrackingButtonId == aButtonId)
            {
            document.getElementById(TheTrackingButtonId + 'Image').src = rollover[TheTrackingButtonId + "_down"].src;
            }
        if (isFirefox())
            {
            anEvent.stopPropagation()  //Don't let anyone else see it.
            }
        else (isMSIE())
            {
            anEvent.cancelBubble = true;
            }
    }

    /**
     *  Handle mouseout events while the user is changing the button.
     *
     *  If the buttonId is different from the TheTrackingButtonId, do nothing.
     *  If the TheTrackingButtonId and currentButtonId are different, then turn OFF the button image.
     *  If they are the same, then turn ON the button image.
     */
    function outHandler(e) {
        var aTarget;
        var anEvent;
        if (isMSIE())
            { // Handle IE event.
            anEvent = window.event;
            aTarget = anEvent.srcElement;
            }
        else
            {
            anEvent = e;
            aTarget = anEvent.target;
            }

        var aButtonId = aTarget.parentNode.id;
        if (TheTrackingButtonId == aButtonId)
            { // See if the button image is on or off
            if (currentToolbarButtonId != TheTrackingButtonId)
                { // turn it off
                document.getElementById(TheTrackingButtonId + 'Image').src = rollover[aToolbarButton.id].src;
                }
            else
                { // turn it on
                document.getElementById(TheTrackingButtonId + 'Image').src = rollover[aToolbarButton.id + "_on"].src;
                }
            }
    }

    /**
     *  Handle a mouseup event while the user is changing the button.
     *
     *  If the old and new ids are the same, no change happens.
     *  If the old and new ids are different, change iff the up event happens
     *  in the new button.
     */
    function upHandler(e) {
        var aTarget;
        if (isMSIE())
            { // Handle IE event.
            anEvent = window.event;
            aTarget = anEvent.srcElement;
            }
        else
            {
            anEvent = e;
            aTarget = anEvent.target;
            }

        //Now unregister my handlers
        if (isFirefox())
            { // DOM Event Model
            document.removeEventListener("mouseover", overHandler, true);
            document.removeEventListener("mouseout", outHandler, true);
            document.removeEventListener("mouseup", upHandler, true);
            }
        else if (isMSIE())
            { // IE 5+ event model
            aToolbarButton.detachEvent("onmouseover", overHandler);
            aToolbarButton.detachEvent("onmouseout", outHandler);
            aToolbarButton.detachEvent("onmouseup", upHandler);
            aToolbarButton.releaseCapture();
            }
        TheTrackingButtonId = null;
    }
}

/**
 *  Invoked as a method on a toolbar button link.
 *
 *  1. Turn off the current button,
 *  2. Set the currentButtonId
 *  3. Change the image of the receiver.
 */
function buttonDown_(e) {
    if (isTracking()) return;

    var aTarget;
    var anEvent;
    if (isMSIE())
        { // Handle IE event. Frob the ANCHOR, not the image.
        anEvent = window.event;
        aTarget = anEvent.srcElement.parentNode;
        }
    else
        {
        anEvent = e;
        aTarget = anEvent.target.parentNode;
        }
    startFrobbingButton_event_(aTarget, anEvent);
}

/**
 *  Invoked as a method on a toolbar button link.
 */
function buttonOut_(e) {
    if (isTracking()) return;

    var aTarget;
    var anEvent;
    if (isMSIE())
        { // Handle IE event.
        anEvent = window.event;
        aTarget = anEvent.srcElement;
        }
    else
        {
        aTarget = e.target;
        }
    var aButtonId = aTarget.parentNode.id;
    if (isValidButtonId_(aButtonId)) {
        //logLine_('buttonOut_:: aButtonId: ' + aButtonId);
        document.getElementById(aButtonId + 'Image').src = rollover[aButtonId].src;
        }
}

/**
 *  Invoked as a method on a toolbar button link.
 */
function buttonOver_(e) {
    if (isTracking()) return;

    var anEvent = null;
    var aTarget = null;
    if (isMSIE())
        { // Handle IE event.
        anEvent = window.event;
        aTarget = anEvent.srcElement;
        }
    else
        {
        anEvent = e;
        aTarget = anEvent.target;
        }
    var aButtonId = aTarget.parentNode.id;
    if (isValidButtonId_(aButtonId)) {
        //logLine_('buttonOver_:: aButtonId: ' + aButtonId);
        document.getElementById(aButtonId + 'Image').src = rollover[aButtonId + "_over"].src;
        }
}

/**
 * Use the level2 event model to install the toolbar button event handlers.
 *
 * Call this from the onLoadHandler!
 */
function initializeToolbarButtons() {
    var anId;
    var anArray = ['firstButton', 'previousButton', 'nextButton', 'lastButton']
    var i;
    var anElement;

    for (i in TheToolbarButtons) {
      /* Quick exit if not on portfolio page */
      anId = TheToolbarButtons[i];
      anElement = document.getElementById(anId);
      if (!anElement) {return};
      }

    if (document.addEventListener)
        {// DOM Level 2 Event model
        for (i in TheToolbarButtons) {
            anId = TheToolbarButtons[i];
            anElement = document.getElementById(anId);
            if (!anElement) {alert("No element for: "+anId)};
            anElement.addEventListener("mousedown", buttonDown_, true);
            anElement.addEventListener("mouseover", buttonOver_, true);
            anElement.addEventListener("mouseout", buttonOut_, true);
            }
        }
    else if (document.attachEvent)
        { // IE 5+ event model
        for (i in TheToolbarButtons) {
            anId = TheToolbarButtons[i];
            anElement = document.getElementById(anId);
            anElement.attachEvent("onmousedown", buttonDown_);
            anElement.attachEvent("onmouseover", buttonOver_);
            anElement.attachEvent("onmouseout", buttonOut_);
            }
        }
}

/**
 * Portfolio images
 */
var ThePortfolioImageArray= new Array()
ThePortfolioImageArray[0] = new Image(); ThePortfolioImageArray[0].src = "/images/photos/portfolio_01.jpg";
ThePortfolioImageArray[1] = new Image(); ThePortfolioImageArray[1].src = "/images/photos/portfolio_02.jpg";
ThePortfolioImageArray[2] = new Image(); ThePortfolioImageArray[2].src = "/images/photos/portfolio_03.jpg";
ThePortfolioImageArray[3] = new Image(); ThePortfolioImageArray[3].src = "/images/photos/portfolio_04.jpg";
ThePortfolioImageArray[4] = new Image(); ThePortfolioImageArray[4].src = "/images/photos/portfolio_05.jpg";
ThePortfolioImageArray[5] = new Image(); ThePortfolioImageArray[5].src = "/images/photos/portfolio_06.jpg";
ThePortfolioImageArray[6] = new Image(); ThePortfolioImageArray[6].src = "/images/photos/portfolio_07.jpg";
ThePortfolioImageArray[7] = new Image(); ThePortfolioImageArray[7].src = "/images/photos/portfolio_08.jpg";
ThePortfolioImageArray[8] = new Image(); ThePortfolioImageArray[8].src = "/images/photos/portfolio_09.jpg";
ThePortfolioImageArray[9] = new Image(); ThePortfolioImageArray[9].src = "/images/photos/portfolio_10.jpg";
ThePortfolioImageArray[10] = new Image(); ThePortfolioImageArray[10].src = "/images/photos/portfolio_11.jpg";
ThePortfolioImageArray[11] = new Image(); ThePortfolioImageArray[11].src = "/images/photos/portfolio_12.jpg";
ThePortfolioImageArray[12] = new Image(); ThePortfolioImageArray[12].src = "/images/photos/portfolio_13.jpg";
ThePortfolioImageArray[13] = new Image(); ThePortfolioImageArray[13].src = "/images/photos/portfolio_14.jpg";
ThePortfolioImageArray[14] = new Image(); ThePortfolioImageArray[14].src = "/images/photos/portfolio_15.jpg";
ThePortfolioImageArray[15] = new Image(); ThePortfolioImageArray[15].src = "/images/photos/portfolio_16.jpg";
ThePortfolioImageArray[16] = new Image(); ThePortfolioImageArray[16].src = "/images/photos/portfolio_17.jpg";

/**
 * Start with index of 0.
 */
var ThePortfolioImageIndex = 0;

/**
 * Load the portfolio image corresponding to the current
 * value of ThePortfolioImageIndex.
 *
 */
function loadPortfolioImage() {
    document.getElementById('portfolioImage').src = ThePortfolioImageArray[ThePortfolioImageIndex].src;
}

/**
 *  The user has pressed the "first" button.
 */
function _first() {
    ThePortfolioImageIndex = 0;
    loadPortfolioImage();
}

/**
 * The user has pressed the "previous" button.
 */
function _previous() {
    if (ThePortfolioImageIndex > 0) {
        ThePortfolioImageIndex -= 1;
        }
    loadPortfolioImage();
}

/**
 * The user has pressed the "next" button.
 */
function _next() {
    if (ThePortfolioImageIndex < (ThePortfolioImageArray.length-1)) {
        ThePortfolioImageIndex += 1;
        }
    loadPortfolioImage();
}

/**
 * The user has pressed the "last" button.
 */
function _last() {
    ThePortfolioImageIndex = ThePortfolioImageArray.length-1;
    loadPortfolioImage();
}
