var rRequestCalculateur = "/home/calculateur/";
var rRequestOptions = "/home/options/";
var rRequestGetTarif = "/home/tarifs/";

/* ================================================================================ */
/* = fonctions = */
/* ================================================================================ */

$(document).ready(function() {
    //mazedia.ui.accueil.applyRoundedMenu('.roundedTop');

    // gestion du menu et sous menus
    $("#menuTop li ul").hide();
    $('#menuTop li').hover(function() {
        $(this).find('.sousmenu').slideDown('fast');
    }, function() {
        $(this).find('.sousmenu').slideUp('fast');
    });

    // menuMain
    $("#menuMain li").click(function() {
        $("#menuMain li").each(function() {
            $(this).removeClass("current");
        });
        $(this).addClass("current");
    });

    // menu left
    $("#menuLeft li").click(function() {
        $("#menuLeft li").each(function() {
            $(this).removeClass("current");
        });
        $(this).addClass("current");
    });

    // menu left tarifs
    $("#menuLeftTarifs li").click(function() {
        $("#menuLeftTarifs li").each(function() {
            $(this).removeClass("current");
        });
        $(this).addClass("current");
        getPrixGamme();
    });


    // Gestion des onglets du détail d’une page agence
    $("#infos_prat_agences .more_infos").hide();
    $("#infos_prat_agences .currentMap").show();

    $("#infos_prat_agences h4").click(function() {
        $("#infos_prat_agences .more_infos").slideUp("fast");
        if ($(this).next("#infos_prat_agences .more_infos").is(":visible")) {
            $(this).next("#infos_prat_agences .more_infos").slideUp("fast");
        } else {
            $(this).next("#infos_prat_agences .more_infos").slideDown("fast");
        }
    });

    // Position current d’un onglet détail d’une page agence
    $("#infos_prat_agences h4").click(function() {
        $("#infos_prat_agences h4").each(function() {
            $(this).removeClass("current");
        });
        $(this).addClass("current");
    });

    // FAQ 
    $(".extendedForm").hide();
    $("h2.conducteur").click(
		function() {
		    if ($(this).hasClass("moins")) {
		        $("#secondConducteur").val("");
		        $(this).removeClass("moins");
		        $(this).addClass("plus");
		        $(this).next().hide();
		    } else {
		        //symbole + ou -
		        $("#secondConducteur").val("1");
		        $("h2.conducteur").removeClass("moins");
		        $("h2.conducteur").addClass("plus");
		        $(this).removeClass("plus");
		        $(this).addClass("moins");

		        //affichage de la zone étendue
		        $("#conducteur .extendedForm").hide();
		        $(this).next().show();
		    }
		}
	);
    $("h2.options").click(
		function() {
		    if ($(this).hasClass("moins")) {
		        $(this).removeClass("moins");
		        $(this).addClass("plus");
		        $(this).next().hide();
		    } else {
		        //symbole + ou -
		        $(this).removeClass("plus");
		        $(this).addClass("moins");

		        //affichage de la zone étendue
		        $("#options .extendedForm").hide();
		        $(this).next().show();
		    }
		}
	);

});


//function calculePrixAccueil(nbJour){
//    // Formule
//    var formule = $("#modes a.current").attr("rel");
//    var gamme = $("#listeGamme").val();
//    
//    if (nbJour=="1"){ nbJour = "2"; }
//    $.post(rRequestCalculateur, 
//            { jour: nbJour,
//              formule: formule,
//              gamme: gamme
//            }, 
//            function(results) { 
//                $("#prix span").html(results.RETOUR + " &euro;");
//            }, "json"
//        );
//}

function getPrixGamme() {
    // Formule en cours
    var formule = $("#menuLeftTarifs .current a").attr("rel");

    // Gammes a tarifer
    var gammes = "";
    $(".gammeCode").each(function() {
        if (gammes != "") { gammes = gammes + ","; }
        gammes = gammes + $(this).html();
    });

    // Tarifs associés
    $.post(rRequestGetTarif,
            { formule: formule, gammes: gammes },
            function(results) {
                if (results.RETOUR == "OK") {
                    $.each(results.TAB_TARIF, function(i, item) {
                        _gamme = item.idGamme;
                        _valeur = item.valeur;
                        if (_valeur != "") {
                            $(".gammeTarifs .gamme" + _gamme + " .prix span").html(_valeur + " &euro;");
                            $(".gammeTarifs .gamme" + _gamme + " .prix").show();
                        }
                    });
                }
            }, "json"
        );

}

function abonnementInitForm() {
    if ($("#secondConducteur").val() == "1") {
        $("h2.conducteur").removeClass("plus");
        $("h2.conducteur").addClass("moins");
        $("h2.conducteur").next().show();
    }

    // Check box
    $(".checkBoxOne").each(function() {
        var _this = $(this);
        $(this).find(".field_checkbox").click(function() {
            _this.find(".field_checkbox").attr("checked", false);
            $(this).attr("checked", true);
        });
    });
}

function getHeure(value) {
    var minutes = Math.round(value * 15);
    var heure = Math.floor((minutes) / 60);
    minutes = minutes - (heure * 60);
    var quart = Math.floor(minutes / 15);

    var newQuart = 15 * quart;
    newQuart = String(newQuart);
    if (newQuart == "0") { newQuart = "00"; }

    var newheure = String(heure);
    if (newheure.length == 1) { newheure = "0" + newheure; }

    value = newheure + "h" + newQuart;
    return value;
}

function afficheMessageRetour(message, erreur) {
    //alert(message);
    if (erreur == "1") {
        alert(message);
    } else {
    tb_show(message, '/home/MessageNewsletter/?height=200&width=700&TB_iframe=1', 'false')
    }


}

function getOptions(detail) {
    $("#options").hide();
    $("#options .options").empty();

    $.post(rRequestOptions,
            { gamme: $("#GAM_CODE").val()
            },
            function(results) {
                var retour = "";
                $.each(results.TAB_OPTIONS, function(i, item) {
                    retour = "1";
                    _libelle = item.libelle;
                    _id = item.id;
                    _detail = item.detail;
                    _nb = item.nb;
                    _prix = item.prix;
                    _prix_max = item.prix_max;
                    _is_prix_jour = item.is_prix_jour;
                    _nbJour = parseInt($("#RES_NB_JOUR").val());

                    if (_libelle != "") {

                        _libellePrix = _prix + " &euro;";
                        if (_is_prix_jour == true) {
                            _libellePrix += " par jour";
                        } else {
                            _libellePrix += " la location";
                        }

                        if ((_prix_max != "") && (_prix_max != "0")) {
                            _libellePrix += " (maxi " + _prix_max + " &euro;)";
                        }

                        if (detail == "1") {
                            if ($("#RES_PRESTATIONS").val() != "") {

                                if (_is_prix_jour == true) {
                                    _prixPrestation = parseFloat(_prix) * _nbJour;
                                } else {
                                    _prixPrestation = parseFloat(_prix);
                                }

                                tabPrestations = $("#RES_PRESTATIONS").val().split('|');
                                for (var i = 0; i < tabPrestations.length; i++) {
                                    if (tabPrestations[i] != "") {
                                        tabElement = tabPrestations[i].split(':');

                                        if (_id == tabElement[0]) {
                                            _qte = parseInt(tabElement[1]);
                                            _prixPrestation = _prixPrestation * _qte;
                                            if ((_prix_max != "") && (_prix_max != "0")) {
                                                _prixPrestationMax = parseInt(_prix_max);
                                            }

                                            if (_prixPrestationMax <= _prixPrestation) { _prixPrestation = _prixPrestationMax }

                                            if (_qte != "1") {
                                                $("#options .options").append("<li id=\"prestationGamme_" + _id + "\"><span style=\"display: none\" class=\"id\">" + _id + "</span><span class=\"libelle\">" + _libelle + " (" + _qte + ")</span> <span class=\"prix\">" + _prixPrestation + " &euro;</span></li>");
                                            } else {
                                                $("#options .options").append("<li id=\"prestationGamme_" + _id + "\"><span style=\"display: none\" class=\"id\">" + _id + "</span><span class=\"libelle\">" + _libelle + "</span> <span class=\"prix\">" + _prixPrestation + " &euro;</span></li>");
                                            }
                                            break;
                                        }

                                    }
                                }
                            }

                        } else {
                            if (item.is_multiple == true) {
                                $("#options .options").append("<li id=\"prestationGamme_" + _id + "\"><input type=\"checkbox\" style=\"width: 20px\" class=\"chk\" value=\"1\" /><input style=\"width: 30px\" class=\"inptQte\" value=\"1\" /><span style=\"display: none\" class=\"id\">" + _id + "</span><span class=\"libelle\">" + _libelle + "</span><span class=\"prix\">" + _libellePrix + "</span><a href=\"#\" title=\"Plus d'informations\" class=\"lkInfoOption\"><img src=\"/Content/medias/moreInfos_bleu.png\" alt=\"Plus d'informations\" /></a><span class=\"qte\" style=\"display: none\">0</span><span class=\"detail\" style=\"display: none\">" + _detail + "</span></li>");
                            } else {
                                $("#options .options").append("<li id=\"prestationGamme_" + _id + "\"><input type=\"checkbox\" style=\"width: 20px\" class=\"chk\" value=\"1\" /><input style=\"display: none; width: 30px\" class=\"inptQte\" value=\"1\" /><span style=\"display: none\" class=\"id\">" + _id + "</span><span class=\"libelle\">" + _libelle + "</span><span class=\"prix\">" + _libellePrix + "</span><a href=\"#\" title=\"Plus d'informations\" class=\"lkInfoOption\"><img src=\"/Content/medias/moreInfos_bleu.png\" alt=\"Plus d'informations\" /></a><span class=\"qte\" style=\"display: none\">0</span><span class=\"detail\" style=\"display: none\">" + _detail + "</span></li>");
                            }
                        }
                    }
                });
                if (retour != "") {
                    if (detail == "1") {
                        if ($("#RES_PRESTATIONS").val() != "") {
                            $("#options").show();
                        }
                    } else {
                        $("#options").show();
                    }
                    getOptionsAction();
                }

                // Récupération des anciennes valeurs saisies
                if ($("#RES_PRESTATIONS").val() != "") {
                    getOptionsValue($("#RES_PRESTATIONS").val());
                }

            }, "json"
        );
}

function getOptionsAction() {


    $("#options .options .lkInfoOption").click(function() {
        $("#options .options .detail").hide();

        var elem = $(this).parent();
        elem.find(".detail").show();

        return false;
    });

    /*$("#options .options .changeMoins a").click(function(){
    var elem = $(this).parent().parent();
    id = elem.find(".id").text();
    qte = parseInt(elem.find(".qte").text());
    if (qte>0){
    qte--;
    elem.find(".qte").text(qte);
    }
    return false;
    });*/
}

function getOptionsValide() {
    var listeOptions = "";
    $(".options li").each(function() {
        id = $(this).find(".id").text();
        chk = $(this).find(".chk").attr("checked");

        if (chk) {

            qte = parseInt($(this).find(".inptQte").val());
            if (qte > 0) {
                if (listeOptions != "") { listeOptions += "|"; }
                listeOptions += id + ":" + qte;
            }
        }
    });
    $("#RES_PRESTATIONS").val(listeOptions);

    var heureDepart = $("#heuredepart").slider("value");
    var heureRetour = $("#heureretour").slider("value");

    $("#RES_HEURE_DEPART").val(getHeure(heureDepart));
    $("#RES_HEURE_RETOUR").val(getHeure(heureRetour));

    $("#RES_HEURE_DEPART_S").val(heureDepart);
    $("#RES_HEURE_RETOUR_S").val(heureRetour);
}

function getOptionsValue(prestations) {
    tabPrestations = prestations.split('|');
    for (var i = 0; i < tabPrestations.length; i++) {
        if (tabPrestations[i] != "") {
            tabElement = tabPrestations[i].split(':');
            idGamme = tabElement[0];
            qte = tabElement[1];

            if ($("#prestationGamme_" + idGamme).length) {
                $("#prestationGamme_" + idGamme).find(".chk").attr("checked", true);
                $("#prestationGamme_" + idGamme).find(".qte").text(qte);
                $("#prestationGamme_" + idGamme).find(".inptQte").val(qte);
            }

        }
    }
}

function favori(phrase, lien) {
    if (window.sidebar) {
        window.sidebar.addPanel(phrase, lien, "");
    } else if (document.all) {
        window.external.AddFavorite(lien, phrase);
    } else {
        return true;
    }
}

function getPrixGamme() {
    // Formule en cours
    var formule = $("#menuTarifs .current a").attr("rel");

    // Gammes a tarifer
    var gammes = "";
    $(".gammeCode").each(function() {
        if (gammes != "") { gammes = gammes + ","; }
        gammes = gammes + $(this).html();
    });

    // Tarifs associés
    $.post(rRequestGetTarif,
            { formule: formule, gammes: gammes },
            function(results) {
                if (results.RETOUR == "OK") {
                    $.each(results.TAB_TARIF, function(i, item) {
                        _gamme = item.idGamme;
                        _valeur = item.valeur;
                        if (_valeur != "") {
                            $(".gammeTarifs .gamme" + _gamme + " .prix span").html(_valeur + " &euro;");
                            $(".gammeTarifs .gamme" + _gamme + " .prix").show();
                        }
                    });
                }
            }, "json"
        );

}

   