﻿ejungle.runtime.controls.header = {}

/* Initializes the control */
ejungle.runtime.controls.header.init = function() {
    if ($.browser.msie) {
        $(".search-button").css("opacity", 0.1);
        $(".search-button").css("background-color", "#ffffff");
    }

    // Set up Main Navigation
    $(".main-nav ul.level-1 > li").each(function(i) {
        var self = $(this);

        var listItems = self.find("> ul.level-2 li");

        if (listItems.length > 0) {
            var columns = self.find(".sub-nav ul.level-2 li");

            var rowsPerColumn = parseFloat(listItems.length) / 3;

            var columnRowCount = new Array();
            columnRowCount[0] = Math.ceil(rowsPerColumn);
            columnRowCount[2] = Math.floor(rowsPerColumn);
            columnRowCount[1] = Math.ceil(listItems.length - columnRowCount[0] - columnRowCount[2]);

            for (var columnIndex = 0; columnIndex < 3; columnIndex++) {
                var currentColumn = columns.eq(columnIndex).find("ul");
                var currentLow = 0;

                for (var tmp = 0; tmp < columnIndex; tmp++) {
                    currentLow += columnRowCount[tmp];
                }
                var currentHigh = currentLow + columnRowCount[columnIndex];

                for (var index = currentLow; index < currentHigh; index++) {
                    var listItem = listItems.eq(index);
                    var href = listItem.find("a").attr("href") || "";
                    var title = listItem.find("span").text() || "";

                    currentColumn.append($.format("<li><a href='{0}'><span>{1}</span></a></li>", href, title));
                }
            }
        }
        else {
            self.find(".dropdown-connector").remove();
            self.find(".sub-nav").remove();
        }
        self.find("> ul.level-2").remove();

        // This is required so that the dimensions are calculated
        self.addClass("on"); //.removeClass("on");

        var bodyHeight = self.find(".body").height();
        bodyHeight = $.PxToEm(bodyHeight);

        self.find(".body .bg").css("height", bodyHeight);

        self.hover(function() {
            $(this).addClass("on");
        }, function() {
            $(this).removeClass("on");
        });

        self.removeClass("on");
    });

    // Set up Search
    var search = $(".btn-search");

    $("*").bind('keydown', 'esc', function(e) {
        $(this).removeClass("btn-search-on");
        return false;
    });


    search.find(".search-input").focus(function() {
        $(this).attr("status", "focus");
    });

    search.find(".search-input").blur(function() {
        $(this).attr("status", "");
    });

    jQuery(document).bind('keydown', 'return', function(e) {
        if (search.hasClass("btn-search-on")) {
            ejungle.runtime.controls.header.search();
        }
        return false;
    });

    search.find(".search-button").click(function() {
        ejungle.runtime.controls.header.search();
        return false;
    });


    search.hover(function() {
        $(this).addClass("btn-search-on");
    }, function() {
        if (search.find(".search-input").attr("status") != "focus") {
            $(this).removeClass("btn-search-on");
        }
    });

    $(".top-nav .body ul.level-1 li").each(function() {
        var self = $(this);

        if (self.find("ul.level-2 li").length == 0) {
            self.find(".dropdown-background").remove();
        }
    });


    if ($.browser.msie6) {
        $(".top-nav .body ul.level-1 li").hover(function() {
            $(this).addClass("on");
        }, function() {
            $(this).removeClass("on");
        });
    }
};

ejungle.runtime.controls.header.search = function() {
    var search = $(".btn-search");
    var searchTerm = escape(search.find(".search-input").val());
    var searchType = search.find("[name='searchOption']:checked").val();
    var siteId = escape(search.find(".search-siteId").val());

    var url = siteId == "1" ? "/Pages/Search.aspx?s={0}&type={1}" : "/Pages/ALI/Search.aspx?s={0}&type={1}";

    document.location = $.format(url, searchTerm, searchType);
};
