/// <reference path="jquery-1.7.1.intellisense.js" />
/// <reference path="jquery-ui-1.8.20.js" />

$(document).ready(function () {

    var all = $('.TimeTable .green');
    var first;
    var prev = undefined;
    var colspan = 1;

    all.each(function () {
        var txt = $(this).text();
        if (prev === txt) {
            colspan += 1;
            $(this).remove();
        } else {
            // doesnt match, set colspan on first and reset colspan counter
            if (colspan > 1) {
                first.attr('colspan', colspan);
                colspan = 1;
            }
            first = $(this);
            prev = txt;
        }
    });


    var alls = $('.substitue');
    var firsts;
    var prevs = undefined;
    var rowspan = 1;

    alls.each(function () {
        var txt = $(this).text();
        if (prevs === txt) {
            rowspan += 1;
            $(this).remove();
        } else {
            // doesnt match, set colspan on first and reset colspan counter
            if (rowspan > 1) {
                firsts.attr('rowspan', rowspan);
                rowspan = 1;
            }
            firsts = $(this);
            prevs = txt;
        }
    });


    var radio1 = $('#pernamentRadio');
    var radio2 = $('#onceRadio');

    if (!(radio1.filter(':checked').val() && radio2.filter(':checked').val())) {
        if ($('#datepicker').val() == "") {
            radio1.prop('checked', true);
            $('#onceTable').hide();
        } else {
            radio2.prop('checked', true);
            $('#pernamentTable').hide();
        }

    }
    radio1.change(
        function () {
            $('#onceTable').hide();
            $('#onceTable input').attr("value", "");
            $('#pernamentTable').show();
        });

    radio2.change(
        function () {
            $('#onceTable').show();
            $('#pernamentTable').hide();
            $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' });
        });


    $('#RoomId').change(function () {
        $("#HiddenPostBack").attr("value", "True");
        $('#mainform').submit();
    });

    $('#btnRoomRegistration').click(function () {
        $("#HiddenPostBack").attr("value", "False");
    });

    if ($('.deleteroom').length > 0) {
        $('.deleteroom').each(function () {
            $(this).click(function (e) {
                e.preventDefault();
                if (window.confirm("Naozaj chcete zmazať túto miestnosť a všetko jej vyučovanie?")) {
                    location.href = this.href;
                }
            });
        });
    }
    if ($('.deletelesson').length > 0) {
        $('.deletelesson').each(function() {
            $(this).click(function(e) {
                e.preventDefault();
                if (window.confirm("Naozaj chcete zmazať tento predmet?")) {
                    location.href = this.href;
                }
            });
        });
    }
});