// JavaScript Document
/*************************************************************************/

function echeck(str) {
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false
    }
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false
    }
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false
    }
    if (str.indexOf(at, (lat + 1)) != -1) {
        return false
    }
    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false
    }
    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false
    }
    if (str.indexOf(" ") != -1) {
        return false
    }
    return true
}
var openeditem = -1;
var mustopen = false;
var matchedWidth;
var matchedHeight;
$(function () {
    var docloc = document.location.toString();
    //if(docloc == 'http://www.coco-pazzo.com/restaurant/le-meilleur-restaurant-italien-de-mont-tremblant/'){
    //$("ul:eq(0)", "#extracontent").find('a:eq(0)').css({'font-weight':'bold'});
    $("ul", "#extracontent").find('a').each(function () {
        if ($(this).attr('href') == docloc) {
            $(this).css({
                'font-weight': 'bold'
            });
        }
    });
    //}
    //
    $("a[rel='imagegallery']").colorbox();
    $("input[type=submit]", "#dialogreservation").click(function () {
        if ($('#res_nom').val() == "" || $('#res_prenom').val() == "" || $('#res_telephone').val() == "" || $('#res_email').val() == "" || $('#res_nombre').val() == "" || $('#res_date').val() == "" || $('#res_heure').val() == "") {
            if ($('#isEN').length) {
                alert('Please fill all fields');
            } else {
                alert('Veuillez remplir tous les champs.');
            }
            return false;
        }
        if (!echeck($('#res_email').val())) {
            if ($('#isEN').length) {
                alert('Your email is invalid.');
            } else {
                alert('Votre courriel est invalide.');
            }
            return false;
        }
        $.post('http://www.coco-pazzo.com/sendmail.php', {
            'act': 'RESERVATION',
            'titre': $('#res_titre').val(),
            'name': $('#res_prenom').val() + ' ' + $('#res_nom').val(),
            'telephone': $('#res_telephone').val(),
            'mail': $('#res_email').val(),
            'hotel': $('#res_hotel').val(),
            'nombre': $('#res_nombre').val(),
            'date': $('#res_date').val(),
            'heure': $('#res_heure').val(),
            'remarques': $('#res_remarques').val()
        }, function (p_data) {
            if (p_data.res == 'TRUE') {
                if ($('#isEN').length) {
                    alert('Thanks, your message has been sent.');
                } else {
                    alert('Merci, votre message Ã  Ã©tÃ© envoyÃ©.');
                }
                $('#res_prenom').val("");
                $('#res_nom').val("");
                $('#res_telephone').val("");
                $('#res_email').val("");
                $('#res_nombre').val("");
                $('#res_date').val("");
                $('#res_heure').val("");
                $('#res_titre').val("");
                $('#res_hotel').val("");
                $('#res_remarques').val("");
                $("#dialogreservation").dialog('close'); /**/
            }
        }, 'json');
    }); /*************************************************************************/
    $("a[rel=reservation]", "#maincontent").click(function () {
        $("#dialogreservation").dialog('open');
    }); /*************************************************************************/
    $("input[type=submit]", "#nousjoindre").click(function () {
        if ($('#Full_Name').val() == "" || $('#Email_Address').val() == "" || $('#Your_Message').val() == "") {
            alert('Veuillez remplir tous les champs.');
            return false;
        }
        if (!echeck($('#Email_Address').val())) {
            alert('Votre courriel est invalide.');
            return false;
        }
        $.post('http://www.coco-pazzo.com/sendmail.php', {
            'act': 'CONTACT',
            'name': $('#Full_Name').val(),
            'mail': $('#Email_Address').val(),
            'message': $('#Your_Message').val()
        }, function (p_data) {
            if (p_data.res == 'TRUE') {
                alert('Merci, votre message Ã  Ã©tÃ© envoyÃ©.');
                $('#Full_Name').val("");
                $('#Email_Address').val("");
                $('#Your_Message').val("");
                $("#nousjoindre").dialog('close');
            }
        }, 'json');
/*$('#Full_Name').val("");

		$('#Email_Address').val("");

		$('#Your_Message').val("");*/
        return false;
    }); /*************************************************************************/
    $(".dialog").dialog({
        autoOpen: false,
        height: 900,
        width: 450,
        modal: true
    });
    $("#res_date").datepicker();
    $('.ui-datepicker').css({
        'display': 'none'
    }); /*************************************************************************/
    // BUTTON PART
    $('.arrow', '#extracontent').click(function (e) {
        $('#extracontent').data('open', false);
        mustopen = false;
        $('#maincontent').animate({
            'margin-left': '-560px'
        }, function () {
            $('#extracontent').animate({
                'margin-left': -210
            });
        });
    });
    $('li', '#subcontent').click(function (e) {
        $('li', '#subcontent').removeClass('active');
        //console.log($(this).find('a').attr('href'));
        //return false;
        var index = $(this).index();
        if (!mustopen && index == 0) {
            document.location = $(this).find('a').attr('href');
            return false;
        }
        if (!mustopen && index == 1) {
            document.location = $(this).find('a').attr('href');
            return false;
        }
        if (!mustopen && index == 2) {
            document.location = $(this).find('a').attr('href');
            return false;
        }
        if (!mustopen && index == 3) {
            document.location = $(this).find('a').attr('href');
            return false;
        }
        //console.log($('ul', '#extracontent').eq(index));
        $('ul', '#extracontent').css({
            'display': 'none'
        });
        $('ul', '#extracontent').eq(index).css({
            'display': 'block'
        });
        if (!mustopen) {
            $('#maincontent').animate({
                'margin-left': '-541px'
            });
        }
        if (openeditem == index) {
            if ($('#extracontent').data('open') == true) {
                $(this).removeClass('active');
                openeditem = -1;
                $('#extracontent').data('open', false);
                if (mustopen) {
                    mustopen = false;
                    $('#maincontent').animate({
                        'margin-left': '-560px'
                    }, function () {
                        $('#extracontent').animate({
                            'margin-left': -210
                        });
                    });
                } else {
                    $('#extracontent').animate({
                        'margin-left': -210
                    });
                }
            } else {
                $(this).addClass('active');
                openeditem = index;
                $('#extracontent').data('open', true);
                $('#extracontent').animate({
                    'margin-left': 203
                }, function () {
                    if (mustopen) {
                        $('#maincontent').animate({
                            'margin-left': '360px'
                        });
                    }
                });
            }
        } else {
            $(this).addClass('active');
            openeditem = index
            $('#extracontent').animate({
                'margin-left': -210
            }, function () {
                $('#extracontent').animate({
                    'margin-left': 203
                }, function () {
                    if (mustopen) {
                        $('#maincontent').animate({
                            'margin-left': '334px'
                        });
                    }
                    mustopen = false;
                });
            });
            $('#extracontent').data('open', true);
        }
        return false;
    }); /*************************************************************************/
    { // BUTTON ON LOAD PART
        //console.log(document.location.toString().indexOf('restaurant'));
        //$('div:first', '#maincontent').css({ 'min-height':'215px', 'margin':'55px 0 35px 0 '});
        var requestURI = document.location.toString();
        //$('div:first', '#maincontent').css({ 'width':'179px', 'height':'217px', 'margin':'0px 0 35px 0 '});
        //http://www.coco-pazzo.com/the-best-of-italian-cuisine-in-mont-tremblant
        if (requestURI.indexOf('le-chef-vous-cuisine-dexquises-specialites-italiennes') != -1) {
            //console.log('open page');
            mustopen = true;
            //$('div:first', '#maincontent').css({'background':'url(http://www.coco-pazzo.com/images/chef.png) left top no-repeat', 'width':'179px', 'height':'247px', 'margin':'55px 0 35px 0 '});
        }
        if (docloc.indexOf('meilleur-cuisine-italienne-mont-tremblant') != -1 || requestURI.indexOf('/restaurant/') != -1 || requestURI.indexOf('the-best-of-italian-cuisine-in-mont-tremblant') != -1) {
            mustopen = true;
            $('li:eq(0)', '#subcontent').click();
        } else if (requestURI.indexOf('/boutique/') != -1) {
            mustopen = true;
            $('li:eq(1)', '#subcontent').click();
        }
    } /*************************************************************************/
    // W3C COMPATIBILITY PART
    $('a[href^="http://"]:not([href^="http://www.coco-pazzo.com/"]):not([href^="http://www.coco-pazzo.com/"])').addClass('external').attr('target', '_blank'); /*************************************************************************/
    { // IE6 SUPPORT PART
        if ($.browser.msie && parseInt($.browser.version, 10) == 6) {
            //		alert('IE6')
            $('html').addClass('IE6');
            $('div', '#subcontent').eq(0).addClass('logo');
        }
    } /*************************************************************************/
    { //SLIDESHOW PART
        if (!$.browser.msie || ($.browser.msie && parseInt($.browser.version, 10) >= 6)) {
            if (document.location.toString().indexOf('/home')) {
                //check browser aspect ratio against window aspect ratio
                $('#backpics').cycle({
                    fx: 'fade',
                    speed: 2000,
                    timeout: 5000
                });
            }
        }
    } /*************************************************************************/
    { // ONRESIZE PART
        resizeIt();
        $(window).resize(function () {
            resizeIt(); /*$('div.current-slide').resizeForBack();*/
        });
    } /*************************************************************************/
    /*************************************************************************/
    { // CSS PART
		/*
        $("#maincontent").append($('<div>').css({
            'height': 25
        }));
		*/
        $('div:eq(0)', "#footer").css({
            'border-top': '3px solid #FFF',
            'position': 'absolute',
            'width': '100%',
            'top': '0',
            'left': '200px'
        });
        //$('p:eq(0)',"#infos").css({'font-size':'22px','margin':'0'});
        $('p:eq(1)', "#infos").css({

            'font-size': '18px',
            'margin': '0 0 12px 0'
        });
        //$('li', '#subcontent').eq(0).css({'font-weight':'bold'});
        $('li', '#subcontent').css({
            'margin': '0 0 8px 0'
        });
        $('li', '#subcontent').eq(2).css({
            'margin': '30px 0 12px 0'
        });
        $('li:lt(2)', '#subcontent').css({
            'font-size': '18px'
        });
    } /*************************************************************************/
});
$(window).load(function () {
    $("body").css({
        'visibility': 'visible'
    });
}) /*************************************************************************/

function resizeIt() {
    //alert(parseInt($('#subcontent').height(),10));
    if (parseInt($('#subcontent').height(), 10) < 720) {
        $('#subcontent').css({
            'height': '700px'
        });
    }
    if (parseInt($('#subcontent').height(), 10) < $(window).height()) {
        $('#subcontent').css({
            'height': parseInt($(window).height(), 10)
        });
    }
    $('#minusfooter').equalHeights();
    //console.log($('#subcontent').height());
    //$('#footer').css({'top':parseInt($('#subcontent').height(),10)});
    $('#minusfooter').css({
        'height': parseInt($('#subcontent').height(), 10)
    });
    $('#backpics').css({
        'height': parseInt($('#subcontent').height(), 10),
        'width': $(window).width()
    });
} /*************************************************************************/
$.fn.equalHeights = function () {

    var currentTallest = 920;
    if (document.location.toString() == 'http://www.coco-pazzo.com/') {
        currentTallest = 400;
    }
    if (document.location.toString() == 'http://www.coco-pazzo.com/the-best-of-italian-cuisine-in-mont-tremblant/') {
        currentTallest = 775;
    }
    if (document.location.toString() == 'http://www.coco-pazzo.com/restaurant/la-cave-a-vin-de-notre-sommelier/') {
        currentTallest = 900;
    }
    if ($('body').attr('id') == 'galerie') {
        currentTallest += 250;
    }
    $(this).each(function () {
        $(this).children().each(function (i) {
            //if($(this).attr('id')=="extracontent"){return;}
            if ($(this).attr('id') != "extracontent" && parseInt($(this).height(), 10) - 50 > currentTallest) {
                currentTallest = parseInt($(this).height(), 10) - 50;
            }
            //
            //			
            //console.log($(this).attr('id')+'_'+$(this).height());
        });
        currentTallest = parseInt(currentTallest, 10);
        //console.log($(this).children());
        // for ie6, set height since min-height isn't supported
        //if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
        $(this).children().css({
            'height': currentTallest
        });
    });
    return this;
	
}; /*************************************************************************/
