
if(!window.__rageLib){
  window.__rageLib = {};
  window.__rageLib.instances = {};
}

__rageLib.rageView = {};

__rageLib.rageView.Controller = function(){};
__rageLib.rageView.Controller.prototype = {
  
  idx_by_id: [],
  
  process: function(id){
    this.idx_by_id[id].process();
  },
  
  changeURL: function(id, new_url){
    this.idx_by_id[id].ajax.setURL(new_url);
  },
  
  getView: function(id){ return this.idx_by_id[id]; },
  saveView: function(id, obj){ this.idx_by_id[id] = obj; },
  
  setParam: function(viewId, k, v){
    var view = null;
    if(view = this.getView(viewId)){
      view.setParam(k,v);
      return view;
    }
  },
  
  setDisplayLimit: function(viewId, limit, proc){
    var view = null;
    if(view = this.getView(viewId)){
      this.setActPage(viewId, 1, false);
      view.setParam(view.getVarKey('limit'),limit);
      if(proc) view.process();
    }
  },
  
  setActPage: function(viewId, page, proc){
    var view = null;
    if(view = this.getView(viewId)){
      view.setParam(view.getVarKey('act_page'),new Number(page));
      if(proc) view.process();
    }
  },
  
  nextPage: function(viewId, proc){
    var view = null;
    if(view = this.getView(viewId)){
      view.setParam(view.getVarKey('act_page'),view.getParam(view.getVarKey('act_page'))+1);
      if(proc) view.process();
    }
  },
  
  prevPage: function(viewId, proc){
    var view = null;
    if(view = this.getView(viewId)){
      view.setParam(view.getVarKey('act_page'),view.getParam(view.getVarKey('act_page'))-1);
      if(proc) view.process();
    }
  },
  
  setSorting: function(viewId, idx, col, val, proc){
    var view = null;
    if(view = this.getView(viewId)){
      view.setSorting(idx, col, val);
      if(proc) view.process();
    }
  },
  
  setFiltering: function(viewId, col, idx, type, val, proc){
    var view = null;
    if(view = this.getView(viewId)){
      view.setFiltering(col, idx, type, val);
      if(proc) view.process();
    }
  },
  
  generateExcel: function(viewId){
    var view = null;
    if(view = this.getView(viewId)){
      view.setParam('action',view.getVarKey('action_generate_excel'));
      var link = view.createPostParams();
      location.href = location.href+'&'+link;
    }
  }
  
}

__rVC = __rageViewController = new __rageLib.rageView.Controller();

/*****************/

__rageLib.rageView.Processor = function(id, viewProcessor, separator){
  this.__construct(id, viewProcessor, separator);
}

__rageLib.rageView.Processor.prototype = {
  
  __construct: function(id, viewProcessor, separator){
    
    
    this.params = new Array();
    this.ajax = null;
    this.sorting = new Array();
    this.filtering = new Array();
    this.var_keys = new Array();
    this.tempPostParams = '';
    
    if(!viewProcessor) viewProcessor = 'index.php';
    
    this.id = id;
    
    __rageViewController.saveView(this.id, this);
    
    this.params['CUSTOM'] = new Array();
    
    this.separator = separator;
    
    this.params['URL'] = viewProcessor;
    this.params['ON_SUCCESS'] = function(XmlHttp){
      var view = __rageViewController.getView(id);
      var t = XmlHttp.responseText.split(view.separator);
      if(t[1] == view.getVarKey('action_create_view')){
        document.getElementById('rv_'+t[0]+'_container').innerHTML = t[2];
      }
    };
    
    this.ajax = new __rageLib.rageAJAX(this.params);
    return this;
  },
  
  setSorting: function(idx, col, val){
    if(this.sorting[idx]){
      if(this.sorting[idx][0] == col && this.sorting[idx][1] == val){
        //this.sorting[idx] = null; //unset sorting
        while(this.sorting[idx]){
          this.sorting[idx++] = null;
        }
      }else 
        this.sorting[idx] = new Array(col, val);
    }else
      this.sorting[idx] = new Array(col, val);
  },
  
  setFiltering: function(col, idx, type, val){
    if(!this.filtering[col]) 
      this.filtering[col] = new Array();
    this.filtering[col][idx] = new Array(type, val);
  },
  
  setVarKey: function(k,v){
    this.var_keys[k] = v;
  },
  
  getVarKey: function(k){
    return this.var_keys[k] ? this.var_keys[k.toLowerCase()] : null;
  },
  
  setParam: function(k, v){
    if(k == 'fname'){
      //__rageViewController.getParam(this.id, 'fname');
    }
    this.params['CUSTOM'][k] = v;
  },
  
  getParam: function(key){
    if(this.params['CUSTOM'][key])
      return this.params['CUSTOM'][key];
    else return null; 
  },
  
  process: function(){
    //alert(this.getParam('fname'));
    this.ajax.postParams = this.createPostParams();
    this.ajax.process();
  },
  
  createPostParams: function(){
    var t = null;
    var separator = this.getVarKey('separator');
    this.tempPostParams = new Array();
    this.tempPostParams.push(this.getVarKey('post_id')+'='+this.id);
    for(k in this.params['CUSTOM']){
      if(this.params['CUSTOM'][k] instanceof Function){ continue;  /*fix for prototype extensions */ }
      this.tempPostParams.push(k+'='+this.params['CUSTOM'][k]); 
    }
    
    
    var sort_key = this.getVarKey('order_by');
    for(k in this.sorting){
      if(this.sorting[k] instanceof Function){ continue;  /*fix for prototype extensions */ }
      if(this.sorting[k]){
        var k2 = sort_key+separator+k;
        this.tempPostParams.push(k2+'='+ this.sorting[k][0]+separator+this.sorting[k][1]);
      } 
    }
    
    
    var filter_key = this.getVarKey('filter');
    for(col in this.filtering){
      if(this.filtering[col] instanceof Function){ continue;  /*fix for prototype extensions */ }
      if(this.filtering[col]){
        for(idx in this.filtering[col]){
          if(this.filtering[col][idx]){
            var k2 = filter_key+separator+col+separator+idx;
            this.tempPostParams.push(k2+'='+ this.filtering[col][idx][0]+separator+this.filtering[col][idx][1]);
          } 
        }
      }
    }
    
    
    return this.tempPostParams.join('&');  
  },
  
  setDefaultPostParam: function(k, v){
    k = k.toLowerCase();
    if(typeof(this.params['CUSTOM'][k]) == 'undefined'){
      this.tempPostParams.push(k+'='+v);
    }
  },
  
  reset: function(){
    this.params['CUSTOM'] = new Array();
    this.process();
  },
  
  reload: function(){
    this.process();
  }
  
};

