$(function(){
	$('.ui-state-default').hover(
		function(){$(this).addClass('ui-state-hover');},
		function(){$(this).removeClass('ui-state-hover');}
	)
	.mousedown(function(){$(this).addClass('ui-state-active');})
	.mouseup(function(){$(this).removeClass('ui-state-active');})
	.mouseout(function(){$(this).removeClass('ui-state-active');});
	
	$('#lecontenu a').addClass('lien2 ui-state-active');
	$('.suite_vignette a').removeClass('lien2');
	$('#boutons_action a').removeClass('lien2');
});

$(function(){
	$('.showcommentaire').click(function() {
			id_article = $(this).attr("id");
			id_article = id_article.substring(1, id_article.length);
			div_comm = "#avis"+id_article;
			$(div_comm).addClass("display");
			if($(div_comm).css("display")=='none'){
				$(div_comm).css("display","block");
				$(this).html("Cacher les commentaires.");
			}else{
				$(div_comm).css("display",'none');	
				$(this).html("Afficher les commentaires.");
			}
		})
});

$(function() {
	$("#tabs").tabs();
});
$(function(){
$('tr').hover(
		function(){$(this).addClass('tablehover');},
		function(){$(this).removeClass('tablehover');}
	);
});

$(function() {
	$('#datepicker').datepicker({changeMonth: true,changeYear: true, dateFormat: 'ddmmyy', firstDay: 1, dayNamesMin: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'], monthNamesShort: ['Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Decembre'] , onSelect: function(dateText, inst) { window.location.href = "index.php?type=10&date="+dateText; }
});

});
$(function() {
		function init(){
			sf_nom = $("#sf_nom"),
			sf_mail_emetteur = $("#sf_mail_emetteur"),
			sf_mail_destinataire = $("#sf_mail_destinataire"),
			nom_page = $("#nom_page"),
			lien_page = $("#lien_page"),
			allFields = $([]).add(sf_nom).add(sf_mail_emetteur).add(sf_mail_destinataire).add(nom_page).add(lien_page),
			tips = $("#validateTips");
		}
		
		function updateTips(t) {
			tips.text(t).effect("highlight",{},10000);
		}

		function checkLength(o,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Le nombre de caractere doit etre compris entre "+min+" et "+max+".");
				return false;
			} else {
				return true;
			}

		}
		
		function trim (myString)
		{
			return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
		} 

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#send_friend").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 310,
			width: 320,
			modal: true,
			buttons: {
				'Envoyer': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					bValid = bValid && checkLength(sf_nom,4,20);
					bValid = bValid && checkLength(sf_mail_emetteur,8,50);
					bValid = bValid && checkLength(sf_mail_destinataire,8,50);

					bValid = bValid && checkRegexp(sf_nom,/^[a-z]([0-9 a-z_])+$/i,"Votre nom doit contenir les caracteres de a-z, 0-9, underscores et commencer par une lettre.");
					bValid = bValid && checkRegexp(sf_mail_emetteur,/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i,"l'email doit contenir les caracteres de a-z, 0-9, underscores, arobase et commencer par une lettre.");
					bValid = bValid && checkRegexp(sf_mail_destinataire,/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i,"l'email doit contenir les caracteres de a-z, 0-9, underscores, arobase et commencer par une lettre.");
					
					
					if (bValid) {
							$.ajax({
							   type: "POST",
							   url: "html/communs/sendfriend.php",
							   data: "action=send&sf_nom="+sf_nom.val()+"&sf_mail_emetteur="+sf_mail_emetteur.val()+"&sf_mail_destinataire="+sf_mail_destinataire.val()+"&nom_page="+nom_page.val()+"&lien_page="+lien_page.val(),
							   success: function(msg){
								//window.location.reload()
								//alert(msg);
								return false;
							   }
							 });
							
							//alert("A faire");
						$(this).dialog('close');
					}
				},
				'Annuler': function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});   
		   
	$('#sendFriend').click(function() {
			init();
			$('#send_friend').dialog('open');
		})
});



$(function() {
		function init(){
			login = $("#login"),
			password = $("#password"),
			allFields = $([]).add(login).add(password),
			tips = $("#validateTips_ident");
		}
		
		function updateTips(t) {
			tips.text(t).effect("highlight",{},10000);
		}

		function checkLength(o,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Le nombre de caractere doit etre compris entre "+min+" et "+max+".");
				return false;
			} else {
				return true;
			}

		}
		
		function trim (myString)
		{
			return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
		} 

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#identification").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 230,
			width: 300,
			modal: true,
			buttons: {
				'Se connecter': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					bValid = bValid && checkLength(login,6,20);
					bValid = bValid && checkLength(password,6,20);	
										
					if (bValid) {
						$('#form').submit();
						$(this).dialog('close');
					}
				},
				'Creer un compte': function() {
					
					$(this).dialog('close');
					window.location = "index.php?type=6";
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});   
		   
	$('#connexion').click(function() {
			init();
			$('#identification').dialog('open');
		})
	$('#connexion_commentaire').click(function() {
			init();
			$('#identification').dialog('open');
		})
});