var date = new Date();
var mois = date.getMonth()+1;
var an = date.getFullYear();

$(document).ready(function() {
	initBlocsDroite();
	retourAfficherMoisDroite();
	$("#droite .agenda .precedent").click( function(){
		if ( mois == 1 ) {
			mois = 12;
			an = an - 1;
		}
		else {
			mois = mois - 1;
		}
		
		changerMoisDroite(mois, an);
		
		return false;
	});
	
	$("#droite .agenda .suivant").click( function(){
		if ( mois == 12 ) {
			mois = 1;
			an = an + 1;
		}
		else {
			mois = mois + 1;
		}
		
		changerMoisDroite(mois, an);
		
		return false;
	});	
	
	$("section#droite a.deplier").click( function() {
		var bloc = $(this).attr("name");
		
		if ( $(this).attr("class") == "deplier" )
		{
			if ( tabBlocs[bloc].deplier == 1 ) {
				tabBlocs[bloc].deplier = 0;
				$("section#droite section#contenu_"+bloc).hide( "blind" );
			}
			else {				
				tabBlocs[bloc].deplier = 1;
				$("section#droite section#contenu_"+bloc).show( "blind" );
			}
		}
		
		majBlocs();
		
		return false;
	});
	
	$("section#droite section.compte div.droite a[href=#]").click( function() {
		$("section#droite ul."+$(this).attr("class")).toggle( "blind" );
		
		if ( $(this).hasClass("notifications") ) {
			$.ajax({
				url: URL+"ajax.php",
				type: "POST",
				dataType: 'json',
				data: { module : "profil",
						action : "effacerNotifications"
					},
				success: function(reponse){
				}
			});
		}
		
		return false;
	});
	
	$(".flasher").click( function() {
		var pour = $(this).attr("name").split("_")[1];
		var id_flash = $(this).attr("name").split("_")[2];
		
		dialog("dialog", "confirm", "Voulez-vous vraiment flasher cette personne ?", "confirmFlasher('"+pour+"','"+id_flash+"')");
		
		return false;
	});
	
	$("a.cacher_flash").click( function() {		
		var id = $(this).attr("name").split("_")[1];
		
		cacherFlash(id);
               return false;
	});
	
	$("a.cacher_notification").click( function() {		
		var id = $(this).attr("name").split("_")[1];
		
		$.ajax({
			url: URL+"ajax.php",
			type: "POST",
			dataType: 'json',
			data: { module : "profil",
					action : "cacherNotification",
					notification : id
				},
			success: function(reponse){
				$("#notification_"+id).fadeOut();
			}
		});
		
		return false;
	});
	
	$(".demande_ami").click( function() {
		var pour = $(this).attr("name").split("_")[1];
		
		dialog("dialog", "confirm", "Voulez-vous vraiment demander cette personne en amie ?", "confirmDemandeAmi('"+pour+"')");
		
		return false;
	});
	
	$("a.accepter_ami").click( function() {
		var de = $(this).attr("name").split("_")[1];
		
		$.ajax({
			url: URL+"ajax.php",
			type: "POST",
			dataType: 'json',
			data: { module : "profil",
					action : "reponseDemandeAmi",
					reponse : 1,
					de : de
				},
			success: function(reponse){
				$("#demande_"+de).fadeOut();
			}
		});
		
		return false;
	});
	
	$("a.refuser_ami").click( function() {
		var de = $(this).attr("name").split("_")[1];
		
		dialog("dialog", "confirm", "Voulez-vous vraiment refuser comme ami cette personne ?", "refuserAmi('"+de+"')");
		
		return false;
	});
	
	
				
	var sortable = $("#droite").sortable({
		handle: 'a.deplacer',
		helper: 'clone',
		placeholder: 'deplace',
		opacity: 0.8,
		revert: true,
		update: function(event, ui) {
			$("#droite > section").each( function(i) {
				tabBlocs[$(this).attr("class")].ordre = (i+1);
			});
			
			majBlocs();
			
		}
	});
	
	$("#droite").disableSelection();
});

// Initialise les blocs � droite
function initBlocsDroite() {
	if( typeof(tabBlocs) != 'undefined' ) {
		$.each(tabBlocs, function(i) {
			if ( this.deplier == 0 ) {
				$("#droite section#contenu_"+i).hide();
			}
		});
	}
}

// Change le mois de l'agenda
function majBlocs() {	
	$.ajax({
		url: URL+"ajax.php",
		type: "POST",
		data: {	module : "profil",
				action : "majBlocs",
				blocs : $.toJSON(tabBlocs, 1, 0)
			},
		success: function(reponse){}
	});
	
	return false;
}

// Change le mois de l'agenda
function changerMoisDroite(m, a) {
	$.ajax({
		url: URL+"ajax.php",
		type: "POST",
		data: {	module : "agenda",
				action : "agenda",
				affichage : "home",
				mois : m,
				an : a
			},
		success: function(reponse){
			afficherPage("agenda-droite", reponse, null, "retourAfficherMoisDroite");				
		}
	});
	
	return false;
}

// Retour vote sondage
function retourVoteSondage(reponse) {
	$.ajax({
		url: URL+"ajax.php",
		type: "POST",
		data: { module : "sondage",
				action : "unSondage"
			},
		success: function(reponse){
			afficherPage("contenu_sondage", reponse);
		}
	});
	
	
	return false;
}

// Flasher une personne
function confirmFlasher(personne, flash) {
	$.ajax({
		url: URL+"ajax.php",
		type: "POST",
		dataType: 'json',
		data: { module : "profil",
				action : "flasher",
				pour : personne
			},
		success: function(reponse){
			if ( reponse.erreur ) {
				dialog("dialog", "erreur", reponse.erreur);
			}
			else {
				dialog("dialog", "message", reponse.message);
			}
			cacherFlash(flash);
		}
	});
	
	return false;
}

// Cache un flash
function cacherFlash(id) {
	$.ajax({
		url: URL+"ajax.php",
		type: "POST",
		dataType: 'json',
		data: { module : "profil",
				action : "cacherFlash",
				flash : id
			},
		success: function(reponse){
			$("#flash_"+id).fadeOut();
		}
	});
	
	return false;
}

// Cacher Flash
function confirmDemandeAmi(personne) {
	$.ajax({
		url: URL+"ajax.php",
		type: "POST",
		dataType: 'json',
		data: { module : "profil",
				action : "demandeAmi",
				pour : personne
			},
		success: function(reponse){
			if ( reponse.erreur ) {
				dialog("dialog", "erreur", reponse.erreur);
			}
			else {
				dialog("dialog", "message", reponse.message);
			}
		}
	});
	
	return false;
}

// Refuser ami
function refuserAmi(de) {
	$.ajax({
		url: URL+"ajax.php",
		type: "POST",
		dataType: 'json',
		data: { module : "profil",
				action : "reponseDemandeAmi",
				reponse : 0,
				de : de
			},
		success: function(reponse){
			$("#demande_"+de).fadeOut();
		}
	});
	
	return false;
}

// Retour d'affichage d'un mois
function retourAfficherMoisDroite(reponse) {
	
	$("#agenda-droite figure.icone").mouseenter( function() {
		var id = this.id.split("_")[1];
		
		var positionIcone = $(this).position();
		
		var topInfos = positionIcone.top - $("#infos_"+id).outerHeight();
		var leftInfos = positionIcone.left - ( $("#infos_"+id).outerWidth() / 2 );
		
		$("#infos_"+id).css({
		      'top' : topInfos,
		      'left' : leftInfos
		});
		
		$("#infos_"+id).fadeIn("fast");
	});
	
	$("#agenda-droite figure.icone").mouseleave( function() {
		var id = this.id.split("_")[1];
		
		$("#infos_"+id).fadeOut("fast");
	});
}

