// Jquery init event
$(document).ready(function(){
   filtersListener();
});



//
// This function make an hidden element to
// be displayed
//
function show(element)
{
	if (obj = document.getElementById(element))
	{
		obj.style.display = "block";
	}
}


//
// This function make a displayed element to
// be hidden
//
function hide(element)
{
	if (obj = document.getElementById(element))
	{
		obj.style.display = "none";
	}
}


//
// This function hide all element having a specific
// className
//
function hideAll(objects_class)
{
	
	targeted_obj = getElementsByClass(objects_class);
	for (var x=0; x<targeted_obj.length; x++)
	{
		targeted_obj[x].style.display = "none";
	}
	
}


//
// This function show or hide an element depending
// of its current state (displayed or hidden)
//
function showHide(element)
{
	if (obj = document.getElementById(element))
	{	
		if (obj.style.display != "block")
		{
			show(element);
		}
		else
		{
			hide(element);
		}
	}
}


// 
// This function set the status to '' (empty)
// in order to avoid ugly 'javascript:;'
// to be showed when used
//
function no_status()
{
	window.status='';
	return true;
}



//
// This function add a class to an
// element
//
function addClassName(obj,class2add)
{
	obj.className = obj.className != '' ? obj.className + ' ' + class2add : class2add;
}


//
// This function remove a class to an
// element
//
function removeClassName(obj,class2remove)
{
	var regex = new RegExp(" "+ class2remove + "|" + class2remove + " |" + class2remove);

	var str = obj.className;
	var reg = new RegExp(class2remove);

	if (reg.test(str))
		obj.className = obj.className.replace(regex, "");
}


//
// This function get all the elements having a specific
// className and return them into an array
//
function getElementsByClass(searchClass, node, tag)
{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


//
// This function reset all 'objects_class'
// same className
// It uses getElementsByClass()
//
function reset_ref_cat(objects_class)
{
	targeted_obj = getElementsByClass(objects_class);
	for (var x=0; x<targeted_obj.length; x++)
	{
		targeted_obj[x].style.display = "none";
		removeClassName(targeted_obj[x],'current');
	}
}

//
// This function reset (hide) all 'objects_class'
// same className
// It uses getElementsByClass()
//
function reset_ref_cat_menu(objects_class)
{
	targeted_obj = getElementsByClass(objects_class);
	for (var x=0; x<targeted_obj.length; x++)
	{
		removeClassName(targeted_obj[x],'current');
	}
}


//
// This function show or hide a subitem element depending
// of its current state (displayed or hidden)
//
function handle_ref_cat(obj,menu_item)
{
	reset_ref_cat('reference_category_block');
	
	reset_ref_cat_menu('ref_menu_item');
	
	addClassName(menu_item.parentNode,"current");
	
	ref_cat_title = document.getElementById('reference_category_title');
	ref_cat_title.innerHTML = menu_item.innerHTML;
	
	if (targeted_obj = document.getElementById(obj))
	{	
		if (targeted_obj.style.display != "block")
		{
			targeted_obj.style.display = "block";
			//addClassName(obj,"current");
		}
		else
		{
			targeted_obj.style.display = "none";
		}
	}
	
}


//
// This function show or hide a subitem element depending
// of its current state (displayed or hidden)
//
function handle_faq_results_item(obj,element)
{
	showHide(obj);
	if (obj = document.getElementById(obj))
	{	
		if (obj.style.display != "block")
		{
			removeClassName(element,"expanded");
			addClassName(element,"default");
		}
		else
		{
			removeClassName(element,"default");
			removeClassName(element,"collpsed");
			addClassName(element,"expanded");

		}
	}
}

//
// This function handle the search offers filter (carreer pages)
//
function search_offers_filter()
{
//	filtersListener();
	//getSelectedFilter();
}


//
// This function extend jquery and create
// check and uncheck methods
// (requires jquery lib)
//
jQuery.fn.extend({
   check: function() {
     return this.each(function() { this.checked = true; });
   },
   uncheck: function() {
     return this.each(function() { this.checked = false; });
   }
 });


//
// This function listen for change on filters
// (requires jquery lib)
//
function filtersListener()
{
	// Each time an input of the specified form is clicked
	// we update the filters
	$("#search_offers_filter_form input").click(function(){ updateFilters(); });
	
	// When the cancel filters links is clicked
	$("#filter_canceler").click(function()
	{
		// We update the contents of the filter summary
		resetFilters("filter_agence", "#agence_0", "#filters_agence_callback");
		resetFilters("filter_metier", "#pole_metier_0", "#filters_metier_callback");
		resetFilters("filter_contrat", "#contrat_0", "#filters_contrat_callback");
	});
}


//
// This function handle filters listener functions
// (requires jquery lib)
//

function updateFilters()
{
	updateFilterInputs("filter_agence", "#agence_0", "#filters_agence_callback");
	updateFilterInputs("filter_metier", "#pole_metier_0", "#filters_metier_callback");
	updateFilterInputs("filter_contrat", "#contrat_0", "#filters_contrat_callback");
	
	checkResultsNb();
}


//
// This function handle changes on filters
// (requires jquery lib)
//
function updateFilterInputs(filterInputName, firstInputID, tag2UpdateID)
{
	total_elements = $("input[@name=" + filterInputName + "]").length; // total elements number
	limit = $("input[@name=" + filterInputName + "][@checked]").length; // checked elements number
	
	// Case where all values are checked, except the first one (all)
	// Then, we uncheck them and just check the first
	if (limit == total_elements - 1 && $(firstInputID + "[@checked]").length == 0)
	{
		$("input[@name=" + filterInputName + "][@checked]").uncheck();
		$(firstInputID).check();
	}
	else
	{
		$("input[@name=" + filterInputName + "][@checked]").not($(firstInputID)).each(function(i)
		{
			// When values other than the first (all) are checked
			// the first element must be unchecked
			$(firstInputID).uncheck();
		});
		
		// Case where we check the first one (all)
		// then whe have to uncheck the others
		// and force the filter callback to be updated
		$(firstInputID).click(function(){ resetFilters(filterInputName, firstInputID, tag2UpdateID); });

	}

	// We update the contents of the filter summary
	updateFilterCallback(filterInputName, firstInputID, tag2UpdateID);
	
	// We update the datas
	updateFilteredDatasblock(filterInputName, firstInputID)
}


//
// This function updates the summary of selected the filters
// (requires jquery lib)
//
function updateFilterCallback(filterInputName, firstInputID, tag2UpdateID)
{
	var selection = "";
	limit = $("input[@name=" + filterInputName + "][@checked]").length;
	
	$("input[@name=" + filterInputName + "][@checked]").each(function(i)
	{
		// For the last element or when there's only one
		// checked element, we do not want a coma after it
		if (i==limit-1 || limit < 2) { selection += this.alt; }
		else { selection += this.alt + ", &nbsp;"; }
	});
	$(tag2UpdateID).html(selection);
}


//
// This function updates the filtered datas blocks
// (requires jquery lib)
//
function updateFilteredDatasblock(filterInputName, firstInputID)
{	
	// Case where "everything" is checked
	if ( $(firstInputID + "[@checked]").length == 1 )
	{
		// We display everything
		$("input[@name=" + filterInputName + "]").each(function(i){ $("." + this.id).show(); });
	}
	else
	{
		// We display everything that's checked
		$("input[@name=" + filterInputName + "][@checked]").each(function(i)
		{
			$(".search_offers_results " + "." + this.id).show("slow");
			
			// In addition of the show, we force the display to "block"
			// because jqery doesn't seem to manage show/hide playing on css "display"
			$(".search_offers_results " + "." + this.id).css("display", "block");
		});

		// Then we hide everything that's not checked
		$("input[@name=" + filterInputName + "]").not($("[@checked]")).each(function(i)
		{
			$(".search_offers_results " + "." + this.id).hide("slow");
			
			// In addition of the hide, we force the display to "block"
			// because jqery doesn't seem to manage show/hide playing on css "display"
			$(".search_offers_results " + "." + this.id).css("display", "none");
		});

	}
}


//
// This function resets the filters to default state
// (requires jquery lib)
//
function resetFilters(filterInputName, firstInputID, tag2UpdateID)
{

	$("input[@name=" + filterInputName + "]").uncheck();
	$(firstInputID).check();


	updateFilteredDatasblock(filterInputName, firstInputID);
	updateFilterCallback(filterInputName, firstInputID, tag2UpdateID);
}

// 
// this function check the number of results (remaining items
// after the filtering operation and insert a "no results" if
// it's there's no.
//
function checkResultsNb()
{
	$(".filtered_results").hide().html("");
	
	//  For each of the level 2 items (métiers)
	$(".search_offers_results .lv_02_item").each(function()
	{
		var count = 0;
		
		// For each item
		$(this).children(".item_block").each(function()
		{
			// if it's displayed, we increase a count
			if ( $(this).css("display") == "block" ) { count++; }
		});

		// At least, if there's less than one result displayed
		// we insert there "no results" message
		if ( count < 1)
		{
				$(this).children(".filtered_results").show().html("<em>Pas de r&eacute;sultats avec ce(s) type(s) de contrat !</em>");
		}
	});

}


//
// This function handles hidden contents
// on "Carrière Relations Ecoles" page
//
function SchoolsRelationItemsHandler()
{
	$(".rte_hidden_full_content").hide();
	
	$(".testimony_block a.read_more").click(function()
	{
		parent_item_id = this.parentNode.parentNode.id;

		if ( $("#" + parent_item_id + " .rte_hidden_full_content").css("display") == "block" ) { $(this).html("Lire"); }
		else { $(this).html("Replier"); }
		
		$("#" + parent_item_id + " .rte_hidden_full_content").slideToggle("slow");
	});
	
}