$(document).ready(function() {
    if ($('#ctl00_CenterContentPlaceHolder_DepartureDateTextBox').length > 0) {

        $('#ctl00_CenterContentPlaceHolder_DepartureDateTextBox').datepicker({ onSelect: function() { }, dateFormat: "dd.mm.yy" });
        $('#ctl00_CenterContentPlaceHolder_BackDepartureDateTextBox').datepicker({ onSelect: function() { }, dateFormat: "dd.mm.yy" });
        jReservation.recomputePrice();
    }
});

var jReservation = {
    roundTrip: false,

    // Show / hide round trip block
    showRoundTripBlock: function(show) {
        if (show) {
            $('#backRoutePanel').slideDown("fast");
        }
        else {
            $('#backRoutePanel').slideUp("fast");
        }

        this.roundTrip = show;
        this.recomputePrice();
    },

    // Compute available cars
    computeCars: function() {
        var personsDropDownList = $('#ctl00_CenterContentPlaceHolder_PersonsDropDownList')[0];
        var carsDropDownList = $('#ctl00_CenterContentPlaceHolder_CarDropDownList')[0];
        var persons = parseInt(personsDropDownList.value);
        var selectedCarId = carsDropDownList.value;
        var selectorInnerHtml = "<option>";

        carsDropDownList.options.length = 0;
        // Find all available cars
        var idx = 0;
        for (var i = 0; i < jPriceList.length; i++) {
            if (jPriceList[i]["from"] <= persons && jPriceList[i]["to"] >= persons) {
                carsDropDownList.options[idx] = new Option(jCarList[jPriceList[i]["carId"]], jPriceList[i]["carId"]);
                idx++;
            }
        }

        carsDropDownList.value = selectedCarId;
        if (!carsDropDownList.value) {
            carsDropDownList.value = carsDropDownList.options[0].value;
        }

        this.recomputePrice();
    },

    // Compute transfer price
    computePrice: function(carId) {
        var personsDropDownList = $('#ctl00_CenterContentPlaceHolder_PersonsDropDownList')[0];
        var priceTextBox = $('#ctl00_CenterContentPlaceHolder_PriceTextBox')[0];
        var totalPrice = "N/A";
        var persons = parseInt(personsDropDownList.value);
        carId = parseInt(carId);

        for (var i = 0; i < jPriceList.length; i++) {
            if (jPriceList[i]["from"] <= persons && jPriceList[i]["to"] >= persons && jPriceList[i]["carId"] == carId) {
                if (this.roundTrip) {
                    totalPrice = jPriceList[i]["backPrice"];
                }
                else {
                    totalPrice = jPriceList[i]["price"];
                }
            }
        }

        priceTextBox.value = totalPrice;
    },

    recomputePrice: function() {
        var carsDropDownList = $('#ctl00_CenterContentPlaceHolder_CarDropDownList')[0];
        if (carsDropDownList) {
            this.computePrice(carsDropDownList.value);
        }
    }
};

function openSupplement() {
    width = screen.width;
    height = screen.height;

    var url = '/SupplementCart?sId=' + document.getElementById('supplId').value + "&sQty=" + document.getElementById('supplQty').value;

    wnd = window.open(url, 'Supplements', 'left=' + ((width - 600) / 2) + ', top=' + ((height - 400) / 2) + ',width=600,height=400,scrollbars=yes,resizable=yes');
    wnd.focus();
}

/**
* ON MOUSE OVER IMAGE 
**/
var timerT = false;

function show_small(elem, filename, title) {
    if (timerT) clearTimeout(timerT);
    if (!filename || filename == "/") return;
    var small_div = document.getElementById("smallDiv");

    small_div.innerHTML = "<strong>" + title + "</strong><br /><img src='" + filename + "' alt='" + title + "' title='" + title + "' />";
    var cmd = "del_show();";
    timerT = setTimeout(cmd, 600);
}

function del_show() {
    clearTimeout(timerT);
    var small_div = document.getElementById("smallDiv");
    small_div.style.top = eval(ttYpos + 20) + "px";
    small_div.style.left = eval(ttXpos + 20) + "px";
    small_div.style.visibility = "visible";
}

function image_hide() {
    var small_div = document.getElementById("smallDiv");
    small_div.style.visibility = "hidden";
}

var ttXpos = 0, ttYpos = 0;
var ttXadd = 10, ttYadd = -10;
var ttDisplay = 0, ttHoldIt = 0;

