// Tooltip Object
var activeTooltip = undefined;
var Tooltip = Class.create();
Tooltip.prototype = {
  initialize: function(el, options) {
		this.el = $(el);
    this.el.toolTip = this;
		this.initialized = false;
    this.showTimer = false;
    this.hideTimer = false;
    this.enabled = true;
		this.setOptions(options);
		
		// Event handlers
		this.showEventListener = this.showEvent.bindAsEventListener(this);
		this.hideEventListener = this.hideEvent.bindAsEventListener(this);
		Event.observe(this.el, "mouseover", this.showEventListener );
		Event.observe(this.el, "mouseout", this.hideEventListener );
		
		// Removing title from DOM element to avoid showing it
		this.content = this.el.title;
		this.el.title = "";

		// If descendant elements has 'alt' attribute defined, clear it
		//this.el.descendants().each(function(el){
		//	if(Element.readAttribute(el, 'alt'))
			//	el.alt = "";
		//});
	},
	setOptions: function(options) {
		this.options = {
			backgroundColor: '#fff', // Default background color
			borderColor: '#666', // Default border color
			textColor: '', // Default text color (use CSS value)
			textShadowColor: '', // Default text shadow color (use CSS value)
			maxWidth: 250,	// Default tooltip width
			align: "left", // Default align
			delay: 100, // Default delay before tooltip appears in ms
      hideDelay: 1000, // Default delay before tooltip disappears in ms 
			mouseFollow: true, // Tooltips follows the mouse moving
			appearDuration: .0, // Default appear duration in sec
			hideDuration: .0 // Default disappear duration in sec
	};
		Object.extend(this.options, options || {});
	},
	showEvent: function(e) {
		if(!this.initialized && !this.showTimer){
		  this.showTimeout = window.setTimeout(this.appear.bind(this), this.options.delay);
      this.showTimer = true;
    } else {
      if(this.hideTimer){
        this._clearTimeout(this.hideTimeout);
        this.hideTimer = false;
      }
    }
	},
	hideEvent: function(e) {
		if(this.initialized && !this.hideTimer) {
			this.hideTimeout = window.setTimeout(this.disappear.bind(this), this.options.hideDelay);
      this.hideTimer = true;
    } else {
      if(this.showTimer){
        this._clearTimeout(this.showTimeout);
        this.showTimer = false;
      }
    } 		
	},
  hide: function(){
    if(this.initialized) {
        //Reset possible slider
        var elements = $(this.tooltip).select('.slider-box-content-left');
        if(elements.length > 0){
          RatingWrapper.reset_bar(elements[0]);
        }        
        Event.stopObserving(this.tooltip, "mouseover", this.showEventListener );
        Event.stopObserving(this.tooltip, "mouseout", this.hideEventListener );
        this.tooltip.hide();
        if(this.tooltip.parentNode != null){
          this.tooltip.remove();
        }
        this.initialized = false;
        activeTooltip = undefined;
    }
  },
  disable: function(){
    this.hide();
    if(this.showTimer){
        this._clearTimeout(this.showTimeout);
        this.showTimer = false;
    } else if(this.hideTimer){
        this._clearTimeout(this.hideTimeout);
        this.hideTimer = false;
    }
 		Event.stopObserving(this.el, "mouseover", this.showEventListener );
		Event.stopObserving(this.el, "mouseout", this.hideEventListener );
    this.enabled = false;
  },
  enable: function(){
    Event.observe(this.el, "mouseover", this.showEventListener );
		Event.observe(this.el, "mouseout", this.hideEventListener );
    this.enabled = true;
  },
  toggle: function(){
      if(this.enabled){
        this.disable();
      } else {
        this.enable();
      }
  },
  appear: function() {
		this.showTimer = false;
    if(activeTooltip != undefined){
      var element = $(activeTooltip);
      if(element != undefined && element.toolTip != undefined){
        element.toolTip.hide();
      }
    }
    
   //alert(this.el.offsetLeft);
    
    // Building tooltip container
		this.tooltip = Builder.node("div", {className: "tooltips-box", style: "display: none;" }, [
			Builder.node("div", {className: "tooltips-box-content"}, this.content)]);
		
    if(typeof(this.options.parentId) != 'string'){
      this.el.parentNode.insertBefore(this.tooltip, this.el.parentNode.childNodes[0]);
    } else {
      var element = $(this.options.parentId);
      if(element != null){
        element.insertBefore(this.tooltip, element.childNodes[0]);
      } else {
        this.el.parentNode.insertBefore(this.tooltip, this.el.parentNode.childNodes[0]);
      }
    }
		
		//Element.extend(this.tooltip); // IE needs element to be manually extended
		this.tooltip = $(this.tooltip);
    this.options.width = this.tooltip.getWidth();
		this.tooltip.style.width = this.options.width + 'px'; // IE7 needs width to be defined
		
    //Element.clonePosition(this.tooltip, this.el, {setWidth: false, setHeight: false, offsetLeft: -7, offsetTop: 12});
    var offsetParent = Position.offsetParent(this.tooltip);
    var cumulativeOffsetParent = Position.cumulativeOffset(offsetParent);
    var cumalativeOffsetElement = Position.cumulativeOffset(this.el);
    this.tooltip.style.left = cumalativeOffsetElement[0] - cumulativeOffsetParent[0]  - 82 + "px";//this.xCord - 7 + "px";
		this.tooltip.style.top = cumalativeOffsetElement[1] - cumulativeOffsetParent[1] + 5 - $(this.tooltip).getHeight() + "px";//this.yCord + 12 + "px";
		//this.setup();
		
    Event.observe(this.tooltip, "mouseover", this.showEventListener);
		Event.observe(this.tooltip, "mouseout", this.hideEventListener);
    
    this.initialized = true;
		activeTooltip = this.el.id;
    //this.tooltip.show();
    
    //Show the slider using an ajx request
    var element = this.tooltip;
    var ratingValue = $(this.options.sliderFormID).ratingValue.value;
    new Ajax.Request("/programdetails/showSliderTooltip", 
                                                { evalScripts: true,
                                                  parameters: { rating: ratingValue,
                                                                tooltipID: this.el.id,
                                                                formID: this.options.sliderFormID,
                                                                displayID: this.options.sliderDisplayID,
																displayType: this.options.sliderDisplayType},
                                                  onSuccess: function(transport){
                                                    element.update(transport.responseText);
                                                    element.show()}});
    
    //this.appearingFX = new Effect.Appear(this.tooltip, {duration: this.options.appearDuration});
	},
  disappear: function() {
      this.hideTimer = false;
      this.hide();

  },
	setup: function(){
		// If content width is more then allowed max width, set width to max
		if(this.options.width > this.options.maxWidth) {
			this.options.width = this.options.maxWidth;
			this.tooltip.style.width = this.options.width + 'px';
		}
			
		// Tooltip doesn't fit the current document dimensions
		if(this.xCord + this.options.width >= Element.getWidth(document.body)) {
			this.options.align = "right";
			this.xCord = this.xCord - this.options.width + 20;
		}
		
		this.tooltip.style.left = this.el.offsetLeft - 50 + "px";//this.xCord - 7 + "px";
		this.tooltip.style.top = this.el.offsetTop - 0 - $(this.tooltip).getHeight() + "px";//this.yCord + 12 + "px";
	},
	_clearTimeout: function(timer) {
		clearTimeout(timer);
		clearInterval(timer);
	}
};

function hideTooltipEvent(event, tooltipId){
 	if (!event) var event = window.event;
  event.cancelBubble = true;
	if (event.stopPropagation) event.stopPropagation();

  hideTooltip(tooltipId)
}

function hideTooltip(tooltipId){
  var element = $(tooltipId);
  if(element != null && element.toolTip != undefined){
    element.toolTip.hide();
  }
}

function enableTooltip(tooltipId){
  var element = $(tooltipId);
  if(element != null && element.toolTip != undefined){
    element.toolTip.enable();
  }
}

function disableTooltip(tooltipId){
  var element = $(tooltipId);
  if(element != null && element.toolTip != undefined){
    element.toolTip.disable();
  }
}

function toggleTooltip(tooltipId){
  var element = $(tooltipId);
  if(element != null && element.toolTip != undefined){
    element.toolTip.toggle();
  }
}