var globalCl = 0;

var all = [];

function Sel(x, type){	
    var div = x.parentNode;
    var list = div.getElementsByTagName("li");
    var input = div.getElementsByTagName("input")[0];
    var hidden = div.getElementsByTagName("input")[1];
    var inDiv = div.getElementsByTagName("ul")[0];
    var subList = inDiv.getElementsByTagName("ul");
    var checks = inDiv.getElementsByTagName("input");
	
	input.value = "";
	hidden.value = "";
    
    var id = inDiv.id;
    
    var num;
    
    var actLi;
	
	this.changeReady = false;			//событие getValue срабатывает раньше чем нужно
    
    this.select = function(n){
        num = n;
        //input.value='';                 
        var wi = (div.scrollWidth == 0) ? 222 : div.scrollWidth;
        input.style.width = (wi - 23) + 'px';
        inDiv.style.width = (div.scrollWidth == 0) ? (wi - 4) + 'px' : (wi - 2) + 'px';
        x.onmouseover = function(){
            x.style.backgroundPosition = "-17px 0"
        }
        x.onmouseout = function(){
            x.style.backgroundPosition = "0 0"
        }
        x.onmousedown = function(){
            x.style.backgroundPosition = "-34px 0"
        }
        x.onmouseup = function(){
            x.style.backgroundPosition = "-17px 0"
        }
        x.onclick = this.showlist;
        input.onclick = this.showlist;
        
        
        div.onmouseover = function(){
            all[num].closeOther();
            globalCl = 1;
        }
        
        div.onmouseout = function(){
            if (globalCl == 1) {
                globalCl = 0;
                setTimeout("all[" + n + "].hidelist(globalCl)", 100)
            }
        }
        //div.onmouseout = function(){closer = 0;hidelist(inDiv,closer);};
        for (var i = 0; i < list.length; i++) {
            list[i].onmouseover = this.flash;
            list[i].onmouseout = this.flash;
            list[i].onclick = this.getValue;
        }
        for (var i = 0; i < checks.length; i++) {
            checks[i].onclick = function(){
                this.checked = (this.checked == false) ? true : false
            }
        }
        all[num].updateValue();
    }
    this.flash = function(){
        if (navigator.appName.search(/Microsoft/) == -1) {
            switch (this.className) {
                case "zz":
                    this.className = "";
                    break;
                case "":
                    this.className = "act";
                    break;
                case "selected":
                    this.className = "selected act";
                    break;
                case "selected act":
                    this.className = "selected";
                    break;
                case "act":
                    this.className = "";
                    break;
            }
        }
        else {
            switch (this.style.backgroundColor) {
                case "#99b2cb":
                    this.style.backgroundColor = "";
                    this.style.color = "#500700";
                    break;
                case "":
                    this.style.backgroundColor = "#dfe8f6";
                    this.style.color = "#500700";
                    break;
                case "transparent":
                    this.style.backgroundColor = "#dfe8f6";
                    this.style.color = "#500700";
                    break;
                case "#99b3cc":
                    this.style.backgroundColor = "#99b3cb";
                    this.style.color = "#fff";
                    break;
                case "#99b3cb":
                    this.style.backgroundColor = "#99b3cc";
                    this.style.color = "#fff";
                    break;
                case "#dfe8f6":
                    this.style.backgroundColor = "transparent";
                    break;
            }
        }
    }
    this.getValue = function(){
        if (type != 'check') {
            actLi = this;
            input.value = this.innerHTML;
            if (hidden) 
                hidden.value = this.getAttribute("value");
            all[num].hidelist(0)
            input.focus();
        }
        else {
            var check = this.getElementsByTagName("input")[0];
            
            if (check.checked == true) {
                check.checked = false;
            }
            else {
                check.checked = true;
            }
            
            input.value = '';
            hidden.value = '';
            
            all[num].updateValue();
            if (check.checked != true) {
                if (navigator.appName.search(/Microsoft/) == -1) {
                    check.parentNode.className = 'zz';
                }
                else {
                    check.parentNode.style.backgroundColor = "#99b2cb";
                }
            }
        }
    }
    this.updateValue = function(){
        for (var i = 0; i < checks.length; i++) {
            if (checks[i].checked == true) {
                if (checks[i].parentNode.lastChild.nodeValue != '') {
                    input.value += (input.value != '') ? ", " + checks[i].parentNode.lastChild.nodeValue : checks[i].parentNode.lastChild.nodeValue;
                    hidden.value += (hidden.value != '') ? "," + checks[i].value : checks[i].value;
                    //hidden.value = hidden.value+checks[i].value+",";							
                }
                if (navigator.appName.search(/Microsoft/) == -1) {
                    if (checks[i].parentNode.className == 'act') 
                        checks[i].parentNode.className = "selected act"
                    else 
                        checks[i].parentNode.className = "selected"
                }
                else { //IE die
                    if (checks[i].parentNode.style.backgroundColor == '#dfe8f6') {
                        checks[i].parentNode.style.backgroundColor = "#99b3cb";
                        checks[i].parentNode.style.color = "#fff";
                    }
                    else {
                        checks[i].parentNode.style.backgroundColor = "#99b3cc";
                        checks[i].parentNode.style.color = "#fff";
                    }
                }
            }
            else {
                checks[i].parentNode.className = ''
            }
        }
		if(this.changeReady)					
        	new GetGoodDepend(null,null,"options");
    }
    this.showlist = function(){
    
        /*	if(all.length>1){ 
         for(var i=0; i<all.length; i++){
         all[i].hidelist(0);
         }
         }   */
        all[num].closeOther();
        
        if (inDiv.style.display != "block") {
            if (type != 'check') {
                for (var i = 0; i < list.length; i++) {
                    list[i].className = '';
                }
                if (typeof(actLi) != "undefined") {
                    actLi.className = "act";
                }
            }
            inDiv.style.display = "block";
            inDiv.style.opacity = 1;
            input.blur();
            div.style.zIndex = 100;
            inDiv.style.zIndex = 1000;
            
            //	document.getElementById("st").style.zIndex = 0;
            //	document.getElementById("sb").style.zIndex = 0;
            
            if (subList.length > 0) {
                for (var i = 0; i < subList.length; i++) {
                    subList[i].style.display = "block";
                }
            }
            
        }
        else {
            all[num].hidelist(0)
        }
    }
    this.hidelist = function(cl){
        if (cl == 0) {
            inDiv.style.display = "none";
            inDiv.style.zIndex = 0;
            div.style.zIndex = 0;
            /*	if(inDiv.style.display == "none"){
             document.getElementById("st").style.zIndex = 2000;
             document.getElementById("sb").style.zIndex = 2000;
             } */
            //	fade(inDiv)      //effect
        }
    }
    this.closeOther = function(){
        if (all.length > 1) {
            for (var i = 0; i < all.length; i++) {
                if (all[i] != this) 
                    all[i].hidelist(0);
            }
        }
    }
	this.init = function(){
		this.changeReady = true;
	}
}

function initCombo(x, type){
    switch (type) {
        case 1:
            type = "check"
            break;
        default:
            type = "";
    }
    var select = new Sel(x, type)
    all[all.length] = select;
    select.select(all.length - 1);
}


