var ie7 = ($.browser.msie && $.browser.version == 7) ? true : false;
var ie6 = ($.browser.msie && $.browser.version == 6) ? true : false;

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

function initScripts()
{
	//$('p.tempmessage').animate({'color': '#616161'}, 1000);
	//$('input:text:first').focus();
	initHovers();
	initShuffleWords();
	initTogglers();
	initOffresSelections();
	initRecherche();
}

function initHovers()
{
	if (ie7 || ie6)
	{
		//hideous hack for the this glorious IE browser
		$('#menu ul li a span').animate({'opacity': 0}, 0);
		$('#backhome').animate({'opacity': 0}, 0);
		$('.espaceheader').animate({'opacity': 0}, 0);
	}

	//because of IE7 png display sucking
	//logo
	$('#logoandcatchline').hover(
		function () {
			if (!ie7) { $('#logo', this).stop().animate({'opacity': 0.5}); }
			$('#backhome', this).stop().animate({'opacity': 1});
		},
		function () {
			if (!ie7) { $('#logo', this).stop().animate({'opacity': 1}); }
			$('#backhome', this).stop().animate({'opacity': 0});
		}
	);
	
	$('#splash').hover(
		function () {
			$(this).stop().animate({'opacity': 0.8});
		},
		function () {
			$(this).stop().animate({'opacity': 1});
		}
	);

/*
	$('.slidingtoggler').hover(
		function () {
			$(this).stop().animate({'color': '#4949492589af'});
		},
		function () {
			$(this).stop().animate({'color': '#2589af'});
		}
	);
*/

	$('.espaceheader').hover(
		function () {
			$(this).stop().animate({'opacity': 1});
		},
		function () {
			$(this).stop().animate({'opacity': 0});
		}
	);

	//menu dans le header
	$('#headermenu li a').hover(
		function () {
			$(this).stop().animate({'opacity': 0});
		},
		function () {
			$(this).stop().animate({'opacity': 1});
		}
	);

	//menu principal
	$('#menu ul li a:not(.selected) span').hover(
		function () {
			$(this).stop().animate({'opacity': 1});
			$(this).parent().stop().animate({'color': 'white'});
		},
		function () {
		}
	);

	$('#menu ul li a:not(.selected)').hover(
		function () {
		},
		function () {
			$(this).children("span").stop().animate({'opacity': 0});
			$(this).stop().animate({'color': '#686868'});
		}
	);
	
	//menus de la home
	$('.espace ul li a').hover(
		function () {
			$(this).stop().animate({'color': '#FFFFFF'});
		},
		function () {
			$(this).stop().animate({'color': '#494949'});
		}
	);
	
}

function initTogglers()
{
	/*
	if (typeof(entreprisesMethodes) != "undefined")
	{
		var anchor = location.hash;
		$("a[href='" + anchor + "']").parent().parent().children().toggle();
	}
*/
	$('.toggler').click(function() { $(this).children().toggle(); return false; });
	$('.slidingtoggler').click(function() {
		$('.slidingtoggler').next().next().slideUp("slow");
		$('.slidingtoggler').next().next().next().next().slideUp("slow");
		$(this).next().next().stop().slideToggle("slow");
		$(this).next().next().next().next().stop().slideToggle("slow");
		return false;
	});
}

function initShuffleWords()
{
	if ($('.shufflewords p').show().css({'opacity': 0}).size() && !ie6)
	{
		shuffleWordsOn();
	}
	
	if (ie6)
	{
		$('.shufflewords p').show();
	}
}

function shuffleWordsOff()
{
	$('.shufflewords p').animate({'opacity': 0}, 300);
	setTimeout('shuffleWordsOn()', 300);
}

function shuffleWordsOn()
{
	$('.shufflewords').each(function()
	{
		$('p:random', this).css({'margin-bottom': Math.ceil(100*Math.random())}).css({'margin-left': (-70 + Math.ceil(50*Math.random()))}).animate({'opacity': 1}, 1000);
	});

	setTimeout('shuffleWordsOff()', 3000);
}

function initOffresSelections()
{
	if ($('#offresselection > div > div').size() > 5)
	{
		var i = 6;
		while (typeof($('#offresselection > div > div')[i]) != "undefined")
		{
			$('#offresselection > div > div').eq(i).hide();
			i++;
		}

		//on définit la taille du container en la calculant
		$('#offresselection').height($('#offresselection').height());

		setTimeout('switchOffresSelections(5)', 2000);
	}
}

function switchOffresSelections(firstElement)
{
	//if the first given element doesnt exist, we start over
	if (typeof($('#offresselection > div > div')[firstElement]) == "undefined")
	{
		firstElement = 0;
	}
	
 	var tempHeight = $('#offresselection > div').height();
	
	var currentElement = firstElement;

	//we hide all the contained div
	$('#offresselection > div > div').fadeOut(300);
	
	//after on second : we reset all borders, we strip the first element's border
	setTimeout("$('#offresselection > div > div').css({'borderTop': '1px solid #686868'}); $('#offresselection > div > div').eq(" + firstElement + ").css({'borderTop': 0});", 300);

	//after one second : we display the 5 next tr
	while (typeof($('#offresselection > div > div')[currentElement]) != "undefined" && currentElement < firstElement + 5)
	{
		setTimeout("$('#offresselection > div > div').eq(" + currentElement + ").fadeIn()", 300);
		currentElement++;
	}
	
	//after 2 seconds, we animate the height of the container
	setTimeout("$('#offresselection').animate({ 'height': $('#offresselection > div').height() }, 300)",  600);

	//and we set the animation to start again in 3 second
	setTimeout('switchOffresSelections(' + currentElement + ')', 8000);
}

function initRecherche()
{
	//setting the keyup function to hide tr from table where the search elements aren't found and show the other
	$('#recherche').keyup(function()
	{
		$('#searchwait').stop().animate({'opacity': 1}, 0);
		$('#recherchecompetences').val('');
		setTimeout('doSearch("metadata", "recherche")', 10);
	});

	$('#recherchecompetences').keyup(function()
	{
		$('#searchwait').stop().animate({'opacity': 1}, 0);
		$('#recherche').val('');
		setTimeout('doSearch("metadatacompetences", "recherchecompetences")', 10);
	});


	$('#reset').click(function()
	{
		//triggering the search for when the user resets the search
		$('#recherche').val('');
		$('#recherche').keyup();
	});
	
	//triggering the search for when the page loads and the search query is already in place
	$('#recherche').keyup();
}

function doSearch(searchBoxClassName, searchFieldId)
{
	if ($('#' + searchFieldId).val() != '')
	{
		var keywords = $('#' + searchFieldId).val().toLowerCase().split(' ');
		
		$('div.' + searchBoxClassName).each(function()
		{
			var currentMetadata = $(this);
			foundAllKeywordsInCurrentCell = 1;

			//we check all the keywords
			$.each(keywords, function(i, n)
			{
 				//we look in the metadata cell
				if (currentMetadata.html().toLowerCase().indexOf(n) == -1)
				{
					foundAllKeywordsInCurrentCell = 0;
				}
			});
			
			//if the  search gave results on the first tr, we keep it
			if (foundAllKeywordsInCurrentCell)
			{
				currentMetadata.parent().parent().show();
				currentMetadata.parent().parent().next().not('.hidden').show();
			}
			else
			{
				currentMetadata.parent().parent().hide();
				currentMetadata.parent().parent().next().hide();
			}
		});
	}
	else
	{
		$('tr').not('.hidden').show();
	}

	$('#searchwait').stop().animate({'opacity': 0}, 500);
}