﻿$(document).ready(function()
{
    InitCalendar("fin");
    InitCalendar("lex");
});

function InitCalendar(calendarType)
{
    //get todays values
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var day = d.getDate();

    exd = new Date();
    exd.getDate();

    //calendar
    var datastring = "year: " + year + ", month: " + month + ", calendarType: '" + calendarType + "'";
    //AddLog("initCALENDAR - " + datastring);

    GetData(
        "GetCalendar",
        datastring,
        function(data)
        {
            $("#" + calendarType + "-cal-container").html(data.d);
            BindCalendarEvents(calendarType);
        });

    //events
    var datastring = "date: '" + day + "." + month + "." + year + "', calendarType: '" + calendarType + "'";
    //AddLog("initEVENTS - " + datastring);

    GetData(
        "GetEvents",
        datastring,
        function(data)
        {
            $("#" + calendarType + "-events-container").html(data.d);
            BindEventsEvents(calendarType);
        });
}

function BindCalendarEvents(calendarType)
{
    $("#" + calendarType + "-cal .month input.prev").click(function() { SelectMonth(-1, calendarType); return false; });
    $("#" + calendarType + "-cal .month input.next").click(function() { SelectMonth(1, calendarType); return false; });
    $("#" + calendarType + "-cal table td").click(function()
    {
        SelectDay(this, calendarType);
    });
}

function BindEventsEvents(calendarType)
{
    $("#" + calendarType + "-events .controls img").click(function()
    {
        if (calendarType === "fin")
        {
            window.location = "MonthlyCalendar.aspx?date=" + GetSelectedDate(calendarType);
        }
        else
        {
            window.location = "LexEvents.aspx?date=" + GetSelectedDate(calendarType);
        }

        //AddLog("SHOW - date: '" + GetSelectedDate(calendarType) + "'");
    });
}

function SelectDay(cellObject, calendarType)
{
    $("#" + calendarType + "-cal table td").removeClass("selected");
    $(cellObject).addClass("selected");

    //alert("Show event counts for date: " + GetSelectedDate());

    var datastring = "date: '" + GetSelectedDate(calendarType) + "', calendarType: '" + calendarType + "'";
    //AddLog("EVENTS - " + datastring);

    GetData(
        "GetEvents",
        datastring,
        function(data)
        {
            $("#" + calendarType + "-events-container").html(data.d);
            BindEventsEvents(calendarType);
        });
}

function SelectMonth(direction, calendarType)
{
    var month = parseInt($("#" + calendarType + "-selected-month").val());
    var year = parseInt($("#" + calendarType + "-selected-year").val());

    if (month === 1 && direction === -1) { month = 12; year = year - 1; }
    else if (month === 12 && direction === 1) { month = 1; year = year + 1; }
    else { month = month + direction; }

    var datastring = "year: " + year + ", month: " + month + ", calendarType: '" + calendarType + "'";
    //AddLog("CALENDAR - " + datastring);

    GetData(
        "GetCalendar",
        datastring,
        function(data)
        {
            $("#" + calendarType + "-cal-container").html(data.d);
            BindCalendarEvents(calendarType);
        });
}

function GetSelectedDate(calendarType)
{
    //alert("get selected date: " + calendarType);
    var d = new Date();
    var day = d.getDate();
    if ($("#" + calendarType + "-cal table td.selected").length > 0)
    {
        day = $("#" + calendarType + "-cal table td.selected").text().replace(/^\s*|\s*$/g, "");
    }
    var month = $("#" + calendarType + "-selected-month").val();
    var year = $("#" + calendarType + "-selected-year").val();

    var date = day + "." + month + "." + year;

    return date;
}

function AddLog(message)
{
    $("div#log").prepend("<div>" + message + "</div>");
}

/* ajax-y sh*t... */
function GetData(method, datastring, successCallback, errorCallback)
{
    $.ajax({
        type: "POST",
        url: "WebSvc/TflCalendars.asmx/" + method,
        data: "{ " + datastring + " }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(data, textstatus)
        {
            if (successCallback) { successCallback(data); }
        },
        error: function(request, textstatus, exception)
        {
            if (errorCallback)
            {
                errorCallback(request, textstatus, exception);
            }
            else
            {
                alert(request.responseText);
            }
        }
    });
}
