//Apply the Javascript-enabled styles (hidding expandable boxes for instance)
$('html').addClass('js'); 

$(document).ready(function(){  
    //Main navigation display
    display_main_nav();
    
    //form's init
    forms_init();        
    
    //Popup Links
    //init_popup_links();

    //Module 
    //InitExpandableModules();
    EventExpandableModules();
    
    	
	//Hide directors details is JS enabled
	hide_details();
});

function display_main_nav()
{   
    //Display current navigation items recursively
    display_current_nav_items("navigation_main", "");
    display_current_nav_items("alternative_navigation", 1);
}

function display_current_nav_items(id_nav, maxlevel)
{
    pageID = $("body").attr("id");
    nav = $("#" + id_nav); 
	
	if(pageID != "" && nav.length > 0)
    {
        pageID = pageID.slice(5);
		
        if (pageID != "0")
        {                                    
            current_item = $("#nav_"+pageID);
            current_item.find("a:eq(0)").addClass("current");      
            //console.log(maxlevel);      
			if (!(current_item.attr("id").indexOf("0")!=-1) && maxlevel != 1){
				expand_current_item(id_nav, current_item);
			}
        }
    }
}

function expand_current_item(id_nav, current)
{             
    current.find("ul:eq(0)").show();
    if(current.parent("ul:eq(0)").attr("id") != id_nav)
    {        
        expand_current_item(id_nav, current.parents("li:eq(0)"));
    }
    else
    {
        //current.addClass("current");        
        if (current.find("li:eq(0)").attr("id") != null)
        {            
            current.find("a:eq(0)").addClass("current");
        }
    }
}

function forms_init()
{
    init_input_text_default_value("#text_search", "Enter your keywords");
}

//Initialize Focus and Blur events for an input text with a default value
function init_input_text_default_value(id_input_search, default_value)
{    
    $(id_input_search).focus( function(){
        if($(this).val() == default_value)
        {
            $(this).val("");
        }   
    });
    
    $(id_input_search).blur( function(){
        if($(this).val() == "")
        {
            $(this).val(default_value);
        }   
    });
}

function hide_details(){
    $("#subcolumn_copy").css({ height: "200px"});
	$(".directordetail").hide();
}
function show_details(layerid){
	$(".directordetail").not("#name"+layerid).hide();
	$("#name"+layerid).show();
	if ($("#highlight"+layerid).length>0) {
		$("#directorimage img").not("#mainimg").hide();;
		$("#directorimage img#highlight"+layerid).show();
	}
	$(".subcolumn_extra .directordetail").show();
}

/*****************************************************************************/
function form_scripts()
{
    //Search input
    $("#query_string").focus(function() {
       if (this.value == "Search our site")
         this.value = "";
     }); 
     
    $("#query_string").blur(function() {
        if (this.value == "")
         this.value = "Search our site";
     }); 
    
    //Email this page's Form
    $("#form_email_page").validate({
        event: "keyup",
        rules: {
            your_name: { required: true },
            your_email: { 
                required: true,
                email: true
            },
            recip_name: { required: true },
            recip_email: { 
                required: true,
                email: true
            }
        },
	    errorPlacement: function(error, element) {
		    error.appendTo( element.parent("label").find("span.error_message"));
	    }
    });
}

function init_popup_links()
{
    if($("#popup_kpi"))
    {
        link_hrefkpi = SITE_URL + $("#popup_kpi").attr("href");        
        $("#popup_kpi").click(function(){
            window.open(link_hrefkpi, "", "resizable=1, scrollbars=yes,width=550,height=450");
            $("#popup_kpi").attr("href", "#");
        });
    }
    
    if($("#popup_mi"))
    {
        link_hrefmi = SITE_URL + $("#popup_mi").attr("href");        
        $("#popup_mi").click(function(){
            window.open(link_hrefmi, "", "resizable=1, scrollbars=yes,width=640,height=350");
            $("#popup_mi").attr("href", "#");
        });
    }
    
    if($(".popup_pdf"))
    {
        $(".popup_pdf").each(function(index){
            $(this).click(function(){
                window.open(this, "", "resizable=1, scrollbars=yes,width=800,height=600");
                return false;
            });
        });
    }
}

//--------------------------------------------- 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.header_link').toggleClass("close");
}
 
function EventExpandableModules()
{
    $("a.header_link").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(){
            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("fast"); 
            /*module_chklist.hide(function(){
                module_body.slideToggle("fast"); 
                $(this).hide();           
            });*/
        }else{
            module_body.slideToggle("fast"); 
        }
      } 
                 
      $(this).toggleClass("close");
      return false;
    });
} 

function EventExpandableModules_withoutfade()
{
    $("a.header_link").click(function () {       
      var module_body = $(this).parents(".expanding_module").find(".module_body");
            
      module_body.slideToggle("fast");            
      $(this).toggleClass("close");
      return false;
    });
} 
 