$(document).ready(function(){    

    //Shadowbox init
    var options = {      
        assetURL: '/',  
        keysClose: ['c', 27] // c or esc        
    };

    Shadowbox.init(options);


    //Layout
    $('#content_wrapper').css('min-height', $('#menu_left').height());

    //Table striping
     $("table.striped tbody tr:nth-child(even)").addClass("striped");

    //Table highlighting
    $("table.datatable").not('.striped').find("tbody tr").hover(function(){                                 
        $(this).addClass("hover");
    },function(){
        $(this).removeClass("hover");
    });

	//Forms        
    InitInputDefaultValue(".defaultvalue_input");    
    InitSubmitDefaultValue(".defaultvalue_submit", ".defaultvalue_input");

    //Module 
    InitExpandableModules();
    EventExpandableModules();   
    //EventExpandableModules_withoutfade(); 
});

//--------------------------------------------- Expandable Modules
function InitExpandableModules()
{
    //Close all modules that doesn't use the class 'default_open'
    $('div.expanding_module').not('.default_open').find('div.module_body').hide();     
    $('div.expanding_module').not('.default_open').find('div.modulelist_container').hide();    
    $('div.expanding_module').not('.default_open').find('a.toggle').toggleClass("close");
}
 
function EventExpandableModules()
{
    $("a.toggle").click(function () {       
      var module_body = $(this).parents(".expanding_module").find(".module_body");
      var module_chklist = $(this).parents(".expanding_module").find(".module_body .modulelist_container");      
      //var module_chklist_list = $(this).parents(".expanding_module").find(".module_body ul");      
      
      //if the module is CLOSE and has to OPEN (because the link doesn't display the close button)
      if(module_body.css('display') == 'none'){
        module_body.slideToggle("fast", function(){
           
            //$('div.last_opened').slideToggle('fast');
           // $('div.last_opened').removeClass('last_opened');
            //module_body.addClass('last_opened');
            
            if($(this).parents(".expanding_module").find("a.toggle").text() == 'Show Biography'){
            $(this).parents(".expanding_module").find("a.toggle").text("Hide Biography")};   
            module_chklist.show();   
        });      
        
      //if the module is OPEN and has to CLOSE
      }else{       
        if($(this).parents(".expanding_module").find(".modulelist_container")){               
            module_chklist.hide();
            module_body.slideToggle(); 
            
            if($(this).parents(".expanding_module").find("a.toggle").text() == 'Hide Biography'){
            $(this).parents(".expanding_module").find("a.toggle").text("Show Biography")};    
            module_body.removeClass('last_opened');
            /*module_chklist.hide(function(){
                //module_body.slideToggle("fast"); 
                $(this).hide();           
            });*/
        }else{
            module_body.slideToggle("fast"); 
        }
      } 
                 
      $(this).toggleClass("close");
      return false;
    });
} 

function EventExpandableModules_withoutfade()
{
    $("div.module_header a").click(function () {       
      var module_body = $(this).parents(".expanding_module").find(".module_body");
            
      module_body.slideToggle("fast");            
      $(this).toggleClass("close");
      return false;
    });
} 
  
//--------------------------------------------- Forms
/* initialize the inputs which requires a 'default value' system */
function InitInputDefaultValue(input_class){
      
    $(input_class).each(function(i){   
        var current_input_id =  $(this).attr("id");
        var default_value = GetInputDefaultValue(current_input_id);        
        if($(this).val() == ""){
            $(this).val(default_value);
        }
        InitInputDefaultValueEvents("#" + current_input_id, default_value)
    });            
}

/* set the focus and blur events for the 'default value' inputs */
function InitInputDefaultValueEvents(input_id, default_value){

    $(input_id).click(function(){
        if($(this).val() == default_value)
            $(this).val("");
    });
    
    $(input_id).blur(function(){
        if($(this).val() == "")
            $(this).val(default_value);
    });
}

/* return the value of the label associated to the input */
function GetInputDefaultValue(input_id){
         
    var input_default_value = "";    
    $("label").each(function(j){
        if($(this).attr("for") == input_id){
            input_default_value = $(this).text();
        }
    });
    return input_default_value;
}

/* clear the value of the input still using their 'default value' when the form is submitted */
function InitSubmitDefaultValue(submit_class, defaultvalue_class){
    
    $(submit_class).click(function(){                                        
        $(this).parent().find(defaultvalue_class).each(function(i){
            var default_value = GetInputDefaultValue($(this).attr("id"));
            if($(this).val() == default_value){
                $(this).val("");
            }
        });        
    });
}
 
