﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />
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, combo) {

    if (typeof combo != "undefined" && combo.options[combo.selectedIndex].value == "více")
        addMoreOptions(combo, combo.options.length - 1);
    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 != "") {
            var price = parseInt($(reservationPrices[i]).text().replace(/\s|\u00a0/g, ''))
            if (!isNaN(price))
                totalPrice += parseInt(reservationCount) * price;
        }
    }
    $("#TotalPriceDiv").text(formatNumber(totalPrice, ',', ',', ' ') + " Kč");
}

function addMoreOptions(combo, startValue) {
    combo.remove(startValue);
    
    for (i = startValue; i < startValue + 10; i++)
        appendOptionLast(combo, i, i, "", "");
    if (startValue < 29) {
        appendOptionLast(combo, "více", "více", "", "");
    }   
}


function appendOptionLast(select, value, text, selectedDestination, cssClass) {
    var optionNew = document.createElement('option');
    optionNew.text = text;
    optionNew.value = value;
    optionNew.setAttribute('class', cssClass);

    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 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;
}


/*google maps*/
var map = null;
function showAddress(name, addr) {
    if (!GBrowserIsCompatible()) return;

    if (!map) {
        map = new GMap2(document.getElementById(name));
        map.addControl(new GSmallZoomControl());
    }
    geocoder = new GClientGeocoder();
    geocoder.getLocations(addr, callbackLocations);
}


function showPoint(name, lat, lng, acc) {
    if (!GBrowserIsCompatible()) return;

    if (!map) {
        map = new GMap2(document.getElementById(name));
        map.addControl(new GSmallZoomControl());
    }

    point = new GLatLng(lat / 3600, lng / 3600);
    map.setCenter(point, acc);
    marker = new GMarker(point);
    map.addOverlay(marker);
}



function callbackLocations(response) {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
    acc = place.AddressDetails.Accuracy;
    if (acc > 6) {
        map.setCenter(point, 15);
        marker = new GMarker(point);
        map.addOverlay(marker);
    } else if (acc > 4) {
        map.setCenter(point, 12);
    } else {
        map.setCenter(point, 11);
    }
}
// A function to create the marker and set up the event window
function addMarker(lat, lng, name, html) {
    // use a custom icon with letter A - Z
    var letter = String.fromCharCode("A".charCodeAt(0) + (gmarkers.length));
    var myIcon = new GIcon(G_DEFAULT_ICON, "http://www.google.com/mapfiles/marker" + letter + ".png");
    myIcon.printImage = "http://maps.google.com/mapfiles/marker" + letter + "ie.gif"
    myIcon.mozPrintImage = "http://maps.google.com/mapfiles/marker" + letter + "ff.gif"

    var pt = new GLatLng(lat / 3600, lng / 3600);
    if (gbounds) gbounds.extend(pt);

    var marker = new GMarker(pt, { icon: myIcon });
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    // save the info we need to use later for the side_bar
    if (gmarkers) gmarkers.push(marker)
    return marker;
}

function showMarker(i) {
    if (!gmarkers) return true;

    GEvent.trigger(gmarkers[i], "click");
    return false;
}

