/* namespace for the recent items management */
RecentItemsNS = {

  _sectionName: '',

  _currentWorkingItem: {},

  /* set the item we're working on; it'll be lately retrieved by
   * getCurrentWorkingItem */
  setCurrentWorkingItem: function(category, id, actionurl) {
    /* <QUICK FIX> */
    if(category == '')
      category = 'message';
    /* </QUICK FIX> */
    if($type(id) == "array")
      id = $A(id).filter(function(x) { return $type(x) == "number"; })[0];
    else if($type(id) == "object")
      id = id.id;
    RecentItemsNS._currentWorkingItem.category = category;
    RecentItemsNS._currentWorkingItem.id = id;
    RecentItemsNS._currentWorkingItem.actionurl = actionurl;
    requestDebug('recent item = ',RecentItemsNS._currentWorkingItem);
  },

  /* get the current working item previously set */
  getCurrentWorkingItem: function() {
    return RecentItemsNS._currentWorkingItem;
  },

  /* add current working item to bucket */
  addWorkingItemToBucket: function() {
    // get out parameters
    var category = RecentItemsNS._currentWorkingItem.category;
    var id = RecentItemsNS._currentWorkingItem.id;
    // check the current working item is set
    if($defined(category) && $defined(id))
    {
      requestDebug('calling ' + "http://www.cuboware.com/reQUEST/RecentItems/addRecentItemAndGetDiv" +
        ' with data = ' + Object.toQueryString({category: category, 'id': id}));
      // make the call
      new AjaxReload(
        "http://www.cuboware.com/reQUEST/RecentItems/addRecentItemAndGetDiv", {
          hideLoadingScreen: true,
          replace: 'recentitemsbox',
          evalScripts: true,
          data: Object.toQueryString({category: category, 'id': id, sectionName: RecentItemsNS._sectionName})
        }
      ).request();
    }
  },

  reloadBucket: function() {
    new AjaxReload(
      "http://www.cuboware.com/reQUEST/RecentItems", {
        hideLoadingScreen: true,
        replace: 'recentitemsbox',
        evalScripts: true,
        data: Object.toQueryString({sectionName: RecentItemsNS._sectionName})
      }
    ).request();
  },

  /* elements already existing in the bucket */
  _existingItems: null,

  /* set the items existing in the bucket. if no previous existing items where
   * present, do nothing. if there were elements, instead, make a yellow-fade
   * of the new elements. */
  setExistingItems: function(existingItems)
  {
    if(RecentItemsNS._existingItems == null)
      RecentItemsNS._existingItems = $A(existingItems);
    else
    {
      // check if there are new items
      existingItems.forEach(function(item) {
        if(RecentItemsNS._existingItems.filter(function(i) {
          return i.id == item.id && i.category == item.category; }).length == 0)
        {
          // get out the td
          var td = $('recentitem_' + item.category + '_' + item.id);
          // apply the background fade
          new Fx.Style(td, 'background-color', {duration: 2000}).start('ffff00', 'ffffff');
        }
      });
      RecentItemsNS._existingItems = $A(existingItems);
    }
  }
}
