var refContent = Class.create({
  initialize:function() {
    this.rootClassName = SiteVars.modules.RefContent.className;
    this.displayClass  = SiteVars.modules.RefContent.displayAreaClass;
    this.imgPanelClass  = SiteVars.modules.RefContent.imgPanelClass;
    this.smryClass  = SiteVars.modules.RefContent.smryClass;
    this.loadingMSG    = '';
    this.loadFlag = false;
    
  },
  
  load:function(){
    this.refSets = $$('.'+this.rootClassName);
    
    //alert('called load');
     
     
    if(typeof(this.refSets)!='undefined'){
      
      
      var self = this;
      
      this.refSets.each(function(item){
 
        item.displayArea = Element.select(item, '.'+self.displayClass)[0];
        item.rHandler = Element.select(item, '.refcontentrhandler')[0];
        item.show = true;
        
        //alert('about to call behavior');
        
        self.behaviour(item);
      });
    }
  },
  
  behaviour:function(item){
        
    //alert('called behavior');
    var self = this;
    
    item.titleAttribute = (Element.readAttribute(item, 'title'))? Element.readAttribute(item, 'title') : '';
    if(item.titleAttribute.indexOf('params')!=-1 || item.titleAttribute.indexOf('url')!=-1){;
      var json = item.titleAttribute.evalJSON();
      var ajax = new Ajax.Request(json.url + '?' + json.params,
         {
            method:'get',
            onLoad:function() {
              self.loadFlag = false;
              item.displayArea.innerHTML = self.loadingMSG; 
            },
            onComplete:function(r) {
              
              item.displayArea.innerHTML = r.responseText;
              self.loadFlag = true;
              self.updateProps(item);
            }
        });
      item.title='';
      item.removeAttribute('title');
    }
    
  },
  
  updateProps:function(item) {
    var self = this;
    
    item.refreshLinks = Element.select(item.displayArea, '.refresh');
    item.imagePanel = Element.select(item.displayArea, '.'+this.imgPanelClass)[0];
    item.summaryPanel = Element.select(item.displayArea, '.'+this.smryClass)[0];
    
    if(item.refreshLinks[0]){
      item.refreshLinks.each(function(link){
        link.parentItem = item;
        
        //alert('about to call linkbehavior');
        self.linkbehaviour(link);
      });
    }
    
  },
  
  linkbehaviour:function(link){
    var self = this;
        
    link.relAttribute = (Element.readAttribute(link, 'rel'))? Element.readAttribute(link, 'rel') : '';
    if(link.relAttribute.indexOf('params')!=-1 || link.relAttribute.indexOf('url')!=-1){;
      link.getAjaxDirective = modules.sanitizeResponse(link.relAttribute, {});
      
      if(link.getAjaxDirective){
        
        //alert(item.getAjaxDirective);
        
        link.hasContent = false;
        link.getAjaxDirective.isStatic = (Element.hasClassName(link, 'static')) ? true : false;
        //link.getAjaxDirective.sendMethod = 'get';
        link.getAjaxDirective.onLoad = function(){
          //link.parentItem.displayArea.innerHTML = content.loadingMSG;
        }
        link.getAjaxDirective.onSuccess = function(){
        };
        link.getAjaxDirective.onComplete = function(){
          
          //alert(link.getAjaxDirective.displayType);
          
          var flash = (link.getAjaxDirective.displayType == 'FlashContent')?true:false;
          
          //alert(flash);
          
          self.parseContent(link.parentItem, flash);
        }
        link.getAjaxDirective.updateElem = link.parentItem.rHandler;
        //alert(link.getAjaxDirective.updateElem);
        Event.observe(link, 'click', function(e){
            //console.log(link.parentItem.rHandler);
            
            //alert('send async');
            
            modules.sendAsync(link.getAjaxDirective);
            Event.stop(e);
        });
        
      }
      //alert('remove rel attribute');
      link.rel='';
      link.removeAttribute('rel');
      //alert('skipped remove rel attribute');
      //console.log(item)
    }
    
  },
  
  parseContent:function(item, flash){
    
    if(flash){
      item.imagePanel.innerHTML = "";
      item.imagePanel.innerHTML = Element.select(item.rHandler, '.'+this.imgPanelClass)[0].innerHTML;
    }
    else{
      Element.setStyle(item.imagePanel, {
        backgroundImage:Element.select(item.rHandler, '.'+this.imgPanelClass)[0].style.backgroundImage
      });
      item.summaryPanel.innerHTML = Element.select(item.rHandler, '.'+this.smryClass)[0].innerHTML;
    }
  }
  
  
});


document.observe("dom:loaded", function() {
    var ref = new refContent();
    ref.load();
});

