﻿


/* NEWS */
var newsInterTransitionMs = 2500 // 2 secondes et demi
var newsTransitionMs = 400 // 0.4 secondes
$(document).ready(function () {
    setupNews()
});
function setupNews() {
    $(".news_acc_container .event_acc_subcontainer").width($(".eventContainerResume").length * 250);
    var $currentNews = null;
    $currentNews = $(".news_acc_container .eventContainerResume").first().css("margin-left", "9px");
    if ($(".news_acc_container .eventContainerResume").length > 4) {
        window.setInterval(function () {
            $(".event_acc_subcontainer").animate({ marginLeft: "-=229" }, newsTransitionMs, "swing", function () {
                if ($(this).width() > 30000) {
                    setupNews();
                    return false;
                };
                $(this).width($(this).width() + 232);
                $currentNews.clone().css("margin-left", "0px").appendTo($(this));
                $currentNews = $currentNews.next();

            });
        }, newsInterTransitionMs);
    }
}


/* DIAPORAMA */
var webDiapoSpeed = 30000;
$(document).ready(function () {
    $("li>a.aSliderImage").each(function () {
        var $d = $("div:first", this);
        //alert($d.attr("src"));
        $("<img/>").attr("src", $d.attr("src")).attr("encodedUri", $d.attr("encodedUri")).appendTo($(this));
        $d.remove();
    });

    $(".imgGalleryContainer").css("cursor", "pointer").each(function () {
        $(this).bind("click", function () {
            var idGallery = $(this).attr("galleryId");
            var titre = $(".imgGalleryTitle:first", this).html();
            var width = Number($("#gall_" + idGallery).attr("imgWidth"));
            var height = Number($("#gall_" + idGallery).attr("imgHeight"));
            var $ul = $("ul:first", this).clone().css("width", width).prop("id", "sliderContent");
            

            $("<div/>").css("overflow", "hidden").prop("id", "popGal" + idGallery).dialog({
                modal: true,
                title: titre,
                resizable: false,
                width: width,
                height: height + 26,
                create: function () {
                    $(".ui-dialog").css("border", "solid 2px #000");
                    $(this).css("padding", "0px");
                    $(this).css("margin", "0px");
                    $("<div/>").css("width", width).css("height", height).prop("id", "slider").appendTo($(this));
                    $("#slider").append($ul);
                    $("#slider span").css("width", width - 10);
                    $("#slider ul:first").PikaChoose({
                        speed: webDiapoSpeed,
                        transition: [1]
                    });

                    $iLikeDiapo = $("<iframe src='http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fmakila.cobalt-informatique.com%2Fgallery%2F10%2Fthc_02.jpg&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=35' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:60px; height:35px; position:relative;top:-40px;left:20px' allowTransparency='true'></iframe>");
                    $iLikeDiapo.appendTo($(this));
                    PikaChooseChange($("img:first", $ul).attr("src"));
                },
                close: function () {
                    $("#popGal" + idGallery).remove();
                }
            });
        });
    });
});
var $iLikeDiapo = null
function PikaChooseChange(itemSrc) {
   
    if ($iLikeDiapo != null) {
        if ($("img[src='" + itemSrc + "'][encodedUri]").length > 0) {
            var src = $("img[src='" + itemSrc + "'][encodedUri]").first().attr("encodedUri");
            var iSrc = "http://www.facebook.com/plugins/like.php?href=" + src + "&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=35"
            $iLikeDiapo.show().attr("src", iSrc);
            //open(iSrc);
        } else {
            $iLikeDiapo.hide()
        };
    };
};


/* UTILS */
function setMask() { $('<div></div>').attr("id", "mask").addClass('ui-widget-overlay').css("height", $(document).height()).appendTo($("body")) };
function hideMask() { $("#mask").remove() };
function urlParam(url) { var d = new Date(); return (url.indexOf("?") > -1) ? url + "&date=" + encodeURI(d.toTimeString()) : url + "?date=" + encodeURI(d.toTimeString()) };
function showDatas(cId) { $("#" + cId).toggle() };

var theForm

/* Formulaires */
function __doPostBack(eventTarget, eventArgument) {
    $("#__EVENTTARGET").val(eventTarget);
    $("#__EVENTARGUMENT").val(eventArgument);
    if (CheckForm()) {
        theForm.submit()
    };
};
function CheckForm() {
    var ret = true;
    var $focusInput = null;
    var err = "";
    var err2 = "";
    $("input.required").each(function () {
        if ($(this).val().replace(" ", "").length == 0) {
            ret = false;
            if ($focusInput == null) { $focusInput = $(this) }
            var label = $(this).parents("tr").first().find("td:eq(0)").text();
            err += " - " + label + "<br/>";
        }
    });
    if ($("#userPassbis").length > 0) {
        if ($("#userPassbis").val() != $("#userPass1").val()) {
            ret = false;
            err2 = "<br/><br/>Le mot de passe n'est pas correctement confirmé.";
        }
    }
    if (err != "") {
        inlineAlert("Les champs suivants sont obligatoires.<br/>" + err + err2, "Erreur de saisie");
    } else {
        if (err2 != "") {
            inlineAlert(err2, "Erreur de saisie");
        }
    }
    return ret;
};


/* Login */
var toolToggleSpeed = 150;
$(document).ready(function () {

    $(".header_compte:not([href])").bind("click", function () {
        $(".header_compte_log").slideToggle(toolToggleSpeed);
        $(".header_newsletter_log").hide();
    });
    $(".header_newsletter").bind("click", function () {
        $(".header_newsletter_log").slideToggle(toolToggleSpeed);
        $(".header_compte_log").hide();
    });
    $(".header_compte_log").mouseleave(function () {
        $(".header_compte_log").slideUp(toolToggleSpeed);
    });
    $(".header_newsletter_log").mouseleave(function () {
        $(".header_newsletter_log").slideUp(toolToggleSpeed);
    });

    /* PAGES UTILISATEUR */
    theForm = document.forms['userForm'] ? document.forms['userForm'] : document.userForm;
});

function inlineAlert(message, titre) {
    if ($("#modalAlert").length == 0) {
        $("<div/>").attr("id", "modalAlert").appendTo("body");
    };
    $("#modalAlert").empty().html(message);
    $("#modalAlert").dialog({ modal: true, title: titre });
};



function zoomz(elmt) {
    $(elmt).toggleClass('zoom').toggleClass('zoom_out');
}

/* PAGES UTILISATEUR */
function asyncLogin() {
    var email = $("#userLogin").val();
    var pass = $("#userPass").val();
    if (email != "" && pass != "") {
        var cUrl = urlParam("inc/async/SetUserLogin.aspx?e=" + email + "&p=" + pass)
        $.ajax({
            url: cUrl,
            success: function (HtmlData) {
                if (HtmlData == "ok") {
                    window.location.href = "user_page.aspx";
                } else {
                    inlineAlert("Email ou mot de passe invalide.", "Erreur de saisie");
                    $("#userPass").val("");
                };
            },
            error: function () {
                inlineAlert("Une erreur est survenue.<br/>Merci d'essayer à nouveau ultérieurement.", "Erreur");
            }
        });
    }
}

function asyncRegisterNewsLetter() {
    var email = $("#userNewsLetter").val();
    if (email != "") {
        $.ajax({
            url: urlParam("inc/async/SetUserNewsletter.aspx?e=" + email),
            success: function (HtmlData) {
                if (HtmlData == "ok") {
                    inlineAlert("Votre inscription a bien été prise en compte.", "Confirmation");
                } else {
                    inlineAlert("Une erreur est survenue.<br/>Merci d'essayer à nouveau ultérieurement.", "Erreur");
                    $("#userNewsLetter").val("");
                };
            },
            error: function () {
                inlineAlert("Une erreur est survenue.<br/>Merci d'essayer à nouveau ultérieurement.", "Erreur");
            }
        });
    }

}

/* Réservation */
function setResa() {
    if ($("#resa_le").val() == "0" || $("#resa_a").val() == "0" || $("#resa_pour").val() == "0" || $("#resa_de").val() == "") { return false };
    $(".userMainPageLnk").hide();
    $("#resaConfirm").show();
    var $confirm = $("<div/>").addClass("userMainPageLnk");
    var s = "Veuillez confirmer la réservation pour le " + $("#resa_le").val() + " à " + $("#resa_a").val().replace(":", "h");
    s += " pour " + $("#resa_pour").val() + " personnes au nom de " + $("#resa_de").val();
    $("#resa_text").html(s);
};
function cancelResa() {
    $(".userMainPageLnk").show();
    $("#resaConfirm").hide();
};
function confirmResa(u) {
    var rUrl = "inc/async/SetUserResa.aspx?u=" + u + "&d=" + $("#resa_le").val() + "&a=" + $("#resa_a").val() + "&p=" + $("#resa_pour").val() + "&n=" + $("#resa_de").val()
    setMask();
    $.ajax({
        url: urlParam(rUrl),
        success: function (HtmlData) {
            if (HtmlData.indexOf("ok") > -1) {
                hideMask();
                $("#resa_text").html("Merci.<br/>Votre réservation a bien été enregistrée.");
                $("#resaConfirm a").hide()
            }
        },
        error: function () { hideMask(); $("#resa_text").html("Une erreur est survenue, veuillez réessayer dans quelques instants."); $("#resaConfirm a").hide() }
    });
};

/* Page contact */
function validateContactForm() {
    var ret = true;
    var $focusInput = null;
    var err = "";
    $("input.contactRequired").each(function () {
        if ($(this).val().replace(" ", "").length == 0) {
            ret = false;
            if ($focusInput == null) { $focusInput = $(this) }
            var label = $(this).parents("tr").first().find("td:eq(0)").text();
            err += " - " + label + "<br/>";
        }
    });
   
    if (err != "") {
        inlineAlert("Les champs suivants sont obligatoires.<br/>" + err, "Erreur de saisie");
    } 
    return ret;
};

function contactUs() {
    if (validateContactForm() == true) {
        var rUrl = "inc/async/sendContactMail.aspx";
        rUrl += "?n=" + encodeURI($("#tbCtNom").val());
        rUrl += "&p=" + encodeURI($("#tbCtPrenom").val());
        rUrl += "&s=" + encodeURI($("#tbCtSociete").val());
        rUrl += "&e=" + encodeURI($("#tbCtMail").val());
        rUrl += "&m=" + encodeURI($("#tbCtMobile").val());
        rUrl += "&c=" + encodeURI($("#tbCtSujet").val());
        setMask();
        //open(rUrl)
        $.ajax({
            url: urlParam(rUrl),
            success: function (HtmlData) {
                if (HtmlData.indexOf("ok") > -1) {
                    hideMask();
                    inlineAlert("Merci.<br/>Votre message a bien été envoyé", "Message envoyé");
                    $("#tbCtNom").val("");
                    $("#tbCtPrenom").val("");
                    $("#tbCtSociete").val("");
                    $("#tbCtMail").val("");
                    $("#tbCtMobile").val("");
                    $("#tbCtSujet").val("");
                } else {
                    hideMask(); 
                    inlineAlert("Une erreur est survenue, veuillez réessayer dans quelques instants.", "Erreur");
                }
            },
            error: function () { hideMask(); inlineAlert("Une erreur est survenue, veuillez réessayer dans quelques instants.", "Erreur"); }
        });
    };
};
