﻿var natDays = [];

(function($) {

    $(document).ready(function() {

        String.prototype.endsWith = function(str) {
            return (this.match(str + '$') == str)
        }

        jQuery.expr[":"].asp = function(a, i, m) {
            return (id = jQuery(a).attr('id')) && id.endsWith(m[3]);
        };

        function disableAllDays(date) {
            for (i = 0; i < natDays.length; i++) {
                if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1] && date.getFullYear() == natDays[i][2]) {
                    //$('.withevent').bind('click',function(){alert($(this).html())});

                    return [true, natDays[i][1] + "." + natDays[i][0] + "." + natDays[i][2] + " " + natDays[i][4], natDays[i][3]];
                }
            }
            return [false, ''];
        }

        function Select(date) {
            alert(date);
        }
        /* INIT CALENDAR */
        $('#calendar').datepicker({ beforeShowDay: disableAllDays, onSelect: function(dateText, inst) {
            alert('The chosen date is ' + inst.html());
            //setTimeout("",10);
        }
            //         onSelect: function(dateText, inst) {
            ////        alert(dateText);
            //            alert('td.' + dateText + ' div.tooltip a');
            //            window.location = $('td.' + dateText + ' div.tooltip a').attr("href");
            //        }
        }, $.datepicker.regional["ro"]);

        $('#calendar').datepicker('option', 'monthNames', ['Ianuarie', 'Februarie', 'Martie', 'Aprilie', 'Mai', 'Iunie', 'Iulie', 'August', 'Septembrie', 'Octombrie', 'Noiembrie', 'Decembrie']);
        $('#calendar').datepicker('option', 'dayNamesMin', ['D', 'L', 'M', 'M', 'J', 'V', 'S']);
        $('#calendar').datepicker('option', 'minDate', new Date(2011, 1 - 1, 1));
        $('#calendar').datepicker('option', 'maxDate', '+11m');
        $('#calendar').datepicker('option', 'hideIfNoPrevNext', true);
        $('#calendar').datepicker('option', 'prevText', '');
        $('#calendar').datepicker('option', 'nextText', '');
        $('#calendar').datepicker('option', 'firstDay', 1);
        $('#calendar').datepicker('option', 'dateFormat', 'yyyy.mm.dd');

        $('#calendar table tbody>tr').each(function(index) {
            if (index % 2 == 0)
                $(this).addClass('w');
        })
        $("#calendar td.withevent a.ui-state-default,#calendar td.withevent1 a.ui-state-default").each(function() {
            $(this).parent().append('<div class="tooltip">' + $(this).parent().attr("title") + '</div>');
            $(this).parent().attr("title", "");
        });
        $('#calendar td.withevent a.ui-state-default,#calendar td.withevent1 a.ui-state-default').tooltip({ position: "center left", offset: [80, 7], effect: 'slide', relative: 'false' });
        $('#calendar .ui-corner-all').click(function() {
            $('#calendar table tbody>tr').each(function(index) {
                if (index % 2 == 0)
                    $(this).addClass('w');
            });
            $("#calendar td.withevent a.ui-state-default,#calendar td.withevent1 a.ui-state-default").each(function() {

                $(this).parent().append('<div class="tooltip">' + $(this).parent().attr("title") + '</div>');
                $(this).parent().attr("title", "");


            });

            $('#calendar td.withevent a.ui-state-default,#calendar td.withevent1 a.ui-state-default').tooltip({ position: "center left", offset: [80, 7], effect: 'slide', relative: 'false' });
            Cufon.replace('#calendar tr td span, #calendar a', { hover: true });
            Cufon.refresh('#calendar tr, #calendar a');

        });
    });
})(jQuery);
