jQuery(function($){
    // handle the page image and place it in the correct spot.
    // 
    var width_of_body = 613; // 634 is max
    $("#body img").each(function() {
        var element = $(this);
        if (element.width() > width_of_body) {
            element.addClass("page_image");
        }
        if (element.parent("p").prev("h1").length) {
            var parent_p = $(this).parent("p");
            element.clone().prependTo("#body-inner");
            element.remove();
            if (parent_p.html() == '') {
                parent_p.remove();
            }
        }
    });

    // ugly form button replacement
    $("input.button").each(function() {
        var button_text = $(this).attr("value");
        var button_onClick = $(this).attr("onClick");
        var button_style = $(this).attr("class");
        $(this).replaceWith("<a class='submit-button "+button_style+"' onClick=\""+button_onClick+"\"><span>"+button_text+"</span></a>");
    });



    // handle submission of js_reserve_submit
    function parse_date() {
        var date_check_in = $("#date-check-in").val();
        var arr_date = date_check_in.split("/");
        $("#arriveDate").attr("value", arr_date[1]);
        $("#arriveMonth").attr("value", arr_date[0]);
        $("#arriveYear").attr("value", arr_date[2]);
        return true;
    }

    $("a.submit-button").click(function() {
        if ($(this).hasClass("js_reserve_submit")) {
            parse_date();
        }
        $(this).parents("form").get(0).submit();
    });
    
    //date-picker for interior content forms
    $(".js_date").datepicker({                                           
        minDate: "2d", 
        maxDate: "6y"
    });

    /* kind of a workaround for the moment. - 2009-2-13 */
    // cycler - first remove the image and place in a cycle_box
    $("#body-inner").prepend("<div class='cycle_box'></div>");
    $(".page_image").each(function() {
        $(this).clone().appendTo("div.cycle_box");
        $(this).remove();
    });
    $(".cycle_box").after("<div class='allclear'></div>");

    function thumbnail_page_image(index, DOMelement) {
        var thumbnail = $("div.cycle_box").children().slice(index, index+1).attr("src");
        var thumbnail_tag = $("<a href='#'><img width='127' height='62' src='"+thumbnail+"' /></a>");
        return(thumbnail_tag);
    }

    // http://malsup.com/jquery/cycle/options.html
    $('.cycle_box').after("<div id='page_image_pager'></div>")
        .cycle({

            fx: 'fade',
            timeout: 8000,
            pause: 1,
            pager: '#page_image_pager',
            pagerAnchorBuilder: thumbnail_page_image,
            fastOnEvent: 1
        });
});
