﻿var ListBoxes=[];
function ListBox_Create(ElementId, ListBoxText, DefaultText, Width, Items, OnSelectedChanged){
    ListBox_Remove(ElementId);
    var ListBoxHTML="", ItemsHTML="", SelValueTextBoxHTML="";
    SelValueTextBoxHTML='<input type="text" id="'+ElementId+'_SelValue" value="0" style="display: none" />';
    ListBoxHTML+='<table id="ListBox'+ListBoxes.length+'" onclick="IsObjectClick=true; ListBox_Click('+ListBoxes.length+');" onmouseover="ListBox_Over('+ListBoxes.length+');" onmouseout="ListBox_Out('+ListBoxes.length+');" onmousedown="ListBox_Down('+ListBoxes.length+');" onmouseup="ListBox_Over('+ListBoxes.length+');" cellpadding="0" cellspacing="1" border="0" style="background-color: #abadb3; cursor: default; width: '+Width+'px"><tr><td style="background-color: #ffffff">'
    var ListBoxTextPaddingTop=1; if (isFirefox){ ListBoxTextPaddingTop=0; }
    var ShowImgSrc="/img/ListBox/show.png", ListBoxTextColor="";
    if (Items.length<=1){
        ShowImgSrc="/img/ListBox/disabled.png";
        ListBoxTextColor="; color: #888888";
    }
    ListBoxHTML+='<table cellpadding="0" cellspacing="0" border="0" style="width: '+(Width-2)+'px; height: 18px"><tr><td id="ListBox_DefaultTd"><div id="ListBox'+ListBoxes.length+'_Text" style="width: '+(Width-24)+'px; height: 14px; overflow: hidden; padding: '+ListBoxTextPaddingTop+'px 0px 0px 5px'+ListBoxTextColor+'">'+ListBoxText+'</div></td><td style="width: 17px"><img id="ListBox_ShowImg'+ListBoxes.length+'" src="'+ShowImgSrc+'" style="width: 17px; height: 18px" /></td></tr></table></td></tr></table>';
    gebi(ElementId).innerHTML=ListBoxHTML+SelValueTextBoxHTML;
    
    var ItemsRightPadding=3, ListBoxWidthImgWidth=Width-2;
    if (Items.length>15 && !isFirefox){ ItemsRightPadding=20; }
    if (Items.length>15 && isFirefox){ ListBoxWidthImgWidth=Width-19; }
    ItemsHTML+='<table id="ListBoxItems'+ListBoxes.length+'" cellpadding="0" cellspacing="1" border="0" style="background-color: #abadb3; position: absolute; z-index: 100; cursor: default; display: none"><tr><td style="background-color: #ffffff">';
    if (Items.length>15){ ItemsHTML+='<div style="height: 240px; overflow-y: scroll; overflow-x: hidden;">'; }
    if (DefaultText!=""){ ItemsHTML+='<div style="padding: 0px 1px 0px 1px">'+DefaultText+'</div>'; }
    ItemsHTML+='<table cellpadding="0" cellspacing="0" border="0">';
    if (Items.length>0){
        for(var n in Items){
            var ItemLeftPadding=Items[n][2]*10+10;
            var ItemColor="#000000";
            if (Items[n][2]>0){ ItemColor="#555555"; }
            ItemsHTML+='<tr><td id="ListBox'+ListBoxes.length+'_ItemTd'+n+'" onclick="ListBoxItem_Click('+ListBoxes.length+','+n+');" onmouseover="ListBoxItem_Over('+ListBoxes.length+','+n+');" onmouseout="ListBoxItem_Out('+ListBoxes.length+','+n+');" style="background-color: #ffffff; color: '+ItemColor+'"><div id="ListBox'+ListBoxes.length+'_ItemTd'+n+'Div" style="padding: 0px '+ItemsRightPadding+'px 0px '+ItemLeftPadding+'px">'+ReplaceAll(ReplaceAll(Items[n][0],"'","\'")," ","&nbsp;")+'</div><div style="font-size: 1px"><img src="/img/empty.gif" style="width: '+ListBoxWidthImgWidth+'px; height: 1px" /></div></td></tr>'
        }
    }else{
        ItemsHTML+='<tr><td style="background-color: #ffffff; color: #000000"><div style="text-align: center">список пуст</div><div style="font-size: 1px"><img src="/img/empty.gif" style="width: '+ListBoxWidthImgWidth+'px; height: 1px" /></div></td></tr>'
    }
    ItemsHTML+='</table>';
    if (Items.length>15){ ItemsHTML+='</div>'; }
    ItemsHTML+='</td></tr></table>';
    gebi("ListBoxesItemsDiv").innerHTML+=ItemsHTML;
    var SelItemId=-1;
    if (Items.length>0){ SelItemId=0; }
    ListBoxes[ListBoxes.length]=new Array(ElementId, ListBoxText, DefaultText, Width, Items, SelItemId, OnSelectedChanged);
}
function ListBox_Remove(ElementId){
    try{
        for (var n in ListBoxes){
            if (ListBoxes[n]!=null){
                if (ListBoxes[n][0]==ElementId){
                    try{
                        gebi(ElementId).innerHTML="";
                        gebi("ListBoxesItemsDiv").removeChild(gebi("ListBoxItems"+n));
                    }catch(ex){}
                    ListBoxes[n]=null;
                    return;
                }
            }
        }
    }catch(ex){}
}

function ListBox_Over(id){
    if (ListBoxes[id][4].length>1){ gebi("ListBox_ShowImg"+id).src="/img/ListBox/show_over.png"; }
}
function ListBox_Out(id){
    if (ListBoxes[id][4].length>1){ gebi("ListBox_ShowImg"+id).src="/img/ListBox/show.png"; }
}
function ListBox_Down(id){
    if (ListBoxes[id][4].length>1){ gebi("ListBox_ShowImg"+id).src="/img/ListBox/show_down.png"; }
}
function ListBox_Click(id){
    if (ListBoxes[id][4].length<=1){ return; }
    if (gebi("ListBoxItems"+id).style.display=="none"){
        ListBox_HideAllItems();
        var w=document.body.clientWidth, h=document.body.clientHeight;
        if (gebi("MainPageDiv").offsetHeight>h){ h=gebi("MainPageDiv").offsetHeight; }
        var AddLeft=0;
        gebi("ListBoxItems"+id).style.left=GetAbsPosition(gebi("ListBox"+id)).x+"px";
        gebi("ListBoxItems"+id).style.top=(GetAbsPosition(gebi("ListBox"+id)).y+19)+"px";
        gebi("ListBoxItems"+id).style.display="block";
        if (gebi("ListBoxItems"+id).offsetLeft+gebi("ListBoxItems"+id).offsetWidth>w){
            gebi("ListBoxItems"+id).style.left=(GetAbsPosition(gebi("ListBox"+id)).x+gebi("ListBox"+id).offsetWidth-gebi("ListBoxItems"+id).offsetWidth)+"px";
        }
        if (gebi("ListBoxItems"+id).offsetTop+gebi("ListBoxItems"+id).offsetHeight>h){
            var NewTop=GetAbsPosition(gebi("ListBox"+id)).y+gebi("ListBox"+id).offsetHeight-gebi("ListBoxItems"+id).offsetHeight-19;
            if (NewTop>=0){ gebi("ListBoxItems"+id).style.top=NewTop+"px"; }
        }
        if (ListBoxes[id][5]!=-1){
            ListBoxItem_Over(id, ListBoxes[id][5]);
        }
    }else{
        gebi("ListBoxItems"+id).style.display="none";
    }
}

function ListBoxItem_Over(ListBoxId, ItemId){
    for(var n=0; n<ListBoxes[ListBoxId][4].length; n++){
        if (n!=ItemId){
            gebi("ListBox"+ListBoxId+"_ItemTd"+n).style.backgroundColor="#ffffff";
            var ItemColor="#000000";
            if (ListBoxes[ListBoxId][4][n][2]>0){ ItemColor="#555555"; }
            gebi("ListBox"+ListBoxId+"_ItemTd"+n).style.color=ItemColor;
        }else{
            gebi("ListBox"+ListBoxId+"_ItemTd"+ItemId).style.backgroundColor="#3399ff";
            gebi("ListBox"+ListBoxId+"_ItemTd"+ItemId).style.color="#ffffff";        
        }
    }

}
function ListBoxItem_Out(ListBoxId, ItemId){
    var ItemColor="#000000";
    if (ListBoxes[ListBoxId][4][ItemId][2]>0){ ItemColor="#555555"; }
    gebi("ListBox"+ListBoxId+"_ItemTd"+ItemId).style.backgroundColor="#ffffff";
    gebi("ListBox"+ListBoxId+"_ItemTd"+ItemId).style.color=ItemColor;
}
function ListBoxItem_Click(ListBoxId, ItemId){
    gebi("ListBoxItems"+ListBoxId).style.display="none";
    gebi(ListBoxes[ListBoxId][0]+"_SelValue").value=ListBoxes[ListBoxId][4][ItemId][1];
    gebi("ListBox"+ListBoxId+"_Text").innerHTML=gebi("ListBox"+ListBoxId+"_ItemTd"+ItemId+"Div").innerHTML;
    if (ItemId!=ListBoxes[ListBoxId][5] && ListBoxes[ListBoxId][6]!=""){
        var js=document.createElement("script");
        js.setAttribute("type", "text/javascript");
        js.text=ListBoxes[ListBoxId][6];
        gebi("ScriptDiv").appendChild(js);
    }
    ListBoxes[ListBoxId][5]=ItemId;
}

function ListBox_HideAllItems(){
    for (var n=0; n<ListBoxes.length; n++){
        try{
            gebi("ListBoxItems"+n).style.display="none";
        }catch(ex){}
    }
}
