/* Custom jQuery plugins */
(function($) {
	var tooltip_created = false;
	var tooltip_showing = false;
	var tooltip_x_offset = 0;
	var tooltip_y_offset = 0;

	$.fn.extend({
		/* Tooltip plugin for jQuery */
		tooltip: function(settings) {
			var default_settings = {
				title:      '',
				tip:        'This is a tip!',
				opacity:    .80,
				style:      'default',
				track:      false,
				x_offset:   20,
				y_offset:   0
			};
			settings = $.extend({}, default_settings, settings);
			return this.each(function() {
				this.settings = settings;
				//if(!tooltip_created) { create_tooltip(); }
			})
			.hover(show_tooltip, hide_tooltip);
		},
		disable: function(type) {
			return this.each(function() {
				// If it is a form input
				if(this.type)
				{
					if(typeof type == 'string' && type == 'readonly')
					{
						this.readOnly = true;
					}
					else
					{
						this.disabled = true;
					}
				}
				// If it is a form
				else
				{
					var elements = this.getElementsByTagName('*');
					for(key in elements)
					{
						if(typeof elements[key] != 'undefined' && elements[key].type)
						{
							if(typeof type == 'string' && type == 'readonly')
							{
								elements[key].readOnly = true;
							}
							else
							{
								elements[key].disabled = true;
							}
						}
					}
				}
			});
		},
		enable: function(type) {
			return this.each(function() {
				// If it is a form input
				if(this.type)
				{
					if(typeof type == 'string' && type == 'readonly')
					{
						this.readOnly = false;
					}
					else
					{
						this.disabled = false;
					}
				}
				// If it is a form
				else
				{
					var elements = this.getElementsByTagName('*');
					for(key in elements)
					{
						if(typeof elements[key] != 'undefined' && elements[key].type)
						{
							if(typeof type == 'string' && type == 'readonly')
							{
								elements[key].readOnly = false;
							}
							else
							{
								elements[key].disabled = false;
							}
						}
					}
				}
			});
		}
	});

	/* Tooltip helper functions */
	function create_tooltip() {
		$(document.body).append('<div id="tooltip_container" style="display: none; position: absolute;"><div id="tooltip_title"></div><div id="tooltip_tip"></div></div>');
		tooltip_created = true;
	}
	function show_tooltip() {
		if(!tooltip_created) { create_tooltip(); }

		// Remember this for tracking
		tooltip_x_offset = this.settings.x_offset;
		tooltip_y_offset = this.settings.y_offset;
		
		// Start tracking if we need to
		if(this.settings.track)
		{
			$(this).bind('mousemove', move_tooltip);
		}

		var x = 0;
		var y = 0;
		// Get the starting position for the tooltip
		if(this.settings.track)
		{
			x = mX;
			y = mY;
		}
		else
		{
			// Get the position of the element
			data = find_pos(this);
			// Add the height of the source
			x = data.x;
			y = data.y;
		}
		// Move tooltip to the right a bit
		x += tooltip_x_offset;
		y += tooltip_y_offset;
		

		$('#tooltip_container').css('left', x);
		$('#tooltip_container').css('top', y);

		$('#tooltip_title').html(this.settings.title);
		$('#tooltip_tip').html(this.settings.tip);

		$('#tooltip_container').show();

		// make sure the height isn't not more than the viewable space
//		if($('#tooltip_container').width() > this.settings.max_width)
//		{
//			$('#tooltip_container').width(this.settings.max_width);
//		}
	}
	function hide_tooltip() {
		$(this).unbind('mousemove', move_tooltip);
		$('#tooltip_container').hide();
	}
	function move_tooltip(e)
	{
		// Calculate top and left offset for the prompt
		var arrayPageSize = getPageSize();

		var x = mX+tooltip_x_offset;
		var y = mY+tooltip_y_offset;
		
		if(x+$('#tooltip_container').width() > arrayPageSize[0])
		{
			x = mX-tooltip_x_offset-$('#tooltip_container').width();
		}
		
		$('#tooltip_container').css('left', x);
		$('#tooltip_container').css('top', y);
	}
	/* End Tooltip helper functions */

})(jQuery);