var $_originalImages = new Array();

$(document).ready(function () {

    // Open External links in a new window

    $('a').filter(function () {
        return this.hostname && this.hostname !== location.hostname;
    }).click(function () {
        window.open(this.href);
        return false;
    }); 

    $_originalImages = $(".slide-bg img");
    $.each($_originalImages, function (index, value) {
        $_originalImages[index]["width"] = $_originalImages[index].width;
        $_originalImages[index]["height"] = $_originalImages[index].height;
        $_originalImages[index]["ratioHeight"] = $_originalImages[index].height / $_originalImages[index].width;
        $_originalImages[index]["ratioWidth"] = $_originalImages[index].width / $_originalImages[index].height;
    });

    resizeElements();

    $("#servicesAccordion").hrzAccordion(
      { eventTrigger: "click",
          openOnLoad: "1",
          cycle: false,
          closeOpenAnimation: 1,
          closeEaseAction: "easeOutSine",
          closeSpeed: 500,
          openEaseAction: "easeInSine",
          openSpeed: 500
      });

    resizeElements();

    $(window).resize(function () {
        resizeElements();
    });

    $(".remove-last li:last").addClass("last")

    $("#eventsBox .cycle").cycle({
        fx: "fade",
        prev: $("#eventsBox .bottom-nav .previous-link"),
        next: $("#eventsBox .bottom-nav .next-link"),
        nowrap: 1,
      width: "100%",
      fit: 1
    });

    $("#newsBox .cycle").cycle({
        fx: "fade",
        prev: $("#newsBox .bottom-nav .previous-link"),
        next: $("#newsBox .bottom-nav .next-link"),
        nowrap: 1,
      width: "100%",
      fit: 1
    });
  
    $("#blogBox .cycle").cycle({
        fx: "fade",
        prev: $("#blogBox .bottom-nav .previous-link"),
        next: $("#blogBox .bottom-nav .next-link"),
        nowrap: 1,
      width: "100%",
      fit: 1
    });

    $(".cycle").cycle("pause");

    /* envoie du courriel de contact */

    $(".form-name").watermark($(".name-label").attr("value"), { usenative: false });
    $(".form-email").watermark($(".email-label").attr("value"), { usenative: false });
    $(".form-message").watermark($(".message-label").attr("value"), { usenative: false });

    $('.form-email').blur(function () {
        var email = cleanupValue($(this).val());
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

        if (email == $(".email-label").attr("value") || email == 'Champ obligatoire' || !emailPattern.test(email)) {
            $(this).addClass("error");
            $(this).watermark('Champ obligatoire', { className: 'watermark-error', usenative: false });
        } else {
            $(this).removeClass("error");
        }
    })

    for (var cpt = 0; cpt < $(".form-button").length; cpt++) {
        $("#form-button-" + (cpt + 1)).click(function () {
            $(this).parent().parent("form").submit(function (event) {
                event.preventDefault();
                var name = cleanupValue($(this).closestChild(".form-name").val());
                var email = cleanupValue($(this).closestChild(".form-email").val());
                var message = cleanupValue($(this).closestChild(".form-message").val());

                if (name == '' || name == 'Champ obligatoire') {
                    $(this).closestChild(".form-name").addClass("error");
                    $(this).closestChild(".form-name").watermark('Champ obligatoire', { className: 'watermark-error', usenative: false });
                } else {
                    $(this).closestChild(".form-name").removeClass("error");
                }
                var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

                if (email == '' || email == 'Champ obligatoire' || !emailPattern.test(email)) {
                    $(this).closestChild(".form-email").addClass("error");
                    $(this).closestChild(".form-email").watermark('Champ obligatoire', { className: 'watermark-error', usenative: false });
                } else {
                    $(this).closestChild(".form-email").removeClass("error");
                }

                if (message == '' || message == 'Champ obligatoire') {
                    $(this).closestChild(".form-message").addClass("error");
                    $(this).closestChild(".form-message").watermark('Champ Obligatoire', { className: 'watermark-error', usenative: false });
                } else {
                    $(this).closestChild(".form-message").removeClass("error");
                }
                if ($(this).find(".error").length == 0 && $(this).parent().find(".waiting-message").length != 1) {
                    $(this).hide();
                    $(this).before("<p class='waiting-message'>Envoie en cours</p>");
                    $.ajax({
                        type: "post",
                        url: $(this).attr("action"),
                        datatype: "json",
                        data: {
                            "name": name,
                            "email": email,
                            "message": message,
                            "submit": "ok"
                        },
                        success: function (message) {
                            $(".waiting-message").replaceWith("<p class='send-message'>" + message + "</p>")
                        }
                    });
                } else {
                    return false;
                }
            });

            $(this).parent().parent("form").submit();

        });
    }
    for (var cpt = 0; cpt < $(".office .mail-button").length; cpt++) {
        $("#mail-button-" + (cpt + 1)).click(function () {
            $(this).parent().parent().children(".form-contact-filler").fadeIn();
        });
    }

    $(".close-button").click(function () {
        $(this).parent().parent().children(".form-contact-filler").fadeOut();
    });
  
  /* Active colors for special pages */
  
  if ($(".accompany-content-wrapper").length != 0){
    $("#main-menu .active a").css({"color" : "#EB1C23"});
  }
  if ($(".guidance-content-wrapper").length != 0){
    $("#main-menu .active a").css({"color" : "#ADC32B"});
  }
  if ($(".develop-content-wrapper").length != 0){
    $("#main-menu .active a").css({"color" : "#F8A433"});
  }

});

function cleanupValue(stringValue) {
    var myRegExp = new RegExp("'", "g");
    var myResult = stringValue.replace(myRegExp, " ");
    return myResult;
}

function resizeElements() {
    var windowHeight = $(window).height();
    var scrollTop = $(window).scrollTop();
  var contentHeight = $(".content-wrapper").height();
    $(".slide-bg").height(scrollTop + windowHeight - 187 - 6);
    $(".handle").height($(".contentContainer").height() - 6);
    $("#menu-column").height($(".content-wrapper").height() + 75);

    var remainingWidth = $("#bottom-content").width() - $("#bottom-content .first").width() - 1;
    var individualWidth = remainingWidth / 3;
    var myBoxes = $(".bottom-box-wrapper[class!='bottom-box-wrapper first']");
    $.each(myBoxes, function (index, value) {
        $(value).width(individualWidth);
    });

    /* Positionnement du footer */
    if(contentHeight + 262 < windowHeight){
      $("#menu-column").height(windowHeight - 187 - 7);
    }
    $(".bg").css({"max-height" : $("#menu-column").height() + "px"});

    if ($("#servicesAccordion").length != 0) {
        $(".handle").height(windowHeight - 187 - 7);
        $(".contentContainer").height($(".handle").height());
    }
  
    resizeIE();
  
}

function validerForm(event) {
    event.preventDefault();
    alert(this);
    var name = cleanupValue($(this).closestChild(".form-name").val());
    var email = cleanupValue($(this).closestChild(".form-email").val());
    var message = cleanupValue($(this).closestChild(".form-message").val());

    if (name == '' || name == 'Champ obligatoire') {
        $(this).closestChild(".form-name").addClass("error");
        $(this).closestChild(".form-name").watermark('Champ obligatoire', { className: 'watermark-error', usenative: false });
    } else {
        $(this).closestChild(".form-name").removeClass("error");
    }
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    if (email == '' || email == 'Champ obligatoire' || !emailPattern.test(email)) {
        $(this).closestChild(".form-email").addClass("error");
        $(this).closestChild(".form-email").watermark('Champ obligatoire', { className: 'watermark-error', usenative: false });
    } else {
        $(this).closestChild(".form-email").removeClass("error");
    }

    if (message == '' || message == 'Champ obligatoire') {
        $(this).closestChild(".form-message").addClass("error");
        $(this).closestChild(".form-message").watermark('Champ Obligatoire', { className: 'watermark-error', usenative: false });
    } else {
        $(this).closestChild(".form-message").removeClass("error");
    }
    if ($(this).find(".error").length == 0 && $(this).parent().find(".waiting-message").length != 1) {
        $(this).hide();
        $(this).before("<p class='waiting-message'>Evnoie en cours</p>");
        $.ajax({
            type: "post",
            url: $(this).attr("action"),
            datatype: "json",
            data: {
                "name": name,
                "email": email,
                "message": message,
                "submit": "ok"
            },
            success: function (message) {
                $(".waiting-message").replaceWith("<p class='send-message'>" + message + "</p>")
            }
        });
    } else {
        return false;
    }
}

function resizeIE() {
    var windowHeight = $(window).height();
    var scrollTop = $(window).scrollTop();
    if (getInternetExplorerVersion() != -1 && getInternetExplorerVersion() < 9) {        
        $(".slide-bg").height(scrollTop + windowHeight - 187);
        $(".container").css({ "width": "100%" });
        
    }
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
