$(document).ready(function(){
/* current data for initial event list state */   
   /* check for cookies */
var current_day = ($.cookie('current_day') || '');
var current_month = ($.cookie('current_month') || '');
var current_year = ($.cookie('current_year') || '');
var current_cat = ($.cookie('current_cat') || '');
var current_cat_name = ($.cookie('current_cat_name') || '');
var current_group = ($.cookie('current_group') || '');
var current_group_name = ($.cookie('current_group_name') || '');
var big_cal = ($.cookie('big_cal') || '');
var ev_list = ($.cookie('ev_list') || '');

if (current_day=='') {
   var current_date = new Date();
   current_day = current_date.getDate();
   current_month = current_date.getMonth();
   current_month+=1;
   current_year = current_date.getFullYear();
};

if (current_cat=='') {current_cat = "all";};
if (current_cat_name=='') {current_cat_name = "All Areas, ";};
if (current_group=='') {current_group = "all";};
if (current_group_name=='') {current_group_name = "All Affiliates, ";};
if (big_cal=='') {big_cal = "off";};
if (ev_list=='') {ev_list = "on";};
/*
console.log("current_day:"+current_day);
console.log("current_month:"+current_month);
console.log("current_year:"+current_year);
console.log("current_cat:"+current_cat);
console.log("current_cat_name:"+current_cat_name);
console.log("current_group:"+current_group);
console.log("current_group_name:"+current_group_name);
console.log("big_cal:"+big_cal);
console.log("ev_list:"+ev_list);
*/

/* updates the event list based on all parameters */
   function update_events(day, month, year, cats, group, catname, groupname, bigstate){
      $('#eventlist').fadeOut().load('/ajax-events.php', {'day': day, 'month': month, 'year':year, 'cats':cats, 'group':group, 'catname':catname, 'groupname':groupname, 'bigstate':bigstate}, 
         function(){
            $(this).fadeIn();
         });
      };//update_events
/* initial event list on page load */  
   if (ev_list=="on") {
      update_events(current_day, current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, big_cal);
   };
   if (big_cal=="on") {
      update_cal(current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, ev_list);
      event_fade();
   }
   select_size();
   
/* event list update based on clicking a minical link */
   $('#smallcal td.linked-day a').click(function(){
      var path = this.pathname.replace(/^\//,'');
      var chunks = path.split('/');
      current_year = chunks[1];
      current_month = chunks[2];
      current_day = chunks[3];
      /* set cookies for date selected */
      $.cookie('current_day', current_day);
      $.cookie('current_month', current_month);
      $.cookie('current_year', current_year);
      ev_list="on";
      if (big_cal=="on") {
         cal_fade();
         big_cal="off";
         $.cookie('big_cal', 'off');
      };
      update_events(current_day, current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, big_cal);
      return false;
   });
   
/* "Loading" indicator */
   $('p.loading').ajaxStart(function() { 
       $(this).show();
     }).ajaxStop(function() { 
       $(this).hide(); 
     });

/* Auto-population of the Affiliate List based on Category */
     function update_eventgroups(cats){
        $('#eventgroups').fadeOut().load('/ajax-eventgroups.php', {'cats': cats}, 
        function(){
           $(this).fadeIn();
        });
      };//update_eventgroup

   $('#eventcats').change(function() {
         if ($(this).attr({selected:'selected'})) {
            current_cat="";
            current_cat_name = "";
            $('#eventcats option:selected').each(function(){
               current_cat += $(this).val() + ",";
               current_cat_name += $(this).text() + ", ";
            });
         };
         current_group = "all";
         current_group_name = "All Affiliates, ";
         if (current_cat==",") {
            current_cat="all";
         };
         update_eventgroups(current_cat);
         if (ev_list=="on") {
            update_events(current_day, current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, big_cal);
         };
         if (big_cal=="on") {
            update_cal(current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, ev_list);
         };
         select_size();
         $.cookie('current_cat', current_cat);
         $.cookie('current_cat_name', current_cat_name);
         $.cookie('current_group', current_group);
         $.cookie('current_group_name', current_group_name);
   });

/* update Event List based on Affiliate Selected */
   $('#eventgroups').change(function() {
      current_group = "";
      current_group_name = "";
      $('#eventgroups option:selected').each(function(){
         current_group += $(this).val() + ",";
         current_group_name += $(this).text() + ", ";
      });
      if (current_group==",") {
         current_group="all";
      };
      if (big_cal=="on") {
         update_cal(current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, ev_list);
         event_fade();
      }
      else {
            update_events(current_day, current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, big_cal);
         };
      $.cookie('current_group', current_group);
      $.cookie('current_group_name', current_group_name);
   });

/* Notification for browsers w/o js */
   $('p#notify').hide();
   
/* bring in Large Cal based on parameters */

   function update_cal(month, year, cats, group, catname, groupname, ev_list) {
      $('#calendar-outer').fadeOut().load('/ajax-calendar.php', {'month': month, 'year': year, 'cats':cats, 'group':group, 'catname':catname, 'groupname':groupname, 'ev_list':ev_list}, 
         function(){
            $(this).fadeIn();
         });
         big_cal="on";
         $.cookie('big_cal', big_cal);
         
      };//update_cal_
   
   function event_fade() {
      $('#eventlist').fadeOut();
   };//event_fade
   
   function cal_fade() {
      $('#calendar-outer').fadeOut();
   };//cal_fade
   
/* clicking on minical month brings in large cal set to the small cal's month */
   $('a.monthLink').click(function() {
      //Get the text in the caption of the open mini-cal
//      var opencap = $('#smallcal div[id^=\"mini-cal\"]:visible caption.calendar-month a').text();
      // Trim whitespace from beginnning and end of string
      var opencap = this.pathname.replace(/^\//,'');
      var pieces = opencap.split('/');
      current_month=pieces[2];
      current_year=pieces[1];
//      $('#test').html('<h4>Before jquery sends to php:</h4><p>Caption:'+opencap+'</p><p>Current Month:'+current_month+'</p><p>Current Year:'+current_year+'</p>');
      update_cal(current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, ev_list);
      event_fade();
      big_cal="on";
      ev_list="off";
      $.cookie('current_month', current_month);
      $.cookie('current_year', current_year);
      $.cookie('big_cal', big_cal);
      $.cookie('ev_list', ev_list);
      return false;
   });
   

/* set select box sizes to fit all items */
   function select_size() {
      eventcatsize = $('#eventcats option').size();
      $('#eventcats').attr("size", eventcatsize);
      $('#eventgroups').ajaxStop(function(){
         eventgroupsize = $('#eventgroups option').size();
         $(this).attr("size", eventgroupsize);
         });
      }; //selectsize
      
      $(document).ajaxStop(function(){

         /* display a list of events when clicking on a calendar day */
         $('#content #allevents td.linked-day a').click(function() {
            var path = this.pathname.replace(/^\//,'');
            var pieces = path.split('/');
            current_year = pieces[1];
            current_month = pieces[2];
            current_day = pieces[3];
            ev_list = "on";
            update_events(current_day, current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, big_cal);
            $.cookie('current_year', current_year);
            $.cookie('current_month', current_month);
            $.cookie('current_day', current_day);
            $.cookie('ev_list', ev_list);
            return false;
         });
         
         $('p#eventback a').click(function() {
            cal_fade();
            big_cal = "off";
            $.cookie('big_cal', big_cal);
            update_events(current_day, current_month, current_year, current_cat, current_group, current_cat_name, current_group_name, big_cal);
            ev_list = "on";
            $.cookie('ev_list', ev_list);
            return false;
         });
      });//document.ajaxStop

});//document.ready

$().ajaxStart(function(){
   jQuery.blockUI({ css: { 
           border: 'none', 
           padding: '15px', 
           backgroundColor: '#333', 
           '-webkit-border-radius': '10px', 
           '-moz-border-radius': '10px', 
           opacity: '.5', 
           color: '#fff' 
         }, 
         overlayCSS: {
            backgroundColor: '#333',
            opacity: '0.1'
         }
       
       });
});

$().ajaxStop($.unblockUI);