/*
 *  DROPMENU v1.01
 *
 *  created by Jan "johno" Suchal
 *  http://johno.jsmf.net/
 *  inspired by menu at http://www.dobryweb.cz/
 *
 *  required lib
 *  evt - http://www.fczbkk.com/js/evt/
 */

dropMenu = {
    // public
    count : false,
    hideSelects : false,
    triggersStyleHandler : false,

    showTimeout : 150,
    hideTimeout : 0,

    // private
    menus : false,
    triggers : false,
    selects : false,
	buttons : false,
    
    currentMenuIndex : false,
    targetMenuIndex : false,
    currentMenuState : 0,       // 0 - hidden, 1 - visibility triggered, 2 - visible, 3 - hide triggered
    currentTimeoutId: false,
    
    init : function() {
        if(this.count) {
            this.menus = new Array();
            this.triggers = new Array();
			this.buttons = new Array();
            var trigger;
			var button;
            var menu;
            for(i = 1; i <= this.count; i++) {
                trigger = document.getElementById("dropmenu-trigger-" + i);
				button 	= document.getElementById("dropmenu-trigger-img-" + i);

                if(trigger) {
                    trigger.selfIndex = i;
                    evt.add(trigger, "mouseover", dropMenuTriggerOver);
                    evt.add(trigger, "mouseout", dropMenuTriggerOut);
					if(button){
						this.buttons[i] = button;
					}
                    this.triggers[i] = trigger;
                    menu = document.getElementById("dropmenu-menu-" + i);
                    if(menu) {
                        menu.selfIndex = i;
                        evt.add(menu, "mouseover", dropMenuMenuOver);
                        evt.add(menu, "mouseout", dropMenuMenuOut);
                        this.menus[i] = menu;
                        menu.style.display = "none";
						/* IE hack: posun spat na zaciatok */
						if (document.all) {
							//if (i == 1) menu.style.marginLeft = '-'+(trigger.offsetWidth - 21)+'px';
							//else menu.style.marginLeft = '-'+(trigger.offsetWidth - 41)+'px';
							var href_obj = trigger.childNodes(0);
							if (href_obj) {
								menu.style.marginLeft = '-'+(Math.ceil(href_obj.offsetWidth/2) + Math.ceil(trigger.offsetWidth/2) + 3)+'px';
								//alert(href_obj.offsetWidth);
							}
						}
                    } else {
                        this.menus[i] = false;
                    }
                } else {
                    this.triggers[i] = false;
                }
            }
            // initialize select tags array
            if(this.hideSelects) {
                this.selects = document.getElementsByTagName("select");
            }
        }
    },
    
    triggerOver : function(e) {
        e = evt.fix(e);
        index = this.getSelfIndex(e.target);
        if(!e.target.selfIndex) e.target.selfIndex = index; // next time will be faster
	if(this.triggers[index].className == 'dmd') return;
		//alert(this.buttons[index].src)
		this.buttonOn(index);
        switch(this.currentMenuState) {
            case 0:
                // TODO: trigger style handler
                this.targetMenuIndex = index;
                this.currentMenuState = 1;
                this.currentTimeoutId = setTimeout("dropMenuMenuShow()", this.showTimeout);
                break;
                
            case 3:
                clearTimeout(this.currentTimeoutId);
                if(index == this.currentMenuIndex) {
                    this.currentMenuState = 2;
                } else {
                    this.targetMenuIndex = index;
                    this.currentMenuState = 1;
                    this.menuShow();    // for fast users
                }
                break;
        }
    },
    
    triggerOut : function() {
		this.buttonOff(index);
        switch(this.currentMenuState) {
            case 2:
				this.currentMenuState = 3;
                this.currentTimeoutId = setTimeout("dropMenuMenuHide()", this.hideTimeout);
                break;

            case 1:
                clearTimeout(this.currentTimeoutId);
                this.currentMenuState = 0;
                break;
        }
    },

    menuOver : function() {
        if(this.currentMenuState == 3) {
            clearTimeout(this.currentTimeoutId);
            this.currentMenuState = 2;
        }
    },
    
    menuOut : function() {
        if(this.currentMenuState == 2) {
            this.currentMenuState = 3;
            if(this.menus[this.currentMenuIndex]) {
                this.currentTimeoutId = setTimeout("dropMenuMenuHide()", this.hideTimeout);
            }
        }
    },
    
    menuHide : function() {
        switch(this.currentMenuState) {
            case 3:
                if(this.menus[this.currentMenuIndex]) {

                    this.menus[this.currentMenuIndex].style.display = "none";
					iframe = document.getElementById('dropmenu-iframe');
					if (iframe) {
						elm_roll = this.menus[this.currentMenuIndex];
						iframe.style.display = 'none';
					}
                }
            
            case 1:
                this.triggerStyleOff();
				this.currentMenuState = 0;
                if(this.hideSelects) {
                    for(i = 0; i < this.selects.length; i++) {
                        this.selects[i].style.visibility = "visible";
                    }
                }
                break;
        }
    },
    
    menuShow : function() {
        if(this.currentMenuState == 1) {
            if(this.hideSelects) {
                for(i = 0; i < this.selects.length; i++) {
                    this.selects[i].style.visibility = "hidden";
                }
            }
            if(this.currentMenuIndex) {
                if(this.menus[this.currentMenuIndex]) {
                    this.menus[this.currentMenuIndex].style.display = "none";
                }
                this.triggerStyleOff();
            }
            this.currentMenuIndex = this.targetMenuIndex;
            this.triggerStyleOn();
            if(this.menus[this.targetMenuIndex]) {
                this.menus[this.targetMenuIndex].style.display = "block";
                iframe = document.getElementById('dropmenu-iframe');
				if (iframe) {
					elm_roll = this.menus[this.currentMenuIndex];
					iframe.style.left = elm_roll.offsetLeft+'px';
					iframe.style.top = elm_roll.offsetTop+0+'px';
					iframe.style.height = elm_roll.offsetHeight+'px';
					iframe.style.width = elm_roll.offsetWidth+'px';
					iframe.style.display = 'block';
				}
           }
            this.currentMenuState = 2;
        }
    },
    
    triggerStyleOn : function() {
        if(this.triggersStyleHandler) {
            this.triggersStyleHandler.over(this.triggers[this.targetMenuIndex]);
        }
    },
    
    triggerStyleOff : function() {
        if(this.triggersStyleHandler) {
            this.triggersStyleHandler.out(this.triggers[this.currentMenuIndex]);
        }
    },
    
    getSelfIndex : function(elm) {
        if(!(index = elm.selfIndex)) {
            index = this.getSelfIndex(elm.parentNode);
        }
        return index;
    },

    buttonOn : function(index) {
		if(this.buttons[index]){ 
			oldImage = this.buttons[index].src.toString();
			//alert(oldImage.replace(/_btn/, "_a_btn"));
			newImage = oldImage.replace(/_btn/, "_a_btn");
			this.buttons[index].src = newImage;
		}
    },

    buttonOff : function(index) {
		if(this.buttons[index]){
			oldImage = this.buttons[index].src.toString();
			//alert(oldImage.replace(/_btn/, "_a_btn"));
			newImage = oldImage.replace(/_a_btn/, "_btn");
			this.buttons[index].src = newImage;
		}
    },

    toggleIframe : function(elm, show) {
        if(!(index = elm.selfIndex)) {
            index = this.getSelfIndex(elm.parentNode);
        }
        return index;
    }
}

// shadow functions
function dropMenuInit() { dropMenu.init(); }
function dropMenuTriggerOver(e) { dropMenu.triggerOver(e); }
function dropMenuTriggerOut() { dropMenu.triggerOut(); }
function dropMenuMenuOver() { dropMenu.menuOver(); }
function dropMenuMenuOut() { dropMenu.menuOut(); }
function dropMenuMenuHide() { dropMenu.menuHide(); }
function dropMenuMenuShow() { dropMenu.menuShow(); }

evt.add(window, "load", dropMenuInit);