/* Google Search */
if(window.location.href.indexOf('/admin/') == -1)
{
    google.load("search", "1");

    function OnLoad()
    {
    	// Create a search control
        var searchControl = new google.search.SearchControl();
        searchControl.setOnKeepCallback(this, CleanPage);

        // Add in a full set of searchers
        var options = new google.search.SearcherOptions();
        
        // Display results target :
        options.setRoot(document.getElementById("searchresult"));

        // Search restriction
        searchSite = new google.search.WebSearch();
        searchSite.setSiteRestriction("ihecs.be");
       
        // Clean content
        searchControl.addSearcher(searchSite, options);
        searchControl.setSearchCompleteCallback(this, CleanPage);

        // Tell the searcher to draw itself and tell it where to attach
        searchControl.draw(document.getElementById("searchcontrol"));
    }
    google.setOnLoadCallback(OnLoad);
}

/* Nettoyage de la page pour affichage du résultat de la recherche */
function CleanPage(sc, result)
{
	if($('div.sublevel:visible'))
	{
		// Fermeture du menu si actif
		$('div.sublevel:visible').slideUp('normal');
		$('a.open').removeClass('open'); 
		$('a.selected').removeClass('selected');
		if($('li.active')) $('li.active').removeClass('active');
	}
	
	$('#breadcrumbs').empty().append('<p>Vous êtes ici: <a href="/">Accueil</a> - Recherche</p>');
	l = result.results.length;
	
	var id = $('#home-inner').length > 0 ? '#home-inner' : '#text-inner';
	
	$(id).empty();
	$(id).append('<h1>Résultat de votre recherche</h1>');

    for(i=0; i<l; i++){
        $(id).append(result.results[i].html);
    }
	
	if(id == '#home-inner'){
		$('#home-inner').addClass('frame boxshadow searchresulthome');
	}
}

/* Menu Accordéon */
function initMenu()
{
	$('.menu ul').hide();
	$('.menu .active a').addClass('open');
	$('.active ul').show();
	
	$('.menu .parent').click(function(e)
	{
		e.preventDefault();
		$('.menu ul:visible').slideUp('normal');
		$('a.open').removeClass('open');
		
		if($(this).next().is('ul') && !$(this).next().is(':visible'))
		{
			$(this).next('ul').slideDown('normal');
			$(this).addClass('open');
			
			$('a.open').css({'cursor':'default'});
		}
	});
}

$(document).ready(function()
{
	/* Init Clearbox */
	$("a.clearbox").each(function(i){
		var href = $(this).attr("href");
		$("a.clearbox").eq(i).attr("href", href.replace("#","/contents/index/index/id/"));
		
		if(!$("a.clearbox").eq(i).attr('rel') || $("a.clearbox").eq(i).attr('rel') == '')
		{
			$("a.clearbox").eq(i).attr("rel", "clearbox[width=600,,height=280]");
		}
	});
	
	/* Init menu accordéon */
	initMenu();
	
	/* Init tabs */
	$(function() {
		$(".tabs").tabs();
	});
	
	/* Init list */
	$('#list').listnav();		
	$('#liste').append($('#personnel'));
	
	/* Actu slide */
	if($('#new-inner').length > 0)
	{
		// number of items
		var num = $('.actu').length;
		
		// reference values
		var step = 4 * ($('.actu').innerHeight());
		var roll = $('#roll').height();
		var ref = parseInt(-(roll - step));
		
		$('#news .up').bind('click', function(e){
			e.preventDefault();
			
			if($('#roll').position().top == 0)
			{
				return false;
			}
			
			$('#roll').animate({top: '+=' + step}, 500);
		});
		
		$('#news .down').bind('click', function(e){
			e.preventDefault();
			
			if($('#roll').position().top <= ref)
			{
				return false;
			}
			
			$('#roll').animate({top: '-=' + step}, 500);
		});
	}
});
