var currentTip = null;
var tooljaxTerms = Class.create();

tooljaxTerms.prototype = {
	initialize: function() {
		this.term = new Array();
		this.termCount = 0;
	}
}

// Preload the popper images...
var icntr = 0;

images = new Array(
		"/rms/images/pop_top.gif",
		"/rms/images/pop_mid.gif",
		"/rms/images/pop_bot.gif"
	);

imagePreload = new Image();
for( icntr = 0; icntr < images.length; icntr++ ) {
	imagePreload.src = images[icntr];
}


var tooljax = Class.create();
tooljax.prototype = {
	timeout: null,	// for the timer delay
	initialize: function( element, url ) {
		var options = Object.extend({
			delta_x: -37, // Baseline adjustment
			delta_y: 12,
			relative_delta: 1,
			zindex: 1000
		    }, arguments[2] || {});
		this.term		  = '';
		this.definition	  = '';

		// element_name is the root for building the href and gloss div ids
		this.element_name = element;
		this.options      = options;
	
		// If we get a url, we should use it as an AJAX callback
		// If we don't, it's the master-div of a pre-existing
		// element to show/hide/whatever.
		var foo = new String( url );
		if( foo.indexOf( '/' ) != -1 ) {
			// We wait for an activation before building AJAX poppers
			this.url = foo;
			this.div_element  = 'gloss_term_' + this.element_name;
			this.href_element = 'href_term_' + this.element_name;
			this.element      = $(this.href_element);
			this.options.delta_x = -37; // Baseline adjustment
			this.options.delta_y = 12;
		} else if( $(url) ) {
//alert('Hello');
//alert(url);
			this.url = url;
			this.div_element = url;
			this.href_element = element;
			this.options.relative_delta = 0;
			this.tool_tip = $(url);
			this.element  = $(element);
			this.options.delta_x = 0; // Baseline adjustment
			this.options.delta_y = 22;
			
			// If we have the DIV, we build the tooltip now
			this.buildTool();
		} else {
			// uh-oh
		}
//alert('Hello');

		this.elEventMouseOut	= this.hideTool.bindAsEventListener( this, 0 );
		this.elEventMouseOver	= this.showTool.bindAsEventListener( this, 1500 );
		this.elEventMouseClick	= this.showTool.bindAsEventListener( this, 0 );

		this.element.observe( "click", this.elEventMouseClick );
		this.element.observe( "mouseover", this.elEventMouseOver );
		this.element.observe( "mouseout", this.elEventMouseOut );
	},

	showTool: function( event, delay ) {
		event.stop( );
		
		// This catches AJAX tooltips
		if( !this.tool_tip ) {
			this.buildTool();
		}

		this.clearTimer();
		if( currentTip && currentTip.element == this.element ) {
			return;
		}
		if( currentTip && currentTip.element != this.element ) {
			currentTip.hideToolReal();
			currentTip = null;
		}

		this.timer = setTimeout( '$("' + this.element.id + '").tool.showToolReal()', delay );
	},
	
	showToolReal: function() {
// Seems the 'currentTip' bit here is key in breaking the ZipSearch
		if( currentTip && currentTip.element != this.element ) {
			currentTip.hideToolReal();
		}

		currentTip = this;

		this.element.stopObserving( "mouseout", this.elEventMouseOut );
		this.element.stopObserving( "mouseout", this.elEventMouseOver );
		this.tool_tip.observe( "mouseout", this.ttEventMouseOut );
		this.tool_tip.observe( "blur", this.onBlurHandler );

		var xy_page = Position.page( this.element );
		var xy_offs = Position.realOffset( this.element );
		var x = xy_page[0] + xy_offs[0];
		var y = xy_page[1] + xy_offs[1];
	
		// Center tooltip under selected element
		var elem_width = this.element.getWidth();
		
		var local_delta_x = this.options.delta_x;
		var local_delta_y = this.options.delta_y;
		
		if( this.options.relative_delta ) {
			local_delta_x += Math.round(elem_width / 2);
		}

		// IE needs an extra pixel, X & Y, to align properly...
		// Moz on Win just needs the y adjustment.
		if( navigator.userAgent.indexOf( 'Windows' ) != -1 ) {
			local_delta_y++;
			if( navigator.userAgent.indexOf( 'MSIE' ) != -1 ) {
				local_delta_x++;
			}
		}

		this.tool_tip.setStyle( { cssFloat: 'left', position:'absolute', top:y + local_delta_y + "px", left:x + local_delta_x + "px", zindex:this.options.zindex } );

		this.element.addClassName('active');
	//	this.tool_tip.observe( 'onmouseout', function(event){  $(this.element.id).tool.hideTool(2000); });

		this.timer = null;
		this.tool_tip.show();
	},

	
	hideTool: function( event, delay ) {
		Event.stop( event );
		if( !currentTip || ( currentTip && currentTip.element == this.element )) {
			this.clearTimer();
			this.timer = setTimeout( '$("' + this.element.id + '").tool.hideToolReal()', delay );
		}
	},
	
	hideToolReal: function( ) {
		this.clearTimer();

		this.tool_tip.stopObserving( "click", this.ttEventMouseClick );
		this.tool_tip.stopObserving( "mouseout", this.ttEventMouseOut );

		this.element.observe( "mouseout", this.elEventMouseOut );
		this.element.observe( "mouseover", this.elEventMouseOver );

		this.element.removeClassName('active');
		this.tool_tip.hide();
		currentTip = null;
	},
	
	buildTool: function () {
		var tTerms = new tooljaxTerms();

		// We need to verify this.url is a url
		// If it isn't, we'll assume it's a DIV we
		// already have and need to display.
		if( this.url.indexOf( '/' ) != -1 ) {
			var glossy = new Ajax.Request( this.url, {
					asynchronous: false,
					onSuccess: function( transport, json ) {
							tTerms.term[0] = json[0];
						}
				}
			);

			this.tool_tip = new Element('div'
				, { 'class' : 'glossary-popup' });
				
			var term_arrow_div = new Element( 'div'
				, { 'class' : 'glossary-arrow' });
			this.tool_tip.appendChild(term_arrow_div);

			var term_content_div = new Element( 'div' 
				, { 'class' : 'glossary-content'} );
	
			var term_title_div = new Element( 'div'
				, { 'class' : 'glossary-term'} );
			this.term = tTerms.term[0].term;
			term_title_div.innerHTML = this.term;

			term_content_div.appendChild(term_title_div);
			//
			// does this button do anything?
			var term_close_button = new Element( 'div' 
				, { 'class' : 'glossary-close-button'} );
	
			term_content_div.appendChild(term_close_button);

			var term_def_div = new Element( 'div' 
				, { 'class' : 'glossary-definition'} );
			this.definition = tTerms.term[0].definition;
			term_def_div.innerHTML = this.definition;
			
			term_content_div.appendChild(term_def_div);

			this.tool_tip.appendChild(term_content_div);

			var term_closure_div = new Element( 'div'
				, { 'class' : 'glossary-closure'} );
			this.tool_tip.appendChild(term_closure_div);

			// make it live
			document.body.appendChild( this.tool_tip );
		}

		this.tool_tip.hide();
		this.tool_tip.id = this.div_element;

		this.ttEventMouseClick	= this.hideTool.bindAsEventListener( this, 0 );
		this.ttEventMouseOut	= this.hideTool.bindAsEventListener( this, 500 );
		this.onBlurHandler	= this.hideTool.bindAsEventListener( this, 0 );
		this.ttEventMouseOver	= this.showTool.bindAsEventListener( this, 0 );	
		
		this.tool_tip.observe( "mouseover", this.ttEventMouseOver );
		this.tool_tip.observe( "blur", this.onBlurHandler );

//alert('start1');
		var kids = new Array();
		kids = this.tool_tip.descendants();
		kids.each(function(node) {
//alert( node.innerHTML);
			node.observe("mouseover", this.ttEventMouseOver);
		});
// the bug is 2 lines UP
		
		var parents = new Array();
		var ttEventMouseOut = this.ttEventMouseOut;
		parents = this.tool_tip.ancestors();
		parents.each(function(node) {
			// this is needed so if popup is up, then cursor
			// leaves the browser, the box still disappears.
			// enabling it just seems to cause errors
			node.observe( "mouseover", ttEventMouseOut );
		});
//alert('done1');
	},
	
	clearTimer: function() {
		if( this.timer ) {
			clearTimeout( this.timer );
			this.timer = null;
		}
	},
	
	// unused 10Jun09
	//setTimer: function( code, timeout ) {
		////alert( 'setTimer():\n' + timeout + '\n\n' + code );
		//if( this.timer ) {
			//clearTimer();
		//}
		//this.timer = setTimeout( code, alert );
	//}
}

