$rotatePartenaire = {
	context: false,
	timer: false,
	liste: new Array(),
	timeout: 6000,
	visible: 0,
	next: 0,
	height: 0,
	width: 0,

	init: function() {
		this.context = $("#jPartenaire");
		this.context.children("a").each(function() {
			$rotatePartenaire.liste.push($(this));
		});
		this.analyse();
		this.addCss();
		this.rotate();
	},
	
	addCss: function() {
		this.liste[this.visible].css("display", "block");
		this.context.css("height", this.height+"px");
		this.context.css("width", this.width+"px");
		/*for(elem in this.liste) {
			var h = $rotatePartenaire.height/2 - this.liste[elem].find('img').css("height").split("px")[0]/2;
			var w = $rotatePartenaire.width/2 - this.liste[elem].find('img').css("width").split("px")[0]/2;
			this.liste[elem].find('img').css("margin-top", h +"px")
			this.liste[elem].find('img').css("margin-left", w +"px")
		} */
	},
	
	analyse: function() {
		for(elem in this.liste) {
			var h = this.liste[elem].find('img').css("height").split("px")[0];
			var w = this.liste[elem].find('img').css("width").split("px")[0];
			if(h > this.height) {this.height = h;}
			if(w > this.width) {this.width = w;}
		}
	},
	
	chang: function() {
		if(this.visible < this.liste.length -1 ) {this.next = this.visible + 1;}
		else {this.next = 0}
		
		this.liste[this.visible].fadeOut(500, function() {$rotatePartenaire.liste[$rotatePartenaire.next].fadeIn(); $rotatePartenaire.visible = $rotatePartenaire.next;});
			
		
		$rotatePartenaire.rotate();
	},
	
	
	rotate: function() {
		this.timer = clearTimeout(this.timer);
		this.timer = setTimeout(function(){$rotatePartenaire.chang();}, this.timeout);
	}
	
	

}

//Concerne les images du header
var allimgloaded = 0;
var c = 0;
var t;
var timer_is_on = 0;
var start = 1;
var animation = false;

function timedCount(){
    if(c > 2) c = 0;
    c++;
    if(start){
        start = 0;
    } else {
        var title = $('#small' + c).attr("alt");
        $("#lightbox_title").delay(100).slideUp(500, function(){
            $(this).text(title);
            $("#big" + c).addClass('visiblenext').show();
            $('#big img.visible').fadeOut(1000, function(){
                $('#big img.visible').removeClass('visible');
                $("#big" + c).removeClass('visiblenext').addClass('visible');
                $("#lightbox_title").delay(200).slideDown(500);
            });
            $("#small img.selected").animate({opacity : 0.6}, 500).removeClass('selected').addClass('notselected');
            $('#small' + c).animate({opacity : 1}, 500).removeClass('notselected').addClass('selected');
        });

    }
    t = setTimeout("timedCount()",7000);
}
function doTimer(){
    if (!timer_is_on){
      timer_is_on = 1;
      timedCount();
    }
}

function startAnimation(){
    if(!animation){
        animation = true;
        $("#big img.hidden").removeClass('hidden');
        $("#lightbox_title").text($("#small img.selected").attr("alt")).delay(500).slideDown(500);

        doTimer();
    }
}

$(document).ready(function(){
	$rotatePartenaire.init();

    $('.chg_lng').click(function(){
        $('#hdLang').val($(this).attr('rel'));
        $('#change_lang').submit();
    });

    $("#big, #small").jImagePreloader({
        afterEnd: startAnimation
    });

    $('body').jImagePreloader({
        imageClass:'imgPreload'
    });

    try{
        $("a.lightbox").lightBox({
        	imageLoading: 'library/js/jquery-lightbox-0.5/images/lightbox-ico-loading.gif',
        	imageBtnClose: 'library/js/jquery-lightbox-0.5/images/lightbox-btn-close.gif',
        	imageBtnPrev: 'library/js/jquery-lightbox-0.5/images/lightbox-btn-prev.gif',
        	imageBtnNext: 'library/js/jquery-lightbox-0.5/images/lightbox-btn-next.gif',
            imageBlank:	'library/js/jquery-lightbox-0.5/images/lightbox-blank.gif'
        });
    } catch(e){}

    try{
        $('.jcarousel_auto').jCarouselLite({
            auto : 5000,
            speed : 1000,
            scroll : 1,
            visible : 3
        });
    } catch(e){}

    try{
        $('.bookmark').bookmark({
          sites : ['delicious', 'digg', 'linkedin', 'facebook', 'myspace', 'tumblr', 'twitter', 'yahoo', 'google'],
          hint : "Partager sur {s}"
        });
    } catch(e){}
});


