/* author: Rob Crowther - 03/09/09 */
/* (c) informa 2009 */

function prepareDataSectionToggler() {
    if(!document.getElementById) return false;
    if(!document.getElementById("tabs")) return false;
    $("#tabs a").each(function() {
    this.onclick = function() {
    var hashValue = this.href.split("#");
         $("#tabs a").each(function() {
        this.className = ""; //remove class 'selected' from section links
         });
        //close all section panels, then open the requested one
        $("#tabsVessels").css("display", "none");
        $("#tabsPlaces").css("display", "none");
        $("#tabsCompanies").css("display", "none");
        $("#tabsPeople").css("display", "none");
        $("#"+hashValue[1]).css("display", "block"); // requested
        this.className = "selected";
        var url;
        var panelType;
        var linkType;
        if (hashValue[1] == 'tabsVessels') {
            url = getUrl(hashValue[1], "v");
            panelType = "vO";
            linkType = "vesselLink";
        } else if (hashValue[1] == 'tabsPlaces') {
            url = getUrl(hashValue[1], "p");
            panelType = "pO";
            linkType = "placeLink";
        } else if (hashValue[1] == 'tabsCompanies') {
            url = getUrl(hashValue[1], "c");
            panelType = "cO";
            linkType = "companyLink";
        } else {
            url = getUrl(hashValue[1], "peO");
            panelType = "peO";
            linkType = "peopleLink";
        }
        sendLinkData(url, 1, panelType, linkType, true);
        return false;
    }
    });
    return true;
}

function getUrl(hashValue, section) {
    return $("#"+hashValue + " #"+section+"ComboOption1").attr("href");
}

function togglePanels(element, panelNum, panel, numOfSections, clickSource){
    /*alert("element: " + element + "\npanelNum: " + panelNum +
    "\npanel: " + panel + "\nnumOfSections: " + numOfSections);*/
    var toggleStatus = getToggleStatus(element, panel);
    switchArrow(panel);
    $(element).removeClass(panel + "rightArrowButton");
    $(element).addClass(panel + "downArrowButton");
    for (var i = 1; i <= numOfSections; i++) {
        if (i == panelNum) {
            //alert(panelNum);
            if (toggleStatus == "down") {
                if (!clickSource) { // check to see what was clicked - if section menu we don't close the panel
                switchArrow(panel);
                $("#" + panel + "Contents" + i).slideUp('fast');
                }
            }
            else {
                $("#" + panel + "Contents" + i).slideDown('slow');
            }
        }
        else {
            $("#" + panel + "Contents" + i).slideUp('fast');
        }
    }
}

function getToggleStatus(element, panel){
    var toggleStatus; // get the initial 'up'/'down status of the panel - user can re-close panel if wanted
    if ($(element).attr("class") == panel + "rightArrowButton") {
        toggleStatus = "up";
    }
    else {
        toggleStatus = "down";
    }
    return toggleStatus;
}

function switchArrow(panel){
    $("." + panel + "downArrowButton").removeClass(panel + "downArrowButton").addClass(panel + "rightArrowButton");
}