/**
* Fichier	: dialog.js
* Contenu	: Code javascript gérant les dialogues affichés dans la page avec jquery
* Date		: 16/12/10
* @author	Emmanuel Dewaele
* @version	1.0
*/

$j(function() {
        $j( "#dialog-form-changerMdp" ).dialog({
                autoOpen: false,
                height: 'auto',
                width: 350,
                modal: true,
                resizable: false,
                buttons: {
                        "Valider": function() {
				var formulaire = xajax.getFormValues('form-changerMdp');
				
				if((formulaire.nouveauMdp1.length < 8) || (formulaire.nouveauMdp2.length < 8))
				{
					messageModal('Le mot de passe doit contenir au moins huit caractères.');
				}
				else
				{
					formulaire.hashancienMdp = sha1(formulaire.ancienMdp);
					formulaire.ancienMdp = '';
					formulaire.hashnouveauMdp1 = sha1(formulaire.nouveauMdp1);
					formulaire.nouveauMdp1 = '';
					formulaire.hashnouveauMdp2 = sha1(formulaire.nouveauMdp2);
					formulaire.nouveauMdp2 = '';
		                        xajax_changerMdp(formulaire);
				}
                        },
                        "Annuler": function() {
                                $j( this ).dialog( "close" );
                        }
                }
        });

        $j( "#dialog-form-recupererMdp" ).dialog({
                autoOpen: false,
                height: 'auto',
                width: 350,
                modal: true,
                resizable: false,
                buttons: {
                        "Valider": function() {
                                xajax_recupererMdp(xajax.getFormValues('form-recupererMdp'));
                        },
                        "Annuler": function() {
                                $j( this ).dialog( "close" );
                        }
                }
        });
        
        $j( "#dialog-form-connexion" ).dialog({
            autoOpen: false,
            height: 'auto',
            width: 500,
            modal: true,
            resizable: false
        });
        
        $j( "#dialog-form-creerCompte" ).dialog({
            autoOpen: false,
            height: 'auto',
            width: 500,
            modal: true,
            resizable: false,
            buttons: {
                "Valider": function() {
        			xajax_creerCompte(xajax.getFormValues('form-creerCompte'));
                },
                "Annuler": function() {
                        $j( this ).dialog( "close" );
                }
        	}
        });
        
        $j( "#dialog-form-connexion input[type=button]" ).button();
        $j("#button-connexion").click(function(){$j('#dialog-form-connexion').dialog('open');});
});

/**
 * Affiche le dialogue de partage d'élément
 * @param lien lien à partager
 * @param type type d'élément à partager (0: permalien, 1: notation, 2:balade)
 */
function afficherDialoguePartager(lien,titre,type)
{
	finChargement();
	$j("#partagerNote-div").empty();
	$j('#partagerNote-div').share({
            url:		lien,
            title:		titre,
            services:		['twitter', 'digg', 'linkedin', 'facebook', 'netvibes', 'google', 'yahoo', 'delicious' ],
            img_size:		20,
            a_target:		'_blank',
            img_alt:	'Partager sur $service'
        });
	$j("#partagerNote-lien").attr("href", lien);
	$j("#partagerNote-input").attr( "value",lien);
	$j("#dialogPartager-explication").children().hide();
	$j("#dialogPartager-explication"+type).show();
	$j("#dialogPartager").dialog("open");
}

/**
 * 
 * @param num
 * @returns
 */
function partager()
{
	debutChargement();
	if(typeof(mode_colonne_droite) == 'undefined' || mode_colonne_droite == 1 || mode_colonne_droite == 6)
		afficherURL('enregistrer',null);
	else if(mode_colonne_droite == 2)
		xajax_partagerNotation();
	else if(mode_colonne_droite == 4)
	{
		if(!creationBaladeEnCours && $j('#formCreerBalade #idBalade').val() != -1)
		{
			afficherDialoguePartager(URL_BASE_SITEWEB+DOSSIER_SITEWEB+"?bal="+$j('#formCreerBalade #idBalade').val(),"Balade "+$j("#affichage_nom_balade").html()+" sur Géovélo par "+$j("#affichage_auteur_balade").html(),2);
		}
		else
		{
			finChargement();
			alert("Vous devez consulter une balade avant de pouvoir la partager");
		}
	}
}

