var rotation;
var top_actu = 0;
var timer_top_actu = new Array;

jQuery(document).ready(function () {

	if (!$j.browser.msie) {rotation = setTimeout("topactufirst(top_actu)", 100);}
else { rotation = setTimeout("topactunext(1)", 9000);}

    
	compteur = $j("#hp_pagination li").size();
	timer_top_actu[0] = "9000";
	for(i=1;i<compteur;i++) {
		timer_top_actu[i] = "7000";
	}
});

function topactufirst(index) {
   // $j("#hp_pagination li:eq(" + index + ")").addClass("current");
    rotation = setTimeout("topactunext(top_actu)", 7000);
   if((top_actu < 1 ) && (!$j.browser.msie))//CONDTION POUR EVITER LE DOUBLON D'ONGLET ACTIF AU CHARGEMENT DE LA PAGE
		{top_actu = top_actu + 1;}

}

function topactunext(index) {
	clearTimeout(rotation);
    var nbrli = $j("#hp_pagination li").size();
    $j("#hp_pagination li").removeClass("current");
    $j("#hp_topactus li:visible").fadeOut(300, function () {
        $j("#hp_topactus li:eq(" + index + ")").fadeIn(300);
    });
    $j("#hp_pagination li:eq(" + index + ")").addClass("current");
    if (top_actu == (nbrli)) {
		$j("#hp_topactus li:last").fadeOut(300, function () {
			$j("#hp_topactus li:first").fadeIn(300);
		});
		rotation = setTimeout("topactunext(top_actu)", timer_top_actu[top_actu]);
		top_actu = 0;
    	//rotation = setTimeout("topactunext(top_actu)", timer_top_actu[top_actu]);
    	//top_actu = 0
    } else {
		$j("#hp_topactus li:visible").fadeOut(300, function () {
			$j("#hp_topactus li:eq(" + index + ")").fadeIn(300);
		});
		rotation = setTimeout("topactunext(top_actu)", timer_top_actu[top_actu]);
		top_actu = top_actu + 1;
    	//rotation = setTimeout("topactunext(top_actu)", timer_top_actu[top_actu]);
    	//top_actu = top_actu + 1
    }
}

$j("#hp_pagination li").click(function () {
    clearTimeout(rotation);
    var a = $j("#hp_pagination li").index(this);
    top_actu = a;
    topactunext(top_actu);
});

$j("#hp_topactus img").click(function () {
    var xiti = $j("#hp_topactus img").index(this) + 1;
    xt_med("C", "23", "homepage::Top_actu::onglet_" + xiti + "", "N");
});

//xiti - 24/01/2012
// marquage sidebar
$j("#hp_sidebar .hp_bloc a").bind("click",function()
	{
	// on recupere l index du bloc parent clique
	var index_publi = $j(".hp_bloc").index($j(this).parents('.hp_bloc'))+1;
	console.log(index_publi);
	xt_med("C","23","homepage::totem::encart_"+index_publi+"","N");
	}
)

// marquage HP Z2 - produits grands
$j("#hp_z2 .hp_bloc_grand a").bind("click",function()
	{
	// on recupere l index du bloc parent clique
	var index_pdt = $j(".hp_bloc_grand").index($j(this).parents('.hp_bloc_grand'))+1;
	console.log(index_pdt);
	xt_med("C","23","homepage::top_produits::produit_"+index_pdt+"","N");
	}
)

// marquage HP Z2 - produits petits
$j("#hp_z2 .hp_bloc_petit a").bind("click",function()
	{
	// on recupere l index du bloc parent clique
	var index_pdt = $j(".hp_bloc_petit").index($j(this).parents('.hp_bloc_petit'))+3;
	console.log(index_pdt);
	xt_med("C","23","homepage::top_produits::produit_"+index_pdt+"","N");
	}
)

// marquage HP Z3 - bloc
$j("#hp_z3 .hp_bloc a").bind("click",function()
	{
	// on recupere l index du bloc parent clique
	var index_bloc = $j("#hp_z3 .hp_bloc").index($j(this).parents('.hp_bloc'))+1;
	console.log(index_bloc);
	xt_med("C","23","homepage::selection_famille::selection_"+index_bloc+"","N");
	}
)

// marquage HP Z4 - bloc
$j("#hp_z4 .hp_bloc a").bind("click",function()
	{
	// on recupere l index du bloc parent clique
	var index_bloc = $j("#hp_z4 .hp_bloc").index($j(this).parents('.hp_bloc'))+7;
	console.log(index_bloc);
	xt_med("C","23","homepage::selection_famille::selection_"+index_bloc+"","N");
	}
)

// marquage HP Z5 - bloc
$j("#hp_z5 .hp_bloc a").bind("click",function()
	{
	// on recupere l index du bloc parent clique
	var nom_service = $j(this).parents('.hp_bloc').attr("id").substr(2);
	console.log(nom_service);
	xt_med("C","23","homepage::services_boulanger::"+nom_service+"","N");
	}
)


$j("#hp_pagination li:not(:first)").css("margin-left", "0");
$j("#hp_pagination li:first").addClass("current");
$j("#hp_topactus li:first").show();
