var sport_id = '';
$(document).ready(function() {

	$(".widgetdelete").click(function(e){
	e.preventDefault();
		
		jQuery.facebox('<div style="background-color:#ffffff;"><strong>Confirmation</strong><p>Supprimer définitivement ce widget ?</p><p style="text-align:center; margin-top:10px"><input type="button" class="inputbutton" id="del_confirm" rel="'+$(this).attr('rel')+'" value="Valider" /> <input type="button" class="inputbutton" id="del_decline" value="Annuler" /></p></div>');
		$("#del_confirm").click(function(){
			id = $(this).attr('rel').split("/");
			$.get(
			"/profile/del_widget/" + $(this).attr('rel'),
			function(datas) 
			{
				$("#widget_" + id[1]).fadeOut();
				$.facebox.close();
			});
			
		});
		$("#del_decline").click(function(){
			$.facebox.close();
		});
	});	

  $(".widget_home").click(function(e){
    e.preventDefault();
    $.get(
			"/profile/home_widget/" + $(this).attr('rel')+"/on",
			function(datas) 
			{
			alert("Le widget est maintenant en page d\'accueil");		
		});
  });
  
 $(".widget_home_remove").click(function(e){
    e.preventDefault();
    $.get(
			"/profile/home_widget/" + $(this).attr('rel')+"/off",
			function(datas) 
			{
			location.href = "";
		});
  });

reset_widget();
sport_id = $('div#sortable').attr("rel");
$('div#sortable').Sortable(
	{
		accept: 'widget',
		helperclass: 'sortHelper',
		handle: 'a.drag',
		tolerance: 'pointer',
		onChange : function(ser)
		{
			$.ajax({
			type: "POST",
			url: "/profile/ajax_save_position/"+  sport_id + ".html",
			data: $.SortSerialize('sortable').hash,
			success: function(datas){
			}
			});
			reset_widget();
		},
		onStop : function()
		{
			reset_widget();
		}
	}
	);
$('#save').click(function(e)
	{
		e.preventDefault();

			$('#saveInfo').removeClass('rinfo').addClass('yinfo').empty().append('Enregistrement en cours ...').fadeIn('slow');
			$.post(
				"/profile/ajax_save_profile.html",
				{
				fname : $('#fname').val(),
				lname : $('#lname').val(),
				birthday : $('#birthday').val(),
				birthmonth : $('#birthmonth').val(),
				birthyear : $('#birthyear').val(),
				gender : $("input[@name=gender][@checked]").val(),
				city : $('#city').val(),
				postalcode : $('#postalcode').val(),
				address : $('#address').val(),
				dextre : $("input[@name=dextre][@checked]").val(),
				idol : $('#idol').val(),
				excuse : $('#excuse').val()
				},
				function(datas) {
					if(datas=="2")
					{
						jQuery.facebox('<div style="background-color:#ffffff;"><strong>Votre profil a bien été enregistré !</strong><p style="padding-top:10px;">Vous n\'avez toujours pas ajouté de sports à votre profil. Nous vous invitons à les completer dès maintenant.<br /></p><p style="text-align:center; margin-top:10px"><input type="button" class="inputbutton" id="redirect_sport" value="Compléter mes sports" /></p></div>');
						$("#redirect_sport").click(function(){
							window.location.href = "/profile/index/options.html";
						});
						
					}
					else if(datas=="1")
					{
						window.location.href = "/profile.html";
						
					}
					else if(datas=="0")
					{
						$('#saveInfo').removeClass('yinfo').addClass('rinfo').empty().append('Tous les champs marquées d\'une étoile rouge n\'ont pas étés renseignés');
						$('#saveInfo').animate({'background-color': 'blue'},3000);
						$('#saveInfo').fadeOut('slow');
					}
					else
					{
					  $('#saveInfo').removeClass('yinfo').addClass('rinfo').empty().append('Erreur dans un des champs du profil');
						$('#saveInfo').animate({'background-color': 'blue'},3000);
						$('#saveInfo').fadeOut('slow');
						
          }
					
				}
			);
		
	});
});

function reset_widget() {
	$(".widget_edit").unbind('click');
	$(".widget_edit").toggle(function(){
		id = $(this).attr("rel");
		$(this).empty().append('Visualiser');
		$("#widget_"+id+" .widgetcontent").empty().append('<p class="center"><img src="http://static.wanasport.com/img/template/facebox/loading.gif" alr="loading" /><br />Chargement...</p>').load("/widget_"+id+"/edit.html");
	},
	function(){
		$(this).empty().append('Editer');
		$("#widget_"+id+" .widgetcontent").empty().append('<p class="center"><img src="http://static.wanasport.com/img/template/facebox/loading.gif" alr="loading" /><br />Chargement...</p>').load("/widget_"+id+"/ajax_display.html");
	});	
}

function remove_help() {
  $("#help_overlay").hide();
}