/*

 Pour l'animation des éléments communs du site

 */
/*

 Les outils latéraux

 */
var initSocialNetwork = function() {
    if (jQuery("#socialNetwork").is("div")) {
        jQuery("#socialNetwork").mouseover(function () {
            jQuery("#networks").stop().animate({width: "47px"}, 1000);
        });
        jQuery("#socialNetwork").mouseleave(function () {
            jQuery("#networks").stop().animate({width: 0}, 1000);
        });
    }
};

/*

 Le formulaire d'authentification

 */
var initLogin = function() {
    if (jQuery(".login").is("a")) {
        jQuery(".login").click(function () {
            jQuery("#myVinciLogin").slideDown();
            jQuery("#overlay").show();
            return false;
        });
        jQuery("#overlay").click(function () {
            jQuery("#myVinciLogin").slideUp();
            jQuery("#overlay").hide();
        });
    }
    if (jQuery("form#loginForm").size() < 1)
        return;

    jQuery("form#loginForm").each(function() {
        jQuery(this).submit(function () {
            var validationError = false;
            jQuery(".msgError").remove();
            jQuery(".error").removeClass("error");

            jQuery("input, select, textarea, input:radio", this).each(function() {
                if (jQuery(this).attr("class")) {
                    if (!js_tools.validateField(this))
                        validationError = true;
                }
            });

            if (!validationError) {
                /* Traitement cote serveur puis affiche popin */
                login();
                return false;
            }

            return !validationError;
        });
    });
};

var login = (function () {
    var url;
    if (jQuery("form#loginForm input[name='desturl']").length > 0) {
        url = jQuery("form#loginForm input[name='desturl']")[0].value;
    } else if (jQuery("form#loginForm").length > 0) {
        url = jQuery("form#loginForm").attr('action');
    }
    if (!url || url.length == 0 || url == '#') {
        return false;
    }
    jQuery.ajax({
                url : url,
                dataType : 'json',
                type : 'post',
                data : jQuery('form#loginForm').serialize(),
                success : function (datas) {
                    try {
                        if (datas.form_errors) {
                            jQuery.each(datas.form_errors, function(key, value) {
                                jQuery.each(jQuery('form#loginForm select[name=' + key + '], form#loginForm input[name=' + key + ']'), function() {
                                    js_tools.showError(this, value);
                                });
                                if (key == 'formError') {
                                    jQuery("#formError").html(value);
                                    jQuery("#formError").show();
                                }
                            });
                        } else if (datas.popin_code) {
                            // On injecte le texte d'attente
                            jQuery("#popin").html('<img src="file/sitemodel/vinciauto/img/common/loading.gif" alt="..." class="wait"/>');
                            // et on affiche
                            showPopin();
                            // maintenant on charge
                            var url = jQuery('form#loginForm #current').val();
                            var params = {};
                            params.popin = datas.popin_code;
                            if (datas.redirect && datas.redirect.length > 0 && datas.redirect != '#') {
                                params.redirect = datas.redirect;
                            }
                            jQuery("#popin").load(url, params, function() {
                                initClosePopin();
                            });
                        } else if (datas.redirect && datas.redirect.length > 0 && datas.redirect != '#') {
                            document.location = datas.redirect;
                        } else if (datas.send) {
                            // popin V1
                            showPopin();
                            initClosePopin();
                        }
                    } catch (e) {
                    }
                    return false;
                }
            });
});

var accountAccess = function(){
	var url;
	if (jQuery("form#accountAccessForm").size() < 1){
		return false;
	}
	if (jQuery("form#accountAccessForm input[name='desturl']").length > 0) {
        url = jQuery("form#accountAccessForm input[name='desturl']")[0].value;
    }
    if (!url || url.length == 0 || url == '#') {
        return false;
    }
    jQuery("form#accountAccessForm").attr("action", url);
    jQuery("form#accountAccessForm").submit();
};

var initMyAccount = function(){
	var myaccount = jQuery("#myaccount");
	if(myaccount && (myaccount.attr("href")=="#")){
		myaccount.click(function(){
			 accountAccess();
		});
	}
};


/*

 Initialisation

 */
jQuery(document).ready(function () {
    initSocialNetwork();
    initLogin();
    initMyAccount();
});
