﻿$(document).ready(function() {
    // Figure out which connections center was last viewed
    var connCtr = getLastSelectedConnCenter();
    var connCtrURL = readCookie("connCtrURL");
    if (connCtrURL != null && !isNaN(parseInt(connCtrURL))) {
        if (contentIDs.indexOf(parseInt(connCtrURL)) != -1) {
            connCtr = ids[contentIDs.indexOf(parseInt(connCtrURL))];
        }
        else if (parseInt(connCtrURL) == unsegmentedID) {
            connCtr = "unsegmented";
        }
    }

    // Set up hover effects for items in connection center list
    $(".inConnCenterList").hover(function() { $(this).addClass("over"); }, function() { $(this).removeClass("over"); });

    // Set up list item click events
    $(".inConnCenterList").bind("click", connCenterClick);

    // Set up button click and drop down list visibility
    $("#topBarDropDownButton").click(function() {
        if ($("#topBarDropDownList:visible").length == 0) {
            $("#topBarDropDownList").slideDown();
        }
    });
    $("#topBarDropDownContainer").hoverIntent({
        timeout: 500,
        over: function() { },
        out: function() {
            if ($("#topBarDropDownList:visible").length > 0) {
                $("#topBarDropDownList").slideUp();
            }
        }
    });

    // Select this connections center and load appropriate content
    selectConnectionsCenter(connCtr, false);
});

function connCenterClick(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    selectConnectionsCenter(targ.id, true);
};

function orderListControl(cb) {
    // Maintains order of list control items
    for (var i = 0; i < ids.length; i++) {
        var which = $("#" + ids[i]);
        $("#topBarDropDownList").append(which.removeClass("currentConnCenter").removeClass("first").addClass("inConnCenterList"));
    }
    $(".inConnCenterList").bind("click", connCenterClick);
    if (cb != null) { cb(); }
}

function selectConnectionsCenter(id, fromClick) {
    // Hide list control
    $("#topBarDropDownList").slideUp(function() {
        // Rebuild list control
        orderListControl(function() {
            // Called whenever content needs to be loaded for a particular connections center.
            var which = $("#" + id);
            if (which.length == 0) {
                id = "unsegmented";
                which = $("#forSeniorExecutives");
            }

            // Set cookie
            if (id != "unsegmented") {
                createCookie("connCtr", id, 365);
            }

            // Select chosen id
            $("#topBarDropDown").append(which.removeClass("inConnCenterList").removeClass("first").addClass("currentConnCenter").unbind("click", connCenterClick));

            // Mark the first item in the list with the 'first' class
            $("#topBarDropDownList .inConnCenterList:first").addClass("first");

            // Trigger a custom event so that the consuming page can implement custom behavior
            $("#topBar").trigger("connectionsCenterSelected", [id, fromClick]);
        });
    });
}