$(document).ready(function() {
    hideSearch();
    initAOLinks();
    initAOMonth();
    initRooms();
});

/**
 * Hides the search form when the hotel page loads
 */
function hideSearch() {
    if ($('.book').size() > 0) $('div.allocs').hide();
    $('div.allocsbottom').each(function() {
        var toggle = $(this);
        var toggle_link = $('a:first', toggle);
        $(this).click(function(e) {
            e.preventDefault();
            if (toggle_link.hasClass('show')) {
                toggle.css('border-top-style', 'none');
                $(this).prev().slideDown('slow', function() {
                    toggle_link.html('Hide Search').toggleClass('hide').toggleClass('show');
                });
            }
            else {                
                toggle.css('border-top-style', 'solid');
                $(this).prev().slideUp('slow', function() {
                    toggle_link.html('Search This Hotel Again').toggleClass('hide').toggleClass('show');
                });
            }
            toggle_link.blur();
        });
    });
}

/**
 * Adds ajax events to the availability overview so the whole
 * page doesn't have to reload
 */
function initAOLinks() {
    $('a.aolink').click(function(e) {
        e.preventDefault();
        var start = $(this).attr('href').split('=')[1].split('#')[0];
        getAvailabilityOverview(start);
    });
}

function getAvailabilityOverview(start) {
    $.get(
        "/ajax/availability_overview.php",
        { start_date: start },
        function(data) {
            $('.allocs_table').each(function(i) {
                $(this).fadeOut(150, function() {
                    $(this).html(data);
                    $(this).fadeIn(100, function() {
                        if (i == 0) {
                            initAOLinks();
                            initAOMonth();
                        }
                    });
                });
            });
        }
    );
}

function initAOMonth() {
    var width = 145;
    var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
    $('.ao_month').click(function(e) {
        var selector = $(this);
        e.preventDefault();
        // generate months for dropdown
        var monthSequence = [];
        var i=0;
        var currentMonth = (new Date()).getMonth();
        var currentYear = (new Date()).getFullYear();
        while (i<12) {
            if (currentMonth == 12) {
                currentMonth = 0;
                currentYear++;
            }
            var monthLink = $(document.createElement('a'));
            monthLink
                .html(months[currentMonth] + ' ' + currentYear)
                .attr('href', '#' + currentYear + '-' + DateFunctions.addZero(currentMonth+1) + '-01')
                .click(function(e) {
                    e.preventDefault();
                    getAvailabilityOverview($(this).attr('href').replace('#', ''));
                    $(this).parent().remove();
                });
            monthSequence[i] = monthLink;
            currentMonth++;
            i++;
        }
        // create the div
        var div = document.createElement('div');
        var month_selector = $(div);
        var pos = $(this).position();
        month_selector
            .addClass('ao_month_selector')
            .css('top', pos.top).css('left', pos.left+(($(this).width()-width)/2)).css('width', width+'px').css('z-index', '100000');
        for (var i=0; i<monthSequence.length; i++) {
            month_selector.append(monthSequence[i]);
        }
        month_selector.append(
            $(document.createElement('a'))
                .html('<img src="/gfx/allocs_blank.gif" />')
                .attr('href', '#')
                .addClass('ao_month_selector_close')
                .click(function(e) {
                    e.preventDefault();
                    if ($.browser.msie) {
                        $('#ao_iframe').remove();
                    }
                    $(this).parent().slideUp();
                })
        );
        $('body').append(month_selector);
        month_selector.slideDown('normal', function() {                                 
	        // add iframe to hide stuff underneath in ie6
	        if ($.browser.msie) {
	            var iframe = document.createElement('iframe');
	            $(iframe)
                    .attr('id', 'ao_iframe')
	                .css('position', 'absolute')
	                .css('top', pos.top).css('left', pos.left+((selector.width()-width)/2)).css('width', width+'px')
	                .css('width', month_selector.width()+'px')
	                .css('height', month_selector.height()+'px')
	                .css('z-index', '1');
	            $('body').append(iframe);
	        }
        });
    });
}

function initRooms() {
    $('.room').each(function() {
        var room = $(this);
        var details = $('.details', room);
        var subheader = $('.subheader2', room);
        subheader
            .prepend('<img src="/gfx/show.gif" alt="+" />')
            .addClass('toggle')
            .attr("title", "click to view more details about this room");
        subheader.click(function() {
            if (details.css('display') == 'none') {
                $('img:first', subheader).attr('src', '/gfx/hide.gif');
                details.slideDown();
            }
            else {
                $('img:first', subheader).attr('src', '/gfx/show.gif');
                details.slideUp();
            }
        });
        details.hide();
    });
}