/*
    This script handles the setup and operation of the Tabs accross the top of the page.    
    Ensure that the Tab Navigation and Tab content sections are added to the page.    
    
    Author: Richard Gosling
    Date:   20/11/2008
*/
// Store the ids of the info tabs so that we know how many of them have been used.
var arrTabs = new Array();
var arrTabContent = new Array();
var panelOpen = ""; // Used to check which of the panels is currently open.
var pos;

function initTabs() { 
    // Loop through all the "td" tags in the current document
    tds = $("td");
    strIds = "";
    $.each(tds, function(i) {
        var cur = tds[i];

        // Find all the ids of the Tab Buttons and put them into an array.
        try {
            if (cur.id.indexOf("TabButton_") == 0) {
                strIds += cur.id + "\n";
                arrTabs.push(cur.id);
            }
        } catch (e) { }
    });

    // Loop through the tabs and extract the content ready for rebuilding the html.
    $.each(arrTabs, function(i) {
        t = $("#" + arrTabs[i]);

        // Extract and modify the tab label
        curHTML = $("#" + arrTabs[i]).html();
        arrTabContent[i] = "<li";

        // The first li needs a class of nav1 to display the left border.
        if (i == 0)
            arrTabContent[0] += " class='nav1'";

        openPanelInt = i * 1 + 1;
        arrTabContent[i] += "><a href='#' onclick='openPanel(" + openPanelInt + "); return false;'><i>&nbsp;</i><b>" + curHTML + "</b></a></li>";

        // Hide the content sections until we need them
        pnlId = "Info_Panel_" + (i * 1 + 1);
        try {
            $("#" + pnlId).addClass("tblInfoText");
        } catch (e) { }
    });
    
    // Now rebuild the html for the tabs to remove the Table and replace it with a UL.
    strTabsUl = "";
    $.each(arrTabContent, function(i){
        strTabsUl += arrTabContent[i];
    });

    $("#topTabs").html("<ul id='nav'>" + strTabsUl + "</ul>");     
    
    // Now hide all the panels
    closePanels();

    // Set up the dialog
    $("#pnlInfoTabText").dialog({
        autoOpen: false,
        closeOnEscape: true,
        modal: true,
        open: function() {
            $(".ui-widget-overlay").height($(document).height());
            $(".ui-dialog").css({ width: '795px', position: 'absolute', top: '100px', left: '50%', marginLeft: '-397px' });

            // Scroll to the top of the page
            $('html, body').animate({ scrollTop: '0px' }, 500);
        },
        close: function() { $(".tblInfoText").hide(); }
    });

    $("#closeTabButton").click(function() {
        $("#pnlInfoTabText").dialog("close");
    });

    // Assign the click method for WebTrands to the top tabs
    $('#nav a').click(function() {
        var tabName = $.trim($(this).find('b').html());
        tabName = tabName.replace(/ /g, "_");
        try { dcsTrk('WT.cg_n=A_FinanceCalculator&DCS.dcsuri=/workflow/create/topnav/' + tabName + '&WT.pn=' + tabName + '&WT.pc=info_page&WT.src=' + entryPoint); } catch (ex) { }
    });
}

function openFinancePanel() {
    // This is used by the hyperlinks on the finance product names that appear
    // as titles over the finance summary boxes.    
    // The tab order can be different for each region, so we need to know which
    // tab is the finance tab for the current region.     
               
    switch(RegionCode)
    {
        case "SE":
          financePanel = 2;
          break;

        case "DE":
          financePanel = 1;
          break;

        case "ES":
          financePanel = 2;
          break;

        case "DK":
          financePanel = 2;
          break;

        case "NO":
          financePanel = 2;
          break;

        case "FI":
          financePanel = 2;
          break;

        case "PL":
          financePanel = 2;
          break;

        default:
            financePanel = 1;
        }

    // Now we can open the correct panel
    openPanel(financePanel);
}

function openPanel(callingIndex) {
	// Disable other controls whilst panel is open (ie6 only)
	if(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1){
	    j = document.forms[0].length;
        for (i=0; i<j; i++){
          eObj = eType = document.forms[0].elements[i];
          //if(eObj.type == 'text' || eObj.type == 'select-one' || eObj.type == 'button') eObj.disabled = true;
          try{
            if(eObj.type == 'button') eObj.disabled = true;
          } catch(e) { }
          
          try{
            if(eObj.type == 'text' || eObj.type == 'select-one') eObj.style.visibility = "hidden";
          } catch(e) { }          
        }
    }

    $("#Info_Panel_" + callingIndex).show();
    $("#pnlInfoTabText").dialog('open');
}               

// Close all the info panels.
function closePanels(){
    // Hide each of the panels
    try {
        for (i = 1; i <= arrTabs.length; i++) {
            $("#Info_Panel_" + i).hide();
        }
    } catch (e) { }

    // Hide the main panel
    $("#pnlInfoTabText").hide();           
}
