﻿function RessortBox() {
    var RB = this; 

    //assign the class to the div #body to set the background image
    $("#Body").addClass("RessortBox");

    //render the selector element if available
    $("#RessortSelector").mcomSelector({numberOfVisibleEntries: 10});
    
    //get the tab count
    var TabCount =  $(".RessortControl #RessortTabs > li").length

    if (TabCount>0) {
        //create an outer ul for the tabs
        var TabUL = document.createElement("UL");
        $(TabUL).addClass("Tabs");
        
        //create an li for each tab
        var TabLI;
        
        //set tabwidth depending on number of tabs
        var TabWidth = 270;
        if (TabCount>1) TabWidth = 596/TabCount - 28; 
       
        //create a container element for the tabcontents
        var TabContents = document.createElement("DIV");
        
        //get all tabs:    
        $(".RessortControl #RessortTabs > li").each(function(i) {
            //1. work on the tab list
            //create the tab
            TabLI = document.createElement("LI");
            //set passive if this is not the first tab
            if (i > 0) $(TabLI).addClass("Passive");
            //set the id
            $(TabLI)[0].id = $(this)[0].id;
            //assign the width
            $(TabLI).width(TabWidth);
            //get the title of the tab                                      
            $(TabLI).text($(this).children("h3").eq(0).text());
            //assign an onlick event for toggling the tab
            $(TabLI).click(function() {
                RB.ToggleTab($(this)[0].id);
            })
            //append the tab to the tab list
            $(TabUL).append($(TabLI));
            
            //2. work on the tab contents
            //set hidden if this is not the first content
            if (i > 0) $(this).children("div").eq(0).hide();
            //assign the css class
            $(this).children("div").eq(0).addClass("TabContent");
            //set the id
            $(this).children("div").eq(0)[0].id = $(this)[0].id + "Content";
            //append the tabcontent to the container
            $(TabContents).append($(this).children("div").eq(0));
        })
              
        //remove the original list we just parsed
        $(".RessortControl #RessortTabs").remove();
        
        //append the new tab list to the RessortControl
        $(".LeftColumn .RessortControl").append($(TabUL));
        //append the new tab contents to the RessortControl
        $(".LeftColumn .RessortControl").append($(TabContents).children());
    }
}

//tab onclick event: toogle the tab
RessortBox.prototype.ToggleTab = function(id) {
    //check if this tab is passive
    if($("#" + id).hasClass("Passive")) {
        //set all tabs passive
        $(".LeftColumn .RessortControl .Tabs li").addClass("Passive");
        //set selected tab active
        $("#" + id).removeClass("Passive");
        //hide all contents
        $(".LeftColumn .RessortControl .TabContent").hide();
        //show selected content
        $("#" + id + "Content").show();
   }   
}

//change the value of a certain field and submit the form
RessortBox.prototype.SetAndSubmit = function(vals) {
    for (fieldname in vals) {
        $(".RessortControl input[name='" + fieldname + "']").val(vals[fieldname]);
    }
    //$(".RessortControl input[name='" + fieldname + "']").val(val);
    $(".RessortControl form:first").submit();
    return false;
}
