$(document).ready(function(){
	//SI IE6 BERK
	var url = window.location.href;
	if(url.lastIndexOf("ie6=0") == -1) {
		if($.browser.msie) {
			if($.browser.version == "6.0") {
				window.location.href = '../ie6.php?url='+url;
			}
			if($.browser.version == "8.0") {
				//$("#voile").css('background','#FF0000');
			}
		}
	}

	//VERIFICATION SUPPORT PLACEHOLDER
	if(!elementSupporteAttribute('input','placeholder')) {
		$("#fSearch").val('Votre recherche...');
		$("#fSearch").focus(function(){
			$(this).val('');
		});
		$("#fSearch").blur(function(){
			if($(this).val().length == 0) {
				$(this).val('Votre recherche...');
			}
		});
	}
	
	//ON APPUIE SUR ECHAP POUR FERMER LA MODAL
	$(document).keyup(function(e){
		if(e.which == 27) {
			fermer();
		}
	});
	
	//LOADING
	$("#loading").ajaxStart(function(){
		$(this).show();
	});
	$("#loading").ajaxStop(function(){
		$(this).hide();
	});
	
	//BOX CFM
	$("#cfm").click(function(){
		var w = $(this).width();
		var x = $(this).attr('offsetLeft');
		$("#cfmBox").css('width',w);
		$("#cfmBox").css('left',x);
		$("#cfmBox").css('top',$("header").height());
		$("#cfmBox").slideToggle('medium');
		return false;
	});
	
	//INIT
	$("#cfmBox").css('width',$("#cfm").width());
	$("#cfmBox").css('left',$("#cfm").attr('offsetLeft'));
	$("#cfmBox").css('top',$("header").height());
	
	$("#searchBox").css('left',$("#fSearch").attr('offsetLeft')-300+180);
	$("#searchBox").css('top',$("header").height());
	
	//FLASH EXISTE
	var hasFlash = false;
	try {
		var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
		if(fo) hasFlash = true;
	}catch(e){
		if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) hasFlash = true;
	}
	if(hasFlash == false) {
		$("#player").css('background','url(./img/fond_video.png)');
		$("#object").css('display','none');
		$("#player").click(function(){
			window.open('http://get.adobe.com/fr/flashplayer/');
		});
	}
	
	//TIPSY SUR ACTIONS
	$("p.actions>img").tipsy({gravity: 'n'});
	
	//TIPSY SUR VIGNETTES
	$("img.vignette").tipsy();
	$("#archive img").tipsy();
	
	//SUGGESTIONS
	$("#fSearch").keyup(function(){
		if($(this).val().length >= 3 && $(this).val() != 'Votre recherche...') {
			$.ajax({
				type: "POST",
				url: "script/searchEngine.php",
				data: "q="+$(this).val(),
				success: function(data) {
					$("#searchBox").html(data);
					$("#searchBox").slideDown();
				}
			});
		} else {
			$("#searchBox").slideUp();
		}
	});
	
	//ON FERME LA RECHERCHE APRES 200MS
	$("#fSearch").blur(function(){
		setTimeout(function(){
			$("#searchBox").slideUp();
		}, 200);
	});
	
	//MODAL
	$("#permalink").click(function(){
		var idVideo = $("#fIdVideo").val();
		$("#voile").fadeIn(function() {
			$("#modalBox").load('modal/permalink.php?idv='+idVideo, function() {
				$("#modalBox").slideDown();
			});
		});
	});
	
	$("#mail").click(function(){
		var idVideo = $("#fIdVideo").val();
		$("#voile").fadeIn(function() {
			$("#modalBox").load('modal/mail.php?idv='+idVideo, function() {
				$("#modalBox").slideDown();
			});
		});
	});
	
	$("#export").click(function(){
		var idVideo = $("#fIdVideo").val();
		$("#voile").fadeIn(function() {
			$("#modalBox").load('modal/export.php?idv='+idVideo, function() {
				$("#modalBox").slideDown();
			});
		});
	});
	
	$("#ecrireComm").click(function(){
		var idVideo = $("#fIdVideo").val();
		$("#voile").fadeIn(function() {
			$("#modalBox").load('modal/ecrireComm.php?idv='+idVideo, function() {
				$("#modalBox").slideDown();
			});
		});
	});
	
	$("#lireComm").click(function(){
		var idVideo = $("#fIdVideo").val();
		$("#voile").fadeIn(function() {
			$("#modalBox").load('modal/lireComm.php?idv='+idVideo, function() {
				$("#modalBox").slideDown();
			});
		});
	});
	
	$("#fb").click(function(){
		var idVideo = $("#fIdVideo").val();
		var r = randomString(15);
		var u = 'http://www.cfm-ajaccio.fr/webtv/video.php?r='+r+'&id='+idVideo;
		var t = document.title;
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(encodeURIComponent(u))+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	});
	
	$("#buzz").click(function(){
		window.open("http://www.google.com/buzz/post","sharer","toolbar=0,status=0,width=700,height=350");
	});
	
	$("#rss").click(function(){
		window.open("http://www.cfm-ajaccio.fr/webtv/admin/rss.xml","Flux RSS","toolbar=0,status=0,width=700,height=600");
	});
	
	$("#twitter").click(function(){
		var idVideo = $("#fIdVideo").val();
		var u = 'http://www.cfm-ajaccio.fr/webtv/video.php?id='+idVideo;
		window.open("http://twitter.com/home?status=Regarde une video sur WebTV du Centre de Formation des Métiers de la Corse du Sud - "+ encodeURIComponent(u) +"","sharer","toolbar=0,status=0,width=800,height=500"); return false
	});
	
	$("#readMore").click(function(){
		var idVideo = $("#fIdVideo").val();
		$("#voile").fadeIn(function() {
			$("#modalBox").load('modal/description.php?idv='+idVideo, function() {
				$("#modalBox").slideDown();
			});
		});
		return false;
	});
	
	$("#voile").click(function(){
		fermer();
	});
});

function ecrireCommentaire() {
	var idVideo = $("#fIdVideo").val();
	var nom = $("#f_ecrire_nom").val();
	var message = $("#f_ecrire_message").val();
	if(nom.length == 0 || message.length == 0) {
		$("#alert_ecrire").html('<p class="warning">Les champs <strong>Votre nom</strong> et <strong>Votre message</strong> doivent être remplis !</p>');
	} else {
		$.ajax({
			type: 'POST',
			url: 'script/addComment.php',
			data: 'idVideo='+idVideo+'&nom='+nom+'&msg='+message,
			success: function(data) {
				if(data == 1) {
					$("#alert_ecrire").html('<p class="success">Votre commentaire sera validé avant d\'être publié !</p>');
					$("#f_ecrire_nom").val('');
					$("#f_ecrire_message").val('');
				} else {
					$("#alert_ecrire").html('<p class="error">Une erreur est survenue, veuillez réessayer !</p>');
				}
			}
		});
	}
}

function sendMail() {
	var idVideo = $("#fIdVideo").val();
	var nom = $("#f_mail_nom").val();
	var mail = $("#f_mail_email").val();
	if(nom.length == 0 || mail.length == 0) {
		$("#alert_mail").html('<p class="warning">Les champs <strong>Votre nom</strong> et <strong>email du destinataire</strong> doivent être remplis !</p>');
	} else {
		if(checkEmail(mail) == true) {
			$.ajax({
				type: 'POST',
				url: 'script/sendEmail.php',
				data: 'idVideo='+idVideo+'&nom='+nom+'&email='+mail,
				success: function(data) {
					if(data == 1) {
						$("#alert_mail").html('<p class="success">Message envoyé !</p>');
						$("#f_mail_nom").val('');
						$("#f_mail_email").val('');
					} else {
						$("#alert_mail").html('<p class="error">Une erreur est survenue, veuillez réessayer !</p>');
					}
				}
			});
		} else {
			$("#alert_mail").html('<p class="warning">L\'adresse email est invalide !</p>');
		}
	}
}

function fermer() {
	$("#modalBox").hide();
	$("#voile").fadeOut();
}

function elementSupporteAttribute(element,attribut) {
	var test = document.createElement(element);
	if(attribut in test) {
		return true;
	} else {
		return false;
	}
}

function inputSupporteType(test) {
	var input = document.createElement('input');
	input.setAttribute('type',test);
	if(input.type == "text") {
		return false;
	} else {
		return true;
	}
}

function checkEmail(_mail) {
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
	if(reg.test(_mail)) {
		return(true);
	} else {
		return(false);
	}
}

function randomString(length) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
    
    if (! length) {
        length = Math.floor(Math.random() * chars.length);
    }
    
    var str = '';
    for (var i = 0; i < length; i++) {
        str += chars[Math.floor(Math.random() * chars.length)];
    }
    return str;
}

