﻿/**
*   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;        
   var li;
    //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);
            YUE.on('event', 'click',funcs.radioClickCallBack);  
            YUE.on('eventall', 'click',funcs.radioalleventsClickCallBack);   
           
            YUE.onAvailable('calendarBox', function(){
            moreLink=YUD.getElementsByClassName('more', 'a', 'calendarBox');
             YUE.on(moreLink, 'click', funcs.getmore_events);
             calenderRow=YUD.getElementsByClassName('calendarRow', 'li', 'calendarBox');
             for(var i=0;i<calenderRow.length;i++)
             {
              calenderbox=YUD.getElementsBy(function(obj){ 
              hasEvents=YUD.getElementsByClassName('initOverlay','a',obj); 
        if(hasEvents.length>0)
        {
            return true;        
        }
        else
        {
            return false;
        }
    }, 'LI', calenderRow[i]);;             
             YUE.on(calenderbox,'mouseover',function(e, obj){
             YUD.setStyle(this,'background-color','#EDEDED');
             });
             YUE.on(calenderbox,'mouseout',function(e, obj){
             YUD.setStyle(this,'background-color','#FFFFFF');
             });
             }
             });
        },
        
        // 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  
             if(document.getElementById ('event')!=null )
             {
                     if(document.getElementById ('eventall').checked==true )
                     {
                        funcs.getCalendar('GET', '/webservices/feservice.aspx?method=getCalendar&date='+month + ',' + year+"&type=all");                 
                     }
                     else
                     {
                        funcs.getCalendar('GET', '/webservices/feservice.aspx?method=getCalendar&date='+month + ',' + year+"&type=partner");                       
                     }
             }
             else 
             {
                        funcs.getCalendar('GET', '/webservices/feservice.aspx?method=getCalendar&date='+month + ',' + year);                 
             }
                    me.formDisabled = true;                
                    funcs.dialog.dialog.show();

                //clearradio();
//                document.getElementById ('event').checked=false;
//                document.getElementById ('eventall').checked=false;
            }
            
            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;
        },
        
//        clearradio:function()
//        {
//          document.getElementById ('event').checked=false;
//           document.getElementById ('eventall').checked=false;
//        },
        
        
        radioClickCallBack: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+"&type=partner");                 
//                funcs.getCalendar('GET', '../webservices/feservice.aspx?method=getCalendar&date='+month + ',' + year+"&type=partner");                 
                me.formDisabled = true;                
                funcs.dialog.dialog.show();
                document.getElementById ('event').checked=checked ;
            }
            
            return false;
        },
        
        
        
         radioalleventsClickCallBack: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+"&type=all");                 
                me.formDisabled = true;                
                funcs.dialog.dialog.show();
                document.getElementById ('eventall').checked=checked;
            }
            
            return 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}
            li=target.parentNode;
            if(YUD.hasClass(target, 'initOverlay'))
            {
                if(funcs.eventPopUp !== undefined)
                {                    
                    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 
            };
var eventItem = YUD.getElementsByClassName('bd', 'div', 'eventDialog');
            YUD.addClass(li, 'loading');
            YUC.asyncRequest(method, url, callback);          
                        
            
        },        
        
        /**
                    *   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(li, 'loading');
            eventItem[0].innerHTML = json.responseText + '<div class="closeText" style="top:26em;"><a class="closeWindow" href="#">Close Window</a></div>';  
            funcs.eventPopUp.show();
            
        },
        
        /**
                    *   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');
        },
        
         getmore_events: function(e,obj)
        {
             YUE.preventDefault(e);
            var eventItem = YUD.getElementsByClassName('bd', 'div', 'eventDialog');
            YUD.addClass(this.parentNode.parentNode, 'loading');
            var url=this.href+'&mode=bodyContent&id=eInfo';
            var pa=this.parentNode.parentNode;
            var callback = {
                success:function(json){ 
                       var eventItem = YUD.getElementsByClassName('bd', 'div', 'eventDialog'); 
                       eventItem[0].innerHTML =  json.responseText +'<div class="closeText" style="top:26em;"><a class="closeWindow" href="#">Close Window</a></div>';  
                       YUE.on(eventItem, 'click', funcs.showEventDialog);
                            
                YUD.removeClass(pa, 'loading');
            funcs.eventPopUp.show();
                       },
                failure:function(json){
                YUD.removeClass(pa, 'loading');
                       }
               };
                 
            YUC.asyncRequest('GET', url, callback);
        }
    };
})();

eventsCalendar = new AjaxCalendar({
    'form':'eventCalender',
    'calendarBox':'calendarBox',
    'monthSelect':'month',
    'yearSelect':'year',    
    'eventPopUp': true
});