var obj = null;
var objActive = null;
var t = null;
var tActive = null;

function cacher(obj){
    obj.removeClass("on");
    obj.children('ul').hide();
    $("#menu").find('li.active > ul').show();
}

function afficheActive(obj){
    obj.show();
}

$(document).ready(function(){

    $('#leaderboard_pics').cycle({
        fx: 'fade',
        timeout: 5000,
        startingSlide: 0,
        pause: 1,
        pager: '#leaderboard_nav_selec',
        before: showTitle,
        pagerAnchorBuilder: pagerFactory
    });
    $('#certifications').cycle({
        fx:'fade',
        timeout: 5000,
        startindSlide: 0,
        pause:1,
        pager: '#certifications_nav',
        pagerAnchorBuilder: certifPager
    });
    
    function certifPager(idx,slide){
        return '<a href="#" class="certification_nav_element"></a>';
    }
	
    function pagerFactory(idx, slide) {
        return '<a href="#" class="leaderboard_nav_element"></a>';
    }
	
    function showTitle(currSlideElement, nextSlideElement, options, forwardFlag){
        var legende = nextSlideElement.firstChild.title+ "<span class='auteur_legende'>"+nextSlideElement.firstChild.alt+"</span>";
        jQuery('#legende').html(legende);
    }
	
    if($("#leaderboard_pics > a").size()==1){
        var legende = $("#leaderboard_pics > a > img").attr("title") + "<span class='auteur_legende'>"+$("#leaderboard_pics > a > img").attr("alt")+"</span>";
        jQuery('#legende').html(legende);
    }

    $("#menu").find('li.active > ul').show();

    $('#menu > ul > li').hover(function() {
			
        $('#menu > ul > li').each(function(){
            $(this).removeClass("on");
        });
        $('#menu > ul > li > ul ').each(function(){
            $(this).hide();
        });
        $("#menu").find('li.active > ul').hide();
        $(this).addClass("on");
        $(this).children('ul').show();
        clearTimeout(t);
        clearTimeout(tActive);
    }, function() {
        obj = $(this);
        t = setTimeout('cacher(obj);',2000);	
    });	
	
    $("table").each(function(){
        $(this).wrap('<div class="table_surround">');
        $(this).wrap('<div class="fond_gris_middle">');
        $(this).wrap('<div class="fond_gris">');
        $('<div class="fond_gris_top"></div>').insertBefore($(this).parent().parent());
        $('<div class="fond_gris_bottom"></div>').insertAfter($(this).parent().parent());
    });
	
    $("table:even tr:even").addClass("dark");
    $("table:even tr:odd").addClass("light");
    $("table tr.light th:nth-child(1)").addClass("first");
    //$("table tr td:nth-child(1)").addClass("first_col");	
    $('table').attr("cellspacing", 2);
	
    $("a[rel^='prettyPhoto']").prettyPhoto();
	
    $('.open_collapse').each(function(){
        if(this.tagName=="LI"){
            $(this).append('<em> - More </em>');
            $(this).children().each(function(i){
                if(this.tagName!="EM"){
                    $(this).css('display','none');
                }
            });
			
        }else{
            $(this).next("div.object-left").css('display','none');
            $(this).next("p").css('display','none');
            $(this).next("div.object-left").next("p").css('display','none');
            var ajout = '<em> - More </em>';
            var titre_original = $(this).text();
            $(this).html(titre_original+ajout);
        }
    });
					
    $('.open_collapse').toggle(function(){
        if(this.tagName=="LI"){
            $(this).children().each(function() {
                if(this.tagName=="EM"){
                    $(this).css('display','none');
                }else{
                    $(this).slideDown('slow');
                }
            });
        }else{
            $(this).next("div").slideDown('slow');
            $(this).next("p").slideDown('slow');
			
            $(this).next("div").next("p").slideDown('slow');
        }	
    },function(){
        if(this.tagName=="LI"){
            $(this).children().each(function() {
                if(this.tagName=="EM"){
                    $(this).css('display','inline');
                }else{
                    $(this).slideUp('slow');
                }
            });
        }else{
            $(this).next("div").slideUp('slow');
            $(this).next("p").slideUp('slow');
			
            $(this).next("div").next("p").slideUp('slow');
        }	
    });
    if(getCookie('splashDone')==null){
        $.blockUI({message : $('#splash'), css : {width:'80%', top:'10%',left:'10%' , border:'none', margin:'0'} });
       
        $('#closeBtn').click(function(){
            setCookie('splashDone','done','/')
            $.unblockUI();
        })
    }
});
function setCookie ( name, value, path) {
         szCookie = name + "=" + escape (value) +
                  ((path) ? "; path=" + path : "");
                  document.cookie = szCookie;
}

function getCookie (name) {
         if ( document.cookie) { // Le cookie est-il valide ?
                  index = document.cookie.indexOf( name);
                  if ( index != -1) {
                           nDeb = (document.cookie.indexOf( "=", index) + 1);
                           nFin = document.cookie.indexOf( ";", index);
                           if (nFin == -1) {nFin = document.cookie.length;}
                           return unescape(document.cookie.substring(nDeb, nFin));
                  }
         }
         return null;
}
