/*
 * DO NOT REMOVE THIS NOTICE
 *
 * PROJECT:   mygosuMenu
 * VERSION:   1.0.8
 * COPYRIGHT: (c) 2003,2004 Cezary Tomczak
 * LINK:      http://gosu.pl/dhtml/mygosumenu.html
 * LICENSE:   BSD (revised)
 */

function DropDownMenu1_2(id) {

    /* Type of the menu: "horizontal" or "vertical" */
    this.type = "horizontal";

    /* Delay (in miliseconds >= 0): show-hide menu */
    this.delay = {
        "show": 0,
        "hide": 300
    }
    /* Change the default position of sub-menu by Y pixels from top and X pixels from left
     * Negative values are allowed */
    this.position = {
        "top": 0,
        "left": 0
    }
    /* Z-index property for .section_2 */
    this.zIndex = {
        "visible": 1,
        "hidden": -1
    };

    // Browser detection
    this.browser = {
        "ie": Boolean(document.body.currentStyle),
        "ie5": (navigator.appVersion.indexOf("MSIE 5.5") != -1 || navigator.appVersion.indexOf("MSIE 5.0") != -1)
    };
    if (!this.browser.ie) { this.browser.ie5 = false; }

    /* Initialize the menu */
    this.init = function() {
        if (!document.getElementById(this.id)) { return alert("DropDownMenu1_2.init() failed. Element '"+ this.id +"' does not exist."); }
        if (this.type != "horizontal" && this.type != "vertical") { return alert("DropDownMenu1_2.init() failed. Unknown menu type: '"+this.type+"'"); }
        if (this.browser.ie && this.browser.ie5) { fixWrap(); }
        fixsection_2s();
        parse(document.getElementById(this.id).childNodes, this.tree, this.id);
    }

    /* Search for .section_2 elements and set width for them */
    function fixsection_2s() {
        var arr = document.getElementById(self.id).getElementsByTagName("div");
		var arrt = document.getElementById(self.id).getElementsByTagName("td");
        var section_2s = new Array();
        var widths = new Array();
		
		var widthst = new Array();
		var section_2st = new Array();
        
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].className == "section_2") {
                section_2s.push(arr[i]);
            }
        }
		
		//--
		
		for (var i = 0; i < arrt.length; i++) {
            if (arrt[i].className == "tSk_2" && $( arrt[i] ).children(".section_2").html() != null ) {
                section_2st.push(arrt[i]);
            }
        }
		
		 for (var i = 0; i < section_2st.length; i++) {
            widthst.push(getMaxWidth1(section_2st[i]));
        }
		//alert(widthst);
		/*
        for (var i = 0; i < section_2s.length; i++) {
            section_2s[i].style.width = (widthst[i]) + "px";
            //alert(widthst[i])
        }
		*/
		//--
		
		
        for (var i = 0; i < section_2s.length; i++) {
            widths.push(getMaxWidth(section_2s[i].childNodes));
        }
		
		//widths - section_2ai;  widthst - topai
		//alert(widths+"|"+widthst);
        for (var i = 0; i < section_2s.length; i++) {
			if( widths[i] < widthst[i] ) {
				section_2s[i].style.width = (widthst[i]) + "px";
				
				/*
				var sk = section_2s[i];		
				$( sk ).children(".item2_2").each(function() {
					$(this).width( "400" );								
				});
				*/
				
				
			} else {
            	section_2s[i].style.width = (widths[i]) + "px";
			}
        }
		
		
        if (self.browser.ie) {
            for (var i = 0; i < section_2s.length; i++) {
                setMaxWidth(section_2s[i].childNodes, widths[i]);
            }
        }
    }

    function fixWrap() {
        var elements = document.getElementById(self.id).getElementsByTagName("a");
        for (var i = 0; i < elements.length; i++) {
            if (/item2_2/.test(elements[i].className)) {
                elements[i].innerHTML = '<div nowrap="nowrap">'+elements[i].innerHTML+'</div>';
            }
        }
    }

    /* Search for an element with highest width among given nodes, return that width */
    
	
	function getMaxWidth1(nodes) {
        var maxWidth = 0;
        //for (var i = 0; i < nodes.length; i++) {
            //alert(nodes[i].nodeName);
        //	if (nodes[i].nodeType != 1 || /tSk_2/.test(nodes[i].className)) { continue; }
        //    if (nodes[i].offsetWidth > maxWidth) { maxWidth = nodes[i].offsetWidth; }
        //}
        	maxWidth = nodes.offsetWidth;
        return maxWidth;
    }
	
	
	function getMaxWidth(nodes) {
        var maxWidth = 0;
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) { continue; }
            if (nodes[i].offsetWidth > maxWidth) { maxWidth = nodes[i].offsetWidth; }
        }
        return maxWidth;
    }
	

    /* Set width for item2_2 elements */
    function setMaxWidth(nodes, maxWidth) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType == 1 && /item2_2/.test(nodes[i].className) && nodes[i].currentStyle) {
                if (self.browser.ie5) {
                    nodes[i].style.width = (maxWidth) + "px";
                } else {
                    nodes[i].style.width = (maxWidth - parseInt(nodes[i].currentStyle.paddingLeft) - parseInt(nodes[i].currentStyle.paddingRight)) + "px";
                }
            }
        }
    }

    /* Parse nodes, create events, position elements */
    function parse(nodes, tree, id) {
        for (var i = 0; i < nodes.length; i++) {
            if (1 != nodes[i].nodeType) {
                continue;
            }
            switch (true) {
                // .item1_2
                case /\bitem1_2\b/.test(nodes[i].className):
                    nodes[i].id = id + "-" + tree.length;
                    tree.push(new Array());
                    nodes[i].onmouseover = item1_2over;
                    nodes[i].onmouseout = item1_2out;
                    break;
                // .item2_2
                case /\bitem2_2\b/.test(nodes[i].className):
                    nodes[i].id = id + "-" + tree.length;
                    tree.push(new Array());
                    break;
                // .section_2
                case /\bsection_2\b/.test(nodes[i].className):
                    // id, events
                    nodes[i].id = id + "-" + (tree.length - 1) + "-section_2";
                    nodes[i].onmouseover = section_2Over;
                    nodes[i].onmouseout = section_2Out;
                    // position
                    var box1 = document.getElementById(id + "-" + (tree.length - 1));
                    var box2 = document.getElementById(nodes[i].id);
                    if ("horizontal" == self.type) {
                        box2.style.top = box1.offsetTop + box1.offsetHeight + self.position.top + "px";
                        if (self.browser.ie5) {
                            box2.style.left = self.position.left + "px";
                        } else {
                            box2.style.left = box1.offsetLeft + self.position.left + "px";
                        }
                    } else if ("vertical" == self.type) {
                        box2.style.top = box1.offsetTop + self.position.top + "px";
                        if (self.browser.ie5) {
                            box2.style.left = box1.offsetWidth + self.position.left + "px";
                        } else {
                            box2.style.left = box1.offsetLeft + box1.offsetWidth + self.position.left + "px";
                        }
                    }
                    // section_2s, section_2sShowCnt, section_2sHideCnt
                    self.section_2s.push(nodes[i].id);
                    self.section_2sShowCnt.push(0);
                    self.section_2sHideCnt.push(0);
                    break;
            }
            if (nodes[i].childNodes) {
                if (/\bsection_2\b/.test(nodes[i].className)) {
                    parse(nodes[i].childNodes, tree[tree.length - 1], id + "-" + (tree.length - 1));
                } else {
                    parse(nodes[i].childNodes, tree, id);
                }
            }
        }
    }

    /* event, item1_2:onmouseover */
    function item1_2over() {
        var id_section_2 = this.id + "-section_2";
        if (self.visible) {
            var el = new Element(self.visible);
            el = document.getElementById(el.getParent().id);
            if (/item1_2-active/.test(el.className)) {
                el.className = el.className.replace(/item1_2-active/, "item1_2");
            }
        }
        if (self.section_2s.contains(id_section_2)) {
            self.section_2sHideCnt[self.section_2s.indexOf(id_section_2)]++;
            var cnt = self.section_2sShowCnt[self.section_2s.indexOf(id_section_2)];
            setTimeout(function(a, b) { return function() { self.showsection_2(a, b); } } (id_section_2, cnt), self.delay.show);
        } else {
            if (self.visible) {
                var cnt = self.section_2sHideCnt[self.section_2s.indexOf(self.visible)];
                setTimeout(function(a, b) { return function() { self.hidesection_2(a, b); } } (self.visible, cnt), self.delay.show);
            }
        }
    }

    /* event, item1_2:onmouseout */
    function item1_2out() {
        var id_section_2 = this.id + "-section_2";
        if (self.section_2s.contains(id_section_2)) {
            self.section_2sShowCnt[self.section_2s.indexOf(id_section_2)]++;
            if (id_section_2 == self.visible) {
                var cnt = self.section_2sHideCnt[self.section_2s.indexOf(id_section_2)];
                setTimeout(function(a, b) { return function() { self.hidesection_2(a, b); } }(id_section_2, cnt), self.delay.hide);
            }
        }
    }

    /* event, section_2:onmouseover */
    function section_2Over() {
        self.section_2sHideCnt[self.section_2s.indexOf(this.id)]++;
        var el = new Element(this.id);
        el = document.getElementById(el.getParent().id);
        if (!/item1_2-active/.test(el.className)) {
            el.className = el.className.replace(/item1_2/, "item1_2-active");
        }
    }

    /* event, section_2:onmouseout */
    function section_2Out() {
        self.section_2sShowCnt[self.section_2s.indexOf(this.id)]++;
        var cnt = self.section_2sHideCnt[self.section_2s.indexOf(this.id)];
        setTimeout(function(a, b) { return function() { self.hidesection_2(a, b); } }(this.id, cnt), self.delay.hide);
    }

    /* Show section_2 (1 argument passed)
     * Try to show section_2 (2 arguments passed) - check cnt with section_2ShowCnt */
    this.showsection_2 = function(id, cnt) {
        if (typeof cnt != "undefined") {
            if (cnt != this.section_2sShowCnt[this.section_2s.indexOf(id)]) { return; }
        }
        this.section_2sShowCnt[this.section_2s.indexOf(id)]++;
        var el = new Element(id);
        var parent = document.getElementById(el.getParent().id);
        if (!/item1_2-active/.test(parent.className)) {
            parent.className = parent.className.replace(/item1_2/, "item1_2-active");
        }
        if (this.visible) {
            if (id == this.visible) { return; }
            this.hidesection_2(this.visible);
        }
        //document.getElementById(id).style.display = "block";
        document.getElementById(id).style.visibility = "visible";
        document.getElementById(id).style.zIndex = this.zIndex.visible;
        this.visible = id;
    }

    /* Hide section_2 (1 argument passed)
     * Try to hide section_2 (2 arguments passed) - check cnt with section_2HideCnt */
    this.hidesection_2 = function(id, cnt) {
        if (typeof cnt != "undefined") {
            if (cnt != this.section_2sHideCnt[this.section_2s.indexOf(id)]) { return; }
        }
        var el = new Element(id);
        var parent = document.getElementById(el.getParent().id);
        parent.className = parent.className.replace(/item1_2-active/, "item1_2");
        document.getElementById(id).style.zIndex = this.zIndex.hidden;
        document.getElementById(id).style.visibility = "hidden";
        //document.getElementById(id).style.display = "none";
        if (id == this.visible) { this.visible = ""; }
        else {
            //throw "DropDownMenu1_2.hidesection_2('"+id+"', "+cnt+") failed, cannot hide element that is not visible";
            return;
        }
        this.section_2sHideCnt[this.section_2s.indexOf(id)]++;
    }

    /* Necessary when showing section_2 that doesn't exist - hide currently visible section_2. See: item1_2over() */
    this.hideSelf = function(cnt) {
        if (this.visible && cnt == this.section_2sHideCnt[this.section_2s.indexOf(this.visible)]) {
            this.hidesection_2(this.visible);
        }
    }

    /* Element (.section_2, .item2_2 etc) */
    function Element(id) {
        /* Get parent element */
        this.getParent = function() {
            var s = this.id.substr(this.menu.id.length);
            var a = s.split("-");
            a.pop();
            return new Element(this.menu.id + a.join("-"));
        }
        this.menu = self;
        this.id = id;
    }

    var self = this;
    this.id = id; /* menu id */
    this.tree = []; /* tree structure of menu */
    this.section_2s = []; /* all section_2s, required for timeout */
    this.section_2sShowCnt = [];
    this.section_2sHideCnt = [];
    this.visible = ""; /* visible section_2, ex. menu-0-section_2 */
}

/* Finds the index of the first occurence of item in the array, or -1 if not found */
if (typeof Array.prototype.indexOf == "undefined") {
    Array.prototype.indexOf = function(item) {
        for (var i = 0; i < this.length; i++) {
            if ((typeof this[i] == typeof item) && (this[i] == item)) {
                return i;
            }
        }
        return -1;
    }
}

/* Check whether array contains given string */
if (typeof Array.prototype.contains == "undefined") {
    Array.prototype.contains = function(s) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] === s) {
                return true;
            }
        }
        return false;
    }
}
