/*
    Fancy Select
    Creates a styled CSS version of a select object
*/

var activeZIndex = 100;
var fancySelectRegistry = new Array();

function fancySelect(objectName, selectID, width, selectClass){
    this.objectName = objectName;
    this.originalSelect = document.getElementById(selectID);
    this.selectClass = typeof(selectClass) != "undefined" ? selectClass : "ThinBlue";
    this.width = typeof(width) != "undefined" ? width : "";
    
    this.init = function(){
        this.originalSelect.style.display = "none";

        this.wrapperDiv = document.createElement("div");
        this.wrapperDiv.className = "fancySelect" + this.selectClass;
        
        this.selectDiv = document.createElement("div");
        
        if(this.width != ""){
            this.selectDiv.style.width = this.width + "px";
            this.wrapperDiv.style.width = this.width + "px";
        }

        this.wrapperDiv.appendChild(this.selectDiv);
        
        this.topLink = document.createElement("a");
        this.topLink.className = "current";
        this.topLink.setAttribute("href", "javascript: " + this.objectName + ".toggleSelect();");
        this.topLink.innerHTML = this.originalSelect.options[this.originalSelect.selectedIndex].text;

        this.selectDiv.appendChild(this.topLink);
        
        this.dropDiv = document.createElement("div");
        this.dropDiv.style.display = "none";
        this.selectDiv.appendChild(this.dropDiv);
        
        for(var i = 0; i < this.originalSelect.length; i++){
            var tempLink = document.createElement("a");
            tempLink.innerHTML = this.originalSelect.options[i].text;
            tempLink.setAttribute("href", "javascript: " + this.objectName + ".selectOption(" + i + ");");
            
            this.dropDiv.appendChild(tempLink);
        }

        this.originalSelect.parentNode.insertBefore(this.wrapperDiv, this.originalSelect);
        
        
        //Really stupid workaround for a really stupid browser
        setTimeout(this.objectName + ".topLink.style.backgroundImage = 'url(/images/misc/spacer.gif)';", 1);
        setTimeout(this.objectName + ".topLink.style.display = 'inline-block';", 1);
        setTimeout(this.objectName + ".topLink.style.display = 'block';", 1000);
        setTimeout(this.objectName + ".topLink.style.backgroundImage = '';", 1000);
        
        fancySelectRegistry[fancySelectRegistry.length] = this.objectName;
    };
    
    this.toggleSelect = function(){
        this.dropDiv.style.display = this.dropDiv.style.display == "none" ? "block" : "none";
        this.selectDiv.style.zIndex = this.dropDiv.style.display == "none" ? "" : activeZIndex;
        this.topLink.style.display = this.dropDiv.style.display == "none" ? "block" : "none";
    };

    this.closeSelect = function(){
        this.dropDiv.style.display = "none";
        this.selectDiv.style.zIndex = "";
        this.topLink.style.display = "block";
    };
    
    this.selectOption = function(optionIndex){
        this.closeSelect();
        this.originalSelect.selectedIndex = optionIndex;
        
        this.topLink.innerHTML = this.originalSelect.options[optionIndex].text;
        
        if(typeof(this.originalSelect.onchange) == "function"){
            this.originalSelect.onchange();
        }
    };
    
    this.init();
}

function closeFancySelects(){
    for(var i = 0; i < fancySelectRegistry.length; i++){
        eval(fancySelectRegistry[i] + ".closeSelect();");
    }
}

if(document.addEventListener){
    document.addEventListener("click", closeFancySelects, false); 
}else if(document.attachEvent){
    document.attachEvent("onclick", closeFancySelects);
}