function dim()
{
	if ($('#dim_div').html() == null) //don't add another one if one exists!
	{
		$('<div id="dim_div"></div>').appendTo(document.body)
			.addClass('ui-widget-overlay').css(
				{
					width: $(document).width(),
					height: $(document).height()
				}
			);
	}
}

function unDim()
{
	if ($('#dim_div').html() != null) //only remove if it's there, to avoid errors
	{
		$('#dim_div').remove();
	}
}

/**
 * Used instead of JavaScript's alert() function.
 * ok_button is optional, so pass null for it if you are using a callback.
 */
function alertDialog(title, message, ok_button, callback)
{
	if(typeof $().dialog == 'undefined') {
		setTimeout("alert('" + message + "')", 1500);
	}
	else {
		$(document.body).append('<div id="alert_dialog" class="dialog" title="' + title + '"><p>' + message + '</p></div>');
		if (ok_button == null)
		{
			ok_button = 'OK';
		}
		var buttons = new Object();
		buttons[ok_button] = function()
		{
			$('#alert_dialog').remove();
			if (callback != null)
			{
				callback();
			}
		};
		$('#alert_dialog').dialog(
			{
				modal: true,
				buttons: buttons
			}
		);
	}
}

/**
 * Used instead of JavaScript's confirm() function.
 * ok_button and cancel_button are optional, so pass null for them if you are using a callback.
 */
function booleanDialog(title, message, ok_button, cancel_button, callback)
{
	$(document.body).append('<div id="boolean_dialog" class="dialog" title="' + title + '"><p>' + message + '</p></div>');
	if (ok_button == null)
	{
		ok_button = 'OK';
	}
	if (cancel_button == null)
	{
		cancel_button = 'Cancel';
	}
	var buttons = new Object();
	buttons[cancel_button] = function()
	{
		//$(this).dialog('close');
		$('#boolean_dialog').remove();
		if (callback != null)
		{
			callback(false);
		}
		else
		{
			return false;
		}
	};
	buttons[ok_button] = function()
	{
		//$(this).dialog('close');
		$('#boolean_dialog').remove();
		if (callback != null)
		{
			callback(true);
		}
		else
		{
			return true;
		}
	};
	$('#boolean_dialog').dialog(
		{
			modal: true,
			buttons: buttons
		}
	);
}

var compact_text_open = new Object();//tracks which ones are open
$.fn.compactText = function(max_chars, position)
{
	var full_content = $(this).html();
	if (full_content != null)
	{
		var this_id = $(this).attr('id');
		compact_text_open[this_id] = false;
		var off_text = 'View More&#9658;';
		var on_text = 'Close&#9660;';
		//trim leading whitespace
		full_content = full_content.replace(new RegExp(/^\s+|\s+$/g),'');
		//only apply tooltip if content exceeds max length
		if (full_content.length > max_chars)
		{
			var partial_content = full_content.substr(0, max_chars);
			partial_content += '...<br /><span class="tooltip_entry tooltip_entry_' + this_id + '"></span>';
			//translate position parameter into qtip's terms
			position = getQtipPostion(position);
			//replace full content with partial+link
			$(this).html(partial_content);
			$('.tooltip_entry_' + this_id).html(off_text);
			$('.tooltip_entry_' + this_id).qtip(
				{
					content: full_content,
					show:
					{
						when:
						{
							event: 'click'
						},
						effect:
						{
							type: 'fade',
							length: 750
						}
					},
					hide: 
					{
						when: 
						{
							event: 'click'
						},
						effect:
						{
							type: 'fade',
							length: 750
						}
					},
					position: position
				}
			).click(
				function()
				{
					//toggle text to open/close
					if (compact_text_open[this_id])
					{
						compact_text_open[this_id] = false;
						$('.tooltip_entry_' + this_id).html(on_text);
					}
					else
					{
						compact_text_open[this_id] = true;
						$('.tooltip_entry_' + this_id).html(off_text);
					}
				}
			);		
		}
	}
}

function getQtipPostion(position)
{
	var position_obj = new Object();
	position_obj.corner = new Object();
	switch (position)
	{
		case 'top_left':
			position_obj.corner.target = 'topLeft';
			position_obj.corner.tooltip = 'bottomRight';
			break;
		case 'top':
			position_obj.corner.target = 'topMiddle';
			position_obj.corner.tooltip = 'bottomMiddle';
			break;
		case 'top_right':
			position_obj.corner.target = 'topRight';
			position_obj.corner.tooltip = 'bottomLeft';
			break;	
		case 'right':
			position_obj.corner.target = 'topRight';
			position_obj.corner.tooltip = 'topLeft';
			break;
		case 'bottom_right':
			position_obj.corner.target = 'bottomRight';
			position_obj.corner.tooltip = 'topLeft';
			break;
		case 'bottom':
			position_obj.corner.target = 'bottomMiddle';
			position_obj.corner.tooltip = 'topMiddle';
			break;
		case 'bottom_left':
			position_obj.corner.target = 'bottomLeft';
			position_obj.corner.tooltip = 'topRight';
			break;
		case 'left':
			position_obj.corner.target = 'topLeft';
			position_obj.corner.tooltip = 'topRight';
			break;
		default:
			position_obj.corner.target = 'topRight';
			position_obj.corner.tooltip = 'bottomLeft';
	}
	return position_obj;
}

function setAutoTab(element_id, max_chars, next_field_id)
{
	var element = $('#' + element_id);
	element.keyup(
		function(event_object)
		{
			if (next_field_id != null)
			{
				if (this.value.length == max_chars)
				{
					$('#' + next_field_id).focus();
				}
			}
		}
	);
}


//Extend the jquery to see if the selector exists
//USAGE: $("#selector").exists();
//Returns true If an element exists by the given selector
jQuery.fn.exists = function(){return jQuery(this).length>0;}
