﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid");

DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid = function(element) {
    DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid.initializeBase(this, [element]);
}

DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid.prototype = {
    initialize: function() {
        DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid.callBaseMethod(this, 'initialize');

        var context = this;

        this._gridBodyTemplate = jQuery.template(jQuery("#gridBody").html());

        jQuery("#gridBody").hide();
        jQuery("#gridBody tr.details").hide();

        jQuery("<a>").html("See All").prependTo(this.get_element()).click(function() {
            jQuery("#monthDropDownList").val("0");
            context._updateFilter();
        });
        jQuery("<select></select>").attr("id", "yearDropDownList").prependTo(this.get_element());
        jQuery("<select></select>").attr("id", "monthDropDownList").prependTo(this.get_element());

        $addHandlers(jQuery("#monthDropDownList")[0], {
            'change': this._updateFilter
        }, this);

        $addHandlers(jQuery("#yearDropDownList")[0], {
            'change': this._updateFilter
        }, this);

        this._loadYearDropDown();
        this._loadMonthDropDown();
        this._setCurrentDate();
        this._updateFilter();


        // Add custom initialization here
    },

    dispose: function() {
        //Add custom dispose actions here
        DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid.callBaseMethod(this, 'dispose');
    },

    _updateFilter: function() {
        DicksonCounty.Webservices.Event_Calendar.GetEventsByMonth(jQuery("#yearDropDownList").val(), jQuery("#monthDropDownList").val(), "", this._onSuccess, this._onFailure, this);
    },

    _loadYearDropDown: function() {
        var year = new Date().getFullYear();
        var years = [];

        for (var i = 0; i < 10; i++) {
            years.push(year++);
        }

        jQuery.each(years, function() {
            jQuery("<option>" + this + "</option>").attr("value", this).appendTo("#yearDropDownList");
        });
    },

    _loadMonthDropDown: function() {
        var months = {
            "": 0,
            "January": 1,
            "February": 2,
            "March": 3,
            "April": 4,
            "May": 5,
            "June": 6,
            "July": 7,
            "August": 8,
            "September": 9,
            "October": 10,
            "November": 11,
            "December": 12
        };

        jQuery.each(months, function(i, val) {
            jQuery("<option>" + i + "</option>").attr("value", val).appendTo("#monthDropDownList");
        });
    },

    _setCurrentDate: function() {
        var currentDate = new Date();

        jQuery("#yearDropDownList").val(currentDate.getFullYear());
        //jQuery("#monthDropDownList").val(currentDate.getMonth() + 1);
        //jQuery("#monthDropDownList").val("0");
    },

    _onSuccess: function(results, context, methodName) {
        jQuery("#gridBody tr").remove();
        jQuery("#gridBody").show();

        for (var i in results.Results) {
        
            var modified_link = createLink(results.Results[i].EventURL);

            jQuery("#gridBody").append(context._gridBodyTemplate, {
                date: results.Results[i].Date.format('d') + " - " + results.Results[i].EndDate.format('d'),
                event: results.Results[i].Name,
                id: results.Results[i].ID,
                url: modified_link, //results.Results[i].EventURL
                description: results.Results[i].LongDescription
            });

            jQuery("#gridBody tr td a." + results.Results[i].ID).click(function() {
                var className = jQuery(this).attr("class");

                jQuery("#gridBody tr.details." + className).toggle();

                if (jQuery("#gridBody tr td a." + className).html() == "View Details") {
                    jQuery("#gridBody tr td a." + className).html("Hide Details");
                } else {
                    jQuery("#gridBody tr td a." + className).html("View Details");
                }
            });
        }

        jQuery("tr.details").hide();
    },

    _onFailure: function(error, context, methodName) {
        jQuery("<div>An error has occured:<br />" + error.get_message() + "</div>").attr("title", "Error").dialog({
            buttons: {
                Ok: function() {
                    jQuery(this).dialog('close');
                }
            },
            modal: 'true'
        });
    },


}

DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid.createProperty("gridBodyTemplate");

DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid.registerClass('DicksonCounty.Site.js.aspnet.PublicEventCalendarGrid', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();



function createLink(url) {
    if (url) {
        return '<a href="' + url + '">Link</a> ';
    } else {
        return '&nbsp; ';
    }
}