function homeFunctions(){
    
    $('#home_slideshow_images').css("display", "none");
    $("#home_slideshow_load img").each(function(i){
        var img = new Image();
        $(img).load(function () {
            $('#home_slideshow_images').append(this);
            
            if ($("#home_slideshow_load img").size() - 1 == i){ // To be executed at the end of all image loading
                $("#home_slideshow_load").empty();
                $("#home_slideshow_images").fadeIn("slow");

                setInterval( "homeSlideshow()", 8000 );
            }  
        })
        .attr("src", $(this).attr("src"))
        .attr("class", $(this).attr("class"))
        .attr("id", $(this).attr("id"))
        .attr("title", $(this).attr("title"))
        .attr("alt", $(this).attr("alt"));
    });
    
    textRollover($("#home_mask_link a"));
    homeSlideshowClick("#home_slideshow_images IMG.active");

    homeMenu();
    listPlaces();
    homeNews();
    homeSearch();
    homeResults();
    $("#home_slideshow .float_image").unbind().parent("div").unbind();
    rebind();
}

function homeSlideshowClick(object){
    $("#home_slideshow").click(function(){
        window.location.href = $('#home_mask_link a').attr("href");
    });
}

function homeSlideshow() {

    homeSlideshowClick($('#home_slideshow_images IMG.active'));
    
    var $active = $('#home_slideshow_images IMG.active');

    if ( $active.length == 0 ) $active = $('#home_slideshow_images IMG:last');
        
    var $next =  $active.next().length ? $active.next()
        : $('#home_slideshow_images IMG:first');

    $active.addClass('last-active');

    $('#home_mask_title').html($next.attr("title"));
    $('#home_mask_text').html($next.attr("alt"));
    $('#home_mask_link a').attr("href", $next.attr("rel"));
    
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
            
        });
    homeSlideshowClick($next);
}


function homeMenu(){ 
    textRollover($("#home_center_menu_list a"));

    $("#home_center_menu_list a").click(function(){
        var href = $(this).attr("href").replace(new RegExp("/fr/|/en/"), "/block/");
        
        $("#home_center_menu_list .current_menu").removeClass("current_menu").addClass("home_center_menu_list_a");
        $(this).addClass("current_menu");
        
        $.get(href, 
            {}, 
            function(data){
                $("#home_center_content").html(data);
                
                ar = href.split('/');
                page = ar[ar.length-2]
                if (page == "news"){
                    homeNews();
                } else if (page == "search"){
                    homeSearch();
                }  else {
                    listPlaces();
                }
                $("#home_slideshow .float_image").unbind().parent("div").unbind();
                rebind();
        });  
        return false;
    });
}

function newsHideButton(object){
    object.css("visibility", "hidden");
}

function newsShowButton(object){
    object.css("visibility", "visible");   
}

function homeNewsNavigation(){
    if ($(".home_news_item:visible").size() == 1)
        newsHideButton($("#home_news_down"));
    else
        newsShowButton($("#home_news_down"));
    
    if ($(".home_news_item:hidden").size() > 0)
        newsShowButton($("#home_news_up"));
    else
        newsHideButton($("#home_news_up"));
}

function homeNews(){
    if ($(".home_news_item").size() > 1){
        simpleRollover($("#home_news_up"), "\.gif", "_hover\.gif");
        simpleRollover($("#home_news_down"), "\.gif", "_hover\.gif");
    
        $("#home_news_down").click(function(){
            $(".home_news_item:visible:first").slideUp(function(){
                homeNewsNavigation();
            });
        });
        
        $("#home_news_up").click(function(){
            $(".home_news_item:hidden:last").slideDown(function(){
                homeNewsNavigation();
            });
        });
        
        homeNewsNavigation();
    } else {
        newsShowButton($("#home_news_up"));
        newsShowButton($("#home_news_down"));
    }
}

function homeSearch(){
    
    $("ul.radio_list input").hide();
    
    // Rollover List place
    textRollover($("#search_left li"));
    
    $("#search_form .submit").click(function(){
        
        var dataStr = getObjectData($("#search_form"));
         
        var from = $('#from').attr("value");
        var to = $('#to').attr("value");
    
        if ((from == '' && to == '') || compareDate(from, to) != null){
      $.ajax({
            type: "POST",  
            url: $("#search_form").attr('action'),  
            data: dataStr,
            success: function(data) {
                
                $('#home_center_content').html(data);
                homeSearch();
                
                $("input[@id='places']:checked").parents("li").addClass("radio_list_active");
                
                //console.log(dataStr);
                if ($("#success").attr("value")) {
        
                    $.ajax({
                        type: "GET",
                        url: $("#home_result").attr('rel'),
                        data: dataStr,
                        success: function(data) {
                            $('#home_result').show().html(data);
                            homeResults();
                            $('html, body').animate({scrollTop:540}, 'slow');
                        }
                    });
                }
                /*$('#loader').hide();*/
            }  
        });
        
        } else {
            if (isFr()){
                $("#search_error > span").html("La date de début doit être avant la date de fin.");
            } else {
                $("#search_error > span").html("Arrival date must be before departure date.");
            }
        }
      });
    
    $(".radio_list > li").click(function(){
        $(".radio_list .radio_list_active").removeClass("radio_list_active");
        $(this).addClass("radio_list_active");
        $(this).find("input").attr("checked", true);
    });
    
    $('#from').datepicker({
	   onClose: function(dateText, inst)
		{
			$("#add_from").html(dateText);
			
			if (DateIsFilled($('#from').attr("value")) 
			     && compareDate($('#from').attr("value"), $('#to').attr("value")) == null){
			 
			     var to = new Date();
	             if ($("#from").attr("value")){
	               from = $("#from").attr("value");
	               var array = from.split(new RegExp("\/", "g"));
	               to.setDate(array[0]);
	               to.setMonth(array[1]-1);  // 0 based
	               to.setYear(array[2]);
	               to.setDate(to.getDate() + 1);
	               
	               $('#to').datepicker('option', 'minDate', to);
	               
			       $("#to").attr("value", (
			         to.getDate() < 10 ? "0"+to.getDate() : to.getDate())+"/"
			         +(to.getMonth()+1 < 10 ? "0"+(to.getMonth()+1) : (to.getMonth()+1))+"/"
			         +to.getFullYear());
                }
            } 
		}
    });
    
    $("#to").datepicker({});
}

function homeResults(){
    
    /*   Initialisation   */
    //$(".compact:first").hide();
    $(".full").hide();
    //$(".full:first").show();
    rebind();
    
    
    $(".compact").hover(function(){
         $(this).children(".compact_view").removeClass("lightgrey").addClass("black");
    }, function(){
        $(this).children(".compact_view").removeClass("black").addClass("lightgrey");
    });
    
    
    /*  Animation  */
    $(".compact").click(function(){
        $(".compact").show();
        $(this).hide();
        
        $(".full").hide();
        var id = $(this).attr('id').substr(8,$(this).attr('id').length);
        $("#full-"+id).show();
    });
}





