
function bumpersAvailable()
{
    var firstTrack = parseInt($("select[name=firstTrack]").attr("value"));
    var lastTrack = parseInt($("select[name=lastTrack]").attr("value"));
    
    if (firstTrack && lastTrack) {
        for (i = firstTrack; i <= lastTrack; i++) {
            if ($.inArray(i, tracksWithBumpers) < 0)
                return false;
        }
    
        return true;
    }
    
    return false;
}

function reserveDialog(data)
{
    $.post("proxy.php?f=reserve", data, function(data) {
        $("#dialog").html(data).dialog({
            title: "Tee uusi varaus",
            modal: true,
            resizable: false,
            width: 390,
      
            close: function() {
                $(this).dialog("destroy"); 
                $("td._time").removeClass("ui-selected");
            },
            
            buttons: [
                {
                    text: "Varaa",

                    click: function() {
                        $.post("proxy.php?f=reserve-post", $('#reservationForm').serialize(), function(data) {
                            var ret = data.substring(0, 4);
                            
                            if (ret == "[OK]") {
                                $("#reservationForm").parent().html("<div class='reservationConfirm'>" + 
                                    "<h2>Kiitos varauksestasi!</h2>" + 
                                    "<p></p>" + 
                                    "<p><b>Vahvista varauksesi</b> klikkaamalla sähköpostiisi tulleen viestin linkkiä <b>15 minuutin sisällä</b>.</p>" +
                                    "<p>Mikäli et vahvista tekemääsi varausta, niin se poistuu automaattisesti varausjärjestelmästä 15 minuutin kuluttua.</p>" +
                                    "<p>Varauksesi on jo rekisteröity, mutta se on vielä vahvistettava sähköpostin kautta." + 
                                    "   Tarkista roskapostisi, jollet ole saanut varausvahvistusta sähköpostiisi.</p>" + 
                                    "<p>Kiitos online-varauksen käytöstä.</p>" + 
                                    "</div>");
                                
                                $("#dialog").dialog("option", "buttons", { "Sulje": function() { 
                                    $(this).dialog("close"); 
                                    window.location.reload();
                                }});
                            } else {                        
                                alert(data);
                            }
                        });
                    }
                },
                
                {            
                    text: "Sulje",
                    
                    icons: {
                        primary: "ui-icon-locked"
                    },

                    click: function() { 
                        $(this).dialog("close"); 
                    }
                }
            ]
        });
        
        $("input[type=text][name=firstname]").focus();
        
        if (tracksWithBumpers.length > 0) {
            if (!bumpersAvailable()) {
                $("#bumpers-available").hide();
                $("#bumpers-notavailable").show();
                
                $("input[type=checkbox][name=bumpers]").attr("checked", "");
                $("input[type=checkbox][name=bumpers]").attr("disabled", true);
            }
            
            $("select[name=firstTrack], select[name=lastTrack]").change(function() {
                if (bumpersAvailable()) {
                    $("#bumpers-notavailable").hide();
                    $("#bumpers-available").show();
                    
                    $("input[type=checkbox][name=bumpers]").attr("disabled", false);
                } else {
                    $("#bumpers-available").hide();
                    $("#bumpers-notavailable").show();
                    
                    $("input[type=checkbox][name=bumpers]").attr("checked", false);
                    $("input[type=checkbox][name=bumpers]").attr("disabled", true);
                }
            });
        } else {
            $("#bumpersRow").hide();
        }
    });
}

$(function() {
    $("#showNotice").click(function () {
        $(this).hide();
        $("._restOfNotice").show();
        $("#hideNotice").show();
    
        return false;
    });

    $("#hideNotice").click(function () {
        $(this).hide();
        $("._restOfNotice").hide();
        $("#showNotice").show();
    
        return false;
    });

    $("#datepicker").datepicker({
        minDate: 0,
        maxDate: "1y",
        numberOfMonths: 2,
        dateFormat: "yy-mm-dd",
        defaultDate: currentDate,
        
        onSelect: function(dateText) {
            document.location = "index.php?day=" + dateText;
        }
    }, $.datepicker.regional["fi"]);
    
    if ($("td._time:first").length > 0) {
        $("td._time").click(function() {
            reserveDialog({ id: $(this).attr("id") });
            return false;
        });
        
        $("#timeTable").selectable({ 
            delay: 50,
            filter: "td._time, td._reserved",
            
            stop: function() {
                var skipDialog = false;
                var selected = new Array();
            
                $(".ui-selected", this).each(function() {
                    var obj = $(this);
                    
                    if (obj.hasClass("reserved")) {
                        skipDialog = true;
                        return false;
                    }
                    
                    selected.push(obj.attr("id"));
                });
                
                if (selected.length > 0 && !skipDialog) {
                    reserveDialog({ "ids[]": selected });
                } else {
                    $("td._time, td._reserved").removeClass("ui-selected");
                }
            }
        });
    }
    
    $("#timeTable tr._timeRow").hover(
        function() {
            $("td:first, td:last", this).addClass("highLight");
        },
        
        function() {
            $("td:first, td:last", this).removeClass("highLight");
        }
    );
});
