// nezavisla trida selectboxu

function inputSelectItem(iValue, iText, iParent) {
    var self = this;
    this.container = null;
    this.parent = iParent;
    this.disabled = false;

    this.value = iValue;

    if (iText.substr(0, 1) == "!") {
        this.text = iText.substr(1, iText.length - 1);
        this.disabled = true;
    } else this.text = iText;

    this.over = function(scroll) {
        if (!this.disabled) {
            if (this.parent.lastItem != null) {
                switch (this.parent.skin) {
                    default :
                    case 0 :
                        //this.parent.lastItem.container.style.backgroundColor = "";
                        this.parent.lastItem.container.style.textDecoration = "none";
                        break;

                    case 1 :
                        this.parent.lastItem.container.style.fontWeight = "normal";
                        break;
                    
                    case 2 :
                        this.parent.lastItem.container.style.backgroundColor = "";
                        break;
                }
            }

            switch (this.parent.skin) {
                default :
                case 0 :
                    //this.container.style.backgroundColor = "#F7F7F7";
                    this.container.style.textDecoration = "underline";
                    break;

                case 1 :
                    this.container.style.fontWeight = "bold";
                    break;

                case 2 :
                    this.container.style.backgroundColor = "#D2D2D2";
                    break;
            }
            this.parent.lastItem = this;
            this.parent.setLastIndex(this, scroll);
        }
    }

    this.select = function() {
        if (!this.disabled) {
            this.parent.selectItem( this );
            this.parent.close();
        }
    }

    this.mouseDown = function() {
        if (!this.disabled) {
            switch (this.parent.skin) {
                default :
                case 0 :
                    //this.container.style.backgroundColor = "white";
                    break;
    
                case 1 :
                    break;
            }
            
            this.select();
        }
        return false;
    }

    this.mouseUp = function() {
        if (!this.disabled) {
            switch (this.parent.skin) {
                default :
                case 0 :
                    //this.container.style.backgroundColor = "";
                    break;

                case 1 :
                    break;
            }
        }
    }

    this.incLeafNum = function(iNum) {
        var iText = this.container.innerHTML;
        var pos = iText.length - 2;
        var numeric = 0;

        while (pos >= 0 && (iText.substr(pos, 1) != " " || iText.substr(pos + 1, 1) != "(")) {
            pos += -1;
        }

        if (pos != -1) {
            var leaf = iText.substr(pos, iText.length - pos);
            var numeric = isNumericBracket(leaf);

            if (numeric != 0)
                this.container.innerHTML = iText.substr(0, pos);
        }

        if (numeric + iNum != 0)
            this.container.innerHTML += " (<strong>" + (numeric + iNum) + "</strong>)";
    }

    this.construct = function() {
        this.container = document.createElement("DIV");

        //if (this.parent.dataContainer.style.width != "")
        //    this.container.style.width = (parseInt(this.parent.dataContainer.style.width) - 20) + "px";
        this.container.style.cursor = "pointer";
        
        this.container.innerHTML = "";

        switch (this.parent.skin) {
            default :
            case 0 :
                this.container.className = "blueText";
                this.container.style.fontWeight = "normal";
        	    this.container.style.fontSize = "11px";
        	    break;

        	case 1 :
        	    this.container.style.color = "black";
        	    this.container.style.fontSize = "11px";
        	    break;

        	case 2 :
        	    this.container.style.color = "black";
        	    this.container.style.fontSize = "12px";
        	    this.container.style.textDecoration = "none";
        	    this.container.style.padding = "0px 0px 0px 2px";
                this.container.style.border = "1px solid white";
                if (!this.disabled)
                    this.container.innerHTML += "<img src=\"" + pathPrefix + "images/photo/triangle.gif\" align=\"absmiddle\" alt=\"\" border=\"0\"/>&nbsp;";
        	    break;
	    }

        this.container.innerHTML += this.text;

        this.container.onmouseover = function() { self.over(false); }
        this.container.onmousedown = function() { return self.mouseDown(); }
        this.container.onmouseup = function() { self.mouseUp(); }

        this.parent.dataContainer.appendChild( this.container );
    }

    this.construct();
}

var inputSelectBuff = new Array();

function inputSelect_closeAll() {
    var f;
    for (f = 0; f < inputSelectBuff.length; f++)
        inputSelectBuff[f].close();
}

function inputSelect_close(iName) {
    closeTimeout = setTimeout("inputSelect_closeForce(\"" + iName + "\")", 1000);
}

function inputSelect_closeForce(iName) {
    var founded = false;
    var pos = 0;
    
    while (!founded && pos < inputSelectBuff.length)
        if (inputSelectBuff[pos].name == iName) founded = true;
        else pos++;
        
    if (founded)
        inputSelectBuff[pos].close();
}

function inputSelect(iName, iObj, iRedirTo) {
    var self = this;
    
    this.name = iName;
    this.redirTo = iRedirTo;
    inputSelectBuff[ inputSelectBuff.length ] = this;

    this.container = iObj;
    this.inputElement = null;
    this.leftPart = null;
    this.middlePart = null;
    this.rightPart = null;
    this.dataContainer = null;

    this.options = new Array();
    this.lastItem = null;
    this.lastIndex = 0;
    this.selectedIndex = 0;
    this.skin = 0;
    this.opened = false;
    
    this.onchange = null;

    this.insertItem = function(value, text) {
        this.options[ this.options.length ] = new inputSelectItem(value, text, this);
        
        if (this.options.length == 1)
            this.selectItemIndex(0);
    }

    this.getLastItem = function() {
        return this.options[ this.options.length - 1 ];
    }

    this.setLastIndex = function(iRef, scroll) {
        var founded = false;
        var pos = 0;

        while (pos < this.options.length && !founded)
            if (this.options[pos] == iRef && !this.options[pos].disabled) founded = true;
            else pos++;

        if (founded) {
            this.lastIndex = pos;
            if (scroll) {
                var putVal = this.lastIndex * 19 - 100;
                // prasarnicka pro ix - musi byt 2x
                this.dataContainer.scrollTop = putVal;
                this.dataContainer.scrollTop = putVal;
            }
        }
    }

    this.selectItemIndex = function(iIndex) {
        if (iIndex >= 0 && iIndex < this.options.length) {
            this.selectedIndex = iIndex;
            this.inputElement.value = this.options[ this.selectedIndex ].value;
            this.middlePart.innerHTML = this.options[ this.selectedIndex ].text;
        }
    }

    this.selectItemValue = function(iValue) {
        var founded = false;
        var pos = 0;
        
        while (pos < this.options.length && !founded)
            if (this.options[pos].value == iValue && !this.options[pos].disabled) founded = true;
            else pos++;
        
        if (founded)
            this.selectItemIndex( pos );
    }

    this.selectItem = function(iRef) {
        var founded = false;
        var pos = 0;

        while (pos < this.options.length && !founded)
            if (this.options[pos] == iRef && !this.options[pos].disabled) founded = true;
            else pos++;

        if (founded && pos != this.selectedIndex) {
            this.selectItemIndex( pos );
            this.selectBatch();
        }
    }

    this.selectBatch = function() {
        if (this.redirTo != "")
            document.location.href = this.redirTo + this.name + "=" + this.options[ this.selectedIndex ].value;

        if (this.onchange != null)
            this.onchange();
    }

    this.close = function() {
        if (this.opened) {
            this.dataContainer.style.display = "none";
            this.opened = false;
        }
    }

    this.open = function() {
        if (this.opened) this.close();
        else {
            inputSelect_closeAll()

            this.dataContainer.style.display = "block";
            switch (this.skin) {
                default :
                case 0 :
                    this.dataContainer.style.left = (elementPositionX(this.container) - 8) + "px";
                    this.dataContainer.style.top = (elementPositionY(this.container) + 21) + "px";
                    break;

                case 1 :
                    this.dataContainer.style.left = (elementPositionX(this.container)) + "px";
                    this.dataContainer.style.top = (elementPositionY(this.container) + 19) + "px";
                    break;

                case 2 :
                    this.dataContainer.style.left = (elementPositionX(this.container)) + "px";
                    this.dataContainer.style.top = (elementPositionY(this.container) + 22) + "px";
                    break;
            }
            this.opened = true;

            this.options[ this.selectedIndex ].over(true);
        }

        return false;
    }
    
    this.over = function() {
        switch (this.skin) {
            default :
            case 0 :
                //this.container.style.backgroundColor = "white";
                break;
            
            case 1 :
                break;
        }
    }

    this.out = function() {
        switch (this.skin) {
            default :
            case 0 :
                //this.container.style.backgroundColor = "";
                break;

            case 1 :
                break;
        }
    }
    
    this.mouseDown = function() {
        switch (this.skin) {
            default :
            case 0 :
                //this.container.style.backgroundColor = "white";
                break;

            case 1 :
                this.middlePart.style.fontWeight = "bold";
                break;
        }
    }

    this.mouseUp = function() {
        switch (this.skin) {
            default :
            case 0 :
                //this.container.style.backgroundColor = "";
                break;

            case 1 :
                this.middlePart.style.fontWeight = "normal";
                break;
        }
    }

    this.lastDownTime  = 0;
    this.charBuffer = new Array();

    this.keyDown = function(eventRef) {
        switch (eventRef.keyCode) {
            case 38 : // sipka nahoru
                var newInd = this.lastIndex;
                while (newInd >= 0 && (this.lastIndex == newInd || this.options[newInd].disabled)) newInd--;

                if (newInd >= 0)
                    this.options[newInd].over(true);
                break;

            case 40 : // sipka dolu
                var newInd = this.lastIndex;
                while (newInd < this.options.length && (this.lastIndex == newInd || this.options[newInd].disabled)) newInd++;

                if (newInd < this.options.length)
                    this.options[newInd].over(true);
                break

            case 13 : // odbouchnuti
                if (!this.options[this.lastIndex].disabled) {
                    this.selectItemIndex(this.lastIndex);
                    this.selectBatch();
                }
                this.close();
                break;

            case 27 : // esc
                this.close();
                break;

            case 33 : // page up
            case 36 : // home
                var newInd = eventRef.keyCode == 36 ? 0 : this.lastIndex - 10;
                if (newInd < 0) newInd = 0;
                while (newInd < this.options.length && this.options[newInd].disabled) newInd++;

                if (newInd < this.options.length)
                    this.options[newInd].over(true);
                break;

            case 34 : // page down
            case 35 : // end
                var newInd = eventRef.keyCode == 35 ? this.options.length - 1 : this.lastIndex + 10;
                if (newInd >= this.options.length) newInd = this.options.length - 1;
                while (newInd >= 0 && this.options[newInd].disabled) newInd--;

                if (newInd >= 0)
                    this.options[newInd].over(true);
                break;

            default :
                //48-57
                //65-90
                //97-122
                var pressedKey = "";
                var kc = eventRef.keyCode;
                if (kc >= 48 && kc <= 57) pressedKey = (kc - 48) + "";
                else if ((kc >= 65 && kc <= 90) || (kc >= 97 && kc <= 122)) {
                    kc += (kc >= 65 && kc <= 90) ? -65 : -97;

                    var charTable = "abcdefghijklmnopqrstuvwxyz";
                    pressedKey = charTable.substr(kc, 1);
                } else {
                    switch (kc) {
                        case 32 : pressedKey = " "; break;
                        case 189 :
                        case 191 : pressedKey = "_"; break;
                        case 190 : pressedKey = "."; break;
                    }
                }

                if (pressedKey != "") {
                    var founded = false;
                    var brk = false;
                    var pos = this.lastIndex;
                    var tm = (new Date()).getTime();

                    if (tm - this.lastDownTime > 500) {
                        this.charBuffer = new Array();
                        this.charBuffer[0] = pressedKey;
                    } else this.charBuffer[this.charBuffer.length] = pressedKey;
                    this.lastDownTime = tm;

                    while ((this.lastIndex != pos || !brk) && !founded) {
                        //if (this.lastIndex != pos) {
                            var charPos = 0;
                            founded     = true;
                            while (charPos < this.charBuffer.length && founded) {
                                founded = this.options[pos].text.substr(charPos, 1).toLowerCase() == this.charBuffer[charPos];
                                charPos++;
                            }
                        //}

                        if (!founded) {
                            pos++;
                            if (pos >= this.options.length) {
                                pos = 0;
                                brk = true;
                            }
                        }
                    }
                    
                    if (founded) {
                        this.options[pos].over(true);
                    }
                }
                break;
        }
    }

    this.batchRun = function(itemStr) {
        // rozparsovani itemStr
        if (itemStr != "") {
            var f;
            var aktChar = "";
            var inStr = false;
    
            var inText = false;
            var paramValue = "";
            var paramText = "";
            for (f = 0; f < itemStr.length; f++) {
                aktChar = itemStr.substr(f, 1);
    
                if (inStr) {
                    switch (aktChar) {
                        case "\"" :
                            inStr = false;
                            break;
                            
                        default :
                            if (inText) paramText += aktChar;
                            else paramValue += aktChar;
                            break;
                    }
                } else {
                    switch (aktChar) {
                        case "\"" :
                            inStr = true;
                            break;
    
                        case "," :
                            if (inText) {
                                this.insertItem(paramValue, paramText);
    
                                paramValue = "";
                                paramText  = "";
                                inText = false;
                            } else inText = true;
                            break;
                    }
                }
            } // for
    
            if (inText) this.insertItem(paramValue, paramText);
        }
    }

    this.leftPart = document.createElement("DIV");
    this.middlePart = document.createElement("DIV");
    this.rightPart = document.createElement("DIV");
    this.inputElement = document.createElement("INPUT");
    this.dataContainer = document.createElement("DIV");

    this.construct = function(defVal, itemStr, iWidth) {
        this.container.style.cursor = "pointer";
        this.container.style.height = "13px";
        this.container.style.fontSize = "11px";
        this.container.innerHTML = "";
        this.container.onclick = function() { self.open(); }
        this.container.onmouseover = function() { self.over(false); }
        this.container.onmouseout = function() { self.out(); }
        this.container.onmousedown = function() { return self.mouseDown(); }
        this.container.onmouseup = function() { self.mouseUp(); }
        this.container.onkeydown = function(eventRef) { if (self != null) { self.keyDown(eventRef == null ? event : eventRef); } return false; }

        this.inputElement.type = "hidden";
        this.inputElement.name = this.name;

        this.dataContainer.style.display = "none";
        this.dataContainer.style.position = "absolute";
        this.dataContainer.style.overflow = "auto";
        this.dataContainer.id = this.name + "_dataContainer";
        this.dataContainer.onkeydown = this.container.onkeydown;
        this.dataContainer.onmouseout = function(eventRef) { if (closeTry(eventRef == null ? event : eventRef, self.name + "_dataContainer", closeClear)) inputSelect_close(self.name); }

        this.leftPart.style.cssFloat = "left";
        this.leftPart.style.styleFloat = "left";

        this.middlePart.style.cssFloat = "left";
        this.middlePart.style.styleFloat = "left";

        switch (this.skin) {
            default :
            case 0 :
                this.rightPart.style.cssFloat = "left";
                this.rightPart.style.styleFloat = "left";

                this.container.style.width = "auto";
                this.container.style.whiteSpace = "nowrap";

                this.leftPart.style.width = "1px";
                this.leftPart.style.height = "13px";
                this.leftPart.style.overflow = "hidden";

                //this.middlePart.style.width = (iWidth - 18) + "px";
                this.middlePart.style.width = "auto";
                this.middlePart.style.height = "13px";
                this.middlePart.style.overflow = "hidden";
                this.middlePart.className = "blueText";
                this.middlePart.whiteSpace = "wrap";
                this.middlePart.overflow = "hidden";
                this.middlePart.style.fontWeight = "normal";
                this.middlePart.innerHTML = "&nbsp;";

                this.rightPart.style.width = "17px";
                this.rightPart.style.height = "13px";
                this.rightPart.innerHTML = "<img src=\"" + pathPrefix + "images/selectBox_button.gif\" alt=\"\" border=\"0\"/>";

                this.dataContainer.style.width = iWidth + "px";
                this.dataContainer.style.backgroundColor = "white";
                this.dataContainer.className = "tmenu_container";
                break;

            case 1 :
                this.rightPart.style.cssFloat = "right";
                this.rightPart.style.styleFloat = "right";

                this.container.style.width = iWidth + "px";

                this.leftPart.style.width  = "4px";
                this.leftPart.style.hieght = "20px";
                this.leftPart.innerHTML = "<img src=\"" + pathPrefix + "images/photo/slideSel_left.gif\" alt=\"\" border=\"0\"/>";

                this.middlePart.style.width = (iWidth - 23) + "px";
                this.middlePart.style.height = "17px";
                this.middlePart.style.overflow = "hidden";
                this.middlePart.style.paddingTop = "3px";
                this.middlePart.style.color = "black";
                this.middlePart.whiteSpace = "nowrap";
                this.middlePart.innerHTML = "&nbsp;";
                this.middlePart.style.background = "url('" + pathPrefix + "images/photo/slideSel_middle.gif')";

                this.rightPart.style.width = "19px";
                this.rightPart.style.height = "20px";
                this.rightPart.innerHTML = "<img src=\"" + pathPrefix + "images/photo/slideSel_right.gif\" alt=\"\" border=\"0\"/>";

                this.dataContainer.style.width = iWidth + "px";
                this.dataContainer.style.backgroundColor = "#F7F7F7";
                this.dataContainer.style.border = "1px solid #B6B6B6";
                this.dataContainer.style.color = "black";
                this.dataContainer.style.padding = "5px";
                break;

            case 2 :
                this.rightPart.style.cssFloat = "right";
                this.rightPart.style.styleFloat = "right";

                this.container.style.width = iWidth + "px";
                this.container.style.border = "1px solid #7F9DB9";
                this.container.style.height = "21px";

                this.leftPart.style.width  = "8px";
                this.leftPart.style.hieght = "21px";
                this.leftPart.style.fontSize = "1px";
                this.leftPart.innerHTML = "&nbsp;";

                this.middlePart.style.width = (iWidth - 25) + "px";
                this.middlePart.style.height = "19px";
                this.middlePart.style.overflow = "hidden";
                this.middlePart.style.paddingTop = "3px";
                this.middlePart.style.color = "black";
                this.middlePart.style.fontSize = "12px";
                this.middlePart.whiteSpace = "nowrap";
                this.middlePart.innerHTML = "&nbsp;";

                this.rightPart.style.width = "17px";
                this.rightPart.style.height = "21px";
                this.rightPart.innerHTML = "<img src=\"" + pathPrefix + "images/photo/defaultSel_right.gif\" alt=\"\" border=\"0\"/>";

                this.dataContainer.style.width = (iWidth + 50) + "px";
                this.dataContainer.style.backgroundColor = "white";
                this.dataContainer.style.border = "1px solid #7F9DB9";
                this.dataContainer.style.color = "black";
                this.dataContainer.style.padding = "5px";
                break;
        }


        this.container.appendChild( this.leftPart );
        this.container.appendChild( this.middlePart );
        this.container.appendChild( this.rightPart );
        this.container.appendChild( this.inputElement );

        document.getElementById("topContainer").appendChild( this.dataContainer );
        
        this.batchRun(itemStr);

        pHeight = this.options.length * 19;
        if (pHeight > 200) pHeight = 200;
        this.dataContainer.style.height = pHeight + "px";

        this.selectItemValue(defVal);
    }
}

