﻿// JScript File

var calendars = new Array();


$(document).ajaxError(function(e, xhr, settings, exception) {
    alert('error in: ' + settings.url + ' \n' + 'error:\n' + xhr.responseText);
})

$(document).ready(function() {
    $('.calendar-calendar').each(function(index) {
        roomtype = $(this).attr("rt");
        var parent_room = $(this).parents(".room-calendar-item");
        var cal = new CalendarContainer(
    {
        roomtype: $(this).attr("rt"),
        ratecode: $(this).attr("rc"),
        month: $(this).attr("month"),
        container: $(this),
        ajax_load_url: "Popups/getcalendar.aspx",
        ajax_check_url: "Popups/CheckRoom.aspx",
        affcode: $("#hdn_af").val(),
        hotelid: $("#hdn_hid").val(),
        hotsrc: $("#hdn_src").val(),
        persons: parent_room.find('select').val(),
        isbed: parent_room.find("#hdn_bed").val() == "true" ? 1 : 0,
        parent_room: parent_room

    });
        parent_room.find('select').bind('change', function() {
            cal.setPersons(($(this)).find("option:selected").val());
        });

        parent_room.find('#book').bind('click', function() {
            cal.gotoBook($(this));
        });

        if (calendars[roomtype] == null)
            calendars[roomtype] = new Array();
        calendars[roomtype][calendars[roomtype].length] = cal;

    });

});

function show_calendar(roomtype) {
    if ($("#rates_" + roomtype).is(':visible')) {
        $("#rates_" + roomtype).hide();
    }
    else {
        for (var i = 0; i < calendars[roomtype].length; i++) {

            calendars[roomtype][i].load();
        }

        $("#rates_" + roomtype).show();
    }
}


function toggle_options()
{
    $(".addition-param").slideToggle();
}

var timer;
var needToWait = false;

function show_dialog(url, id, title) {
    $.post(url, function(data) {
        needToWait = false;
        clearTimeout(timer);
        $('.waitdialog').dialog('close');
        $('#' + id).html(data);
        $('#' + id).dialog({
            autoOpen: false,
            modal: true,
            resizable: false,
            width: '550px'
        });
        $('#' + id).dialog('open');

    });
    timer = setTimeout("showWait()", 500);
    needToWait = true;
    $('body').append('<div width="500px" title="' + title + '" id="' + id + '"></div>');
}


function showWait() {
    if (!needToWait)
        return;
    $('.waitdialog').dialog({
        position: 'center',
        modal: true,
        autoOpen: false,
        height: 70,
        width: 150,
        resizable: false
    }
    );
    $('.waitdialog').dialog('open');

    $('.waitdialog').prev().hide();
}

function showProgress() {
    $('#waitbox').html($('#waitbox').html());
    $('#waitbox').show();
}



function goto_book(btn, link) {
    var parent_div = $(btn).parents('.room-calendar-rate');

    var cell = parent_div.parents(".room-calendar-item");
    // arrival
    if (arrival[id] == null || departure[id] == null)
        return;

    id = parent_div.children('div').attr('id');

    ar = arrival[id].getTime();
    dp = departure[id].getTime();

    var nights = (dp - ar) / (24 * 60 * 60 * 1000);
    var adults = cell.find('select').val();
    var month = arrival[id].getMonth() + 1;
    var day = arrival[id].getDate();
    location.href = link + '&ad=' + (month < 10 ? '0' + month : month) + (day < 10 ? '0' + day : day) + '&n=' + nights + '&a=' + adults;
    
}
