//
// Methods for dealing with out buttons.  Everything in here but Buttons.isSafeClick() would be
// redundant if it wasn't for IE being such rubbish.  Proper browsers handle it all in CSS.
//
TextButtons = new function() {
    //-----------------------------------------------------------------
    // Some exciting methods to try and stop people double/triple clicking
    //-----------------------------------------------------------------
    this.isSafeClick = function(buttonName, milliseconds) {
        if (!TextButtons.clicks)
            TextButtons.clicks = {};

        // Get the time the button was last clicked (or min date if not clicked)
        var lastClick = (TextButtons.clicks[buttonName]) ? TextButtons.clicks[buttonName] : new Date(0);

        // Update with this click
        TextButtons.clicks[buttonName] = new Date();

        // Return true if the specified time has passed
        return (TextButtons.clicks[buttonName].getTime() - lastClick.getTime() > milliseconds);
    }

    //
    // Initialise all the link buttons in the window
    //
    this.initialise = function(e) {
        setButtonEvents(document.body);
    }

    //-----------------------------------------------------------------
    // Make our buttons work properly in IE... add events to each relevant div
    //-----------------------------------------------------------------
    function setButtonEvents(obj) {
        // We only have to do this for IE... other browsers support the :hover pseudo-class properly.
        if (!TextButtons.linkButtons)
            TextButtons.linkButtons = [];

        var divs = obj.getElementsByTagName("div");

        for (var i = 0; i < divs.length; i++) {
            if (divs[i].className == "lb") {
                // Save it for later
                TextButtons.linkButtons.push(divs[i]);

                EventManager.addEvent(divs[i], 'mouseover', TextButtons.mouseover);
                EventManager.addEvent(divs[i], 'mouseout', TextButtons.mouseout);
            }
        }
    }

    //
    // Change the colour of your button
    //
    this.mouseover = function(e) {
        this.className = "over";
    }

    //
    // Change the button back, yo!
    //
    this.mouseout = function(e) {
        this.className = "out";
    }

    //-----------------------------------------------------------------
    // Disable the mouseovers on link buttons for stupid IE6.  If we don't
    // do this, IE6's status bar will go to complete as soon as you mouse
    // over another button.
    //-----------------------------------------------------------------
    this.removeButtonEvents = function(e) {
        if (document.all && TextButtons.linkButtons) {
            for (var i = 0; i < TextButtons.linkButtons.length; i++) {
                EventManager.removeEvent(TextButtons.linkButtons[i], 'mouseover', TextButtons.mouseover);
                EventManager.removeEvent(TextButtons.linkButtons[i], 'mouseout', TextButtons.mouseout);
            }
        }
    }

    // Add some events for IE
    // This has to be at the bottom (after all the other declarations) for IE's sake also
    if (document.all) {
        EventManager.addEvent(window, 'load', this.initialise);

        // Only do this crazy disabling business for IE6, which sucks like a vampire.
        if (navigator.appVersion.indexOf('MSIE 6.') > -1)
            EventManager.addEvent(window, 'beforeunload', this.removeButtonEvents);
    }
}