$(function(){

    //the html element where we render the request form
    var requestFormInDOM = $("#request_container");

    //adds a room to the booking basket when a 'book now' button is clicked
    $(".request_now").live("click",function(evt) {
        
        bookForm = $(this).parent().children('#req_book_form');
        
        if (bookForm.children('#offer_rate_plan_code').val()) {
            bookForm.submit();
            return false;
        }
        
        var offerData =
                    JSON.parse(
                        decodeURIComponent(    $(this).parents("div[class~=request_data]").get(0).getAttribute("data-details") ) );

        evt.preventDefault();

        $("html, body").animate({ scrollTop : 0 }, 200, function(){

            requestFormInDOM.css({"display":"none"})
            .html(
                ich.requestForm( offerData )
            )
            .slideDown(500, function() {
                enableEditor();
            });

            var elements = $('#req_form input, #req_form select');

            if (elements.length > 0) {
                var offerRequestHandler = new $.fn.Offers(elements);
                elements.each(function(){
                    offerRequestHandler.initial(this);
                });
                
                elements.blur(function(){
                    offerRequestHandler.blurFunc(this);
                });

                elements.focus( function(){
                    offerRequestHandler.focusFunc(this);
                });

                $("#offerConfirm").click(function(){
                    offerRequestHandler.checkInputs(elements);
                });
            }

        });
    });
    
    //adds a room to the booking basket when a 'book now' button is clicked
    $(".bookProxyBtn a.booking_request_now").live("click",function(evt) {

        var bookData =
                    JSON.parse(decodeURIComponent($(this).attr("data-details")));

        evt.preventDefault();

        $("html, body").animate({ scrollTop : 0 }, 200, function(){

            requestFormInDOM.css({"display":"none"})
            .html(
                ich.bookingRequestForm( bookData )
            )
            .slideDown(500, function() {
                enableEditor();
            });

            var elements = $('#req_form input, #req_form select');

            if (elements.length > 0) {
                var requestHandler = new $.fn.Offers(elements);
                elements.each(function(){
                    requestHandler.initial(this);
                });
                
                elements.blur(function(){
                    requestHandler.blurFunc(this);
                });

                elements.focus( function(){
                    requestHandler.focusFunc(this);
                });

                $("#offerConfirm").click(function(){
                    requestHandler.checkInputs(elements);
                });
            }

        });
    });

    $("#cancelOfferRequest").live("click",function(evt) {
        evt.preventDefault();
        requestFormInDOM.slideUp(500);
    });

}); 









