﻿function MyDropdown(dropdownContainerID,jsons,vlaueInputID,textInputID,menuwidth)
{
   
    var _this=this;
    if(!menuwidth)menuwidth="100px";
    
    this.hiddenValueInput=null;
    if(vlaueInputID!=null)this.hiddenValueInput=document.getElementById(vlaueInputID);
    this.hiddenTextInput=null;
    if(textInputID!=null)this.hiddenTextInput=document.getElementById(textInputID);
    
    this.jsonsString=jsons;
    this.autoSetDefault=true;
    this.autoHide=false;
    this.menuwidth=menuwidth; 
    this.menubgcolor='lightyellow';
    this.disappeardelay=250 ;
    this.hidemenu_onclick="yes" ;
    this.dropMaxHeight="300px";
    this.selectedValue=null;
    this.selectedText=null;
    this.dropWay="JOSN";
    this.iframeURL=null;
    
    if(jsons!=null && jsons.indexOf(":")==-1 && jsons.indexOf("[")==-1)
        this.dropWay="IFRAME";
   
    this.myDropDownContainer=document.getElementById(dropdownContainerID);
    this.myDropDownContainer.onclick=function(event)
    {
        _this.dropdownmenu(event)
    };
    
    if(typeof document.myDroupdowns=="undefined")
    {
        document.myDroupdowns=[];
    }
    document.myDroupdowns[document.myDroupdowns.length]=this;
    
    if (this.hidemenu_onclick=="yes")
    {
        
        document.onclick=function()
        {
            _this.hidemenuAll();
        };        
    }    
    
    var elSel=document.createElement("DIV");
    elSel.style.styleFloat=elSel.style.styleFloat!='undefined'?'left':'right';        //兼容IE
    elSel.style.cssFloat=elSel.style.cssFloat!='undefined'?'left':'right';             //兼容FF
    elSel.style.textAlign="left";
    elSel.style.overflow="hidden";
    elSel.style.width=(parseInt(this.myDropDownContainer.style.width)-20)+"px";//"80px;";
    elSel.style.whiteSpace="nowrap"; 
    elSel.style.padding="0 0 0 4px";
    this.selectedContainer=elSel;   
    
    this.myDropDownContainer.appendChild(elSel);   
    
    var elIco=document.createElement("DIV");
    elIco.style.styleFloat=elIco.style.styleFloat!='undefined'?'right':'left';        //兼容IE
    elIco.style.cssFloat=elIco.style.cssFloat!='undefined'?'right':'left';             //兼容FF

    elIco.style.verticalAlign="middle";
    elIco.className="dropmenuICO";
    elIco.innerHTML="&nbsp;"; 
    this.myDropDownContainer.appendChild(elIco);    
   
    this.ie4=document.all;
    this.ns6=document.getElementById&&!document.all;
    
    if (this.ie4||this.ns6)
    {
        var elDrp=document.createElement("DIV");
        elDrp.id="dropmenudiv";
        elDrp.style.visibility="hidden";
        elDrp.style.width=this.menuwidth;
        elDrp.style.overflow="auto";
        elDrp.style.overflowX="hidden";
        elDrp.style.backgroundColor=this.menubgcolor;
        elDrp.onMouseover=function()
        {
            _this.clearhidemenu();
        };
        if(this.autoHide)
        {
            elDrp.onmouseout=function(event)
            {
                _this.dynamichide(event);
            };
        }
        this.dropmenuobj=elDrp;
        this.myDropDownContainer.appendChild(elDrp);
        
    }
    
    this.dropmenuobj.style.display="none";
     window.onload=function()
    {
        
        _this.reDraw();
    }
     if(jsons!=null && this.dropWay=="JOSN")
     {
        this.populatemenu(this.jsonsString);
     }
    
    
    if(this.hiddenTextInput.value!="")
    {
        this.selectedContainer.innerHTML=this.hiddenTextInput.value;
        this.selectedText=this.hiddenTextInput.value;
    }
    if(this.hiddenValueInput.value!="")
    {      
       this.selectedValue=this.hiddenValueInput.value;
    }
}

MyDropdown.prototype.dropdownIframe=function(url)
{
    this.dropmenuobj.style.visibility="hidden";
    this.dropmenuobj.style.display="";
    if(this.iframeURL==url)return;
    this.iframeURL=url;

    if(url.indexOf("popSrc")==-1)
    {
        if(url.indexOf("?")>-1)
            url+="&";
        else
            url+="?";     
        url+="popSrc="+escape(document.location);
    }
    var ifrm='<iframe src="'+url+'" allowTransparency="true" scrolling="auto" frameborder="0" style="width:98%; height:'+this.dropMaxHeight+';"></iframe>';
    this.dropmenuobj.style.overflow="";
    this.dropmenuobj.style.textAlign="right";
    this.dropmenuobj.style.height=this.dropMaxHeight;
    this.dropmenuobj.innerHTML=ifrm;    
};

MyDropdown.prototype.Clear=function()
{
    this.dropmenuobj.innerHTML="";
    this.jsonsString=null;
    this.hiddenValueInput.value="";
    this.hiddenTextInput.value="";
    this.selectedValue=null;
    this.selectedText=null;
};

MyDropdown.prototype.setInfo=function(msg)
{
    this.selectedContainer.innerHTML=msg;
};

MyDropdown.prototype.getCurrentDirectory=function(){
    var locHref = location.href;
    var locArray = locHref.split("/");
    delete locArray[locArray.length-1];
    var dirTxt = locArray.join("/");
    return dirTxt;
};
MyDropdown.prototype.getposOffset=function(what, offsettype)
{
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
    }
    return totaloffset;
};
MyDropdown.prototype.showhide=function(obj, e, visible, hidden, menuwidth)
{
    if(!e)e=window.event;
    if (this.ie4||this.ns6)
        this.dropmenuobj.style.left=this.dropmenuobj.style.top="-500px"
    if (this.menuwidth!=""){
        this.dropmenuobj.widthobj=this.dropmenuobj.style
        this.dropmenuobj.widthobj.width=this.menuwidth
    }
    
    if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
    obj.visibility=visible
    else if (e.type=="click")
    obj.visibility=hidden;
};


MyDropdown.prototype.iecompattest=function()
{
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
};

MyDropdown.prototype.clearbrowseredge=function(obj, whichedge)
{    
    var edgeoffset=0
    if (whichedge=="rightedge"){
    var windowedge=this.ie4 && !window.opera? this.iecompattest().scrollLeft+this.iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
    this.dropmenuobj.contentmeasure=this.dropmenuobj.offsetWidth
    if (windowedge-this.dropmenuobj.x < this.dropmenuobj.contentmeasure)
    edgeoffset=this.dropmenuobj.contentmeasure-obj.offsetWidth
    }
    else{
    var topedge=this.ie4 && !window.opera? this.iecompattest().scrollTop : window.pageYOffset
    var windowedge=this.ie4 && !window.opera? this.iecompattest().scrollTop+this.iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
    this.dropmenuobj.contentmeasure=this.dropmenuobj.offsetHeight
    if (windowedge-this.dropmenuobj.y < this.dropmenuobj.contentmeasure){ //move up?
    edgeoffset=this.dropmenuobj.contentmeasure+obj.offsetHeight
    if ((this.dropmenuobj.y-topedge)<this.dropmenuobj.contentmeasure) //up no good either?
    edgeoffset=this.dropmenuobj.y+obj.offsetHeight-topedge
    }
    }
   
    return edgeoffset
};

MyDropdown.prototype.populatemenu=function(what)
{
    if (this.ie4||this.ns6)
    {
        this.hidemenuAll(true);
        
        if(!what)return;
        if(this.dropmenuobj.childNodes.length>0)return;
        
        if(this.jsonsString==null || this.jsonsString=="")
        {
            this.jsonsString=what;
        }
        
        if(this.dropmenuobj.childNodes.length==0)
        {
            var _this=this;
            var options = JSON.parse(what);
            this.dropmenuobj.innerHTML="";
            this.dropmenuobj.style.height="";            
            var reDrawed=false;
            var firstItem=null;
            for(var i=0;i<options.Items.length;i++)
            {
                var item=options.Items[i];
                var objItem=document.createElement("DIV");
                objItem.innerHTML=item.Text;
                objItem.setAttribute("Text",item.Text);
                objItem.setAttribute("Value",item.Value);
                objItem.style.overflow="hidden";
                objItem.className="drpDownItemNormal";
                objItem.onclick=function()
                {
                    _this.onDropdownChanged(this);
                };
                objItem.onmouseover=function()
                {
                    this.className="drpDownItemOver";
                };
                objItem.onmouseout=function()
                {
                    this.className="drpDownItemNormal";
                };
                if(this.hiddenValueInput!=null && this.hiddenValueInput.value==item.Value)
                {
                    this.onDropdownChanged(objItem);
                }
                
                this.dropmenuobj.appendChild(objItem);
                if(i==0)firstItem=objItem;
                if(!reDrawed)
                    reDrawed=this.reDrawHeight();
                    
            }
            
            
            if(this.selectedValue==null && this.autoSetDefault && firstItem!=null)
            {
              this.onDropdownChanged(firstItem);
            }
                       
        }
    }
};

MyDropdown.prototype.reDraw=function()
{
    for(var i=0;i<document.myDroupdowns.length;i++)
    {
        var curInstance=document.myDroupdowns[i];
        curInstance.reDrawHeight();
    }
    
};
MyDropdown.prototype.reDrawHeight=function()
{
    var redraw=false;
    if(this.dropmenuobj.offsetHeight>document.body.offsetHeight)
    {
        var curHeight=document.body.offsetHeight-100;
        if(curHeight<=100)
            curHeight=document.body.offsetHeight;       
        this.dropmenuobj.style.height=curHeight+"px";
        redraw=true;        
    }
    
    if(this.dropmenuobj.offsetHeight>parseInt(this.dropMaxHeight)){    
        this.dropmenuobj.style.height=this.dropMaxHeight; 
    }
    return redraw;
};

MyDropdown.prototype.dropdownmenu=function(e)
{ 
    if(this.jsonsString==null || this.jsonsString=="")return;    
    
    //this.myDropDownContainer.className="myDropdownDivOver";
    this.dropmenuobj.style.display="";
    
    var menucontents=this.jsonsString;
    var obj=this.myDropDownContainer;
    if (window.event) event.cancelBubble=true
    else if (e.stopPropagation) e.stopPropagation()   
       
    this.clearhidemenu() ;   
    //this.dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
    
    if(this.dropWay=="JOSN")
    {
        this.populatemenu(menucontents)
        this.dropmenuobj.style.height="";
        this.reDrawHeight();
    }
    else 
    {      
        this.hidemenuAll(true);
        this.dropdownIframe(menucontents);
    }

    if (this.ie4||this.ns6){
        this.showhide(this.dropmenuobj.style, e, "visible", "hidden", this.menuwidth)
               
    
    
    this.dropmenuobj.x=this.getposOffset(obj, "left")    
    this.dropmenuobj.y=this.getposOffset(obj, "top")
    this.dropmenuobj.style.left=this.dropmenuobj.x-this.clearbrowseredge(obj, "rightedge")+"px"
    this.dropmenuobj.style.top=this.dropmenuobj.y-this.clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
    }

   
    return this.clickreturnvalue()
}

MyDropdown.prototype.clickreturnvalue=function(){
if (this.ie4||this.ns6) return false
else return true
}

MyDropdown.prototype.contains_ns6=function(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

MyDropdown.prototype.dynamichide=function(e){

if(!e)e=window.event;
if (this.ie4&&!this.myDropDownContainer.contains(e.toElement))
this.delayhidemenu()
else if (this.ns6&&this.myDropDownContainer!= e.relatedTarget&& !this.contains_ns6(this.myDropDownContainer, e.relatedTarget))
this.delayhidemenu()
}

MyDropdown.prototype.hidemenu=function(e){
    if (typeof this.dropmenuobj!="undefined"){
        if(this.myDropDownContainer.className!="myDropdownDiv")
            this.myDropDownContainer.className="myDropdownDiv";
        if(this.dropmenuobj.style.visibility!="hidden" &&
            this.dropmenuobj.style.display!="none")
        {
            if (this.ie4||this.ns6)
            {
                if(this.dropWay=="JOSN")
                    this.dropmenuobj.style.visibility="hidden";
                else
                    this.dropmenuobj.style.display="none";            
            }
        }
    }
}

MyDropdown.prototype.hidemenuAll=function(noSelf)
{
    for(var i=0;i<document.myDroupdowns.length;i++)
    {
        if(!noSelf)
            document.myDroupdowns[i].hidemenu();
        else if(this!=document.myDroupdowns[i])
            document.myDroupdowns[i].hidemenu();
            
    }
    
}

MyDropdown.prototype.delayhidemenu=function(){
    if (this.ie4||this.ns6)
    {
    var _this=this;
    this.delayhide=setTimeout(function(){_this.hidemenu();},this.disappeardelay);
    }
}

MyDropdown.prototype.clearhidemenu=function(){
if (typeof this.delayhide!="undefined")
clearTimeout(this.delayhide)
}

MyDropdown.prototype.SetValue=function(text,value)
{
    this.hiddenValueInput.value=value;
    this.hiddenTextInput.value=text;
    this.selectedValue=value;
    this.selectedText=text;
    this.setInfo(text);
}

MyDropdown.prototype.onDropdownChanged=function(objItem)
{
    var text=objItem.getAttribute("Text");
    if(!text && objItem.Text)value=objItem.Text;   
    var value=objItem.getAttribute("Value");
    if(!value && objItem.Value)value=objItem.Value; 
        
    this.SetValue(text,value);
    
    
    try{
        onMyDropDownChanged(this,text,value);
    }catch(e){}
}

