//******************************************************************************************
// Event Handlers
//******************************************************************************************
function addEvent(obj, type, fn)
{
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)
		obj.attachEvent("on" + type, fn);
}

function removeEvent(obj, type, fn)
{
    if (obj.removeEventListener)
        obj.removeEventListener(type, fn, false);
    else if (obj.detachEvent)
        obj.detachEvent("on" + type, fn);
}

//******************************************************************************************
// ImageButton MouseOver Effects = Correction of PNG Images
//******************************************************************************************
var arrButtonMouseOvers = new Array();
var arrButtonMouseOversPNG = new Array();
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

function setUpMouseOvers(e)
{
    var arrInputs = document.getElementsByTagName('input');
    
    for(var x = 0; x < arrInputs.length; x++)
    {
        var img = arrInputs[x];

        if (img.type != 'undefined')
            if (img.type == 'image')
            {
                if ((version >= 5.5) && (version < 7) && (img.src.toLowerCase().indexOf('.png') > -1))
                    new buttonMouseOverPNG(img);
                else
                    new buttonMouseOver(img);
            }
    }
}

function buttonMouseOver(obj) 
{
    // If the objects already in the array, leave it
    for (var i = 0; i < arrButtonMouseOvers.length; i++) 
        if (obj == arrButtonMouseOvers[i].obj) 
            return;

    // Assign image object to this object and create image objects
    this.obj = obj;
    this.off = new Image();
    this.off.src = obj.src;
    this.on = new Image();

    // Use regex to create the on image SRC and assign to this object
    if (this.off.src.toLowerCase().indexOf('_off') > -1) 
    {
        var onSrc = this.off.src.replace(/_[Oo][Ff]{2}/, "_On");
        this.on.src = onSrc;
    }
    else
        return;

    // Assign this object to a variable so we can assign it in functions below
    var thisObject  = this;

    // Create event handlers to change the image on mouse over
    var fnMouseOver = function() { obj.src = thisObject.on.src;  };
    var fnMouseOut = function() { obj.src = thisObject.off.src;  };
    
    addEvent(obj, "mouseover", fnMouseOver);
    addEvent(obj, "mouseout", fnMouseOut);

    // Put this object into the rollovers array
    arrButtonMouseOvers[arrButtonMouseOvers.length] = this;
}

function buttonMouseOverPNG(img)
{
    if (img.src.toLowerCase().indexOf('_off') > -1) 
    {   
        var imgSrc = img.src;
        var imageName = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
        img.src = imgSrc.replace(imageName, "spacer.gif");
        
        img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +  imgSrc + "', sizingMethod='image')"
        
        var offImg = new Image();
        offImg.style.filter = img.style.filter;
        
        var onImg = new Image();
        onImg.style.filter = img.style.filter.replace(/_[Oo][Ff]{2}/, "_On");

        // Assign image object to this object and create image objects
        this.obj = img;
        this.off = offImg;
        this.on = onImg;

        // Assign this object to a variable so we can assign it in functions below
        var thisObject  = this;

        // Create event handlers to change the image on mouse over
        var fnMouseOver = function() { img.style.filter = thisObject.on.style.filter;  };
        var fnMouseOut = function() { img.style.filter = thisObject.off.style.filter;  };
        
        addEvent(img, "mouseover", fnMouseOver);
        addEvent(img, "mouseout", fnMouseOut);

        // Put this object into the rollovers array
        arrButtonMouseOversPNG[arrButtonMouseOversPNG.length] = this;
    }
}

function correctPNGImages()
{
    if ((version >= 5.5) && (version < 7))
    {
        for (var x = 0; x < document.images.length; x++ )
        {
            var img = document.images[x];
            
            if (img.src.indexOf('.png') > -1)
                if (img.src.toLowerCase().indexOf('_off') == -1)
                { 
                    var imgID = (img.id) ? "id='" + img.id + "' " : ""
                    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                    var imgStyle = "display:inline-block;" + img.style.cssText 
                    if (img.align == "left") imgStyle = "float:left;" + imgStyle
                    if (img.align == "right") imgStyle = "float:right;" + imgStyle
                    if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                    var strNewHTML = "<span " + imgID + imgClass + imgTitle
                    + " style=\"" + "width:" + img.offsetWidth + "px; height:" + img.offsetHeight + "px;" + imgStyle + ";"
                    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                    + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>" 
                    img.outerHTML = strNewHTML
                }
        }
    }
}

//******************************************************************************************
// Set up MouseOver effects for all buttons on the page + Fix PNG images
//******************************************************************************************
addEvent(window, "load", setUpMouseOvers);
addEvent(window, "load", correctPNGImages);