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

                var timer = setInterval("placeSlideshow()", 5000);
                placeGallery(timer);
                $("#place_menu_list .room").hide();
                placeTextSwitch(timer);
            }  
        })
        .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"));
    });
    
    placeBookButton();
}

function clearTimer(timer){
    clearInterval(timer);
}

function placeGalleryAnimate(way, img){

    active = $('#place_gallery_images IMG.active');
    if ( active.length == 0 ) active = $('#place_gallery_images IMG:last');
    
    if (way == "next"){
        // unbind to avoid tilt effect + switch to normal button state
        $("#gallery_navigate_right").unbind('click');
        
        img = active.next();
        if(!img.attr("id")){
            img = $('#place_gallery_images IMG:first');
        }
    } else if (way == "prev") {
        $("#gallery_navigate_left").unbind('click');
        
        img = active.prev();
        if(!img.attr("id")){
            img = $('#place_gallery_images IMG:last');
        }
    }

    if (active.attr("src") != img.attr("src")){ // Don't change if same image.
        $('#place_mask_title').html(img.attr("title"));
    
        active.addClass('last-active');
        img.css({opacity: 0.0})
            .addClass('active')
            .animate({opacity: 1.0}, 500, function() {
                active.removeClass('active last-active');
            
                // rebind button
                if (way == "next"){
                    placeGalleryRight();
                } else {
                    placeGalleryLeft();
                }
            });
    }
}

function placeSlideshow() {

    var active = $('#place_gallery_images IMG.active');
    if ( active.length == 0 ){
        active = $('#place_gallery_images IMG:last');
    }
    
    var next = $('#place_gallery_images IMG:first');
    if (active.next().length){
        var next = active.next()
    }
    
    $('#place_mask_title').html(next.attr("title"));
    
    active.addClass('last-active');
    next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 500, function() {
            active.removeClass('active last-active');
        });
}

function placeGalleryLeft(timer){
    
    $("#gallery_navigate_left").click( function() {
        clearTimer(timer)
        placeGalleryAnimate("prev");
    });
}

function placeGalleryRight(timer){

    $("#gallery_navigate_right").click( function() {
        clearTimer(timer)
        placeGalleryAnimate("next");
    });
}

function placeGallery(timer){
    
    /*  Hover effects  */
    simpleRollover($("#gallery_navigate_left"), "\.gif", "_hover\.gif");
    simpleRollover($("#gallery_navigate_right"), "\.gif", "_hover\.gif");

    placeGalleryLeft(timer);
    placeGalleryRight(timer);
}

function placeTextSwitch(timer){
    
    $("#place_menu_list li").click(function (){
        
        if (!$(this).find('a').hasClass("active_menu")){
            $("#place_menu_list a.active_menu").removeClass("active_menu");
            $(this).find('a').addClass("active_menu");
        
            $.ajax({
                type: "GET",
                url: $(this).find("a").attr("href"),
                success: function(data){
                    $("#place_text_center").html(data);
                }
            });
            
            if ($(this).hasClass("rooms"))
                $("#place_menu_list .room").slideDown();
        
            if ($(this).hasClass("room")){
                clearTimer(timer);
                if ($("#place_gallery_images img."+$(this).attr("id")).size())
                    placeGalleryAnimate("jump", $("#place_gallery_images img."+$(this).attr("id")));
            }
        }
        return false;
    });
}

function placeDecoration(){
    // call color page
    $.get("/color/"+$("ul#place_menu_list").attr("rel")+"/", 
        {}, 
        function(data){
            $("html").css("background-color", data);
        });
    
    // call background image page
    $.get("/background/"+$("ul#place_menu_list").attr("rel")+"/", 
        {}, 
        function(data){
            $("html").css("background-image", "url("+data+")");
        });
}



//  Manages the book form display
function placeBookButton(){
        
    if ($("#step1").html()){ // already been dumped?
    
        // Need to bind Step1!
        bindStep1();
        //$("#place_text_book img").hide();
        
    }
    
       $("#place_text_book").click(function(){
       
            // force reset of the image
            //$("#place_text_book img").attr("src", "/images/105x105.gif");
            //$("#place_text_book").empty();
                 
            $.get(
                $("#place_text_book").attr("rel"), 
                {}, 
                function(data){
                    $("#place_book_step1").html(data);
                    // Need to bind step 1!
                    bindStep1();
                    $("#step1_errors").empty();
                    //$('html, body').animate({scrollTop:1000}, 'slow');
                    $.scrollTo($("#step1"), 800);
                }
            );
        });
    
    if ($("#step2").html()){
        bindStep2();
    }
}






