﻿//
// create closure
//
(function ($) {
    //
    // plugin definition
    //
    $.fn.brAjaxCalendar = function (options) {
        // build main options before element iteration
        var opts = $.extend({}, $.fn.brAjaxCalendar.defaults, options);
        // iterate and reformat each matched element
        return this.each(function () {
            $this = $(this);
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

            $("#basicDialog").dialog("close");

            //show loading
            $("#" + opts.ajaxBuzy).css("height", $(document).height());
            $("#" + opts.ajaxBuzy + " img").css("marginTop", ($(document).height() / 2));
            $("#" + opts.ajaxBuzy).show();


            //add note to data to send
            var showNotes = $this.find("." + o.noteElementClass + " input:checkbox").attr("checked");
            $this.data("showNotes", showNotes);
            //$('#btn').unbind('click'); 
            $this.find("." + o.noteElementClass + " input:checkbox").unbind('click');
            $this.find("." + o.noteElementClass + " input:checkbox").click(function () {
                //alert('omg checked!');
                $("#" + $this.attr("id")).brAjaxCalendar();
                //window.location.href = window.location.href + "&showNotes" + $(this).attr("checked");
            });

            var idList = "";
            //get calendar lists
            var thedate = new Date();
            $.get(o.calendarListUrl + "?element=" + $this.attr("id") + "&thedate=" + thedate.getUTCMilliseconds(), function (data) {
                $this.find("." + o.calendarList).html(data);



                $this.find("." + o.availableCalendars + " input:checkbox").each(function () {
                    if ($(this).attr("checked")) {
                        idList += "," + $(this).val() + "-" + $(this).attr("foreignkeycalendartypeid");
                    }
                });
                //alert(idList);
                $this.data("idList", idList);
                switch (o.command) {
                    case "fetchCalendar":
                        $.fn.brAjaxCalendar.fetchCalendar(o, $this);
                        break;
                }
            });

            //alert($this.data("idList"));

            /*switch (o.command) {
            case "fetchCalendar":
            $.fn.brAjaxCalendar.fetchCalendar(o, $this);
            break;
            }*/
        });
    };
    //
    // define and expose our format function
    //


    $.fn.brAjaxCalendar.fetchCalendar = function (opts, myCalendar) {
        $.post(opts.url, myCalendar.data(), function (data) {

            $(data).find('diaryItem').each(function () {
                var dateItem = $(this).attr('date');
                // alert($(this).find('html')text());
                var dateHTML = $(this).find('html').text();
               // dateHTML += dateItem;
                $("#" + opts.itemCellPrefix + dateItem).html(dateHTML);
            });

            $(data).find('scripts').each(function () {
                var scriptHTML = $(this).find('script').text();
                myCalendar.find("." + opts.scriptsElement).html(scriptHTML);
            });

            if (opts.debugEnabled) {
                $("#" + opts.debug).html(escape(data));
            }
            $("#" + opts.ajaxBuzy).hide();

        });



    };



    //
    // plugin defaults
    //
    $.fn.brAjaxCalendar.defaults = {
        url: '/_ajax/calendar/getCalendarItems.ashx',
        calendarListUrl: '/_ajax/calendar/listAvailableCalendars.ashx',
        command: 'fetchCalendar',
        debug: 'debug',
        debugEnabled: false,
        noteElementClass: 'showNotes',
        ajaxBuzy: "ajaxBusy",
        calendarList: 'calendarListing',
        availableCalendars: 'calendarListing .availableCalendar',
        itemCellPrefix: 'braci',
        scriptsElement: 'bracs'
    };
    //
    // end of closure
    //
})(jQuery);



function recordCalendarPathUserSetting(foreignKeyID, foreignKeyCalendarTypeID,element) {
   
    var thedate = new Date();

    var url = "/_ajax/calendar/recordCalendarPathUserSettings.ashx?foreignKeyID=" + foreignKeyID + "&foreignKeyCalendarTypeID=" + foreignKeyCalendarTypeID + "&time=" + thedate.getUTCDate();
    $.get(url, function (data) {
        $("#" + element).brAjaxCalendar();
    });

}
