// JavaScript Document
//-------------------- rico.js
var Rico = {
  Version: '1.1.2',
  prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
}

if((typeof Prototype=='undefined') || Rico.prototypeVersion < 1.3)
      throw("Rico requires the Prototype JavaScript framework >= 1.3");

Rico.ArrayExtensions = new Array();

if (Object.prototype.extend) {
   Rico.ArrayExtensions[ Rico.ArrayExtensions.length ] = Object.prototype.extend;
}else{
  Object.prototype.extend = function(object) {
    return Object.extend.apply(this, [this, object]);
  }
  Rico.ArrayExtensions[ Rico.ArrayExtensions.length ] = Object.prototype.extend;
}

if (Array.prototype.push) {
   Rico.ArrayExtensions[ Rico.ArrayExtensions.length ] = Array.prototype.push;
}

if (!Array.prototype.remove) {
   Array.prototype.remove = function(dx) {
      if( isNaN(dx) || dx > this.length )
         return false;
      for( var i=0,n=0; i<this.length; i++ )
         if( i != dx )
            this[n++]=this[i];
      this.length-=1;
   };
  Rico.ArrayExtensions[ Rico.ArrayExtensions.length ] = Array.prototype.remove;
}

if (!Array.prototype.removeItem) {
   Array.prototype.removeItem = function(item) {
      for ( var i = 0 ; i < this.length ; i++ )
         if ( this[i] == item ) {
            this.remove(i);
            break;
         }
   };
  Rico.ArrayExtensions[ Rico.ArrayExtensions.length ] = Array.prototype.removeItem;
}

if (!Array.prototype.indices) {
   Array.prototype.indices = function() {
      var indexArray = new Array();
      for ( index in this ) {
         var ignoreThis = false;
         for ( var i = 0 ; i < Rico.ArrayExtensions.length ; i++ ) {
            if ( this[index] == Rico.ArrayExtensions[i] ) {
               ignoreThis = true;
               break;
            }
         }
         if ( !ignoreThis )
            indexArray[ indexArray.length ] = index;
      }
      return indexArray;
   }
  Rico.ArrayExtensions[ Rico.ArrayExtensions.length ] = Array.prototype.indices;
}

/*-----------------------------------*/
Rico.Tips = Class.create();
Rico.Tips.prototype = {
  initialize: function(container, linkcontainer,linkclass) {
      this.container            = $(container);
      this.linkcontainer        = $(linkcontainer);
      this.linkclass            = linkclass;
      this.tipsTips             = new Array();
      this.tipsLinks            = new Array();
      this._attachBehaviors();
      
      //this._attachBehaviors();
      if(!container || !linkcontainer || !linkclass ) {
        return 
        }
      // set the initial visual state...
     this.showTip(0);
   },
  
  showTip: function(index) {
    for( var i = 0 ; i < this.tipsTips.length ; i++ ) {
      if (i == index) {
        this.tipsTips[i].show();
        this.tipsLinks[i].hide();
        } else { 
        this.tipsTips[i].hide();
        this.tipsLinks[i].show();
      }
    }
    
  },
  
 _attachBehaviors: function() {
      var panels = this._getDirectChildrenByTag(this.container, 'DIV');
      for ( var i = 0 ; i < panels.length ; i++ ) {
         this.tipsTips.push( new Rico.Tips.Tip(this,panels[i]));
      }
      
      var tiplinks = this._getDirectChildrenByTag(this.linkcontainer, 'DIV');
      for ( var i = 0 ; i < tiplinks.length ; i++ ) {
         var TipSelector = tiplinks[i];
          this.tipsLinks.push( new Rico.Tips.TipLink(this,TipSelector,i) );
      }
   },
  _getDirectChildrenByTag: function(e, tagName) {
      var kids = new Array();
      var allKids = e.childNodes;
      for( var i = 0 ; i < allKids.length ; i++ )
         if ( allKids[i] && allKids[i].tagName && allKids[i].tagName == tagName )
            kids.push(allKids[i]);
      return kids;
   }
}

/*-----------------------------------*/
Rico.Tips.Tip = Class.create();
Rico.Tips.Tip.prototype = {

   initialize: function(tips,content) {
      this.tips = tips;
      this.content = content;
      this.displaytype = this.content.style.display;
      this.visible = false;
   },

   hide: function() {
      this.visible = false;
      this.content.style.display = 'none';
   },

   show: function() {
      this.visible = true;
      this.content.style.display = this.displaytype;
   }

}
/*-----------------------------------*/
Rico.Tips.TipLink = Class.create();
Rico.Tips.TipLink.prototype = {

   initialize: function(tips,content,index) {
      this.tips = tips;
      this.content = content;
      this.index = index;
      this.displaytype = this.content.style.display;
      this.visible = true;
      this._attachBehaviors();
   },
   
   linkClicked: function(e) {
      this.tips.showTip(this.index);
   },
   
   show : function(e){
      this.visible = false;
      this.content.style.display = this.displaytype;
   },
   
   hide : function(e){
      this.visible = true;
      this.content.style.display = 'none';
   },
   
   _attachBehaviors: function() {
     //add event to link
     var linksContained = this._getDirectDescendantsByTag(this.content,'A');
     for( var i = 0 ; i < linksContained.length ; i++ ) {
      if(linksContained[i].className == this.tips.linkclass) {
        linksContained[i].onclick = this.linkClicked.bindAsEventListener(this);
        break;
      }
     }
   },

    _getDirectDescendantsByTag: function(e, tagName) {
      var kids = new Array();
      var kids = e.getElementsByTagName(tagName);
      return kids;
   }
};
  
