function exists(sFN) {
    if(self[sFN]) return true;
    return false;
}

function trace(msg){
    console.log(msg);
}

function ImageSwap(object, src, dst){
    object.attr('src', object.attr('src').replace(new RegExp(src), dst));
}

function simpleRollover(object, normal, hover){
    
    object.unbind("hover").hover( function() {
        ImageSwap($(this).find("img"), normal, hover);
    }, function() {
        ImageSwap($(this).find("img"), hover, normal);
    });
}

function getObjectData(object){
    var get = new Array();

    object.find("input[type=text], input[type=password], input[type=hidden], textarea").each(function(){
        if ($(this).attr("value")){
            get.push($(this).attr("id")+"="+$(this).attr("value"));
        }
    });
    object.find("select").each(function(){
        if ($(this).attr("value"))
            get.push($(this).attr("id")+"="+$(this).attr("value"));
    });
    object.find("input[type=radio][checked]").each(function(){
        if ($(this).attr("value"))
            get.push($(this).attr("name")+"="+$(this).attr("value"));
    });
    object.find("input[type=checkbox]").each(function(){
        if ($(this).is(':checked'))
            get.push($(this).attr("name")+"=1");
        else 
            get.push($(this).attr("name")+"=0");
    });
    return get.join("&");
}

function removeLinkFromButton(button){
    var a = button.find("a");
    button.attr("rel", a.attr("href"));
    var html = a.html();
    button.find(".button_text").html( html );
    button.addClass("cursor");
}

function getId(str){
    return (str.split('_')[1]);
};

function getHoverImg(img){
    src = img.attr("src");
    var reg=new RegExp("_hover\.gif");
    
    if (src.match(reg)){
        normal = "_hover\.gif";
        hover = "\.gif";
    } else {
        normal = "\.gif";
        hover = "_hover\.gif";
    }
    a = new Array(normal, hover);
    return (a);
}

function isFr(){
    var reg = new RegExp("\/fr\/");
    if (reg.exec(location.href)){
        return true;
    } else {
        return false;
    }
}

function DateIsFilled(date){
    if (date == "DD/MM/YYYY")
        return 0;
    if (date == "")
        return 0;
    return 1;
}

function compareDate(debut,fin) {
    // date format = DD/MM/YYYY
    
    if (parseInt(fin) > -1 && parseInt(debut) > -1){
        
        var reg = new RegExp("\/", "g");
        var tab_fin = fin.split(reg);
        var tab_debut = debut.split(reg);
        
        // reverse date to compare
        var str_fin = parseInt(tab_fin[2]+tab_fin[1]+tab_fin[0]);
        var str_debut = parseInt(tab_debut[2]+tab_debut[1]+tab_debut[0]);
        
        if (str_fin > 0 && str_debut > 0 && str_fin > str_debut){
            return 1;
        } else {
            return null;
        }
    } else {
        return null;
    }
}

// Button rollover
function buttonRollover(){
    // prevent events from behind bound more than once!    
    $(".float_image").each(function(){
        $(this).unbind("hover");   // prevent events from behind bound more than once!
        
        var parent_div = $(this).parent("div");
        var rel = parent_div.attr("rel");
        var a = parent_div.find("a").attr("href");
        
        if (rel != undefined || a != undefined){
            $(this).addClass("cursor");
            parent_div.unbind("hover");
            parent_div.hover( function() {
                img = $(this).find("img");
                files = getHoverImg(img);
                ImageSwap(img, files[0], files[1]); 
        
            }, function() {
                img = $(this).find("img");
                files = getHoverImg(img);
                ImageSwap(img, files[0], files[1]); 
            });
        }
        
        if (a!= undefined){
            $(this).click(function(){
                window.location = $(this).find("a").attr("href");
                return false;
            });
        }
    });
}

function textRollover(object){

    object.hover(function(){
        $(this).addClass("black");
    }, function(){
        $(this).removeClass("black");
    });
}

// Places Select
function placeSelect() {
    
    $("#select_place").hoverIntent( function() {
        ImageSwap($("#current_place_arrow img"), "right\.gif", "down\.gif");
        $(this).removeClass("grey").addClass("black");
        $("#select_place_fold").show();
    }, function() {
        ImageSwap($("#current_place_arrow img"), "down\.gif", "right\.gif");
        $(this).removeClass("black").addClass("grey");
        $("#select_place_fold").hide();
    });
}

// Carousel

function listPlacesShow(item, style, rebind){
    item.removeClass(style)
        .animate({ width: "show", paddingLeft: "12px", paddingRight: "15px" },
            '', '', function(){
                    window[rebind](); // Dynamic function calling: listPlaceNavigationLeft or listPlaceNavigationRight
            }
        );
}

function listPlacesHide(item, style){
    item.addClass(style).animate({paddingLeft: "0px", paddingRight: "0px",  width: "hide" });
}

function listPlaceNavigationLeft(){
    $("#home_place_left").click(function(){
        $(this).unbind('click');    // unbind to avoid simultaneous actions
        
        if ($(".before").size()){
            listPlacesHide($(".home_place:visible:last"), "after");
            listPlacesShow($(".before:last"), "before", "listPlaceNavigationLeft");
            
            if(!$(".before").size()){
                $("#home_place_left").hide();
                $("#home_place_right").show();
            }
        }
    });
}

function listPlaceNavigationRight(){
    $("#home_place_right").click(function(){
        $(this).unbind('click');
        if ($(".after").size()){
            listPlacesHide($(".home_place:visible:first"), "before");
            listPlacesShow($(".after:first"), "after", "listPlaceNavigationRight");
            
            if (!$(".after").size()){
                $("#home_place_right").hide();
                $("#home_place_left").show();
            }
        }
    });
}

function listPlacesNavigation(){
    
    $("#home_place_navigation").show();
    simpleRollover($("#home_place_right").parent(), "\.gif", "_hover\.gif");
    simpleRollover($("#home_place_left").parent(), "\.gif", "_hover\.gif");
    
    listPlaceNavigationLeft();
    listPlaceNavigationRight();
}


function listPlaces(){
    
    if ($.browser.msie && $.browser.version == '7.0'){ // Needed in ie7
        $(".place_over_text span").css("background", "transparent");
        $(".place_over_text span").css("filter", "alpha(opacity=100);");
    } else  if ($.browser.msie && $.browser.version == '8.0'){ // Needed in ie8
        $(".place_over_text").css("background-color", "#FFFFFF");
        $(".place_over_text").css("opacity", "0.5");
    }
    
    $(".place_over_text").hover(function(){
        if (!$.browser.msie)
            $(this).css("background-color", "rgba(150, 150, 150, 0.5)");
        else if ($.browser.version != '7.0'){
            $(this).css("background-color", "#969696");
        }
        
        $(this).find("span:first").removeClass("black").addClass("white");
        $(this).find(".darkgrey").removeClass("darkgrey").addClass("black");
        
    }, function(){
        if (!$.browser.msie)
            $(this).css("background-color", "rgba(250, 250, 250, 0.5)");
        else if ($.browser.version != '7.0'){
            $(this).css("background-color", "#FFFFFF");
        }
        
        $(this).find(".black").removeClass("black").addClass("darkgrey");
        $(this).find(".white").removeClass("white").addClass("black");
    });
    
    $(".home_place").each(function(i){
        if (i > 3)
            $(this).addClass("after").hide();
            
        if (i == 4)
            listPlacesNavigation();
    }); 
}


function rebind(){
    buttonRollover();
}

$(document).ready(function(){

     if($.browser.msie && $.browser.version == '6.0'){
        $('#beta').show();
    }

    // HEADER PlaceSelecter
    $("#select_place_fold").hide();
    rebind();
    
    /*  PLACE  */
    if (exists('placeFunctions')){
        placeFunctions();
    }
    
    /*   HOME   */
    if(exists('homeFunctions')){
        homeFunctions();
        $("#select_place").empty();
    } else {
        placeSelect();
    }
    
    /*  PAGE  */
    if (exists('pageFunctions')){
        pageFunctions();
    }
    
    /*  BOOK & PAYMENT  */
    if (exists('bookFunctions')){
        bookFunctions();
    }
    
    if (isFr()){
        var days = ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'];
        var months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'];
    } else {
        var days = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
        var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    }
    
    $.datepicker.setDefaults({
       firstDay: 1,
       clickInput: true,
	   createButton: false,
	   minDate: new Date(),
	   showAnim: 'blind',
	   dayNamesMin: days,
	   monthNames: months,
	   beforeShow: function() {$('#ui-datepicker-div').maxZIndex(); }
    });
    
});

$.maxZIndex = $.fn.maxZIndex = function(opt) {
    var def = { inc: 10, group: "*" };
    $.extend(def, opt);
    var zmax = 0;
    $(def.group).each(function() {
        var cur = parseInt($(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
    if (!this.jquery)
        return zmax;

    return this.each(function() {
        zmax += def.inc;
        $(this).css("z-index", zmax);
    });
}