﻿var natDays1 = [];

(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 < natDays1.length; i++) {
                if (date.getMonth() == natDays1[i][0] - 1 && date.getDate() == natDays1[i][1] && date.getFullYear() == natDays1[i][2]) {
                    //$('.withevent').bind('click',function(){alert($(this).html())});

                    return [true, natDays1[i][1] + "." + natDays1[i][0] + "." + natDays1[i][2] + " " + natDays1[i][4], natDays1[i][3]];
                }
            }
            return [false, ''];
        }

        function Select(date) {
            alert(date);
        }
        /* INIT CALENDAR */
        $('#calendar2').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"]);

        $('#calendar2').datepicker('option', 'monthNames', ['Ianuarie', 'Februarie', 'Martie', 'Aprilie', 'Mai', 'Iunie', 'Iulie', 'August', 'Septembrie', 'Octombrie', 'Noiembrie', 'Decembrie']);
        $('#calendar2').datepicker('option', 'dayNamesMin', ['D', 'L', 'M', 'M', 'J', 'V', 'S']);
        $('#calendar2').datepicker('option', 'minDate', new Date(2011, 1 - 1, 1));
        $('#calendar2').datepicker('option', 'maxDate', '+11m');
        $('#calendar2').datepicker('option', 'hideIfNoPrevNext', true);
        $('#calendar2').datepicker('option', 'prevText', '');
        $('#calendar2').datepicker('option', 'nextText', '');
        $('#calendar2').datepicker('option', 'firstDay', 1);
        $('#calendar2').datepicker('option', 'dateFormat', 'yyyy.mm.dd');

        $('#calendar2 table tbody>tr').each(function(index) {
            if (index % 2 == 0)
                $(this).addClass('w');
        })
        $("#calendar2 td.withevent a.ui-state-default,#calendar2 td.withevent1 a.ui-state-default").each(function() {
            $(this).parent().append('<div class="tooltip">' + $(this).parent().attr("title") + '</div>');
            $(this).parent().attr("title", "");
        });
        $('#calendar2 td.withevent a.ui-state-default,#calendar2 td.withevent1 a.ui-state-default').tooltip({ position: "center left", offset: [80, 7], effect: 'slide', relative: 'false' });
        $('#calendar2 .ui-corner-all').click(function() {
            $('#calendar2 table tbody>tr').each(function(index) {
                if (index % 2 == 0)
                    $(this).addClass('w');
            });
            $("#calendar2 td.withevent a.ui-state-default,#calendar2 td.withevent1 a.ui-state-default").each(function() {

                $(this).parent().append('<div class="tooltip">' + $(this).parent().attr("title") + '</div>');
                $(this).parent().attr("title", "");

                $(this).removeAttr("href").css("cursor", "pointer");
                $(this).click(function() {
                    return false;
                });

            });

            $('#calendar2 td.withevent a.ui-state-default,#calendar2 td.withevent1 a.ui-state-default').tooltip({ position: "center left", offset: [80, 7], effect: 'slide', relative: 'false' });
            Cufon.replace('#calendar2 tr td span, #calendar2 a', { hover: true });
            Cufon.refresh('#calendar2 tr, #calendar2 a');

            $(".ui-datepicker-calendar a").removeAttr("href").css("cursor", "pointer");
            $(".ui-datepicker-calendar a").click(function() {
                return false;
            });

        });

        $(".ui-datepicker-calendar td a.ui-state-default").removeAttr("href").css("cursor", "pointer");
        $(".ui-datepicker-calendar td a.ui-state-default").click(function() {
            return false;
        });

        $(".ui-datepicker-next ui-corner-all a").live('click', function() {
            alert('y');
            $(".ui-datepicker-calendar a").removeAttr("href").css("cursor", "pointer");
        });
    });
})(jQuery);
