﻿
function dropdown_menu_hack(el){
    if(el.runtimeStyle.behavior.toLowerCase()=="none"){return;}
    el.runtimeStyle.behavior="none";
    
    var ie5 = false;
    //if (!document.namespaces)   //|| document.namespaces==null
    //    ie5 = true; 
        
    el.ondblclick = function(e){
        window.event.returnValue=false;
        return false;
    }

    function showMenu(){
        function selectMenu(obj){
            var o = document.createElement("option");
            o.value = obj.getAttribute('value');
            o.innerHTML = obj.innerHTML;
            while(el.options.length>0){el.options[0].removeNode(true);}
            el.appendChild(o);
            el.title = o.innerHTML.replace('&nbsp;',' ');
            el.contentIndex = obj.selectedIndex ;
            el.menu.hide();
        }
        el.menu.show(0 , el.offsetHeight , 10, 10, el);
        var mb = el.menu.document.body;
        
        //FIX VALUES
        var strippedContent = el.contentHTML;
        for (i=0;i<150;i++){
            strippedContent = strippedContent.replace('value=' + i + '--','value=');
        }
        
        mb.style.cssText ="border:solid 1px black;margin:0;padding:0;overflow-y:auto;overflow-x:auto;background:white;text-aligbn:center;font-family:Tahoma;font-size:11px;";
        var t = strippedContent;
        t = t.replace(/<select/gi,'<ul');
        t = t.replace(/<option/gi,'<li');
        t = t.replace(/<\/option/gi,'</li');
        t = t.replace(/<\/select/gi,'</ul');
        mb.innerHTML = t;
        
        el.select = mb.all.tags("ul")[0];
        el.select.style.cssText="list-style:none;margin:0;padding:0;";
        mb.options = el.select.getElementsByTagName("li");

        for(var i=0;i<mb.options.length;i++){
            mb.options[i].selectedIndex = i;
            mb.options[i].style.cssText = "list-style:none;margin:0;padding:1px 2px;width/**/:100%;cursor:hand;cursor:pointer;white-space:nowrap;"
            mb.options[i].title =mb.options[i].innerHTML;
            mb.options[i].innerHTML ="<nobr>" + mb.options[i].innerHTML + "</nobr>";
            mb.options[i].onmouseover = function(){
                if( mb.options.selected ){mb.options.selected.style.background="white";mb.options.selected.style.color="black";}
                mb.options.selected = this;
                this.style.background="#333366";this.style.color="white";
            }

            mb.options[i].onmouseout = function(){this.style.background="white";this.style.color="black";}
            mb.options[i].onmousedown = function(){selectMenu(this); }
            mb.options[i].onkeydown = function(){selectMenu(this); }


            if(i == el.contentIndex){
                mb.options[i].style.background="#333366";
                mb.options[i].style.color="white";
                mb.options.selected = mb.options[i];
            }
        }

        var mw = Math.max( ( el.select.offsetWidth + 22 ), el.offsetWidth + 22 );
        mw = Math.max( mw, ( mb.scrollWidth+22) );
        var mh = mb.options.length * 15 + 8 ;

        var mx = (ie5)?-3:0;
        var my = el.offsetHeight -2;
        var docH = document.documentElement.offsetHeight ;
        var bottomH = docH - el.getBoundingClientRect().bottom ;
        
        mh = Math.min(mh, Math.max(( docH - el.getBoundingClientRect().top - 50),100) );

        if(( bottomH < mh) ){
            mh = Math.max( (bottomH - 12),10);
            if(mh <100 ){
                my = -100 ;
            }
            mh = Math.max(mh,100);
        }

        self.focus();

        //DEFINE HEIGHT HERE
        mh = 300;

        el.menu.show( mx , my , mw, mh , el);
        sync=null;
        if(mb.options.selected){
            mb.scrollTop = mb.options.selected.offsetTop;
        }

        window.onresize = function(){el.menu.hide()};
    }

    function switchMenu(){
        if(event.keyCode){
            if(event.keyCode==40){ el.contentIndex++ ;}
            else if(event.keyCode==38){ el.contentIndex--; }
        }
        else if(event.wheelDelta ){
            if (event.wheelDelta >= 120)
                el.contentIndex++ ;
            else if (event.wheelDelta <= -120)
                el.contentIndex-- ;
        }
        else{return true;}

        if( el.contentIndex > (el.contentOptions.length-1) ){ el.contentIndex =0;}
        else if (el.contentIndex<0){el.contentIndex = el.contentOptions.length-1;}

        var o = document.createElement("option");
        o.value = el.contentOptions[el.contentIndex].value;
        
        o.innerHTML = el.contentOptions[el.contentIndex].text;
        while(el.options.length>0){el.options[0].removeNode(true);}
        el.appendChild(o);
        el.title = o.innerHTML;
    }

    if(dropdown_menu_hack.menu ==null){
        dropdown_menu_hack.menu = window.createPopup();
        document.attachEvent("onkeydown",dropdown_menu_hack.menu.hide);
    }
    el.menu = dropdown_menu_hack.menu ;
    el.contentOptions = new Array();
    el.contentIndex = el.selectedIndex;
    el.contentHTML = el.outerHTML;

    for(var i=0;i<el.options.length;i++){
        el.contentOptions [el.contentOptions.length] = {"value": el.options[i].value,"text": el.options[i].innerHTML}
        if(!el.options[i].selected){el.options[i].removeNode(true);i--;};
    }


    el.onkeydown = switchMenu;
    el.onclick = showMenu;
    el.onmousewheel= switchMenu;
}