﻿//This function converts JSON date to 
function parseJSONDate(jdate) {
    var edt = new Date();           
    var s = eval(jdate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
    edt = new Date(s);
    var mil = Date.UTC(edt.getFullYear(),edt.getMonth(), edt.getDate(), edt.getHours(), edt.getMinutes(), edt.getSeconds(), edt.getMilliseconds()); //Miliseconds since January 1, 1970
    edt = new Date(mil); //Convert to local time
    
    return edt;
}

//Remove an item from an array
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

function getMonthEvents(mn, yr, inst) {
    var tojson = {
        'MonthValue': mn,
        'YearValue': yr
    };
    $('#' + inst.id).datepicker('disable');
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/EventMethods.aspx/GetMonthEvents",
        data: JSON.stringify(tojson),
        dataType: "json",
        success: function(result) {
            var evs = result.d;
            var tds = $("#" + inst.id + " table").children("tbody").children("tr").children("td").children("a");

            $(tds).each(function() {
                //get the cell date
                var tdt = new Date()
                tdt.setFullYear(yr, mn - 1, $(this).html());
                tdt.setHours(0, 0, 0, 0);
                //get the first date before the loop
                if ($(evs).size() > 0) {
                    var edt = parseJSONDate(evs[0]);

                    //While the cell date is the same
                    var ecount = 0;
                    while ($(evs).size() > 0 && tdt.getFullYear() == edt.getFullYear() && tdt.getMonth() == edt.getMonth() && tdt.getDate() == edt.getDate()) {

                        $(this).css("background", "#a75b95");
                        $(this).css("color", "#FFF");
                        $(this).css("border", "0px solid #363");

                        //Remove the event from the list
                        evs.remove(0);
                        ecount++;

                        //Create the date for the next event, this should really be a function
                        if ($(evs).size() > 0) {
                            edt = parseJSONDate(evs[0]);
                        }
                    }

                    $(this).parent().attr("onclick", "");

                    if (ecount > 0) {
                        $(this).parent().click(function() {

                            var dtStr = "";
                            dtStr += $(this).children('a').html() + "-";
                            dtStr += mn + "-";
                            dtStr += yr;
                            window.location = "/whats-on-listings.aspx?df=" + dtStr + "&dt=" + dtStr;
                            return false;
                        });
                    }
                }
            });
            $('#' + inst.id).datepicker('enable');
        },
        error: function(xhr, status, error) {
            alert(xhr.responseText);
            $('#' + inst.id).datepicker('enable');
        }
    });
}


function slideEvent(dtStr) {
    var tojson = {
        'DayValue': dtStr.split("/")[0],
        'MonthValue': dtStr.split("/")[1],
        'YearValue': dtStr.split("/")[2]
    };

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/EventMethods.aspx/GetEventDay",
        data: JSON.stringify(tojson),
        dataType: "json",
        success: function(result) {

            var msg = '<h2>Events on ' + getDateString(new Date(dtStr.split("/")[2], dtStr.split("/")[1], dtStr.split("/")[0])) + '</h2>';

            for (ee in result.d) {
                if (ee != 'remove') {
                    msg = msg + '<p><strong>' + result.d[ee].ProdName + '</strong><br />';
                    msg = msg + result.d[ee].Description + '</p>';
                }
            }
            $("#eventscalinfo").html(msg);
            $('#eventscalinfo').animate({ "left": "350px", "width": "400px"});

        },
        error: function(xhr, status, error) {
            alert(xhr.responseText);
        }
    });
}

function getDateString(dt) {
            var dd = new Date(dt);
            dd.setMonth(dd.getMonth() - 1);
            var suffix = "th";

            switch (dd.getDate()) {
                case 1:
                case 21:
                case 31:
                    suffix = "st";
                    break;
                case 2:
                case 22:
                    suffix = "nd";
                    break;
                case 3:
                case 23:
                    suffix = "rd";
                    break;
            }

            return days[dd.getDay()] + ", " + dd.getDate() + suffix + " " + months[dd.getMonth()] + " " + dd.getFullYear();
        }
        
        var days = new Array();
        days[0] = "Sunday";
        days[1] = "Monday";
        days[2] = "Tuesday";
        days[3] = "Wednesday";
        days[4] = "Thursday";
        days[5] = "Friday";
        days[6] = "Saturday";

        var months = new Array();
        months[0] = "January";
        months[1] = "February";
        months[2] = "March";
        months[3] = "April";
        months[4] = "May";
        months[5] = "June";
        months[6] = "July";
        months[7] = "August";
        months[8] = "September";
        months[9] = "October";
        months[10] = "November";
        months[11] = "December";
