////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Base Date Cache Class
function calendar_datecache(id, search_type, cal) {
  this.obj_name = 'calendar_cache';
  
  // Store Information
  this.id = id;
  this.search_type = search_type;
  this.cal = cal;  
  
  // Init Data
  this.cache = new Array();
  
  // Register Callback from calendar
  this.cal.register_callback_onchange_month(this.load_busydates.bind(this));
}

calendar_datecache.prototype.iden = function() {
  alert(this.obj_name);
}

calendar_datecache.prototype.get_datestr = function(year, month) {
  month = util_convert_string_to_int(month);
  year = util_convert_string_to_int(year);
  var datestr = year.toString() + '_';
  if(month < 10)
    datestr += '0';
  datestr += month.toString();
  return datestr;
}

calendar_datecache.prototype.load_busydates = function(calobj) {
  var year = this.cal.get_year('display');
  var month = this.cal.get_month('display');
  var datestr = this.get_datestr(year, month);
  
  if(this.cache[datestr] == 1) {
    // Default
  }
  else {
    // set here so it won't double load until we get async response
    this.cache[datestr] = 1;
    
    // Load
    new Ajax.Request('ajax.php',
    {
      parameters: {
        "_action": 'calendar_load_dates', 
        "target_id":this.id,
        "search_type":this.search_type,
        "year":year,
        "month":month,
      },
      onSuccess: function(response) {
        var json_response = response.responseText.evalJSON();
        if(json_response.ajax_status == 1) {
          // Go through type of lists
          var iTypeLen = json_response.dates.length;
          for (var iType = 0;iType < iTypeLen; iType++) {
            var listtype = json_response.dates[iType]['name'];
            var numitems = json_response.dates[iType]['num'];
            var list = json_response.dates[iType]['list'];
            
            var count = 0;
            for(var iList in list) {
              // iterator
              if(count == numitems) break;
              count++;
              
              // set date with new information
              this.cal.set_day_num_events_for_type(listtype, iList, list[iList]);
            }
          }

          // Apply refresh
          this.cal.refresh_display();
        }
        else {
          alert(json_response.error_msg);
          this.cache[datestr] = 0;
        }
      }.bind(this), 
      onFailure: function(){ 
        alert("Error loading your calendar dates.  Please try again later.");
        this.cache[datestr] = 0;
      }
    });
    
  }
}
