﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />

function initDatePicker() {
    $('.datePicker').datePicker({ clickInput: true, createButton: true, displayClose: false });
    //propojit dva vybery datumu od a do tak, aby umoznovali logicky vyber mezi dvema hodnotami.
    //hodnota definovana v OpenTravelControls.HtmlControls.JQueryHtmlControls
    if (typeof combineStartAndEndDates != 'undefined' && combineStartAndEndDates) {
        $('#terminOd').bind(
		        'dpClosed',
		        function(e, selectedDates) {
		            var d = selectedDates[0];
		            if (d) {
		                d = new Date(d);
		                $('#terminDo').dpSetStartDate(d.addDays(1).asString());
		            }
		            else {
		                $('#terminDo').dpSetStartDate((new Date()).zeroTime().asString());
		            }
		        }
		    );

        $('#terminDo').bind(
		        'dpClosed',
		        function(e, selectedDates) {
		            var d = selectedDates[0];
		            if (d) {
		                d = new Date(d);
		                $('#terminOd').dpSetEndDate(d.addDays(-1).asString());
		            }
		            else {
		                var futureDate = new Date();
		                futureDate.setFullYear(2050, 0, 1);
		                $('#terminOd').dpSetEndDate(futureDate.asString());
		            }
		        }
	        );

    }
}

function calculateTotalPrice(reservationCountCss, reservationPriceCss) {
    var reservationCounts = $("select." + reservationCountCss);
    var reservationPrices = $("span." + reservationPriceCss);
    var totalPrice = 0;

    var affectLTPrices = $("td.AffectLT select." + reservationCountCss);
    var LTPrices = $("td.LT select." + reservationCountCss);
    var LTCount = 0;

    for (var i = 0; i < affectLTPrices.length; i++) {
        LTCount += parseInt(affectLTPrices[i].selectedIndex);
    }
    if (LTPrices.length > 0 && LTPrices[0].disabled) {
        for (var i = 0; i < LTPrices.length; i++) {
            LTPrices[i].selectedIndex = LTCount;
        }
    }

    for (var i = 0; i < reservationCounts.length; i++) {
        var reservationCount = reservationCounts[i].options[reservationCounts[i].selectedIndex].value;
        if (reservationCount != "") {
            var price = parseInt($(reservationPrices[i]).text().replace(/\s|\u00a0/g, ''))
            if (!isNaN(price))
                totalPrice += parseInt(reservationCount) * price;
        }
    }
    $("#TotalPriceDiv").text(formatNumber(totalPrice, ',', ',', ' ') + " Kč");

}

function formatNumber(nStr, inD, outD, sep) {
    nStr += '';
    var dpos = nStr.indexOf(inD);
    var nStrEnd = '';
    if (dpos != -1) {
        nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
        nStr = nStr.substring(0, dpos);
    }
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
        nStr = nStr.replace(rgx, '$1' + sep + '$2');
    }
    return nStr + nStrEnd;
}

function appendOptionLast(select, value, text, selectedDestination) {
    var optionNew = document.createElement('option');
    optionNew.text = text;
    optionNew.value = value;

    try {
        select.add(optionNew, null); // standards compliant; doesn't work in IE
    }
    catch (ex) {
        select.add(optionNew); // IE only
    }

    if (selectedDestination == value)
        optionNew.selected = true;
}

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ($active.length == 0) $active = $('#slideshow IMG:last');

    var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.show().css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 5000, function() {
            $active.removeClass('active last-active').hide();
        });
}

function flagSwitch() {
    var $active = $('#flag IMG.active');

    if ($active.length == 0) $active = $('#flag IMG:last');

    var $next = $active.next().length ? $active.next() : $('#flag IMG:first');

    $active.addClass('last-active');

    $next.show().css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 4000, function() {
            $active.removeClass('active last-active').hide();
        });
}

$(function() {
    slideSwitch();
    setInterval("slideSwitch()", 10000);
    setInterval("flagSwitch()", 5000);
});

