var danas = new Date();
var mjesec = danas.getMonth() + 1;
var godina = danas.getFullYear();

function buildCal(m, y, event) {
    var eventsplited = event.split("$");
    var dim = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    var oD = new Date(y, m - 1, 1); //DD replaced line to fix date bug when current day is 31st
    oD.od = oD.getDay() + 1; //DD replaced line to fix date bug when current day is 31st
    var a = true;

    var todaydate = new Date() //DD added
    var scanfortoday = (y == todaydate.getFullYear() && m == todaydate.getMonth() + 1) ? todaydate.getDate() : 0 //DD added

    dim[1] = (((oD.getFullYear() % 100 != 0) && (oD.getFullYear() % 4 == 0)) || (oD.getFullYear() % 400 == 0)) ? 29 : 28;
    var t = '<table cols="7" cellpadding="0" cellspacing="0"><tr align="center">';
    t += ' <thead> <tr align="center">';
    for (s = 0; s < 7; s++) t += '<th>' + dani.substr(s, 1) + '</th>';
    t += '</tr><thead><tr align="center">';

    for (i = 1; i <= 42; i++) {

        if (a) {
            a = false;
            if (oD.od == 1) {
                i = -6;
                oD.od--;
            }
            else {
                oD.od--;
            }
        }

        var x = ((i - oD.od >= 0) && (i - oD.od < dim[m - 1])) ? i - oD.od + 1 : '&nbsp;';
        var todayOrEventClass = "";

        ///pronadji sjednice i oznaci ih
        var odlukaText = "";
        var odlukaDiv = "";

        for (xx = 0; xx < eventsplited.length; xx++) {
            if (eventsplited[xx] == x) {
                //alert(x);
                odlukatext = eventsplited[xx + 1];
                odlukaDiv = "<div class='events'><ul><li><span class='desc'>" + odlukatext + "</li></ul></div>";
                todayOrEventClass = "date_has_event";
                odlukatext = "";
            }
        }

        if (x == scanfortoday)
        { todayOrEventClass += " today"; }


        if (x == "&nbsp;") {
            t += '<td class="padding"></td>';
            if (i > 7 && i == 35) {
                break;
            }
        }
        else {

            t += '<td class="' + todayOrEventClass + '">' + x + odlukaDiv + '</td>';
        }

        if (((i) % 7 == 0) && (i < 36)) t += '</tr><tr align="center">';
    }
    return t += '</tr> </table>';
}

function napraviEvente() {
    $('#events').html();

    $('#events').load("/SecAjax/Kalendar.aspx?mjesec=" + mjesec + "&godina=" + godina + "&lang=" + Lang + " #KalendarData", function() {
        $('#Kalendar').html(buildCal(mjesec, godina, $('#events').html()));
        $('#mj').html(mn[mjesec - 1]);
        $('#god').html(godina);
        hover();
        $('#Kalendar').fadeIn("fast");
    });
}

function clickPrevNext(next) {
    $('#Kalendar').fadeOut('fast', function() {
        $('#Kalendar').empty();

        if (next) {
            if (mjesec == 12) {
                godina = godina + 1;
                mjesec = 1;
            }
            else {
                mjesec = mjesec + 1;
            }
        }
        else {
            if (mjesec == 1) {
                mjesec = 12;
                godina = godina - 1;
            }
            else {
                mjesec = mjesec - 1;

            }
        }
        napraviEvente();
    });
}

function hover() {
    $('.date_has_event').each(function() {
        // options
        var distance = 10;
        var time = 150;
        var hideDelay = 100;

        var hideDelayTimer = null;

        // tracker
        var beingShown = false;
        var shown = false;

        var trigger = $(this);
        var popup = $('.events ul', this).css('opacity', 0);

        // set the mouseover and mouseout on both element
        $([trigger.get(0), popup.get(0)]).mouseover(function() {
            // stops the hide event if we move from the trigger to the popup element
            if (hideDelayTimer) clearTimeout(hideDelayTimer);

            // don't trigger the animation again if we're being shown, or already visible
            if (beingShown || shown) {
                return;
            } else {
                beingShown = true;

                // reset position of popup box
                popup.css({
                    bottom: 20,
                    left: -156,
                    display: 'block' // brings the popup back in to view
                })

                // (we're using chaining on the popup) now animate it's opacity and position
				.animate({
				    bottom: '+=' + distance + 'px',
				    opacity: 1
				}, time, 'swing', function() {
				    // once the animation is complete, set the tracker variables
				    beingShown = false;
				    shown = true;
				});
            }
        }).mouseout(function() {
            // reset the timer if we get fired again - avoids double animations
            if (hideDelayTimer) clearTimeout(hideDelayTimer);

            // store the timer so that it can be cleared in the mouseover if required
            hideDelayTimer = setTimeout(function() {
                hideDelayTimer = null;
                popup.animate({
                    bottom: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function() {
                    // once the animate is complete, set the tracker variables
                    shown = false;
                    // hide the popup entirely after the effect (opacity alone doesn't do the job)
                    popup.css('display', 'none');
                });
            }, hideDelay);
        });
    });
}
