﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />

function fillCategory(categoryIDSelected) {
    var combo = document.getElementById("kategorie");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "téma: (libovolné)", "", "");

        for (var i = 0; i < categories[0].length; i++) {
            appendOptionLast(combo, categories[0][i].id, categories[0][i].name, categoryIDSelected, "indent" + categories[0][i].indent);
        }
    }
}

function fillCountry(categoryID, countryID) {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "země: (libovolná)", "", "");
        if (categoryID == "") categoryID = 0;
        if (typeof countries[categoryID] != "undefined") {
            for (var i = 0; i < countries[categoryID].length; i++) {
                appendOptionLast(combo, countries[categoryID][i].id, countries[categoryID][i].name, countryID, "");
            }
        }
    }
}

function fillDestination(countryID, destinationID) {
    var combo = document.getElementById("destinace");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "destinace: (libovolná)", "", "");
        if (typeof destinations[countryID] != "undefined") {
            for (var i = 0; i < destinations[countryID].length; i++) {
                appendOptionLast(combo, destinations[countryID][i].id, destinations[countryID][i].name, destinationID, "");
            }
            combo.disabled = false;
        }
        else {
            combo.disabled = true;
        }
    }
}

function refreshZeme() {
    var combo = document.getElementById("kategorie");
    if (combo != null) {
        fillCountry(combo.options[combo.selectedIndex].value);
    }
}

function refreshDestinace() {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        fillDestination(combo.options[combo.selectedIndex].value);
    }
}


function zobrazitCeny(typCeny) {
    var icon = $("#typCeny" + typCeny);

    if (icon.attr('src').indexOf("plus") != -1) {
        $("." + typCeny).show();
        icon.attr('src', icon.attr('src').replace("plus", "minus"));
    }
    else {
        $("." + typCeny).hide();
        icon.attr('src', icon.attr('src').replace("minus", "plus"));
    }

}

function calculateTotalPrice(reservationCountCss, reservationPriceCss) {
    var reservationCounts = $("select." + reservationCountCss);
    var reservationPrices = $("span." + reservationPriceCss);
    var totalPrice = 0;

    for (var i = 0; i < reservationCounts.length; i++) {
        var reservationCount = reservationCounts[i].options[reservationCounts[i].selectedIndex].value;
        if (reservationCount != "" && $(reservationPrices[i]).text().indexOf("Kč") > -1) {
            var price = parseInt($(reservationPrices[i]).text().replace(/\s|\u00a0/g, ''))
            if (!isNaN(price))
                totalPrice += parseInt(reservationCount) * price;
        }
    }
    $("#TotalPriceDiv").text(formatNumber(totalPrice, ',', ',', ' ') + " Kč");
}

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() {
    slideSwitch();
    setInterval("slideSwitch()", 10000);
});

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 queryString(searchKey) {
    querystrings = window.location.search.substring(1);
    var strings = querystrings.split("&");
    for (i = 0; i < strings.length; i++) {
        keyValue = strings[i].split("=");
        if (keyValue[0] == searchKey) {
            return keyValue[1];
        }
    }
}

