// relies on
// yahoo-dom-event.js
// utils.js

var ImageButtonType_Button = 0;
var ImageButtonType_Toggle = 1;

var ImageType_Default  = 0;
var ImageType_Rollover = 1;
var ImageType_Pressed  = 2;
var ImageType_Disabled = 3;
var ImageType_Pulsed   = 4;

function ImageButton(el, fnOnClicked, strDefaultSrc, strRolloverSrc, strPressedSrc, strDisabledSrc, strPulsedSrc, nButtonType)
{
    if(el)
    {
        if(typeof el == "string")
        {
            el = document.getElementById(el);
        }
    }
    if(el == null)
    {
    }
    
    var m_elem = el;
	this.m_strElemId = el.id ? el.id : YAHOO.util.Dom.generateId();
	
	this.m_strState = "normal";
	
	//this.m_fnClick = fnOnClicked;
	this.m_bEnabled = true;
	this.m_bPulsed = false;
	this.m_bToggled = false;
	this.m_nButtonType = nButtonType;
    	
    // set image sources
    this.m_strDefaultSrc = strDefaultSrc;
    this.m_strRolloverSrc = strRolloverSrc;
    this.m_strPressedSrc = strPressedSrc;
    this.m_strDisabledSrc = strDisabledSrc;
    this.m_strPulsedSrc = strPulsedSrc;
   
    // set state
    if(m_elem.getAttribute("enabled"))
    {
        this.m_bEnabled = EOIR.Utils.parseBool(m_elem.getAttribute("enabled"));
    }
    this.m_strState = this.m_bEnabled ? "normal" : "disabled";
    m_elem.style.cursor = this.m_bEnabled ? "pointer" : "default";
    
    // assuming any inner HTML is for a button label
    // doesn't work for IMG-based elements
    if(m_elem.tagName != "IMG")
    {
        var strInnerHTML = "<table border=0 style='width:100%; height:100%;table-layout:fixed;' cellspacing=0 cellpadding=0><tr><td align=center valign=middle>" + m_elem.innerHTML + "</td></tr></table>";	
        m_elem.innerHTML = strInnerHTML;
    }
    
    this.m_cacheDefault = null;
	this.m_cacheRollover = null;
	this.m_cachePressed = null;
	this.m_cacheDisabled = null;
	this.m_cachePulsed = null;
	
	
    this.onClicked = new YAHOO.util.CustomEvent('clicked', this, true);
    if(fnOnClicked != null)
    {
        this.onClicked.subscribe(fnOnClicked);
    }
    
	
	this.getButtonEl = function()
	{   
	    return $(this.m_strElemId);
	}
	
	this.getImageSource = function(nImgType)
	{
	    var m_elem = $(this.m_strElemId);
        switch(nImgType)
        {
            case ImageType_Default:
                var strAttrVal = m_elem.getAttribute("defaultsrc");
                if(strAttrVal)
                {
                    this.m_strDefaultSrc = strAttrVal;
                }
                return this.m_strDefaultSrc;
            case ImageType_Rollover:
                var strAttrVal = m_elem.getAttribute("hoversrc");
                if(strAttrVal)
                {
                    this.m_strRolloverSrc = strAttrVal;
                }
                return this.m_strRolloverSrc;
            case ImageType_Pressed:
                var strAttrVal = m_elem.getAttribute("pressedsrc");
                if(strAttrVal)
                {
                    this.m_strPressedSrc = strAttrVal;
                }
                return this.m_strPressedSrc;
            case ImageType_Disabled:
                var strAttrVal = m_elem.getAttribute("disabledsrc");
                if(strAttrVal)
                {
                    this.m_strDisabledSrc = strAttrVal;
                }
                return this.m_strDisabledSrc;
            case ImageType_Pulsed:
                var strAttrVal = m_elem.getAttribute("pulsedsrc");
                if(strAttrVal == null)
                {
                    strAttrVal = m_elem.getAttribute("hoversrc");
                }
                if(strAttrVal)
                {
                    this.m_strPulsedSrc = strAttrVal;
                }
                return this.m_strPulsedSrc;
        }
        return null;
	}
	
	this.updateImageType = function(nImgType)
    {
        var strSrc = this.getImageSource(nImgType);
        switch(nImgType)
        {
            case ImageType_Default:
                if(strSrc && strSrc != "undefined")
                {
                    if(this.m_cacheDefault == null)
                    {
                        this.m_cacheDefault = new Image();
                        YAHOO.util.Event.addListener(this.m_cacheDefault, "load", this.handleCacheLoaded, this, this);
                    }
                    this.m_cacheDefault.src = strSrc;
                }
                else
                {
                    this.m_cacheDefault = null;
                }
                break;
            case ImageType_Rollover:
                if(strSrc && strSrc != "undefined")
                {
                    if(this.m_cacheRollover == null)
                    {
                        this.m_cacheRollover = new Image();
                    }
                    this.m_cacheRollover.src = strSrc;
                }
                else
                {
                    this.m_cacheRollover = null;
                }
                break;
            case ImageType_Pressed:
                if(strSrc && strSrc != "undefined")
                {
                    if(this.m_cachePressed == null)
                    {
                        this.m_cachePressed = new Image();
                    }
                    this.m_cachePressed.src = strSrc;
                }
                else
                {
                    this.m_cachePressed = null;
                }
                break;
            case ImageType_Disabled:
                if(strSrc && strSrc != "undefined")
                {
                    if(this.m_cacheDisabled == null)
                    {
                        this.m_cacheDisabled = new Image();
                    }
                    this.m_cacheDisabled.src = strSrc;
                }
                else
                {
                    this.m_cacheDisabled = null;
                }
                break;
            case ImageType_Pulsed:
                if(strSrc && strSrc != "undefined")
                {
                    if(this.m_cachePulsed == null)
                    {
                        this.m_cachePulsed = new Image();
                    }
                    this.m_cachePulsed.src = strSrc;
                }
                else
                {
                    this.m_cachePulsed = null;
                }
                break;  
        }
    }

    this.setImageSrc = function (state, src)
    {
    	var elem = $(this.m_strElemId);
    	switch(state)
    	{
			case "default":
    		case "normal":
    			elem.setAttribute("defaultsrc", src);
    			break;
    		case "hover":
    			elem.setAttribute("hoversrc", src);
    			break;
    		case "pressed":
    			elem.setAttribute("pressedsrc", src);
    			break;
    		case "disabled":
    			elem.setAttribute("disabledsrc", src);
    			break;
    	}
    }

    this.updateImageCache = function()
    {
        this.updateImageType(ImageType_Default);
        this.updateImageType(ImageType_Rollover);
        this.updateImageType(ImageType_Pressed);
        this.updateImageType(ImageType_Disabled);
        this.updateImageType(ImageType_Pulsed);
            	
	    var strCurSrc;
	    switch(this.m_strState)
	    {
		    case "normal":
			    if(this.m_cacheDefault != null)
				    strCurSrc = this.m_cacheDefault.src;
			    break;
    	
		    case "hover":
			    if(this.m_cacheRollover != null)
			    {
				    strCurSrc = this.m_cacheRollover.src;
			    }
			    break;
    			
		    case "pressed":
			    if(this.m_cachePressed != null)
			    {
				    strCurSrc = this.m_cachePressed.src;
			    }
			    break;
    			
		    case "disabled":
			    if(this.m_cacheDisabled != null)
			    {
				    strCurSrc = this.m_cacheDisabled.src;
			    }
			    break;
	    }
    	
	    if(strCurSrc == null)
	    {
		    if(this.m_cacheDefault != null)
			    strCurSrc = this.m_cacheDefault.src;
	    }
    	
	    this.updateButtonSize();
	    
	    if(strCurSrc != null && strCurSrc != "undefined")
	    {
	        this.setImage(strCurSrc);
	    }
    }
    
    this.setImage = function(image)
    {
        var m_elem = $(this.m_strElemId);
	
	    if(navigator.appName == "Microsoft Internet Explorer")
        {
	        var arVersion = navigator.appVersion.split("MSIE");
	        if(arVersion != null)
	        {
		        var version = parseFloat(arVersion[1]);
		        if ((version <7) && (version >= 5.5)) 
		        {
	                try
		            {
		                if(image.substring(image.length-3, image.length).toUpperCase() == "PNG")
		                {
		                    if(m_elem.filters["DXImageTransform.Microsoft.AlphaImageLoader"] == null)
		                    {
		                        m_elem.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader";
		                    }
			                m_elem.filters["DXImageTransform.Microsoft.AlphaImageLoader"].src =image;
			                return;
			            }
			        }
			        catch(e)
		            {
		                alert(m_elem.id + "\n" + image + "\n" + e.description);
		            }
		        }
	        }
	    }
    	
	    if(m_elem.tagName == "IMG")
	    {
	        m_elem.src = image;
	    }
	    else
	    {
	        m_elem.style.backgroundImage="url('" + image + "')";
	    }
    }
    
    this.updateButtonSize = function()
    {
        var m_elem = $(this.m_strElemId);
	   
        if(m_elem.getAttribute("btnwidth") != null)
        {
            m_elem.style.width = m_elem.getAttribute("btnwidth") + "px";
        }
        else if(this.m_cacheDefault != null && this.m_cacheDefault.width != 0)
        {
            m_elem.style.width = this.m_cacheDefault.width + "px";
        }
        
        if(m_elem.getAttribute("btnheight") != null)
        {
            m_elem.style.height = m_elem.getAttribute("btnheight") + "px";
        }
        else if(this.m_cacheDefault != null && this.m_cacheDefault.height != 0)
        {
            m_elem.style.height = this.m_cacheDefault.height + "px";
        }
    }
    
    this.highlightButton = function(bHighlight)
    {
        if(bHighlight)
        {
            if(this.m_cachePulsed != null)
	            this.setImage(this.m_cachePulsed.src);
        }
        else
        {
            if(this.m_cacheDefault != null)
	            this.setImage(this.m_cacheDefault.src);
        }
    }

    this.pulseButton = function()
    {
        var bPulsed = this.m_bPulsed;
	    if(bPulsed)
        {
            this.highlightButton(false);
        }
        else
        {
            this.highlightButton(true);
        }
        this.m_bPulsed = !bPulsed;
    }
    
    this.setEnabled = function(bEnable)
	{
	    this.m_bEnabled = bEnable;
	    
	    if(bEnable)
	    {
	        if(this.m_nButtonType == ImageButtonType_Toggle && this.m_bToggled)
	        {
                this.m_strState = "pressed";
                $(this.m_strElemId).style.cursor = "pointer";	
	            
	            if(this.m_cachePressed != null)
	            {
		            this.setImage(this.m_cachePressed.src);
	            }
	        }
	        else
	        {
	            this.m_strState = "normal";
	            $(this.m_strElemId).style.cursor = "pointer";	
	            
	            if(this.m_cacheDefault != null)
		        {
		            this.setImage(this.m_cacheDefault.src);
		        }
	        }
	    }
	    else
	    {
	        this.m_strState = "disabled";
	        $(this.m_strElemId).style.cursor = "default";	
		   
		    if(this.m_cacheDisabled != null)
		    {
			    this.setImage(this.m_cacheDisabled.src);
		    }
	    }
    }

    this.getEnabled = function()
	{
		return this.m_bEnabled;
	}
	
	this.setActive = function(bActive)
	{
	    if(bActive != this.m_bEnabled)
	    {
	        this.m_bEnabled = bActive;
	        if(bActive)
	        {
                $(this.m_strElemId).style.cursor = "pointer";
	        }
	        else
	        {
	            $(this.m_strElemId).style.cursor = "default";
	        }
	    
	    }
	}
	
    this.getActive = function()
	{
		return this.m_bEnabled;
	}
	
	this.handleImageButtonMouseOver = function()
	{
	    if(this.m_bEnabled)
	    {
	        this.m_strState = "hover";
	       
		    if(this.m_cacheRollover != null)
		    {
			    this.setImage(this.m_cacheRollover.src);
		    }
	    }
	}
	
	this.handleImageButtonMouseOut = function()
	{
	    if(this.m_bEnabled)
	    {
	        this.m_strState = "normal";
	       
		    if(this.m_cacheDefault != null)
		    {
			    this.setImage(this.m_cacheDefault.src);
		    }
	    }
	}
	
	this.handleImageButtonMouseDown = function()
	{
	    if(this.m_bEnabled)
	    {
	        this.m_strState = "pressed";
	       
		    if(this.m_cachePressed != null)
		    {
			    this.setImage(this.m_cachePressed.src);
		    }
	    }
	}
	
	this.handleImageButtonMouseUp = function()
	{
	    if(this.m_bEnabled)
	    {
	        this.m_strState = "hover";
	       
		    if(this.m_cacheRollover != null)
		    {
			    this.setImage(this.m_cacheRollover.src);
		    }
		    else if(this.m_cacheDefault != null)
		    {
			    this.setImage(this.m_cacheDefault.src);
		    }
	    }
	}
	
	this.handleImageButtonMouseMove = function()
	{
	    if(this.m_bEnabled && this.m_strState != "hover" && this.m_strState != "pressed")
	    {
	        this.handleImageButtonMouseOver();
	    }	
	}
	
	this.handleImageButtonClick = function()
	{
	    if(this.m_bEnabled)
	    {
	        //if(this.m_fnClick != null)
	        //{
	        //    this.m_fnClick(this); // pass the button object
	        //}
	        this.onClicked.fire(this);
	    }
	}
	
	this.setToggled = function(bToggle)
    {
        if(this.m_nButtonType == ImageButtonType_Toggle)
        {
            if(this.m_bToggled != bToggle)
            {
                this.m_bToggled = bToggle;
		        if(!bToggle)
		        {
		            this.m_strState = "normal";
	       
		            if(this.m_cacheDefault != null)
		            {
			            this.setImage(this.m_cacheDefault.src);
		            }
		        }
		        else
		        {
		            this.m_strState = "pressed";
	       
		            if(this.m_cachePressed != null)
		            {
			            this.setImage(this.m_cachePressed.src);
		            }
		        }
            }
        }
    }
    
    this.getToggled = function()
    {
        if(this.m_nButtonType == ImageButtonType_Toggle)
        {
            return this.m_bToggled;
        }
        return false;
    }
	
	this.handleImageToggleButtonMouseOver = function()
	{
	    if(this.m_bEnabled && !this.m_bToggled)
	    {
	        this.m_strState = "hover";
	       
		    if(this.m_cacheRollover != null)
		    {
			    this.setImage(this.m_cacheRollover.src);
		    }
	    }
	}
	
	this.handleImageToggleButtonMouseOut = function()
	{
	    if(this.m_bEnabled && !this.m_bToggled)
	    {
	        this.m_strState = "normal";
	       
		    if(this.m_cacheDefault != null)
		    {
			    this.setImage(this.m_cacheDefault.src);
		    }
	    }
	}
	this.handleImageToggleButtonMouseDown = function()
	{
	    if(this.m_bEnabled)
	    {
	        this.m_strState = "pressed";
	       
		    if(this.m_cachePressed != null)
		    {
			    this.setImage(this.m_cachePressed.src);
		    }
	    }
    }
    
	this.handleImageToggleButtonMouseUp = function()
	{
	    if(this.m_bEnabled && !this.m_bToggled)
	    {
	        this.m_strState = "hover";
	       
		    if(this.m_cacheRollover != null)
		    {
			    this.setImage(this.m_cacheRollover.src);
		    }
		    else if(this.m_cacheDefault != null)
		    {
		        this.setImage(this.m_cacheDefault.src);
		    }	
	    }
    }
    
	this.handleImageToggleButtonMouseMove = function()
	{
		if(this.m_bEnabled && this.m_strState != "hover" && this.m_strState != "pressed")
	    {
	        this.handleImageToggleButtonMouseOver();
	    }
	}
	
	this.handleImageToggleButtonClick = function()
	{
	    if(this.m_bEnabled)
	    {
	        var bToggled = this.m_bToggled;
            this.setToggled(!bToggled);
            
            if(bToggled)
            {
                this.m_strState = "hover";
	       
		        if(this.m_cacheRollover != null)
		        {
			        this.setImage(this.m_cacheRollover.src);
		        }
            }
            
            //if(this.m_fnClick != null)
	        //{
	        //    this.m_fnClick(this); // pass the button object
	        //}
	        this.onClicked.fire(this);
	    }
	}
	
	this.addListener = function(strEventName, fn, objParam)
	{
	    YAHOO.util.Event.addListener(this.m_strElemId, strEventName, fn, objParam);
	}
	
	this.removeListener = function(strEventName, fn)
	{
	    return YAHOO.util.Event.removeListener(this.m_strElemId, strEventName, fn);
	}
	
	this.handleCacheLoaded = function()
	{
	    try
	    {
	        this.updateButtonSize();
	    }
	    catch(e)
	    {
	    }
	}
	
	if(nButtonType == ImageButtonType_Button)
    {
        YAHOO.util.Event.addListener(m_elem, "mouseover", this.handleImageButtonMouseOver, this, this);
        YAHOO.util.Event.addListener(m_elem, "mouseout", this.handleImageButtonMouseOut, this, this);
        YAHOO.util.Event.addListener(m_elem, "mousedown", this.handleImageButtonMouseDown, this, this);
        YAHOO.util.Event.addListener(m_elem, "mouseup", this.handleImageButtonMouseUp, this, this);
        YAHOO.util.Event.addListener(m_elem, "mousemove", this.handleImageButtonMouseMove, this, this);
        YAHOO.util.Event.addListener(m_elem, "click", this.handleImageButtonClick, this, this);
    }
    else
    {
        YAHOO.util.Event.addListener(m_elem, "mouseover", this.handleImageToggleButtonMouseOver, this, this);
        YAHOO.util.Event.addListener(m_elem, "mouseout", this.handleImageToggleButtonMouseOut, this, this);
        YAHOO.util.Event.addListener(m_elem, "mousedown", this.handleImageToggleButtonMouseDown, this, this);
        YAHOO.util.Event.addListener(m_elem, "mouseup", this.handleImageToggleButtonMouseUp, this, this);
        YAHOO.util.Event.addListener(m_elem, "mousemove", this.handleImageToggleButtonMouseMove, this, this);
        YAHOO.util.Event.addListener(m_elem, "click", this.handleImageToggleButtonClick, this, this);
    }
	
    //this.onLoad = new YAHOO.util.CustomEvent('load', this, true, YAHOO.util.CustomEvent.LIST, true);
    this.updateImageCache();
    
}

ImageButton.createImageButtonFromElement = function(el, fnOnClicked, strDefaultSrc, strRolloverSrc, strPressedSrc, strDisabledSrc, strPulsedSrc)
{
    var button = new ImageButton(el, fnOnClicked, strDefaultSrc, strRolloverSrc, strPressedSrc, strDisabledSrc, strPulsedSrc, ImageButtonType_Button);
    ImageButton.register(button.m_strElemId, button);
    return button;
}

ImageButton.createImageToggleButtonFromElement = function(el, fnOnClicked, strDefaultSrc, strRolloverSrc, strPressedSrc, strDisabledSrc, strPulsedSrc)
{
    var button = new ImageButton(el, fnOnClicked, strDefaultSrc, strRolloverSrc, strPressedSrc, strDisabledSrc, strPulsedSrc, ImageButtonType_Toggle);
    ImageButton.register(button.m_strElemId, button);
    return button;
}

ImageButton.ht = [];

ImageButton.register = function(strId, button)
{
	ImageButton.ht[strId] = button;
}

ImageButton.get = function(strName)
{
	return ImageButton.ht[strName];
}

ImageButton.getButtons = function()
{
    var arr = [];
    for(var strKey in ImageButton.ht)
    {
        arr.push(ImageButton.ht[strKey]);
    }
	return arr;
}

ImageButton.updateImageType = function(strSrc, imgCache)
{
    var img;
    if(imgCache != null && imgCache.src != strSrc)
    {
	    img = imgCache;
    }
    else
    {
	    img = new Image();
    }
	
    img.src = strSrc;
		
    return img;
}


