﻿var FaqLoader = {

  show : function(o) {
    var el = o.parentNode;
    var id = this._get_id(el);
    
    var state = el.getAttribute('state');
    
    if(!state) state = 'load';
      
    switch(state){
    
      case 'load':
        this.createContent(el);
        
      case 'error':
        this.load(id, el);
        break;
        
      case 'complite':
        this.expend(id, el);
        break;            
    }
    
    return false;
  },
  
  load : function(id, el) {
  

    var obj = new Object();
    obj.id = id;
    obj.div = el.lastChild;
    
    obj.div.innerHTML = '<p class="ajax_loading">загрузка...</p>';
    obj.div.className = 'top_1em bottom_1em';
    
    el.className = 'plus';
    el.setAttribute('state', 'request');
    
    FaqService.GetContentById(id, Function.createDelegate(this, this.succeeded), Function.createDelegate(this, this.failed), obj);        
  },
  
  succeeded : function(result, obj) {
    obj.div.innerHTML = result;
    var el = obj.div.parentNode;
    el.setAttribute('state', 'complite');
    el.className = 'minus';
  },
  
  failed : function(sender, obj) {
    var div = obj.div;
    div.innerHTML = '<p class="ajax_load_error">Ошибка загрузки. Пожалуйста, щелкните на&nbsp;ссылке еще раз.</p>';
    var el = div.parentNode;
    el.setAttribute('state', 'error');
  }, 
  
  expend : function(id, el) {
  
    var s = el.lastChild.style;
    
    if(s.display == 'none') {
      el.className = 'minus';
      s.display = 'block';
    }
    else {
      el.className = 'plus';
      s.display = 'none';
    }
  },
  
  createContent : function(el) {
  
    var div = document.createElement('DIV');
    div.style.display = 'block';
    el.appendChild(div);      
  },
  
  _get_id : function(el) {
    return el.id.substring(3);
  }
  
};

$FL = FaqLoader;
