/* --------------------------------------------------------------------------------------------*/
/* ------------------------------------->>> COOLTIPS V 1.0 <<<---------------------------------*/
/* --------------------------------------------------------------------------------------------*/

var CollapsibleList = Class.create({
  initialize: function(obj, bgObject) {
      this.element = $(obj);
      this.trans = {};
      this.bgobject = $(bgObject);
      this.minHeight = $("defListBGrpt").getHeight();
      this.bgobject.hide();
      this.terms = this.element.select('dt');
      var that = this;
      var objClose= document.createElement('a');
		  objClose.setAttribute('id', 'closeBtn');
		  objClose.setAttribute('href', '#');
		  objClose.innerHTML = 'X CLOSE';
		  $('defListBGrpt').appendChild(objClose);
	  this.terms.each(function(term) {
        term.definition = $(term).next();
        term.that = that;
        Element.hide(term.definition);
        term.observe('mouseover', that.showLink);
        term.observe('mouseout', that.hideLink);
        term.observe('click', that.toggleDefinition);
      });
  },
  showDefinition: function(event){
      Event.stop(event);
      var obj = event.element();
      if(!obj.that.active){
         Element.show(obj.definition);
		  obj.removeClassName('overL');
          obj.addClassName('over');
          obj.that.setHeight(obj);
          obj.that.bgobject.show();
      }
  },
  
   showLink: function(event){
      Event.stop(event);
      var obj = event.element();
         obj.addClassName('overL');
  },
  
   hideLink: function(event){
      Event.stop(event);
      var obj = event.element();
         obj.removeClassName('overL');
   },
  
  setHeight: function(obj){
      var ddHeight = obj.definition.getHeight();
      var bgHeight = ddHeight + 20;
      if(bgHeight > obj.that.minHeight){
         $("defListBGrpt").setStyle({
            height: (bgHeight + "px")
         });
      }else{
         $("defListBGrpt").setStyle({
            height: (obj.that.minHeight + "px")
         });
      } 
  },
  hideDefinition: function(event){
      Event.stop(event);
      var obj = event.element();
      if(!obj.that.active){
          obj.removeClassName('over');
		  obj.removeClassName('overL');
         Element.hide(obj.definition);
         obj.that.bgobject.hide();
      }
   },
 closeDefinition: function(obj){
      if(!obj.that.active){
          obj.removeClassName('over');
		  obj.removeClassName('overL');
         Element.hide(obj.definition);
         obj.that.bgobject.hide();
      }
   },
   toggleDefinition: function(event){
      Event.stop(event);
      var obj = event.element();
      obj.removeClassName('over');
      if(obj.that.active == obj){
         Element.hide(obj.definition);
         obj.that.bgobject.hide();
         obj.that.active = null;
         obj.removeClassName('active');
		 $('cr2').removeClassName('ghost');
      }else{
         if(obj.that.active){
            Element.hide(obj.that.active.definition);
            obj.that.active.that.bgobject.hide();
            obj.that.active.removeClassName('active');
			 $('cr2').removeClassName('ghost');
         }
         obj.that.setHeight(obj);
         obj.addClassName('active');
			//FUNCTION CLOSE BTN
			$('closeBtn').onclick = function(){
				if(obj.that.active == obj){
					Element.hide(obj.definition);
					obj.that.bgobject.hide();
					obj.that.active = null;
					obj.removeClassName('active');
					 $('cr2').removeClassName('ghost');
					 return false;
				}
			
			};
			
			//FUNCTION CLOSE BTN
         Element.show(obj.definition);
         obj.that.bgobject.show();
		$('cr2').addClassName('ghost');
         obj.that.active = obj;
      }
   }
});
