var site_url = 'http://www.jolatefri.com/';
function getXhr(){
    var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest non support√© par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   xhr = false; 
	} 
    return xhr
}
function go(c){
	if(!c.data.replace(/\s/g,''))
		c.parentNode.removeChild(c);
}
function clean(d){
	var bal=d.getElementsByTagName('*');

	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3)
			go(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			go(b);
	}
	return d;
}
function envoyerCommentaire(idVideo, pseudo){
	var xhr = getXhr()
	// On d√©fini ce qu'on va faire quand on aura la r√©ponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout re√ßu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			
			c=document.getElementById('listCommentaires');

			li=document.createElement('li');
			li.setAttribute('class', 'commentaire displayNone');
			li.innerHTML = xhr.responseText;
			c.appendChild(li);
			textarea=document.getElementById('messageCommentaire');
			textarea.value="";
			$(".commentaire").fadeIn("100");
		}
	}
	xhr.open("POST",site_url+'pages/addCommentaire.php',true); 
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	idUser = document.getElementById('idUser').value;
	message = document.getElementById('messageCommentaire').value;
	titreVideo = document.getElementById('titreVideo').value;
	xhr.send("message="+message+"&idVideo="+idVideo+"&titreVideo="+titreVideo+"&idUser="+idUser); 
}

$(document).ready(function()
{
	$("span.onLike").mouseover(function ()
	{
		$(this).addClass("overLike");
	});

	$("span.onLike").mouseout(function ()
	{
		$(this).removeClass("overLike");
	});
	});
	
	// lightbox
	$(function closeLightbox() {
		$(".closeLightbox").click(function() 
		{
			
			$("#fondLightbox").fadeOut(100);
			$("#lightbox").fadeOut(100, function()
			{
				$("#lightbox div").html('');
			});
			return false;
		});
	});
	
	// Inscription
	$(function() {
		$("#signin").click(function() 
		{
			var content = $(this).attr("id");
			var dataString = 'content='+content ;
			var scrollPosition = document.documentElement.scrollTop;
			var marginTop = scrollPosition + 40;
			$("#lightbox").css('margin-top', marginTop + 'px');
			$.ajax({
				type: "POST",
				url: site_url+"pages/contentLightbox.php",
				data: dataString,
				cache: false,
				success: function(html)
				{
					$("#lightbox div").html(html);
					$("#fondLightbox").fadeIn(100);
				$("#lightbox").fadeIn(100);
				}
			});
			return false;
		});
	});
	
	//login
	$(function() {
		$("a#login").click(function() 
		{
			var content = $(this).attr("id");
			var dataString = 'content='+content ;
			var scrollPosition = document.documentElement.scrollTop;
			var marginTop = scrollPosition + 40;
			$("#lightbox").css('margin-top', marginTop + 'px');
			$.ajax({
				type: "POST",
				url: site_url+"pages/contentLightbox.php",
				data: dataString,
				cache: false,
				success: function(html)
				{
					$("#lightbox div").html(html);
					$("#fondLightbox").fadeIn(100);
				$("#lightbox").fadeIn(100);
				}
			});
			return false;
		});
	});
	
	//Show The Love
	$(function() {
		$(".love").click(function() 
		{
			if( $(this).attr("class") == 'love videoLove' )
			{
				var referer = 0;
			}
			else
			{
				var referer = 1
			}
			var id = $(this).attr("id");
			var dataString = 'id='+ id+'&referer='+referer ;
			var parent = $(this);
			$(this).fadeOut(100);
			$.ajax({
			type: "POST",
			url: site_url+"pages/videoLike.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				parent.html(html);
				parent.fadeIn(300);
			} 
		});
		return false;
		});
	});
	
	// abus vid√©o
	$(function() {
		$(".abus").click(function() 
		{
			var id = $(this).attr("id");
			var dataString = 'id='+ id ;
			var parent = $(this);
			parent.fadeOut(300, function() {
				$.ajax({
				type: "POST",
				url: site_url+"pages/abusVideo.php",
				data: dataString,
				cache: false,
				success: function(html)
				{
					parent.html(html);
					parent.fadeIn(300);
				} 
			});
			return false;
				});
	});
	
	// favoris
	$(function() {
		$(".favori").click(function() 
		{
			var id = $(this).attr("id");
			var dataString = 'id='+ id ;
			var parent = $(this);
			$(this).fadeOut(300);
			$.ajax({
			type: "POST",
			url: site_url+"pages/favori.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				parent.html(html);
				parent.fadeIn(300);
			} 
		});
		return false;
	});
	});
	
	// delete video
	$(function() {
		$("a.deleteVideo").click(function() 
		{
			if (confirm("Supprimer cette vid√©o ?")) { // Clic sur OK
				var idVideo = $(this).attr("id");
				var dataString = "idVideo="+idVideo ;
				$.ajax({ 
				   type: "POST", 
				   url: site_url+"pages/deletePost.php", 
				   data: dataString, 
				   success: function(msg){ 
				     alert( msg );
				 	document.location.href = site_url;
				
				   } 
				 });
			}
		});
	});	
	
	// activer vid√©o
	$(function() {
		$("a.activerVideo").click(function() 
		{
			if (confirm("Confirmer")) { // Clic sur OK
				var referer = window.location.pathname;
				var idVideo = $(this).attr("id");
				var dataString = "idVideo="+idVideo+"&referer="+referer ;
				$.ajax({ 
				   type: "POST", 
				   url: site_url+"pages/activerVideo.php", 
				   data: dataString, 
				   success: function(msg){ 
				     alert( msg );
				 	document.location.href = site_url;
				   } 
				 });
			}
		});
	});	
		//Lors de la prise de focus  
	$('.clear').focus(function(){  

		//Initialisation de l'objet  
		var obj = $(this);  

		//On stock la valeur actuelle de l'input  
		var tmp_value = $(this).val();  

		//Creation d'un attribut rel qui va stocker notre valeur initiale  
		if(obj.attr('rel') == undefined){  
			obj.attr('rel',obj.val());  
		}  

		//Si la valeur de notre input est √©gale √† notre valeur stock√©e dans l'attribut rel,  
		//Nous effacons la valeur de notre input  
		if(obj.val() == obj.attr('rel')){  
			obj.val('');  
		}  
	});  

		//Lors de la perte de focus  
	$('.clear').blur(function(){  
		//Initialisation de l'objet  
		var obj = $(this);  

		//Initialise la valeur initiale  
		var initial_value = obj.attr('rel');      

		//Si la valeur de notre input est null, on affiche la valeur initiale  
		if(obj.val() == ""){  
			obj.val(initial_value);  
		}  
	});
		
	// menu profile
	$(function() {
		$("#menuUserVideo").click(function() 
		{
			$("#modifProfil").fadeOut("50", function() 
			{
				$("#favoriVideo").fadeOut("50", function() 
				{
					$('#userVideo').fadeIn("50");
				});
			});
		});
		$("#menuModifProfil").click(function() 
		{
			$("#userVideo").fadeOut("50", function() 
			{
				$("#favoriVideo").fadeOut("50", function() 
				{
					$('#modifProfil').fadeIn("50");
				});
			});
		});
		$("#menuFavoriVideo").click(function() 
		{
			$("#modifProfil").fadeOut("50", function() 
			{
				$("#userVideo").fadeOut("50", function() 
				{
					$('#favoriVideo').fadeIn("50");
				});
			});
		});
		return false;
	});
});

// chat
function chat()
{
	var referer = window.location.pathname;
	var dataString = 'referer='+ referer ;
	var parent = $('#chatContent');
	$.ajax({
	type: "POST",
	url: site_url+"pages/chatContent.php",
	data: dataString,
	cache: false,
	success: function(html)
	{
		parent.html(html);
	} 
});
}

// envoyer le message
$(function() {
	$('#formChat').submit(function() {
		var pseudo = $('#pseudoChat').attr("value");
		var scrollPosition = document.documentElement.scrollTop;
		var marginTop = scrollPosition + 40;
		if( pseudo != '' )
		{
			var referer = window.location.pathname;
			var contChat = $('#message').attr("value");
			var inviteChat = $('#inviteChat').attr("value");
			var dataString = 'contChat='+ contChat +'&referer='+ referer +'&scrollPosition='+ scrollPosition +'&pseudoChat='+ pseudo + '&inviteChat='+ inviteChat ;
			var parent = $('#chatContent');
			$.ajax({
				type: "POST",
				url: site_url+"pages/chatContent.php",
				data: dataString,
				cache: false,
				success: function(html)
				{
				parent.html(html);
				} 
			});
			$("#fondLightbox").fadeOut(100);
			$("#lightbox").fadeOut(100);
			$("#lightbox div").html('');
			 $('#message').val('');;
			return false;
		}
		else
		{
				var content = 'message';
				var dataString = 'content='+content ;
				$.ajax({
					type: "POST",
					url: site_url+"pages/contentLightbox.php",
					data: dataString,
					cache: false,
					success: function(html)
					{
						$("#lightbox").css('margin-top', marginTop + 'px')
						$("#lightbox div").html(html);
						$("#fondLightbox").fadeIn(100);
					$("#lightbox").fadeIn(100);
					}
				});
				return false;
		}
	});
});

// on recgarge la fonction toutes les 2500ms
setInterval (chat, 2500);
