$(function(){
	// menu omplanete
    $("#menu > li:has('ul')").not($('#registermenu')).each(function() {
        $(this).find("a:first").append('<img src="/images/design/v2011/empty.gif" width="11" height="5" alt="" class="arrow-down" />');
    });
	$("#membermenu").click(function(){
		$("#registerslide").slideToggle("slow");
	});
	$("#registermenu, #register_bouton").click(function(){
		$("#registerslide").slideToggle("slow");
		$('input[name="req_username"]').focus();
	});

	// slide à la une - haut page
	if($('#slide').length){  
		if(/iPhone|iPod|iPad|Android|BlackBerry/i.test(navigator.userAgent)){
			var question = confirm("Voulez-vous consulter la version SmartPhone de ce site ?")
			if(question){
				window.location = "http://mobile.omplanete.com/";
			}
		}

		var imageWidth =598;
		var imageSum = 4;
		var imageReelWidth = imageWidth * imageSum;
		$("#slide_content_div").css({'width' : imageReelWidth});
		
		//Paging + Slider Function
		rotate = function(){	
			var triggerID = $active.attr("rel") - 1; //Get number of times to slide
			var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
	
			$("#slide_paging a").removeClass('active'); //Remove all active class
			$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
			
			//Slider Animation
			$("#slide_content_div").animate({ 
				left: -image_reelPosition
			}, 800 );
			
		}; 
		
		//Rotation + Timing Event
		rotateSwitch = function(){		
			play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
				$active = $('#slide_paging a.active').next();
				if ( $active.length === 0) { //If paging reaches the end...
					$active = $('#slide_paging a:first'); //go back to first
				}
				rotate(); //Trigger the paging and slider function
			}, 4500); //Timer speed in milliseconds (3 seconds)
		};
		
		rotateSwitch(); //Run function on launch
		
		//On Hover
		$("#slide_content_div a").hover(function() {
			clearInterval(play); //Stop the rotation
		}, function() {
			rotateSwitch(); //Resume rotation
		});	
		
		//On Click
		$("#slide_paging a").click(function() {	
			$active = $(this); //Activate the clicked paging
			//Reset Timer
			clearInterval(play); //Stop the rotation
			rotate(); //Trigger rotation immediately
			rotateSwitch(); // Resume rotation
			return false; //Prevent browser jump to link anchor
		});	
	}

	// box news
	if($('#boxnews').length){  
		var ongletpagenav=$("#ongletpage ul li.nav");
		//$("#boxonglet .content:first").show();
		//$("#ongletpage ul li.nav:first").addClass("active").show();
		ongletpagenav.click(function(){
			ongletpagenav.removeClass("active");
			$(this).addClass("active");
			$("#boxonglet .content").hide();
			var activeTab = $(this).find("a").attr("href");
			$(activeTab).fadeIn(1000);
			return false;
		});
	}
	
	//$("#boxnews ul li, .box250 ul li").not($('.box250 .titretop ul li')).hover(function(){
	$(".box250 ul li").not($('.box250 .titretop ul li, #boxtabs li')).hover(function(){
		$(this).addClass("rollover");
	}, 
	function(){
		$(this).removeClass("rollover");
	});
	
	// social network compatible W3c
	if($('#gplusone').length){  
		var gplusone = document.getElementById("gplusone");
		gplusone.setAttribute("data-size", "tall"); // laisser tel quel sinon ca ne fonctionne pas avec IE
	}
	if($('#TwitterShareButton').length){  
		
	}
	
	
	
	// fancy input focus
	if($('.fancyinput').length){
		var fancyinput=$(".fancyinput");
		fancyinput.focus(function() {
			$(this).addClass("active");
		});
		
		fancyinput.blur(function() {
			$(this).removeClass("active");
		});	
	}

	// box match
	if($('#boxmatch').length){
		var count_match=$('#boxmatch div.match').length;
		var affiche_match=$('#boxmatch div.match');
		var title_boxmatch=new Array('DERNIER MATCH', 'A L\'AFFICHE', 'MATCH A VENIR');
		$("#boxmatch .boxnavnext").click(function(){
			var index_match=affiche_match.index($('#boxmatch div.active'));
			affiche_match.hide().removeClass('active');
			if(index_match<(count_match-1)){
				affiche_match.eq(index_match).next('#boxmatch div.match').fadeIn(700).addClass("active");
			}else{
				index_match=-1;
				affiche_match.eq(0).fadeIn(700).addClass("active");
			}
			$("#boxmatch div.titretop a:first").text(title_boxmatch[index_match+1]);
			return false;
		});
		$("#boxmatch .boxnavprev").click(function(){
			var index_match=affiche_match.index($('#boxmatch div.active'));
			affiche_match.hide().removeClass('active');
			if(index_match<=0){
				index_match=count_match;
				affiche_match.eq(count_match-1).fadeIn(700).addClass("active");
			}else{
				affiche_match.eq(index_match).prev('#boxmatch div.match').fadeIn(700).addClass("active");
			}
			$("#boxmatch div.titretop a:first").text(title_boxmatch[index_match-1]);
			return false;
		});
	}

	// carousel à la une - milieu page
	if($('#carousel').length){  
		$('#carousel_slide li div.t, #carousel_nav_prev, #carousel_nav_next').hover(function(){
			$(this).addClass('active');
		}, function(){
			$(this).removeClass('active');
		});
		var item_width=$('#carousel_slide li').outerWidth();
		var left_value=item_width * (-1); 
		$('#carousel_slide li:first').before($('#carousel_slide li:last'));
		$('#carousel_slide ul').css({'left' : left_value});
		$('#carousel_nav_prev').click(function(){
			var left_indent=parseInt($('#carousel_slide ul').css('left')) + item_width;
			$('#carousel_slide ul').animate({'left' : left_indent}, 400,function(){    
				$('#carousel_slide li:first').before($('#carousel_slide li:last'));           
				$('#carousel_slide ul').css({'left' : left_value});
			});
			return false;
		});
		$('#carousel_nav_next').click(function(){
			var left_indent=parseInt($('#carousel_slide ul').css('left')) - item_width;
			$('#carousel_slide ul').animate({'left' : left_indent}, 400, function(){
				$('#carousel_slide li:last').after($('#carousel_slide li:first'));                 	
				$('#carousel_slide ul').css({'left' : left_value});
			});
			return false;
		});
	}
		 
	// boite de vote
	$(".do_vote").live("click", function(){
		var cible=$(this).children('div');
		$.ajax({
			type: "GET",
			url: "/services/rate.php?theme=" + $("#themevote").val() + "&id=" + cible.attr('id').substring(5),
			dataType : "html",
			error:function(msg, string){
				alert('Erreur');
			},
			success:function(data){
				cible.parent("div").removeClass("do_vote").addClass("done_vote");
				cible.html(data);
			}
		});
	});
	
		 
	// Articles les plus lus à la UNE
	if($('#tabbox').length>0){
		var tabboxli=$("#tabbox ul li");
		$("#tabbox DIV:first").show();
		tabboxli.click(function() {
			tabboxli.removeClass("active");
			$(this).addClass("active");
			$("#tabbox .tabboxcontent").hide();
			var activeTab = $(this).find("a").attr("href");
			$(activeTab).fadeIn(1000);
			return false;
		});
	}
	
		 
	// Les sondages
	if($('#boxsondage').length>0){
		$.getScript('http://www.omplanete.com/js/jquery.cookie.js',function(){ 
			var cookie=$.cookie('ompsondage' + $("#sondagefield").val());
			if(cookie){
				$("#sondagestats").show();
				$("#sondageform").hide();
			}else{
				$("#sondagestats").hide();
				$("#sondageform").show();
			}
		});
		$('#sondageform input[type=radio]').click(function(){
			$.ajax({
				type: "GET",
				url: "/services/rate.php",
				data: "id=" + $("#sondagefield").val() + "&theme=2&reponse=" + $(this).val() + "",
				error:function(msg, string){
					alert('Erreur sondage ! Contactez l\'administrateur !');
				},
				success:function(data){
					$("#sondagestats").show();
					$("#sondageform").hide();
				}
			});
		});
	}
	/*if($('#punwrap').length>0){
		function replace_tags(str){
			str1=str.substr(0, (str.length)-1);
			str2=str1.replace(' ', '+');
			return '<a href="http://www.omplanete.com/search.php?search=' + str2 + '&submitsearch=Rechercher" rel="external" class="taglink">' + str1 + '<\/a> ';
		};
		$.getScript('http://www.omplanete.com/js/jquery.ba-replacetext.min.js',function(){ 
			$("#punwrap .postright *").not($('#tableau_pubforum *')).replaceText( /niang |ben arfa |deschamps |diouf |tapie |aulas |thiriez |mbia |diawara |cheyrou |taiwo |bonnart |heinze |mandanda |gignac |gilardino |forlan |zidane |hidalgo |dassier |margarita |azpilicueta |gignac |remy |fanni |ayew | brandao /gi, replace_tags);
		});
	}*/
	
	// liste saison dans page index
	$("#title_listbox_div").each(function(){
        $(this).find("h2").append('<img src="/images/design/v2011/empty.gif" width="11" height="5" alt="" />');
    });

	$('.title_background h2').click(function(){
        $('#title_listbox').toggle();
    });

	if($('input[name="Tcheckall"]').length>0){
		$('input[name="Tcheckall"]').click(function(){
			$('input[type="checkbox"]').attr('checked', true);								
			return false;
		});
	}
	
	if($('#countdown').length>0){
		var count=5;
		var message='';
		countdown=setInterval(function(){
			if(count>1){							 
				message='dans ' + count + ' secondes';
			}else if(count<0){
				message='immédiatement';
			}else{
				message='dans ' + count + ' seconde';
			}
			$("#countdown span").html(message);
			if(count==1){
				window.location=$("#countdown a").attr('href');
			}
			count--;
		}, 1000);
	}
	
	$("#article a.comment_register").click(function(){
		$(this).parent().parent().next().show();
		return false;
	});
	
	$('a[rel~=external]').attr('target','_blank');

	if($('table.colalt').length){
		$('table.colalt tr:even').not('tr.entete').addClass('alterne');
	}



});
