/**********************************************
* dWP's fast and simple js tooltips
* must have already loaded prototype, and common.js
***********************************************/

var ToolTip = {

	/* Member Variables */
	isIE6 : (navigator.appVersion.indexOf('MSIE 6.0') > -1) ? true : false,
	isIFrameLoaded : false,

	/* Member functions */
	setup : function() 
	{
		/* Creates an global iframe when loading for use with each popup */
		var iframe = this.iframe = document.createElement('iframe');

		//- Setup iframe
		iframe.src 			= '/images/common/trans.gif';
		iframe.className	= 'popup_iframe';
	
		//- Body not loaded yet
		//document.body.appendChild(iframe);
		Element.hide(iframe);
		iframe.style.top  = '-900px';  /* ie workaround */
		iframe.style.left = '-900px';  /* ie workaround */
		return { iframe : iframe };
	},

	skins : { 
				'default' : 
				{
					TT_shadow       : 'TT_shadow',
					TT_shadow_IE6   : 'TT_shadow_IE6',
					TT_container    : 'TT_container',
					TT_title        : 'TT_title',
					TT_caption      : 'TT_caption'
				},
				'frog' : 
				{
					TT_shadow       : 'TT_shadow',
					TT_shadow_IE6   : 'TT_shadow_IE6',
					TT_container    : 'TT_container',
					TT_title        : 'TT_frog_title',
					TT_caption      : 'TT_frog_caption'
				},
				'golden' : 
				{
					TT_shadow       : 'TT_shadow',
					TT_shadow_IE6   : 'TT_shadow_IE6',
					TT_container    : 'TT_container',
					TT_title        : 'TT_golden_title',
					TT_caption      : 'TT_golden_caption'
				},
				'navy' :
				{
					TT_shadow       : 'TT_shadow',
					TT_shadow_IE6   : 'TT_shadow_IE6',
					TT_container    : 'TT_container',
					TT_title        : 'TT_navy_title',
					TT_caption      : 'TT_navy_caption'
				},
				'help_icon' :
				{
					TT_shadow       : 'TT_shadow',
					TT_shadow_IE6   : 'TT_shadow_IE6',
					TT_container    : 'TT_container',
					TT_title        : 'TT_help_icon_title',
					TT_caption      : 'TT_help_icon_caption'
				},
				'red' :
				{
					TT_shadow       : 'TT_shadow',
					TT_shadow_IE6   : 'TT_shadow_IE6',
					TT_container    : 'TT_container',
					TT_title        : 'TT_red_title',
					TT_caption      : 'TT_red_caption'
				},
				'metal' :
				{
					TT_shadow       : 'TT_shadow',
					TT_container    : 'TT_container',
					TT_title        : 'TT_metal_title',
					TT_caption      : 'TT_metal_caption'
				}
			},

	addSkin : function(name,options)
	{
		this.skins[name] = options;
	},

	add : function(element,caption,options)
	{
		//dump("ToolTip.add:" + element);
		//- set option defaults
		options = Object.extend(
		{
			title			: false,
			caption			: 'Click For Help',
			offset_x		: 20,
			offset_y		: 15,
			skin			: 'default',
			delay			: 250,
			width			: null,
			show			: true
		},
		options || {});

		if(!this.isIFrameLoaded)
		{
			document.body.appendChild(this.iframe);
			this.isIFrameLoaded = true;
		}

		if(caption)
			options.caption = caption;

		//- Render the HTML to the document
		var elements = this.render(options);
		var div = elements.div;

		//- create some function closures
		var ToolTip = this;
		var moveFunc = function(event){ ToolTip.move(event, div, options); };
		var showFunc = function(event){ ToolTip.show(event, div, elements.inner, options); };
		var hideFunc = function(event){ ToolTip.hide(event, div, options); };

		//- Clear out current mouseover functions (like this one)
		var empty_func = function(){};
		element.onmousemove	= empty_func;
		element.onmouseover	= empty_func;
		element.onmouseout  = empty_func;

		//- Regsiter these events (memory leak avoidance, unobserving, etc)
		Event.observe(element,'mousemove',moveFunc);
		Event.observe(element,'mouseover',showFunc);
		Event.observe(element,'mouseout',hideFunc);

		//- Run the show
		if(options.show)
			this.show(null,div,elements.inner, options);

		//- Pass back useful bits of the mouseover
		return { 
			moveFunc	: moveFunc, 
			showFunc	: showFunc,
			hideFunc	: hideFunc,
			div		 	: div
		};
	},

	render : function(options)
	{
		var css = this.skins[options.skin];
		if(!css)
			alert('You selected an undefined skin: ' + options.skin);

		if (this.isIE6) {
			var div 	= createElement('div',css.TT_shadow_IE6,document.body);
		} else {
			var div 	= createElement('div',css.TT_shadow,document.body);
		}

		var inner 	= createElement('div',css.TT_container,div);
		var title 	= createElement('div',css.TT_title,inner);
		if(options.title)
			title.innerHTML = '&nbsp;' + options.title;
		var caption = createElement('div',css.TT_caption,title);

		//- restrict the width
		if(options.width != null)
		{
			inner.style.width = options.width;
 		}
		else
		{
			if (this.isIE6) {
				inner.style.width = '200px';
			}
		}

		//- If there is a caption element attach, else just put in the text
		if(typeof options.caption == 'object')
		{
			dump("Attaching object to caption: " + options.caption);
			caption.appendChild(options.caption);
		}
		else
		{
			caption.innerHTML = options.caption;
		}
		Element.hide(div);
		div.style.top     = '-900px';  /* ie workaround */
		div.style.left 	  = '-900px';  /* ie workaround */
		return { div : div, inner : inner, title: title, caption : caption};
	},

	move : function(event,div,options)
	{
		//- Get the position and dimensions
		var x = Event.pointerX(event);
		var y = Event.pointerY(event);
		var divDim = Element.getDimensions(div);
		var winDim = getWindowDimensions();
		var offset_x = options.offset_x;
		var offset_y = options.offset_y;
	
		var scrollX = this.getScrollOffsetX();
		var scrollY = this.getScrollOffsetY();

		//TODO: Check for visible scroll bars
//		dump("x:" + x + ",divDim.width:" + divDim.width + ",offset_x:" + offset_x);

		//- Flop right (default) || if there is more room right
		var xWidth = (x + divDim.width + offset_x - scrollX);
		if( xWidth < winDim.width || winDim.width - x > x)
		{
			x = x + offset_x;
		}
		//- Flop left
		else 
		{
			x = x - divDim.width;
		}

		//- Flop down (default)
		if( (y + divDim.height + offset_y - scrollY) < winDim.height)
		{
			y = y + offset_y;
		}
		//- Flop up (default)
		else
		{
			y = y - divDim.height;
		}
		div.style.left = x + 'px';
		div.style.top = y + 'px';
		this.iframe.style.left = x + 'px';
		this.iframe.style.top = y + 'px';
		return;
	},

	show : function(ev,div,inner,options)
	{
		if (this.isIE6)
		{
			this.iframe.style.width = inner.clientWidth;
			this.iframe.style.height = inner.clientHeight;
		}
		else
		{
			this.iframe.style.width = '0px';
			this.iframe.style.height = '0px';
		}
		Element.show(div);
		Element.show(this.iframe);
//		this.tID = setTimeout(function() { Element.show(div); }, options.delay);
	},


	hide : function(ev,div)
	{
		clearTimeout(this.tID);
		Element.hide(div);
		Element.hide(this.iframe);
		div.style.top 	= '-900px';  /* ie workaround */
		div.style.left 	= '-900px'; /* ie workaround */
		this.iframe.style.top 	= '-900px';  /* ie workaround */
		this.iframe.style.left 	= '-900px'; /* ie workaround */
	},

	/*------( Helper Functions )---------------------------*/
	getScrollOffsetX : function()
	{
		return	window.pageXOffset
                || document.documentElement.scrollLeft
                || document.body.scrollLeft
                || 0;
	},

	getScrollOffsetY : function()
	{
    	return window.pageYOffset
                || document.documentElement.scrollTop
                || document.body.scrollTop
                || 0;
	}
};
ToolTip.setup();
