function bookFunctions(){
    // Back from payment
    if (!exists('placeFunctions')){
        placeSelect();
    }
}

function displayImage(img){
    img.fadeIn();
}


function bindStep1(){
    rebind();
    bookRoomChoice();
    bookDateChoice();
    bookAddSelection();
}

function bookRoomChoice(){
    
    //$("#room_choice div.box_content img.active").onImagesLoad({
    //    selectorCallback: displayImage
    //});
    
    // list click
    $("#room_list > ul > li").click(function(){
    
        var id = $(this).attr('id').substr(5, $(this).attr('id').length); // room_id
        $("#room_id").attr("value", id);
        if (!id)
            $("#room_id").attr("value", "place");
                
        ValidateStep1();
        
        $("#nb_adult").attr("rel", $(this).attr("rel"));
        $("#nb_adult").attr("value", 1);
        $("#nb_children").attr("rel", $(this).attr("rel"));
        $("#nb_children").attr("value", 0);
        return false;
    });
    textRollover($("#room_list > ul > li")); // Should be natural link.
}

function bookDateChoice(){
    simpleRollover($("#date_control_previous"), "\.gif", "_hover.gif");
    simpleRollover($("#date_control_next"), "\.gif", "_hover.gif");
    
    $("#date_control_previous img").click(function(){
        $("#date").attr("value", $("#date_control_previous").attr("value"));
        ValidateStep1();
    });
    
    $("#date_control_next img").click(function(){
        $("#date").attr("value", $("#date_control_next").attr("value"));
        ValidateStep1();
    });
    
	$('#from').datepicker({
	   onClose: function(dateText, inst)
		{
			if (DateIsFilled($('#from').attr("value"))  
			 && compareDate($('#from').attr("value"), $('#to').attr("value")) == null){
			 
	           from = $("#from").attr("value");
	           var array = from.split(new RegExp("\/", "g"));
               $("#date").attr("value", array[2]+"-"+array[1]);
			 
			   var to = new Date();
	           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);
			    
			    var to_str = "";
			    if (to.getDate() < 10){
			     to_str += "0";
			    }
			    to_str +=  to.getDate()+"/";
			    if (to.getMonth()+1 < 10){
			     to_str += "0";
			    }
			    to_str +=  to.getMonth()+1;
			    to_str += "/"+to.getFullYear();
			    $("#to").attr("value", to_str);
			}	
			ValidateStep1();
		}
    });
    
    $("#to").datepicker({
        onClose: function(dateText, inst) {
            if (DateIsFilled($('#to').attr("value"))  
			 && compareDate($('#from').attr("value"), $('#to').attr("value")) == null){
			     var reg = new RegExp("\/", "g");
			     var tab_fin = $("#to").attr("value").split(reg);
                 var tab_debut = $("#from").attr("value").split(reg);
	             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){
	                $('#from').attr("value", "");
			     }
			 }
			 ValidateStep1();
		}
    });
    
    $("#date_choice select").keyup(function(){
        ValidateStep1();
    });
    
    buttonPlusLess($("#person_select > div > span"), "validate", 1);
}

function bookAddSelection(){
    $("#add_button").click(function(){
        var dataStr = ValidateStep1();
        if ($('#form1success').attr("value")){
                
            $.ajax({
                type: "GET",  
                url: $("#add_button").attr('rel'),
                data: "add=1&"+dataStr,
                success: function(data) { 
                    $('#place_book_step2').html(data);
                    bindStep2();
//                    $('html, body').animate({scrollTop:1700}, 'slow');
                    $.scrollTo($("#step2"));
                }  
            });
        }
    });
}


function buttonPlusLess(object, call, step){
    
    simpleRollover(object, "\.gif", "_hover.gif");
    object.find(".up, .down").click(function() {
        
        var button = $(this);
        var input = button.parent().parent().find("input");
        var oldValue = input.val();
        if (input.attr("id") == "nb_adult"){
            var other = $("#nb_children");
        } else {
            var other = $("#nb_adult");
        }
        
        var max = button.parent().parent().find("input").attr("rel") - other.attr("value");
        if (!oldValue){
            oldValue = 0;
        }

        if (button.attr("class") == "up") {
            if (max == oldValue){
                return;
            }
            var newVal = parseFloat(oldValue) + 1;
	   } else {
	       if (oldValue >= 1) {
	           var newVal = parseFloat(oldValue) - 1;
	       }
	   }
	   
	   button.parent().parent().find("input").val(newVal);
	   if (call == "validate"){
	       if (step == 1)
    	       ValidateStep1();
    	   else 
    	       validateStep2();
	   }
    });
}

function ValidateStep1(){

    var dataStr = getObjectData($("#step1"));
    $.ajax({
        type: "GET",  
        url: $("#bookForm1").attr('action'),
        data: dataStr,
        success: function(data) {
            $('#place_book_step1').html(data);
            bindStep1();
            /*$("#date_control_previous img").show();*/
        }
    });
    return dataStr;
}

/*
    STEP 2
*/

function bindStep2(){

    rebind();
//    simpleRollover($("#date_control_previous"), "\.gif", "_hover.gif");

    /*$("#selection_book_button").hover(function(){
        $(this).attr("src", "/images/80x80_hover.gif");
    },function(){
        $(this).attr("src", "/images/80x80.gif");
    });*/

    bookAddService();
    bookValidateCart();
    bookDelete();

}

function bookAddService(){
    buttonPlusLess($(".service_input span"), "validate", 2);

    $(".service_input input[type=checkbox]").click(function(){
        validateStep2("&addService=1");
    });    
    
    $(".service_input img").click(function(){
        validateStep2("&addService=1");
    });
}

function bookDelete(){
    
    simpleRollover($(".selection_item_delete"), "\.gif", "_hover.gif");
    
    $(".selection_item_delete")
        .click(function() {
            var str = "&del=1&i="+$(this).attr("id").substr(7, $(this).attr("id").length );
            validateStep2(str);
        });
}

function bookValidateCart(){
    
    $("#add_second a").click(function(){
        $("#from").attr("value", "");
        $("#to").attr("value", "");
        $("#nb_adult").attr("value", 1);
        $("#nb_children").attr("value", 0);
        $("#room_id").attr("value", $("#room_list > ul > li:first").attr('id').substr(5, $("#room_list > ul > li:first").attr('id').length));
        ValidateStep1();
//        $('html, body').animate({scrollTop:1000}, 'slow');
        $.scrollTo("#step1", 600);
        return false;
    });
    
    $("#selection_book_button").click(function(){
        $(this).find("img.button_image:first").attr("src", "/images/80x80_hover.gif");
        if ($(this).find('.button_text').html() == "annuler" || $(this).find('.button_text').html() == "cancel"){
            window.location.reload();
        } else {
            $.ajax({
                type: "GET",
                url: $("#selection_book_button").attr("rel"),
                data: getObjectData($("#step2"))+"&addService=1",
                success: function(data){
                    // Hide Place book
                    $("#place_text_book").removeClass("cursor").hide();
                    $("#place_book_step1").empty();
                
                    // Change to cancel button
                    if(isFr()){
                        var cancel_label = "annuler";
                    } else {
                        var cancel_label = "cancel";
                    }
                
                    $("#selection_book_button").find(".button_text")
                        .html(cancel_label);
                
                    // unbind and hide x button
                    $(".selection_item_delete").unbind().hide();
                    $(".service_input img").each(function(){
                        $(this).css("visibility", "hidden");
                    });
                    $(".service_input input[type=checkbox]").each(function(){
                        $(this).attr("disabled", true);
                    });
                    
                    $("#add_second").hide();
                
                    $("#place_book_step3").show().html(data);
                    bindStep3();
                    $("#details .box_help").empty();
                
 //                   $('html, body').animate({scrollTop:1500}, 'slow');
                    $.scrollTo("#step3", 800);
                }
            });
        }
        return false;
    });
}

function validateStep2(str){
    
    var dataStr = getObjectData($("#step2"));
    if (str)
        dataStr += str;
    $.ajax({
        type: "GET",  
        url: $("#bookForm2").attr('action'),
        data: dataStr,
        success: function(data) {
            $('#place_book_step2').html(data);
            bindStep2();
        }
    });
    return dataStr;
}

/*

    STEP 3

*/

function bindStep3(){
    simpleRollover($("#details_pay_button"), "\.gif", "_hover.gif");
    
    $("#details_pay_button").click(function(){
        
        $.ajax({
            type: "GET",  
            url: $("#bookForm3").attr('action'), 
            data: getObjectData($("#step3")),
            success: function(data) {
                $('#place_book_step3').html(data);
                bindStep3();
                
                if ($("#form3success").attr("value") == 1){
                    
                    $.ajax({
                        type: "GET",
                        url: $("#details_pay_button").attr("rel"),
                        success: function(data){
                            $("#place_book_step4").html(data).show();
                            
                            bindStep4();
                            
                            $.scrollTo("#step4", 800);
                        }
                    });
                } else {
                
//                    $('html, body').animate({scrollTop:1500}, 'slow');
                    $.scrollTo("#step3", 800);
                }
            }  
        }); 
    });
}

/*  step 4  */

function bindStep4(){
    simpleRollover($("#credit_card"), "\.gif", "_hover.gif");
    simpleRollover($("#transfer_button"), "\.gif", "_hover.gif");
                            
    $("#credit_card").click(function(){
        
        $(this).unbind();
        $("#transfer_button").find("img.button_image:first").attr("src", "/images/80x80.gif");
        $(this).find("img.button_image:first").attr("src", "/images/80x80_hover.gif");
        
        $.ajax({
            type: "GET",
            url: $("#details_pay_button").attr("rel"),
            data: "bank=1",
            success: function(data){
                $("#bank").show();
                $("#transfer").hide();
                $("#credit_card").find("img.button_image:first").attr("src", "/images/80x80_hover.gif");
                simpleRollover($("#credit_card"), "\.gif", "_hover.gif");
            }
        });
    });
                            
    $("#transfer_button").click(function(){
        $(this).unbind()
        $("#credit_card").find("img.button_image:first").attr("src", "/images/80x80.gif");
        $(this).find("img.button_image:first").attr("src", "/images/80x80_hover.gif");
        
        $.ajax({
            type: "GET",
            url: $("#details_pay_button").attr("rel"),
            data: "transfer=1",
            success: function(data){
                $("#bank").hide();
                $("#transfer").show();
                
                $("#transfer_button").find("img.button_image:first").attr("src", "/images/80x80_hover.gif");
                simpleRollover($("#transfer_button"), "\.gif", "_hover.gif");
            }
        });
    });
}

