(function($) {

    // Hide the omniture tracking img so it doesnt mess up the layout
    $(document).ready(function() {
        $("img[name='s_i_kohlerenglefieldconz']").css({
            "position" : "absolute",
            "top" : "0px",
            "left" : "0px",
            "width" : "1px",
            "height" : "1px",
            "visibility" : "hidden",
            "display" : "none"
        });
    });
    
    // Navigation
    $(document).ready(function() {

        // Nav element hover states
        $("#nav ul.subnav li").hover(
                function() {
                    $(this).css({
                        "background-color" : "#efefef"
                    });
                },
                function() {
                    $(this).css({
                        "background-color" : ""
                    });
                }
        );


        // When the user clicks on the li element, to go the page
        $("#nav li").click(function(e) {
            var anchor = $(e.target).children("a:first");
            if (anchor != null && anchor.size() > 0) {
                var href = anchor.attr("href");
                window.location.href = href;
                e.stopPropagation();
                return false;
            }
        });
        

        var navTimer = new Gorilla.Timer();
        var subNavTimer = new Gorilla.Timer();
        var showingNav;
        var showingSubNav;

        // Nav menu open and closing
        $("#nav ul").not(".subnav").children("li").hover(
                function() {
                    var navElems = $(this).children("ul");
                    navTimer.clear();

                    if ((showingNav == null || showingNav.get(0) != navElems.get(0))) {
                        if(navElems.size() > 0) {
                            navTimer.clear();
                            subNavTimer.clear();
                            navTimer.set(function() {
                                hideAllSubNavs();
                                showingSubNav = null;
                                hideAllNavs(function() {
                                    showNav(navElems);
                                    showingNav = navElems;
                                });
                            }, 200);

                        } else {
                            navTimer.set(function() {
                                showingNav = null;
                                showingSubNav = null;
                                hideAllSubNavs();
                                hideAllNavs();
                            }, 300);
                        }
                    }

                },
                function() {
                    var navElems = $(this).children("ul");
                    navTimer.set(function() {
                        showingNav = null;
                        hideAllNavs();
                    }, 300);
                }
        );


        // Sub-menu open and closing
        $("#nav ul.subnav").not(".subsubnav").children("li").hover(
                function() {
                    var navElems = $(this).children("ul");
                    navTimer.clear();
                    subNavTimer.clear();
                    if(navElems.size() > 0 && (showingSubNav == null || showingSubNav.get(0) != navElems.get(0))) {
                        subNavTimer.set(function() {
                            hideAllSubNavs(function() {
                                var leftOffset = navElems.parents(":first").outerWidth();
                                navElems.css({
                                    left : leftOffset + "px"
                                })
                                showNav(navElems);
                                showingSubNav = navElems;
                            });
                        }, 200);
                    }
                },
                function() {
                    var navElems = $(this).children("ul");
                    subNavTimer.set(function() {
                        showingSubNav = null;
                        hideAllSubNavs();
                    }, 300);
                }
        );

        // Defines the nav open behavior
        function showNav(elem, callback) {
            if (elem == null) {
                if (callback) {
                    callback();
                }
                return;
            }

            // Dynamically set the width of the menu
            $(elem).css("display", "block");
            var width = 0;
            if ($(elem).is(":not('.subsubnav')")) {
                width = $(elem).parents(":first").outerWidth() - 2;
            }
            var elems = $(elem).children("li").get();
            for (var i = 0; i < elems.length; i++) {
                width = Math.max(width, $(elems[i]).outerWidth());
            }
            $(elem).add(elems).css({
                "display" : "",
                "width" : width + "px"
            });

            // Open the menu
            $(elem).slideDown("fast", function() {
                if (callback) {
                    callback();
                }
            });
        }

        // Defines the nav close behavior
        function hideNav(elem, callback) {
            if (elem == null) {
                if (callback) {
                    callback();
                }
                return;
            }
            $(elem).stop();
            $(elem).css("height", "");
            $(elem).slideUp(150, function() {
                if (callback) {
                    callback();
                }
            });
        }

        function hideAllNavs(callback) {
            var subnavs = $("#nav ul.subnav").not(".subsubnav");
            var count = subnavs.size();
            subnavs.each(function() {
                hideNav(this, function() {
                    count--;
                    if (count == 0 && callback) {
                        callback();
                    }
                });
            });
        }

        function hideAllSubNavs(callback) {
            var subnavs = $("#nav ul.subsubnav");
            var count = subnavs.size();
            subnavs.each(function() {
                hideNav(this, function() {
                    count--;
                    if (count == 0 && callback) {
                        callback();
                    }
                });
            });
        }
    });



})(jQuery);




function openDesigner() {
	var designerWin = window.open('designer/app/designer.html', 'Designer', "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=1,resizable=0,width=654,height=426,alwaysRaised=yes,dependent=no");
	designerWin.focus();
	return false;
}