
if (!NavXp) { var NavXp = {}; }
if (!NavXp.skin) { NavXp.skin = {}; }
if (!NavXp.skins) { NavXp.skins = {}; }

NavXp.skins["HorizontalDropdowns/Box World"] = {
    init : function() {
        // fix widths for child panels
        //avt.navxp.core.$(".navxp_HDD_WindowsStandard").find(".child_container").find("a").width(150);
        //avt.navxp.core.$(".navxp_HDD_WindowsStandard").find(".child_container").find("a").css("width", "block");
        
        // move all child containers to root level since being nested doesn't render well on IE
        // first, parent all panels
        avt.navxp.core.$(".navxp_HDD_BoxWorld").each(function () {
            avt.navxp.core.$(this).find(".child_container").each(function() {
                var _pnl = this;
                if (avt.navxp.core.$(this).parent("div").next("a").get(0)) {
                    avt.navxp.core.$(this).parent("div").next("a").get(0).panel = avt.navxp.core.$(this);
                    this.item = avt.navxp.core.$(this).parent("div").next("a");
                } else {
                    avt.navxp.core.$(this).prev("a").get(0).panel = avt.navxp.core.$(this);
                    this.item = avt.navxp.core.$(this).prev("a");
                }
                avt.navxp.core.$(this).children(".child_container").each(function() {
                    this.parentPanel = _pnl;
                });
            });
            
            avt.navxp.core.$(this).find(".child_container").each(function() {
                //var pos = avt.navxp.core.$(this).position();
                avt.navxp.core.$(this).appendTo(avt.navxp.core.$(this).parents(".navxp_HDD_BoxWorld"));//.css("left", pos.left + "px").css("top", pos.top + "px");
            });
            
            
            // also, let's reset width to the biggest item
            avt.navxp.core.$(this).find(".child_container").each(function() {
                var maxWidth = 0;
                avt.navxp.core.$(this).show();
                avt.navxp.core.$(this).children("a").each(function() {
                    if (avt.navxp.core.$(this).outerWidth() > maxWidth) {
                        maxWidth = avt.navxp.core.$(this).outerWidth();
                    }
                });
                
                //alert(maxWidth);
                avt.navxp.core.$(this).hide().css("width", (maxWidth + 2) + "px");
            });

            avt.navxp.core.$(this).get(0).loaded = true;
        });
    },
    
    show : function(item, panel, dir) {
    
        if (!item.parents(".navxp_HDD_BoxWorld").get(0).loaded) {
            return;
        }
        
        if (!dir) dir = "up";
        
        
        if (item.get(0).panel) {
            panel = item.get(0).panel;
        }
        
        // let us do some caching
        this.cacheItem(item);

        // prevent parents from hiding
        item.parentPanels.each( function() {
            clearTimeout(this["timeout"]);
            this["timeout"] = null;
        });

        // prevent this popup from hiding
        if (panel && panel.get(0)) {
            //if (item.parentPanels.length > 0) {
            //    panel.css("left", item.position().left + item.outerWidth());
            //    panel.css("top", item.position().top);
            //}
            clearTimeout(panel.get(0)["timeout"]);
            panel.get(0)["timeout"] = null;
        }
        
        // hide the rest of the popups
        //alert(item.otherPanels.length);
        //item.otherPanels.filter(":visible").hide();
        item.otherPanels.filter(":visible").not(panel ? panel : "").each(function() {
            if (this["timeout"]) {
                return;
            }
            var _this = this;
            clearTimeout(this["timeout"]); 
            this["timeout"] = setTimeout(function() { 
                avt.navxp.core.$(_this).hide(function() {
                    _this["timeout"] = null;
                })
                //delete _this;
            }, 200);
        });
        
        // keep all parent items marked as selected
        item.parentItems.addClass("selChild");
        
        item.root.find(".itemL0").not(item.rootItem).removeClass("itemL0Sel");
        item.rootItem.addClass("itemL0Sel");
        
        // and unselect the rest
        item.otherItems.removeClass("selChild");
        
        if (!panel) {
            return;
        }
        
        panel.find(".child_container").show().hide(); // solves IE6 bug
        
        // position this panel at the right location
        //alert(item.width());
        
        panel.find("a").unbind("mouseover").unbind("mouseout");
        
        panel.queue(function() {
        
            if (dir == "left") {
                avt.navxp.core.$(this).css("left", item.position().left + "px").css("top", item.position().top + item.outerHeight() + "");
            } else {
                var parentPanel = item.parents(".child_container");
                //alert(parentPanel.position().top);
                avt.navxp.core.$(this).css("left", parentPanel.position().left + parentPanel.width() + "px").css("top", parentPanel.position().top + item.position().top + "px");
            }
        
            avt.navxp.core.$(this).show("drop", {direction: dir}, "normal", function() {
                avt.navxp.core.$(this).find("a")
                    .bind("mouseover", function() {
                    //alert("in");
                        NavXp.skins['HorizontalDropdowns/Box World'].show(avt.navxp.core.$(this));
                    })
                    .bind("mouseout", function() {
                        NavXp.skins['HorizontalDropdowns/Box World'].hide(avt.navxp.core.$(this));
                    });
                //panel.css("width", panel.width() + "px");
                //clearTimeout(panel.get(0)["timeout"]);
            });
            avt.navxp.core.$(this).dequeue();
        });
        //panel.css("width", panel.width() + "px");
    },

    hide : function(item, panel) {
    
        if (!item.parents(".navxp_HDD_BoxWorld").get(0).loaded) {
            return;
        }

        // let us do some caching
        this.cacheItem(item);
        
        if (item.get(0).panel) {
            panel = item.get(0).panel;
        }

        // let's hide all popups
        var timer = 500;
        var iCall = 0;
        if (item.parentPanels.length == 0) {
            if (panel.get(0)) {
                clearTimeout(panel.get(0)["timeout"]);
                panel.get(0)["timeout"] = setTimeout(function() { 
                        panel.filter(":visible").hide("drop", {direction: "down"}, "normal", function() {
                            this["timeout"] = null;
                        }); 
                        item.rootItem.removeClass("itemL0Sel");
                        
                    }, timer);
            }
        } else {
            
            if (panel) {
                clearTimeout(panel.get(0)["timeout"]);

                panel.get(0)["timeout"] = setTimeout(function() { 
                        panel.find("a").unbind("mouseover").unbind("mouseout");
                        panel.hide("drop", {direction: "down"}, "normal", function() {
                            avt.navxp.core.$(this).find("a").removeClass("selChild");
                            this["timeout"] = null;
                        }); 
                    }, 100);
            }
            

            for (var i = item.parentPanels.length - 1; i >= 0 ; i--, timer += 200) {
                clearTimeout(item.parentPanels.get(i)["timeout"]);
                item.parentPanels.get(i)["timeout"] = setTimeout(function() { 
                    item.parentPanels.eq(iCall).find("a").unbind("mouseover").unbind("mouseout");
                    item.parentPanels.eq(iCall).hide("drop", {direction: "down"}, "normal", function() {
                        avt.navxp.core.$(this).find("a").removeClass("selChild");
                        this["timeout"] = null;
                    }); 
                    
                    iCall++; 
                    if (iCall == item.parentPanels.length) {
                        item.rootItem.removeClass("itemL0Sel");
                    }
                    
                }, timer);
            }
        }
    },
    
    cacheItem : function(item) {
    
        var panel = avt.navxp.core.$(item.panel);
    
        if (!item.root) { item.root = item.parents(".NavXpRoot:first"); }
        if (!item.parentPanels) { 
            item.parentPanels = item.parents(".child_container");
            var parentPanel = item.parents(".child_container").get(0);
            while (parentPanel) {
                parentPanel = parentPanel.parentPanel;
                if (parentPanel) {
                    item.parentPanels = item.parentPanels.add(parentPanel);
                }
            }
        }

        if (!item.otherPanels) { item.otherPanels = item.root.find(".child_container").not(item.parentPanels).not(panel); }
        if (!item.rootItem) { item.rootItem = item.parentPanels.length > 0 ? item.parentPanels.filter(":last").get(0).item : item; }

        if (!item.parentItems) { 
            item.parentItems = avt.navxp.core.$();
            item.parentPanels.each(function() {
                item.parentItems = item.parentItems.add(this.item);
            });
        }
        if (!item.otherItems) { item.otherItems = item.root.find(".itemLN:visible").not(item.parentItems); }
    }
}

if (typeof Sys != "undefined" && typeof Sys.Application != "undefined") {
   Sys.Application.notifyScriptLoaded();
} 