/**
 * Plugin de prise en charge des différents formulaires pour s'appuyer sur le framework Idvive
 * @author Yannick (yannickdalbin at gmail.com) 2011-10-12
 *          @corpyright 2011
 *
 * Prend en charge le submit. Simplement appliquer le plugin idform() sur un objet jQuery de type form.
 * Les messages sont contenus dans un div avec comme attribus la classe .errors.
 * Chaque erreur d'un champs particulier est contenu dans un li
 *
 * Les events :
 *  Chaque form aura différents events :
 *      idform_[idFormulaire]_submitted : event sur l'envoi du formulaire par l'utilisateur
 *      idform_[idFormulaire]_actionBeforeSend : event juste avant la requête ajax
 *      idform_[idFormulaire]_savedSuccess : event lorsque tout s'est bien passé
 *      idform_[idFormulaire]_validationError : event lorsque le formulaire est en erreur
 *      idform_[idFormulaire]_savedFailed : event lorsque le traitement du formulaire est en echec
 *
 */
(function ($) {

    $.idurl = function (obj) {

        var hash    = (window.location.hash.substr(1).length > 0) ? window.location.hash.substr(1).split('&'):null;
        var hashs   = new Object();

        if (hash !== null) {
            for (var i = 0; i < hash.length; i++) {
                var h = hash[i].split('=');
                hashs[h[0]] = h[1];
            }
        }

        if (typeof obj === 'object') {
            $.each(obj, function (key, value) {
                hashs[key] = value;
            });

            var ha = ''
            $.each(hashs, function (key, value) {
                if (value !== undefined && value.length > 0) {
                    if (ha !== '') {
                        ha += '&';
                    }
                    ha += key + '=' + value;
                }
            });
            if (ha.length === 0) {
                ha = 'null';
            }
            window.location.hash = ha;
            return hashs;
        }
        else {
            return (hashs[obj] ? hashs[obj]:'');
        }

    }

})(jQuery);
