$(document).ready(function() {
	if($('#imgCaptcha').length > 0){
	   	img = document.getElementById('imgCaptcha'); 
   		img.src = '/podcast/create_image.php?' + Math.floor((new Date()).getTime() / 1000);
   		$('#imgCaptcha').click(function(){
		   	img = document.getElementById('imgCaptcha'); 
	   		img.src = '/podcast//create_image.php?' + Math.floor((new Date()).getTime() / 1000);  			
   		});		
	}
	$(".lireMedia").each(function(){
		if(!$(this).hasClass('mediaImage')){
			 playerID = $(this).parent().find('.playerMultimedia').attr('id');
			 $(this).colorbox({inline:true, href:"#"+playerID+""});		
		}else{
			$(this).colorbox({maxHeight: '90%', maxWidth: '90%'});	
		}
	});
	$('.curseurAudioVideo').each(function(){
			tokens = $(this).attr('id').split(';;;');
			afficherPlayer(tokens[0], tokens[1], tokens[2]);
	});
	$('.player').flowplayer("/podcast/flowplayer/flowplayer.unlimited-3.2.5.swf",
	{
		key: "#@3b0e919cd949f490055",
	    logo: {
	        url: "/podcast/images/FlowLogo.png",
	        fullscreenOnly: false,
	        displayTime: 10
	    },
		clip:  {
			autoPlay: false,
			autoBuffering: true,
			scaling:"fit"
		}
	});  
	$('.cbElement').colorbox({maxHeight: '90%', maxWidth: '90%'});	
	
	$('.openMedia').click(function(){
		theElementID = $(this).attr('id');
		tokens = theElementID.split('_');
		theEpisodeID = tokens[1]
		serverAddress = "/podcast/blog/counter.php";
		$.ajax({
		   type: "POST",
		   url: serverAddress,
		   data: "ajax=true&theEpisode=" + theEpisodeID,
		   success: function(msg){
		   }
		});

	});
	
	// Place ID's of all required fields here.
	required = ["comment_auteur", "comment_email", "commentairePourEpisode"];
	// If using an ID other than #email or #error then replace it here
	email = $("#comment_email");
	// The text to show up within a field when it is incorrect
	emptyerror = " veuillez remplir ce champs";
	emailerror = " Veuillez saisir un email valide";
	capttcha = $('#captchaVal');
	capttchaError = "Erreur de code de sécurité";
	$("#theCommentForm").submit(function(){
				data_string = "action=validate_captcha&captcha="+capttcha.val();
				$.ajax({
			   		type: 'POST',
			   		url: '/podcast/validate_captcha.php',
			  		data: data_string,
			  		async: false,
			   		success: function(msg){
		  	 			img = document.getElementById('imgCaptcha'); 
	   					img.src = '/podcast//create_image.php?' + Math.floor((new Date()).getTime() / 1000);
	   					if(msg != 'ok'){
	   						capttcha.addClass("needsfilled");
	   						capttcha.val(capttchaError);
	   					}		
			   		}	
			 	});

			//Validate required fields
			for (i=0;i<required.length;i++) {
				var input = $('#'+required[i]);
				if ((input.val() == "") || (input.val() == emptyerror)) {
					input.addClass("needsfilled");
					input.val(emptyerror);
				} else {
					input.removeClass("needsfilled");
				}
			}
			// Validate the e-mail.
			if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
				email.addClass("needsfilled");
				email.val(emailerror);
			}
	
			//if any inputs on the page have the class 'needsfilled' the form will not submit
			if ($(":input").hasClass("needsfilled")) {
				return false;
			} else {
				return true;
			}
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});	
	
});
function afficherPlayer(urlFichier, type, episodeID){
	serverAddress = "/podcast/validate_afficherPlayer.php";
	$.ajax({
	   type: "POST",
	   url: serverAddress,
	   data: "urlFichier=" + urlFichier + "&type=" + type + "&episodeID=" + episodeID,
	   success: function(msg){
	     //document.getElementById("playerMultimedia"+episodeID).innerHTML = msg;
	     $("#playerMultimedia"+episodeID).prepend(msg);
	   }
	});
}
