﻿/*ribbon menu - martin willis : oilstone interactive agency*/
var homePageID = "243";
var getIndex = function(jObj) {
    var children = jObj.parent().children();
    for(var i = 0; i < children.length; i++){
        if (children.eq(i).hasClass("open")) {
            return i;
        }
    }
    return 0;
}
$(document).ready(function() {
    $("ul[id^='mnu']").each(function() {
        $(this).data("openIndex", 0);
    });
    if (PageID == homePageID) {
        $("div#homepage-spacer").css("height", "100px").show();
    }
    else {
        $("div#homepage-spacer").css("height", "20px").show();
        $("div.inner-cont, div.inner, div.contentHolder, ul.site-list, div#footer").click(function() {
            $("ul#top-nav li, ul.top-holder li, ul.button-holder li").removeClass("active").removeClass("open").removeAttr("style");
            $("li a#mnu_" + PageID + ",li a#nav_" + PageID).parent().addClass("active");
            $("ul.top-holder:visible, ul.button-holder:visible").slideUp();
            $("li a#mnu_" + PageID + ",li a#nav_" + PageID).parent().addClass("active");
            $("li a#mnu_" + RootPageID + ",li a#nav_" + RootPageID).parent().addClass("active");
        });
    }
    $("li a#mnu_" + PageID + ",li a#nav_" + PageID).parent().addClass("active");
    $("li a#mnu_" + RootPageID + ",li a#nav_" + RootPageID).parent().addClass("active");
    $("ul.top-holder li, ul.button-holder li").hover(function() {
        if ($(this).hasClass("button")) {
            $(this).addClass("over-red");
            var img = $(this).find("img").eq(0);
            img.attr("src", img.attr("oversrc"));
        }
        else {
            $(this).addClass("over");
            var img = $(this).find("img").eq(0);
            img.attr("src", img.attr("oversrc"));
        }
    }, function() {
        $(this).removeClass("over").removeClass("over-red");
        var img = $(this).find("img").eq(0);
        img.attr("src", img.attr("outsrc"));
    });
    $("#top-nav li a[href!='#']").hover(function() {
        $(this).addClass("over-red");
    }, function() {
        $(this).removeClass("over-red");
    });
    $("ul.top-holder li, ul.button-holder li").not($("li.button")).click(function() {
        if ($(this).hasClass("open") != true) {
            $("ul.top-holder li, ul.button-holder li").removeClass("open");
            $(this).addClass("open");
            $(this).parent().data("openIndex", getIndex($(this)));
            $("ul.top-holder li").css("width", "60px");
            $("ul.button-holder li").css("width", "80px");
            var width = $(this).parent().hasClass("top-holder") ? 64 : 84;
            var ibts = $(this).parent().children("li").length - 1;
            var iopen = 972 - (ibts * width);
            var anchor_count = $(this).find("div.fourth_level a").length;
            var cols = Math.round((anchor_count / 4) + 0.5);
            var btn_anchor_width = Math.round((iopen - 84) / cols) - 26;
            $("div.fourth_level").css("max-width", (iopen - 100) + "px");
            if (btn_anchor_width > 250) { btn_anchor_width = 250 };
            $(this).find("div.fourth_level a").css("width", btn_anchor_width + "px");
            $("ul.top-holder li.open, ul.button-holder li.open").css("width", iopen + "px");
        }
    });
    $("li.button").click(function() {
        var href = $(this).find("div a").eq(0).attr("href");
        if (href.length > 5) {
            location.href = href;
        }
    });

    $("ul#top-nav li a[href=#]").click(function() {
        if ($("#mnu" + $(this).attr("id").replace("nav", "") + ":visible").length > 0) {
            if (PageID != homePageID) {
                $("ul#top-nav li, ul.top-holder li, ul.button-holder li").removeClass("active").removeClass("open").removeAttr("style");
                $("li a#mnu_" + PageID + ",li a#nav_" + PageID).parent().addClass("active");
                $("ul.top-holder:visible, ul.button-holder:visible").slideUp();
                $("li a#mnu_" + PageID + ",li a#nav_" + PageID).parent().addClass("active");
                $("li a#mnu_" + RootPageID + ",li a#nav_" + RootPageID).parent().addClass("active");
            }
            return;
        }
        $("ul#top-nav li").removeClass("active");
        var isOpen = $("ul.top-holder:visible, ul.button-holder:visible").length > 0;
        $("ul.top-holder:visible, ul.button-holder:visible").hide();
        var ibts = $("#mnu" + $(this).attr("id").replace("nav", "") + " li").length - 1;
        var width = $("#mnu" + $(this).attr("id").replace("nav", "")).hasClass("top-holder") ? 64 : 84;
        var iopen = 972 - (ibts * width);
        $("ul.top-holder li.open, ul.button-holder li.open").css("width", iopen + "px");
        $("#mnu" + $(this).attr("id").replace("nav", "") + " li").eq($("#mnu" + $(this).attr("id").replace("nav", "")).data("openIndex")).addClass("open");
        $("#mnu" + $(this).attr("id").replace("nav", "") + " li.open").css("width", iopen + "px");
        var anchor_count = $("#mnu" + $(this).attr("id").replace("nav", "") + " li.open div.fourth_level a").length;
        var cols = Math.round((anchor_count / 4) + 0.5);
        var btn_anchor_width = Math.round((iopen - 84) / cols) - 25;
        $("div.fourth_level").css("max-width", (iopen - 100) + "px");
        if (btn_anchor_width > 250) { btn_anchor_width = 250 };
        $("#mnu" + $(this).attr("id").replace("nav", "") + " li.open div.fourth_level a").css("width", btn_anchor_width + "px");
        $("li a#mnu_" + PageID + ",li a#nav_" + PageID).parent().addClass("active");
        $("li a#mnu_" + RootPageID + ",li a#nav_" + RootPageID).parent().addClass("active");
        if (isOpen) {
            $("#mnu" + $(this).attr("id").replace("nav", "")).show();
            $("li a#mnu_" + PageID + ",li a#nav_" + PageID).parent().removeClass("active");
            $("li a#mnu_" + RootPageID + ",li a#nav_" + RootPageID).parent().removeClass("active");
        }
        else {
            $("#mnu" + $(this).attr("id").replace("nav", "")).slideDown("normal");
            $("li a#mnu_" + PageID + ",li a#nav_" + PageID).parent().removeClass("active");
            $("li a#mnu_" + RootPageID + ",li a#nav_" + RootPageID).parent().removeClass("active");
        }
        $(this).parent().addClass("active");
    });
    /* open first ribbon if homepage*/
    if (PageID == homePageID) {
        $("li a#nav_366").trigger("click");
    }
    /* set up roll-over images*/
    $("div.acc-holder img").each(function() {
        var aImg = $(this).attr("src").split("?");
        if (aImg.length > 0) {
            $(this).attr("outsrc", aImg[0]);
        }
        else {
            $(this).attr("outsrc", "images/online.jpg");
        }
        if (aImg.length > 1) {
            $(this).attr("oversrc", aImg[1]);
        }
        else {
            $(this).attr("oversrc", $(this).attr("outsrc"));
        }
    });
    partners();
});

/*specials & news */
$(document).ready(function() {
    $("strong#s_specials, strong#s_news").toggle(function() {
        $("#" + $(this).attr("id").replace("s_", "u_")).slideDown();
        $(this).css({"background-image":"url(../images/up-nav.gif)"});
    }, function() {
        $("#" + $(this).attr("id").replace("s_", "u_")).slideUp();
        $(this).css({"background-image":"url(../images/down-nav.gif)"});
    });
});
