﻿/**
*   AJAX calendar
*   @param calendarBox -> Id of the element in which to dump the returned calendar's html
*   @param monthSelect -> Id of the month select element
*   @param yearSelect  -> Id of the year select element
*   @param button      -> Id of the button element that initiates the request to get a specific calendar        
/*  @param eventPopUp -> Bool.  True if you want to display an inline event popup when a user clicks an event link
**/
(function(){
     
    // Constructor
    AjaxCalendar = function(oParams)
    {
        me = this;       
        funcs.init(oParams);
    };

    // Global
    var me;        
   
    //Private Properties and Methods
    var funcs = {
    
        // Initialize the object
        init: function(oParams)
        {   
            me.form = oParams.form;
            me.formDisabled = false;
            me.monthSelect = oParams.monthSelect;
            me.yearSelect = oParams.yearSelect; 
            me.calendarBox = oParams.calendarBox;            
            
            YUE.onAvailable('calendarWrapper', function(){
                funcs.launchWaitDlg();
                if(oParams.eventPopUp !== undefined && oParams.eventPopUp == true)
                {
                    funcs.eventPopUp = new PopUpDialog({
                        id:'eventDialog', parentId:'calendarWrapper', centerInElement:true
                    });
                    
                    funcs.eventPopUp.onClose.subscribe(function(){
                        var content = YUD.getElementsByClassName('bd', 'div', 'eventDialog');
                        content[0].innerHTML = '';
                    });
                }
            });
            
            
            YUE.on(me.form, 'submit', funcs.getCalendarSubmitCallBack);            
            YUE.on(me.calendarBox, 'click', funcs.showEventDialog);
        },
        
        // Overlay Object for loading
        dialog:{},
        
        /**
                    *   Handles clicks on the go button for grabbing new calendar content.  Validates to make sure the month and year selects have values and then fires the request to retrieve the content
                    *   @param e -> The event object
                    *   @param obj -> Object passed back from the click handler                    
                    **/
        getCalendarSubmitCallBack: function(e, obj)
        {
            YUE.preventDefault(e);
            
            if(!me.formDisabled)
            {   
                // Get the month and the year
                var monthSelect = YUD.get(me.monthSelect),
                    month = monthSelect.options[monthSelect.options.selectedIndex].value,
                    yearSelect = YUD.get(me.yearSelect),
                    year = yearSelect.options[yearSelect.options.selectedIndex].value;
                
                // Build the request                
                funcs.getCalendar('GET', '/webservices/feservice.aspx?method=getCalendar&date='+month + ',' + year);                 
                me.formDisabled = true;                
                funcs.dialog.dialog.show();
            }
            
            return false;
        },
        
        /**
                    *   Wrapper for creating the XHR request to retrieve the calendar
                    *   @param method -> The method of the XHR request. GET
                    *   @param url -> Url of the service for retrieving the calendar
                    **/
        getCalendar: function(method, url)
        {
            var xhr = new JSONRequester();            
            xhr.onSuccess.subscribe(funcs.getCalendarCallBack);
            xhr.onFailure.subscribe(funcs.getCalendarError);
            xhr.makeRequest(method, url);   
        },
        
        /**
                    *   Populates the innerHTML property of the calendar elment
                    *   @param json -> A YUI XMLHttpRequest object wrapper                    
                    **/        
        getCalendarCallBack: function(json)
        {
            if(!json.status || json.status != '1')
            {
                funcs.getCalendarError(json);                
            }
            
            var cal = YUD.get(me.calendarBox);            

            cal.innerHTML = json.data;                     
            funcs.dialog.dialog.hide();
            
            me.formDisabled = false;
        },
        
        /**
                    *   Handles the failure case of the XHR return trip for the product list
                    *   @param json -> A YUI XMLHttpRequest object wrapper                    
                    **/
        getCalendarError: function(json)
        {
            funcs.dialog.dialog.hide();            
            me.formDisabled = false;
        },
        
        /**
                    *   Creates a loading dialog while the XHR is made
                    **/
        launchWaitDlg: function()
        {   
            var calendarBox = YUD.get('calendarBox');   
            funcs.dialog = new LoadingDialog({
                'id':'calendarLoading',
                'parentId':'calendarWrapper',
                'message':'Please wait while we load the calendar...',
                'modal':false
            }, true);              
        },
        
        showEventDialog: function(e, obj)
        {
            var target = YUE.getTarget(e);
            if(!target){return}
            
            if(YUD.hasClass(target, 'initOverlay'))
            {
                if(funcs.eventPopUp !== undefined)
                {
                    funcs.eventPopUp.show();
                    funcs.getEvent('GET', target.href + '?mode=bodyContent&id=eInfo');
                    YUE.preventDefault(e);
                }
            }
            
        },
        
        /**
                    *   Wrapper for creating the XHR request to retrieve event information
                    *   @param method -> The method of the XHR request. GET
                    *   @param url -> Url of the service for retrieving the calendar
                    **/
        getEvent: function(method, url)
        {
            var callback = {
                success:funcs.getEventItemCallBack,
                failure:funcs.getEventItemError 
            };
            
            YUC.asyncRequest(method, url, callback);          
            var eventItem = YUD.getElementsByClassName('bd', 'div', 'eventDialog');            
            YUD.addClass(eventItem[0], 'loading');
        },        
        
        /**
                    *   Populates the innerHTML property of the calendar elment
                    *   @param json -> A YUI XMLHttpRequest object wrapper                    
                    **/        
        getEventItemCallBack: function(json)
        { 
            var eventItem = YUD.getElementsByClassName('bd', 'div', 'eventDialog');            
            YUD.removeClass(eventItem[0], 'loading');
            eventItem[0].innerHTML = json.responseText + '<div class="closeText"><a class="closeWindow" href="#">Close Window</a></div>';  
            
        },
        
        /**
                    *   Handles the failure case of the XHR return trip for the product list
                    *   @param json -> A YUI XMLHttpRequest object wrapper                    
                    **/
        getEventItemError: function(json)
        {
            var eventItem = YUD.getElementsByClassName('bd', 'div', 'eventDialog');                        
            YUD.removeClass(eventItem[0], 'loading');
        }
    };
})();

eventsCalendar = new AjaxCalendar({
    'form':'eventCalender',
    'calendarBox':'calendarBox',
    'monthSelect':'month',
    'yearSelect':'year',    
    'eventPopUp': true
});