var ToolTip = {
 current_tt_handle: null,

 closeToolTip: function(){
   	if (this.current_tt_handle){
  		this.current_tt_handle.close();
  	}
  },
  
  showTip: function(posx,posy,obj) {
    if ( obj.name === undefined ) {obj.name = obj.getAttribute('name');}
  	this.closeToolTip();
  	var objectId = obj.id.toString();

  	/* Classname is infoLink + category number. we want to extract the number */
  	var tipcontent = "info" + objectId.substr(obj.className.length);  
  	var isHover = (obj.name.indexOf("hover") != -1);
  	
  	this.isCloseOnMouseOut = (obj.name.indexOf("close_on_mouse_out") != -1);
    if(this.isCloseOnMouseOut){
      obj.onmouseout = function(){
        try{myWin.close();}catch(e){}
      }
    }
  
  	if(obj.name.indexOf("open-left") != -1){
  		posx -=450;		
  	}
  	
  	var hide_effect = (!isHover)? Effect.Shrink : Element.hide;
  	var show_effect = (!isHover)? Effect.Appear : Element.show;
  	
  	var height = $(tipcontent).offsetHeight;
  	var width = $(tipcontent).offsetWidth;
  
  	if($(tipcontent).style.width){
  		width = parseInt($(tipcontent).style.width.replace("px", ''));
  	}
  	if($(tipcontent).style.height){
  		height = parseInt($(tipcontent).style.height.replace("px", ''));
  	}
  	
    var classname = String(($(tipcontent).classNames() && $(tipcontent).classNames() != "")? $(tipcontent).classNames() : "trexglobal");
    if (classname.indexOf("drop_arrow") != -1 ){
      posx = PMClient.find_element_x_pos(obj) + (obj.offsetWidth/2) - (width/2)-5;
      posy = PMClient.find_element_y_pos(obj) + 20;
      if(navigator.appVersion.indexOf("MSIE 7.0") !=-1 ){
        posy-=10;
      };
    }

    if (classname.indexOf("pickup_arrow") != -1){
      posx = PMClient.find_element_x_pos(obj) + (obj.offsetWidth/2) - (width/2)-5;
      posy = PMClient.find_element_y_pos(obj) - 84;
      if(navigator.appVersion.indexOf("MSIE 7.0") !=-1 ){
        posy-=10;
      };
    }

    myWin = new Window({className: classname, top: posy, left: posx, zIndex: 203, width: width, height: height,  minimizable: false, closable: (obj.name.indexOf("noclose") != -1 ? false : true), maximizable: false, resizable: false, showEffect: show_effect, hideEffect: hide_effect, draggable:false, wiredDrag: false});
  
  
  	myWin.setContent(tipcontent, false);
    $(tipcontent).show();
  	$(tipcontent).style.display="block";
  	$(tipcontent).style.styleFloat="left";
  	$(tipcontent).style.cssFloat="left";
  
  
  	
  	myWin.setCloseCallback(function(){
  		$(tipcontent).style.display = "none";
      document.body.appendChild($(tipcontent)); 
  		myWin = null;
  		return true;
  	})
  	
  	myWin.show();
  	
  	if($(tipcontent).style.zIndex){
  		myWin.setZIndex($(tipcontent).style.zIndex);
  	}
  	myWin.toFront();
  	
  	$(tipcontent).onmouseover = function(){document.onmousedown = function(){}};
  	$(tipcontent).onmouseout = function(){document.onmousedown = ToolTip.closeToolTip};
  	
    this.current_tt_handle = myWin;
    if(this.isCloseOnMouseOut){
      $$(".drop_arrow_close").each(function(el){
        el.style.display = "none";
      });
    }
  },
  
  
  /**
   * Hides the tip layer
   * @returns - none
   */
  hideTip: function(){
  	document.getElementById('infoContainer').style.display="none";
  },
  
  assign_events_to_link: function(link_el){
    if(link_el.name.indexOf("hover") != -1){
			link_el.onmouseover = function(e){ToolTip.onHoverAction(e, this)};
		}else{
			var tipcontent_obj = $("info" + link_el.id.substr(link_el.className.length));

			link_el.removeAttribute("alt");
			link_el.removeAttribute("title");
			link_el.href = "javascript://";
			
			link_el.onmouseover = function(){ToolTip.qTipShowAction(this)};
			link_el.onmouseout = function(){ToolTip.qTipHideAction(this)};
		}
		return true;
  },
  
  /**
   * Attaches the onclick event to the link element and call a function
   * @returns - false : to prevent the scroll up
   */
  tipLinkClick: function() {
  	var linkList = $A(document.getElementsByTagName("a"));
    linkList.each(function(link_el){
  	  if(!($(link_el).hasClassName("infoLink")))
  	    return false;
  	  
  		ToolTip.assign_events_to_link(link_el);
  	});
  	return true;	    
  },
  
  qTipHideAction: function(obj){
		if (obj.name == "open-left"){
			tooltip.offsetX = 0;
		}
		tooltip.hide();
	},
  
  qTipShowAction: function(obj){
  	tip_el = $("info" + obj.id.substr(obj.className.length));
  	if (obj.name == "open-left"){
  		tooltip.offsetX = -300;
  	}
  	if (obj.name == "open-top"){
  		tooltip.offsetY = (parseInt(tip_el.style.height.replace(/px/g, ""))*-1)-20;
  	}
  	
  	tooltip.show(tip_el);
  },
  
  /**
   * Attaches the onclick event to the link element and call a function
   * @returns - false : to prevent the scroll up
   */
  tipLinkFocus: function() {
  	var linkList= document.getElementsByTagName("input");
  	for (var i=0;i<linkList.length;i++) {
  		if (linkList[i].className=="infoLinkFocus") {
  			linkList[i].onselect = function(){ToolTip.tipLinkOnFocus(e, this)};
  		}  
  	}
  	return false;	    
  },
  
  tipLinkOnFocus: function(e, obj){
  	if (!e)	{ var e = window.event;	}
  	this.showTip(PMClient.find_element_x_pos(obj)+90, PMClient.find_element_y_pos(obj), obj);
  	return false;
  },
  
  
  /**
   * Starts finding the absolute postion of the link
   * @param {Event} e - the event (required for Mozilla)
   * @returns - false : to prevent the scroll up
   */
  onHoverAction: function(e, obj){
  	if (!e)	{ var e = window.event;	}
  	this.setCoordinates(e, obj);	
  	return false;
  },
  
  
  /**
   * Finds the absolute position of the link
   * @param {Object} obj - the link object
   * @param {Event} e - the event (required for Mozilla)
   * @returns - false : to prevent the scroll up
   */
  setCoordinates: function(e,obj) {
  	var posx = 0;
  	var posy = 0;	
  	if (!e) { var e = window.event; }
  	if (e.pageX || e.pageY) {
  		posx = e.pageX;
  		posy = e.pageY;
  	}
  	else if (e.clientX || e.clientY) {
  		// posx and posy contain the mouse position relative to the document
  		posx 	= e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  		posy 	= e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
  	}
  	this.showTip(posx,posy,obj);
  },
  
  
  /**
   * Attaches the onclick event to the help link element and call a function
   * @returns - none
   */
  helpLinkClick: function() {
  	Event.observe($("helpLink"), "click", this.helpOnClick);
  },
  
  
  /**
   * Shows the help layer object
   * @returns - false (to prevent scrolling)
   */
  helpOnClick: function() {
  	this.hideHelpContent();	
  	if (document.getElementById("help").style.display=="" || document.getElementById("help").style.display=="none") {
  		document.getElementById("helpLink").style.background = "url('/images/arrow_lg_down.gif') left 2px no-repeat";
  		document.getElementById("rightcolhelp").style.width="150px";
  		document.getElementById("rightcolhelp").style.height="auto";
  		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
  		 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
   		 if (ieversion>=6 && ieversion < 7){
  			document.getElementById("rightcolhelp").style.position="absolute";
  			document.getElementById("rightcolhelp").style.left=$('main').style.right;
  		 }
  		}
  		document.getElementById("help").style.display="block";
  	}
  	else if (document.getElementById("help").style.display=="block") {
  		document.getElementById("helpLink").style.background = "url('/images/arrow_lg_right.gif') left 2px no-repeat";
  		document.getElementById("help").style.display="none";
  		document.getElementById("rightcolhelp").style.height="40px";
  		document.getElementById("rightcolhelp").style.width="60px";
  		document.getElementById("rightcolhelp").style.position = "static";
  	}
  	return false;
  },
  
  
  /**
   * Shows/Hides the item layer
   * @returns - none
   */
  helpItemClick: function() {
  	this.hideHelpContent();	
  	var itemnumber = this.id.substr(this.className.length);
  	var helpcontent= document.getElementById("helpContent"+itemnumber);
  	if(helpcontent.style.display=="" || helpcontent.style.display=="none") {
  		helpcontent.style.display = "block";
  	}
  	else {
  		helpcontent.style.display="none";
  	}
  	return false;  
  },
  
  /**
   * Attaches the onclick event to the item link element and call a function
   * @returns - none
   */
  helpItemLink: function() {
  	var linkList= document.getElementsByTagName("a");
  	for(var i=0;i<linkList.length;i++) {	
  		if(linkList[i].className=="helpItem") {
  			linkList[i].onclick = function(){ToolTip.helpItemClick()};  		    
  		}  
  	}
  },
  
  /**
   * Hides the help layer
   * @returns - none
   */
  hideHelpContent: function() {
  	var i=1;
  	while(document.getElementById("helpContent"+i)) {
  		document.getElementById("helpContent"+i).style.display="none";
  		i++;
  	}
  },
  
  observe_events: function(){
    /* Add new event handlers functions onload */
    Event.observe(window,"load", this.tipLinkClick);
    Event.observe(window,"load", this.tipLinkFocus);
    if (typeof tips_only == "undefined"){
      Event.observe(window,"load",function(){if($("rightcolhelp")){ToolTip.helpLinkClick();}});
      Event.observe(window,"load",this.helpItemLink);
    }
    if ( this.isCloseOnMouseOut )
      Event.observe(document, "mouseup", function(){setTimeout(function(){ToolTip.closeToolTip();}, 100)});
  },
  
  create_tooltip: function(tt_id, content, opts){
    if(typeof opts == "undefined"){
      opts = new Hash();
    }
    if(typeof opts.image_url == "undefined" || opts.image_url == null){
      opts.image_url = "/images/info.png";
    }
    
    if(typeof opts.hover_popup == "undefined" || opts.hover_popup == null){
      opts.hover_popup = false;
    }
    
    var img_el = new Element("img");
    var tip_link = new Element("a");
    var container = new Element("span")
    
    img_el.src = opts.image_url;
    tip_link.appendChild(img_el);
    
    if(opts.hover_popup == true){
      tip_link.name = "hover";
    }
    tip_link.id = "infoLink"+tt_id;
    tip_link.addClassName("infoLink");
    
    var tooltip_content = new Element("div");
    tooltip_content.id = "info"+tt_id;
    tooltip_content.style.width = "300px";
    tooltip_content.hide();
    tooltip_content.appendChild(content);
    
    container.appendChild(tooltip_content);
    container.appendChild(tip_link);
    
    ToolTip.assign_events_to_link(tip_link);
    
    return container;
  }

}

ToolTip.observe_events();

