/*
Magic ToolTip (c) Mohamed Moujami 2006
Author: Mohamed Moujami | http://www.maroctour.com
*/

// Requires the prototype library

var Tooltip = {
    followMouse: true,
    tooltipID: "tooltip",
    showDelay: 300,
    hideDelay: 400,
		ttTimerOn:null,
		ttTimerOff:null,
		tip:null,
		_x:0,
		_y:0,
		_id: 0,
		_evt:null,
		_onDisplay: false,
		
		isIE: (window.ActiveXObject),
		isOpera: (navigator.userAgent.toLowerCase().indexOf('opera') > -1),
		browserMode: document.compatMode,
	
		init:function(){
			var current = document.getElementsByTagName('a');
			var curLen = current.length;
			for ( i=0; i<curLen; i++ ) {
				t=current[i].getAttribute("tooltip");
				if(t!=null && t.length!=0) {
					Event.observe(current[i],"mouseover",this.show);
					Event.observe(current[i],"mouseout",this.hide);
				}
			}
			Event.observe(document,"mousemove",this.move, false);
		},
		
		show:function(e){
			var type='';
			var id=0;
			var targ;
			if (!e) e = Tooltip._evt;
			else Tooltip._evt= e;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			if (targ.nodeType == 3) // defeat Safari bug
				targ = targ.parentNode;
		
			var tooltipInfo = targ.getAttribute("tooltip");
			var data = tooltipInfo.split(';');
			for(i=0;i<data.length;i++) {
				var parameter = data[i].split(':');
				switch(parameter[0]) {
					case 'type':
						type=parameter[1];
						break;
					case 'id':
						id=parameter[1];
						break;
					default:	
				}
			}
		
			if(Tooltip.ttTimerOff!= null) clearTimeout(Tooltip.ttTimerOff);
			// execute the right function for a specific type
			switch(type) {
					case 'menuitem':
						Tooltip.ttTimerOn = setTimeout("Tooltip.update("+id+")", Tooltip.showDelay);
						break;
					default:	
			}
		},
		hide:function(e){
			if(Tooltip.ttTimerOn!= null) clearTimeout(Tooltip.ttTimerOn);
			//if(this.ttTimerOff!= null) clearTimeout(this.ttTimerOff);
			Tooltip.ttTimerOff = setTimeout(function () {new Effect.Fade(Tooltip.tooltipID,{duration: 0.3}); Element.hide('twait'); Tooltip._onDisplay = false; }, Tooltip.hideDelay);
		},
		update:function(id){
			//$('ttitle').innerHTML = "&nbsp;";
			//$('tsummary').innerHTML = "&nbsp;";
			//$('textras').innerHTML = "";
			//Element.hide('tsummary');
			if(id && id != this._id) {
				Element.show('twait'); 
				xajax_GetItemInfo(id);
				this._id = id;
			} else {
				Tooltip._onDisplay = true;
				new Effect.Appear(this.tooltipID,{duration: 0.3});
				Tooltip.adjustPosition(); // adjust the tooltip elements before display
			}
		},
		updateResponse:function(title, summary, extras){
			$('ttitle').innerHTML = title;
			$('tsummary').innerHTML = summary;
			$('textras').innerHTML = extras;
			Element.hide('twait'); 
			//Element.show('tsummary');
			Tooltip._onDisplay = true;
			new Effect.Appear(this.tooltipID,{duration: 0.3});
			Tooltip.adjustPosition(); // adjust the tooltip elements before display
		},
		move:function(e){
			if( !e ) {
				if(window.event) e = window.event;
				else return;
			} else Tooltip._evt = e;
			
			Tooltip._x = Event.pointerX(e);
			Tooltip._y = Event.pointerY(e);
			
			if(Tooltip._onDisplay) Tooltip.adjustPosition();
		},
		adjustPosition:function(){
			var dimensions = Element.getDimensions(Tooltip.tooltipID);
			var tooltipWidth = dimensions.width;
			var tooltipHeight = dimensions.height;
			var bodyWidth = Tooltip.getViewportWidth() +  Tooltip.getPageXOffset() - 42;
			var bodyHeight = Tooltip.getViewportHeight() +  Tooltip.getPageYOffset() + 20;
			
			//Element.hide('tipleft');
			//Element.hide('tipbleft');
			//Element.hide('tipright');
			//Element.hide('tipbright');
			
			$('tipleft').style.visibility = "hidden";
			$('tipbleft').style.visibility = "hidden";
			$('tipright').style.visibility = "hidden";
			$('tipbright').style.visibility = "hidden";
			
			//$('ttitle').innerHTML = Tooltip._x;
			//$('tsummary').innerHTML = "move called";
		
			if((Tooltip._x + tooltipWidth)>bodyWidth){
				// handle right tip
				$(Tooltip.tooltipID).style.left = (Tooltip._x - 50 - tooltipWidth) +"px";
				//$('tipbright').style.top = (tooltipHeight-42) +"px";
		
				if((Tooltip._y + tooltipHeight)>bodyHeight){
					// show upper right tip
					//Element.show('tipbright');
					$('tipbright').style.visibility = "";
					$(Tooltip.tooltipID).style.top = (Tooltip._y - tooltipHeight + 16)  +"px";
				} else {
					// show lower right tip
					//Element.show('tipright');
					$('tipright').style.visibility = "";
					$(Tooltip.tooltipID).style.top = (Tooltip._y - 16) +"px";
				}
				
			} else {
				// handle left tip
				$(Tooltip.tooltipID).style.left = (Tooltip._x + 50) +"px";
				//$('tipbleft').style.top = (tooltipHeight-42) +"px";
				if((Tooltip._y + tooltipHeight)>bodyHeight){
					// show upper left tip
					//Element.show('tipbleft');
					$('tipbleft').style.visibility = "";
					$(Tooltip.tooltipID).style.top = (Tooltip._y - tooltipHeight + 16)  +"px";
				} else {
					// show lower left tip
					//Element.hide('tipbleft');
					//Element.show('tipleft');
					$('tipleft').style.visibility = "";
					$(Tooltip.tooltipID).style.top = (Tooltip._y - 16) +"px";
				}
			}
		},
		
		getDocumentWidth:function() { // missing from prototype?
			var scrollWidth = (Tooltip.browserMode != "CSS1Compat") ? document.body.scrollWidth : document.documentElement.scrollWidth;
			return Math.max(scrollWidth, this.getViewportWidth());
		},

    getDocumentHeight:function() { // missing from prototype?
			var scrollHeight = (browserMode != "CSS1Compat") ? document.body.scrollHeight : document.documentElement.scrollHeight;
			return Math.max(scrollHeight, this.getViewportHeight());
 	  },

		getPageXOffset:function() { // missing from prototype?
			var pageXOffset = 0;
			
			
			if ( (Tooltip.browserMode || Tooltip.isIE) && !Tooltip.isOpera ) {
				switch (Tooltip.browserMode) { // (IE, Gecko)
					 case 'CSS1Compat': // Standards mode
							pageXOffset = document.documentElement.scrollLeft;
							break;
			
					 default: // Quirks
							pageXOffset = document.body.scrollLeft;
				}
			} else { // Safari, Opera
				pageXOffset = self.pageXOffset;
			}
			
			return pageXOffset;
		},

		getPageYOffset:function() { // missing from prototype?
			var pageYOffset = 0;
			
			
			if ( (Tooltip.browserMode || Tooltip.isIE) && !Tooltip.isOpera ) {
				switch (Tooltip.browserMode) { // (IE, Gecko)
					 case 'CSS1Compat': // Standards mode
							pageYOffset = document.documentElement.scrollTop;
							break;
			
					 default: // Quirks
							pageYOffset = document.body.scrollTop;
				}
			} else { // Safari, Opera
				pageYOffset = self.pageYOffset;
			}
			
			return pageYOffset;
		},

/**
 * Returns the current height of the viewport.
 * @return {Int} The height of the viewable area of the page (excludes scrollbars).
 */
		getViewportHeight:function(){
			var height = -1;
			
			
			if ( (Tooltip.browserMode || Tooltip.isIE) && !Tooltip.isOpera ) {
				switch (Tooltip.browserMode) { // (IE, Gecko)
					 case 'CSS1Compat': // Standards mode
							height = document.documentElement.clientHeight;
							break;
			
					 default: // Quirks
							height = document.body.clientHeight;
				}
			} else { // Safari, Opera
				height = self.innerHeight;
			}
			
			return height;
		},

/**
 * Returns the current width of the viewport.
 * @return {Int} The width of the viewable area of the page (excludes scrollbars).
 */

		getViewportWidth:function(){
			var width = -1;
			
			if (Tooltip.browserMode || Tooltip.isIE) { // (IE, Gecko, Opera)
				switch (Tooltip.browserMode) {
					case 'CSS1Compat': // Standards mode 
						 width = document.documentElement.clientWidth;
						 break;
						 
					default: // Quirks
						 width = document.body.clientWidth;
				}
			} else { // Safari
				width = self.innerWidth;
			}
			return width;
		}
		
}

function tooltipLoader() {
	Tooltip.init();
}