DEBUG = false;

/* 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() {
			return this.each(function() {
				// If it is a form input
				if(this.type)
				{
					this.disabled = true;
				}
				// If it is a form
				else
				{
					var elements = this.getElementsByTagName('*');
					for(key in elements)
					{
						if(elements[key].type)
						{
							elements[key].disabled = true;
						}
					}
				}
			});
		},
		enable: function() {
			return this.each(function() {
				// If it is a form input
				if(this.type)
				{
					this.disabled = false;
				}
				// If it is a form
				else
				{
					var elements = this.getElementsByTagName('*');
					for(key in elements)
					{
						if(elements[key].type)
						{
							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_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_tip').html(this.settings.tip);

		$('#tooltip_container').show();
	}
	function hide_tooltip() {
		$(this).unbind('mousemove', move_tooltip);
		$('#tooltip_container').hide();
	}
	function move_tooltip(e)
	{
		var x = mX+tooltip_x_offset;
		var y = mY+tooltip_y_offset;
		$('#tooltip_container').css('left', x);
		$('#tooltip_container').css('top', y);
	}
	/* End Tooltip helper functions */
})(jQuery);

function update_status(status)
{
	var pars = 'serv=home&page=user&action=update_status&status='+status;
	ajax_request(pars, {
		success: function(data) {
			var status = data.status;
			var status_class = data.status_class;

			$('#user_status').html(status);
			document.getElementById('user_status').className='status_'+status_class;
			my_menu.close();
		}
	});
}
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false;
var IEVersion = 0.0;

if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) {
  i || (i = 0);
  var length = this.length;
  if (i < 0) i = length + i;
  for (; i < length; i++)
    if (this[i] === item) return i;
  return -1;
};

if (!Array.prototype.map)
{
  Array.prototype.map = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        res[i] = fun.call(thisp, this[i], i, this);
    }

    return res;
  };
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize() {

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

/* Used in Form.class.php */
// Keep track of mouse x & y for clicking on text
var orig_x = 0;
var orig_y = 0;
// mX and mY come from general.js (mouse coords are recorded constantly in mX and mY)
function track_mouse()
{
	orig_x = mX;
	orig_y = mY;
}

function mouse_was_dragged()
{
	if(orig_x == mX && orig_y == mY)
	{
		return false;
	}
	return true;
}
/* END */

function addslashes(str)
{
	str=str.replace(/'/g, "\\'");
	return str;
}

if(IE) {
	var temp = navigator.appVersion.split("MSIE");
	IEVersion = parseFloat(temp[1]);
}

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);

// Set-up to use get_mouse_pos function onmousemove
document.onmousemove = get_mouse_pos;

// Temporary variables to hold mouse x-y pos.s
var mX = 0;
var mY = 0;

// Main function to retrieve mouse x-y pos.s
function get_mouse_pos(e) {
  if(IE) { // grab the x-y pos.s if browser is IE
  	var scrollLeft = 0;
  	var scrollTop = 0;
  
	if(document.documentElement)
	{
		scrollLeft = document.documentElement.scrollLeft;
		scrollTop  = document.documentElement.scrollTop;
	}
  	else if(document.body)
	{
		scrollLeft = document.body.scrollLeft;
		scrollTop  = document.body.scrollTop;
	}
  
    mX = event.clientX + scrollLeft;
    mY = event.clientY + scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    mX = e.pageX;
    mY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (mX < 0){mX = 0}
  if (mY < 0){mY = 0}  
  return true;
}

// function: find_pos
function find_pos(obj)
{
	if(typeof obj == "undefined" || obj == null)
	{
		return {"x":0,"y":0};
	}
	var curleft = curtop = 0;
	if(obj.offsetParent)
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while(obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {"x":curleft,"y":curtop};
}

/* General JS Functions: tick for textarea */
function set_chars_left(name, max_len)
{
	var len = $('#'+name+'_input').val().length;
	if(len > max_len)
	{
		$('#'+name+'_input').val($('#'+name+'_input').val().substring(0, max_len));
	}
	$('#'+name+'_char_lim').html(max_len-len);
}

/* Set Cookie */
function set_cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

/* Get Cookie */
function get_cookie( name ) {

var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

/* GENERAL CHECKS */
function is_empty(value)
{
	var tmp_val = value;
	tmp_val = tmp_val.replace(/ /ig,"");
	if(tmp_val == '' || tmp_val == null)
	{
		return true;
	}
	return false;
}
function is_len_gt(value, len)
{
	if(value.length > len)
	{
		return true;
	}
	return false;
}
function is_len_gte(value, len)
{
	if(value.length >= len)
	{
		return true;
	}
	return false;
}
function is_len_eq(value, len)
{
	if(value.length == len)
	{
		return true;
	}
	return false;
}
function is_len_lt(value, len)
{
	if(value.length < len)
	{
		return true;
	}
	return false;
}
function is_len_lte(value, len)
{
	if(value.length <= len)
	{
		return true;
	}
	return false;
}
function is_valid_email(value)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(filter.test(value))
	{
		return true;
	}
	return false;
}
function is_valid_domain(value)
{
	var filter = /^(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(filter.test(value))
	{
		return true;
	}
	return false;
}
function is_valid_username(value)
{
	var filter = /[^a-zA-Z0-9\-]/;
	if(filter.test(value))
	{
		return false;
	}
	return true;
}
function is_valid_date(field, value)
{
	value = value.replace(/\-/g,'/');
	$('#'+field).val(value);

	// Are there any invalid characters?
	var filter = /[^0-9\/]/;
	if(filter.test(value))
	{
		return false;
	}

	var date = value.split('/');
	if(date.length != 3)
	{
		return false;
	}

	if(date[2] > 10 && date[2] < 100)
	{
		date[2] = parseInt(date[2]) + 1900;
		$('#'+field).val(date[0] + '/' + date[1] + '/' + date[2]);
	}

	var filter  = /^[0-9]{1,2}$/;
	var filter2 = /^[0-9]{4}$/;
	
	if(filter.test(date[0]) && filter.test(date[1]) && filter2.test(date[2]))
	{
		return true;
	}
	return false;
}
function contains_only(value, regex)
{
	var filter = new RegExp("[^" + regex + "]");
	
	if(filter.test(value))
	{
		return false;
	}
	return true;
}
function make_lowercase(field, value)
{
	$('#'+field).val(value.toLowerCase());
	return true;
}

/* menu */
function menu_obj() {
	this.is_created = false;
	this.is_open = false;
	this.default_opacity = .8;
	this.opacity = this.default_opacity;
}
menu_obj.prototype.initialize = function() {
	$(document.body).bind('mousedown', function(e) {
		e = e || window.event;

		var target = (e.target) ? e.target : e.srcElement;

		if(e.which)
		{
			if(e.which == 1) { click_type = 'left'; }
			else if(e.which == 3) { click_type = 'right'; }
		}
		else
		{
			if(e.button == 1) { click_type = 'left'; }
			else if(e.button == 2) { click_type = 'right'; }
		}

		if(click_type == 'right')
		{
			return;
		}

		if(target.className != 'menu_button' && target.parentNode.className != 'menu_button' && target.id != null && target.id.indexOf('menu_container') == -1 && target.parentNode.id != null && target.parentNode.id.indexOf('menu_container') == -1)
		{
			my_menu.close();
		}
	});
};

menu_obj.prototype.open = function(links, settings) {
	if(!this.is_created) this.create(settings.style);

	if(this.is_open)
	{
		this.close();
		return;
	}

	// Clear content
	$('#menu_container').html('');

	// Set class
	if(settings.class_name != null)
	{
		$('#menu_container').attr('class', settings.class_name);
	}
	else
	{
		$('#menu_container').attr('class', 'default_menu');
	}
	// Set opacity
	if(settings.opacity != null)
	{
		this.opacity = settings.opacity;
	}
	else
	{
		this.opacity = this.default_opacity;
	}

	// Create menu links
	for(var key=0;key<links.length;key++)
	{
		if(links[key].is_divider != null && links[key].is_divider == true)
		{
			$('#menu_container').append('<hr class="option_divider" size="1" />');
		}
		else if(links[key].is_title != null && links[key].is_title == true)
		{
			$('#menu_container').append('<div class="title">' + links[key].disp  + '</div>');
		}
		else
		{
			if(links[key].is_disabled == 1)
			{
				$('#menu_container').append('<div class="disabled_option" onclick="' + links[key].onclick + '" onmouseover="this.className = this.className.replace(\'option\', \'option_hover\');" onmouseout="this.className = this.className.replace(\'option_hover\', \'option\');">' + links[key].disp  + '</div>');
			}
			else
			{
				$('#menu_container').append('<div class="option" onclick="' + links[key].onclick + '" onmouseover="this.className = this.className.replace(\'option\', \'option_hover\');" onmouseout="this.className = this.className.replace(\'option_hover\', \'option\');">' + links[key].disp  + '</div>');
			}
		}
	}

	// Set location
	var flush = 'left';
	if(settings.flush != null && settings.flush == 'right')
	{
		flush = 'right';
	}
	if(settings.flush != null && settings.flush == 'mouse')
	{
		flush = 'mouse';
	}

	var data = {x: 0, y: 0};
	if(flush == 'mouse')
	{
		data.x = mX;
		data.y = mY;
	}
	else
	{
		if(settings.source_id != null)
		{
			// Get the offset of the source
			data = find_pos(document.getElementById(settings.source_id));
			// Add the height of the source
			data.y += $('#'+settings.source_id).height();
		}
		else
		{
			data.x = mX;
			data.y  = mY;
		}
	
		if(flush == 'right')
		{
			// Subtract the width of the menu
			data.x -= $('#menu_container').width();
			if(settings.source_id != null)
			{
				// Add the width of the source
				data.x += $('#'+settings.source_id).width();
			}
		}
	}

	var arrayPageSize = getPageSize();
	if(data.x < 5)
	{
		data.x = 5;
	}
	else if(data.x + $('#'+settings.source_id).width() > arrayPageSize[0]-5)
	{
		data.x = arrayPageSize[0] - $('#'+settings.source_id).width()-5;
	}

	$('#menu_container').css('top', data.y);
	$('#menu_container').css('left', data.x);

	
	$('#menu_container').css('opacity', this.opacity);
	$('#menu_container').show();

	this.is_open = true;
};

menu_obj.prototype.close = function() {
	if(!this.is_open) return;
	$('#menu_container').hide();
	this.is_open = false;
};
menu_obj.prototype.create = function() {
	if(this.is_created) return;

	$(document.body).append('<div id="menu_container" style="display: none; z-index: 1000;"></div><!-- end menu container -->');
	this.is_created = true;
};
my_menu = new menu_obj();
my_menu.initialize();
/* end menu */


/* BUBBLE FUNCS */
var is_bubble_created = false;
function post_new_event(type, message, container_id)
{
	if(!message) return;
	
	if(!is_bubble_created) create_bubble();
	
	// Set the bubble message
	set_event_bubble_message(type, message);

	// Handle bubble (appear, disappear, etc.)
	if(typeof container_id == "string" && container_id.indexOf(':') != -1)
	{
		var data = container_id.split(':');
		if(typeof data[1] == "string")
		{
			container_id = data[1];
			if($('#'+container_id).attr('type') == "hidden")
			{
				container_id = data[0];
			}
		}
		else
		{
			container_id = data[0];
		}
	}
	create_event_bubble(message, container_id);
}

function create_bubble()
{
	var bubble_html = '<div id="event_bubble_container" style="width: 200px; display: none;">'+
		''+
		'<!-- top -->'+
		'<div style="overflow: hidden; height: 25px;">'+
			'<div id="tlc_image_container" style="float: left; width: 10px; height: 25px;">';
			if(IE)
			{
				bubble_html += '<div id="tlc_image" style="width: 10px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/event_bubble/tlc.png\', sizingMethod=\'image\');"></div>';
			}
			else
			{
				bubble_html += '<img id="tlc_image" src="/images/event_bubble/tlc.png" />';
			}
			bubble_html += '</div>'+
			'<div id="event_bubble_top_bg" style="float: left; height: 25px; background: url(\'/images/event_bubble/top_bg.png\') bottom repeat-x;">&nbsp;</div>'+
			'<div id="trc_image_container" style="float: left; width: 10px; height: 25px;">';
			if(IE)
			{
				bubble_html += '<div id="trc_image" style="width: 10px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/event_bubble/trc.png\', sizingMethod=\'image\');"></div>';
			}
			else
			{
				bubble_html += '<img id="trc_image" src="images/event_bubble/trc.png" />';
			}
			bubble_html += '</div>'+
			'<div class="spacer">&nbsp;</div>'+
		'</div>'+
		'<!-- end top -->'+
		''+
		'<!-- center container -->'+
		'<div>'+
		''+
			'<!-- left -->'+
			'<div id="event_bubble_left_bg" style="padding-left: 10px; background: url(\'/images/event_bubble/left_bg.png\') left repeat-y;">'+
		''+
				'<!-- right -->'+
				'<div id="event_bubble_right_bg" style="padding-right: 10px; background: url(\'/images/event_bubble/right_bg.png\') right repeat-y;">'+
		''+
					'<!-- message -->'+
					'<div id="event_bubble_message_container">'+
						'<div id="event_message"></div>'+
						'<div style="text-align: right; color: #000000;"><a href="#" onClick="$(\'#event_bubble_container\').hide(); return false;">close</a></div>'+
					'</div>'+
					'<!-- end message -->'+
		''+
				'</div>'+
				'<!-- end right -->'+
		''+
			'</div>'+
			'<!-- end left -->'+
		''+
		'</div>'+
		'<!-- end center container -->'+
		''+
		'<!-- bot -->'+
		'<div style="overflow: hidden; height: 25px;">'+
			'<div id="blc_image_container" style="float: left; width: 10px; height: 25px;">';
			if(IE)
			{
				bubble_html += '<div id="blc_image" style="width: 10px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/event_bubble/blc.png\', sizingMethod=\'image\');"></div>';
			}
			else
			{
				bubble_html += '<img id="blc_image" src="images/event_bubble/blc.png" />';
			}
			bubble_html += '</div>'+
			'<div id="event_bubble_bot_bg" style="float: left; height: 25px; background: url(\'/images/event_bubble/bot_bg.png\') top repeat-x;">&nbsp;</div>'+
			'<div id="brc_image_container" style="float: left; width: 58px; height: 25px;">';
			if(IE)
			{
				bubble_html += '<div id="brc_image" style="width: 58px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/event_bubble/brs.png\', sizingMethod=\'image\');"></div>';
			}
			else
			{
				bubble_html += '<img id="brc_image" src="images/event_bubble/brs.png" />';
			}
			bubble_html += '</div>'+
			'<div class="spacer">&nbsp;</div>'+
		'</div>'+
		'<!-- end bot -->'+
		''+
	'</div>';
	
	$(document.body).append(bubble_html);
	is_bubble_created = true;
}

var bubble_message = '';
var bubble_status = 'closed';
var max_bubble_width = 300;
var min_bubble_width = 50;
// Every time this is called, we'll try to bring down the fader if
// it's not down and set a timer to bring it back up.  However, we
// may lock the fader so that it can't get a down call while going
// up or vice-versa
function create_event_bubble(message, container_id)
{
	// Calculate the width of the bubble
	var bubble_width = message.length * 7;
	if(bubble_width > max_bubble_width)
	{
		var words = message.split(" ");
		var char_count = 0;
		var first = true;
		for(var i = 0; i < words.length; i++)
		{
			var count = words[i].length;
			if(first == true)
			{
				first = false;
			}
			else
			{
				count++;
			}

			// If this word puts the width over the max, stay with char_count as the width
			if((char_count + count)*7 > max_bubble_width)
			{
				break;
			}
			char_count += count;
		}
		bubble_width = char_count*7;
	}
	
	if(bubble_width < min_bubble_width)
	{
		bubble_width = min_bubble_width;
	}

	if(typeof container_id == "undefined" || container_id == "null" || container_id == null || $(container_id) == null)
	{
		container_id = null;
	}
	
	var is_generic = true;
	if(container_id != null && container_id.indexOf('_input') != -1)
	{
		is_generic = false;
	}
	
	// Get container dimensions and position
	if(is_generic)
	{
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		var container_dim = {width: arrayPageSize[0], height: arrayPageSize[1]};
		var container_pos = {x:arrayPageScroll[0], y:arrayPageScroll[1]};
}
	else
	{
		var container_dim = { width: $('#'+container_id).width(), height: $('#'+container_id).height() };
		var container_pos = find_pos(document.getElementById(container_id));
	}

	if(container_dim.width == 0)
	{
		// Default to this so the bubble doesn't go off the screen
		container_dim.width = 800;
	}

	if(is_generic)
	{
		// Calculate the position for the bubble to be in the exact middle of the container
		var bubble_x = container_pos.x + container_dim.width/2 + bubble_width/2;
		var	bubble_y = mY;//container_pos.y + container_dim.height/2;
	}
	else
	{
		// Calculate the position for the bubble to be just above the container to the left
		var bubble_x = container_pos.x-4;
		var	bubble_y = container_pos.y-4;
	}

	// Set the bubble widths
	$('#event_bubble_message_container').width(bubble_width);
	$('#event_bubble_container').width(bubble_width+20);

	// Set the bubble position
	var bubble_dim = { width: $('#event_bubble_container').width(), height: $('#event_bubble_container').height() }
	// Move the bubble left and up equal to its width and height
	bubble_y -= bubble_dim.height;
	bubble_x -= bubble_dim.width;
	// If the bubble y position is negative (off the screen), let's reposition it
	if(!is_generic)
	{
		var speech_y_pos = 'bot';
		var speech_x_pos = 'right';

		// Determine speech position
		if(bubble_y < 0)
			speech_y_pos = 'top';
		else
			speech_y_pos = 'bot';

		if(bubble_x < 0)
			speech_x_pos = 'left';
		else
			speech_x_pos = 'right';

		// Reset widths
		var tlc_width = 10;
		var trc_width = 10;
		var blc_width = 10;
		var brc_width = 10;
		if(speech_y_pos == 'top')
		{
			$('#event_bubble_top_bg').width(bubble_width-48);
			$('#event_bubble_bot_bg').width(bubble_width);

			if(speech_x_pos == 'left')
				tlc_width = 58;
			else
				trc_width = 58;
		}
		else
		{
			$('#event_bubble_top_bg').width(bubble_width);
			$('#event_bubble_bot_bg').width(bubble_width-48);

			if(speech_x_pos == 'left')
				blc_width = 58;
			else
				brc_width = 58;
		}
		$('#tlc_image_container').width(tlc_width);
		$('#trc_image_container').width(trc_width);
		$('#blc_image_container').width(blc_width);
		$('#brc_image_container').width(brc_width);

		// Reposition bubble
		if(speech_y_pos == 'top')
		{
			bubble_y += bubble_dim.height+container_dim.height+2;
			bubble_x += 1;
			// Make the speech on the top
		}
		if(speech_x_pos == 'left')
		{
			bubble_x += bubble_dim.width+container_dim.width+2;
			bubble_y += 1;
		}

		// Set corner images
		if(speech_y_pos == 'top' && speech_x_pos == 'left')
			if(IE)
				$('#tlc_image').css('filter', $('#tlc_image').css('filter').replace('tlc', 'tls'));
			else
				$('#tlc_image').attr('src', $('#tlc_image').attr('src').replace('tlc', 'tls'));
		else
			if(IE)
				$('#tlc_image').css('filter', $('#tlc_image').css('filter').replace('tls', 'tlc'));
			else
				$('#tlc_image').attr('src', $('#tlc_image').attr('src').replace('tls', 'tlc'));

		if(speech_y_pos == 'top' && speech_x_pos == 'right')
			if(IE)
				$('#trc_image').css('filter', $('#trc_image').css('filter').replace('trc', 'trs'));
			else
				$('#trc_image').attr('src', $('#trc_image').attr('src').replace('trc', 'trs'));
		else
			if(IE)
				$('#trc_image').css('filter', $('#trc_image').css('filter').replace('trs', 'trc'));
			else
				$('#trc_image').attr('src', $('#trc_image').attr('src').replace('trs', 'trc'));

		if(speech_y_pos == 'bot' && speech_x_pos == 'left')
			if(IE)
				$('#blc_image').css('filter', $('#blc_image').css('filter').replace('blc', 'bls'));
			else
				$('#blc_image').attr('src', $('#blc_image').attr('src').replace('blc', 'bls'));
		else
			if(IE)
				$('#blc_image').css('filter', $('#blc_image').css('filter').replace('bls', 'blc'));
			else
				$('#blc_image').attr('src', $('#blc_image').attr('src').replace('bls', 'blc'));

		if(speech_y_pos == 'bot' && speech_x_pos == 'right')
			if(IE)
				$('#brc_image').css('filter', $('#brc_image').css('filter').replace('brc', 'brs'));
			else
				$('#brc_image').attr('src', $('#brc_image').attr('src').replace('brc', 'brs'));
		else
			if(IE)
				$('#brc_image').css('filter', $('#brc_image').css('filter').replace('brs', 'brc'));
			else
				$('#brc_image').attr('src', $('#brc_image').attr('src').replace('brs', 'brc'));
	}
	else
	{
		if(bubble_y < 0)
		{
			bubble_y = container_pos.y;
		}

		// Make no speech
		$('#event_bubble_top_bg').css('width', bubble_width);
		$('#event_bubble_bot_bg').css('width', bubble_width);
		$('#trc_image_container').css('width', 10);
		$('#brc_image_container').css('width', 10);
		$('#tlc_image_container').css('width', 10);
		$('#blc_image_container').css('width', 10);
		// Reset corner images
		if(IE)
		{
			$('#trc_image').css('filter', $('#trc_image').css('filter').replace('trs', 'trc'));
			$('#tlc_image').css('filter', $('#tlc_image').css('filter').replace('tls', 'tlc'));
			$('#brc_image').css('filter', $('#brc_image').css('filter').replace('brs', 'brc'));
			$('#blc_image').css('filter', $('#blc_image').css('filter').replace('bls', 'blc'));
		}
		else
		{
			$('#trc_image').attr('src', $('#trc_image').attr('src').replace('trs', 'trc'));
			$('#tlc_image').attr('src', $('#tlc_image').attr('src').replace('tls', 'tlc'));
			$('#brc_image').attr('src', $('#brc_image').attr('src').replace('brs', 'brc'));
			$('#blc_image').attr('src', $('#blc_image').attr('src').replace('bls', 'blc'));
		}
	}
	$('#event_bubble_container').css('top', bubble_y);
	$('#event_bubble_container').css('left', bubble_x);
	
	if(bubble_status == 'closed')
	{
		$('#event_bubble_container').show();
	}

	bubble_shake();
}

function bubble_shake()
{
	$('#event_bubble_container').animate({left:'-=2px'}, 10);
	$('#event_bubble_container').animate({left:'+=4px'}, 10);
	$('#event_bubble_container').animate({left:'-=4px'}, 10);
	$('#event_bubble_container').animate({left:'+=4px'}, 10);
	$('#event_bubble_container').animate({left:'-=4px'}, 10);
}

function set_event_bubble_message(type, message)
{
	if(bubble_message != message)
	{
		$('#event_message').html(message);
		bubble_message = message;
	}
}






function post_new_event2(type, message, container_id)
{
	if(ip == '68.96.77.116' || ip == '66.27.212.60')
	{
		post_new_event2(type, message, container_id);
		return;
	}

	if(IE && IEVersion < 7.0)
	{
		alert(message);
	}
	else
	{
		// Handle the event (insert, swap, fade, update, etc.)
		handle_event(type, message);
	
		// Handle fader (go up, go down, etc.)
		handle_fader();
	}
}

var slide_up_timer = null;
var fader_status = 'up';
// Every time this is called, we'll try to bring down the fader if
// it's not down and set a timer to bring it back up.  However, we
// may lock the fader so that it can't get a down call while going
// up or vice-versa
function handle_fader()
{
	// If handle_fader is locked, then try again in .1 sec
	if(lock_array['handle_fader'])
	{
		setTimeout("handle_fader();", 100);
		return;
	}

	// Reset the slide_up_timer
	clearTimeout(slide_up_timer);

	// Try to slide the fader down
	// Lock the handle_fader function for .5 sec
	if(fader_status == 'up')
	{
		Effect.SlideDown('event_fader', {duration:.5});
		fader_status = 'down';
		setTimeout("lock_func('handle_fader', .5);", 3000);
	}
	
	// Set the event_fader to slide up in 10 sec
	// Once it's sliding up, lock the handle_fader function for .5 sec
	slide_up_timer = setTimeout("Effect.SlideUp('event_fader', {duration:.5}); lock_func('handle_fader', .5); fader_status = 'up';", 10000);
}

var event_msg = '';
var event_exists = false;
var event_fade_timer = null;
var lock_handle_event = false;
function handle_event(type, message)
{
	// If handle_event is locked, then try again in .1 sec
	if(lock_array['handle_event'])
	{
		setTimeout("handle_event('"+type+"', '"+addslashes(message)+"');", 100);
		return;
	}

	// Reset the event_fade_timer
	clearTimeout(event_fade_timer);

	// If we enounter the same message and the event exists, then let's not give it again
	// (spam protection!)
	if(!event_exists || event_msg != message)
	{
		if(type == 'error')
		{
			error_issued = true;
		}
		
		if(event_exists)
		{
			// If there is currently an event, then fade it out
			Effect.Fade('fader_event', { duration: .5 });
		}
		
		// Replace content with new message in .5 sec
		// Also, toggle the class name so it appears correctly (colors, style, etc.)
		//message.evalScripts();
		//message = message.stripScripts();
		setTimeout("$('fader_event').innerHTML='"+addslashes(message)+"'; $('fader_event').className='"+type+"'; event_exists=true;", 500);

		// Fade in event in .5 sec
		setTimeout("Effect.Appear('fader_event', { duration: .5 });", 500);

		// Store the event message for later comparison
		event_msg = message;
	}

	// Set the event to fade out (slow 1 sec fade) automatically in 9 sec
	// and lock this function for 1 sec
	event_fade_timer = setTimeout("Effect.Fade('fader_event', { duration: 1 }); lock_func('handle_event', 1); event_exists=false;", 9000);
}

lock_array = Array();
function lock_func(name, lock_time)
{
	// Multiply lock time to convert from sec to milisec
	lock_time = lock_time * 1000;

	// Set the lock status for the function to true
	lock_array[name] = true;

	// Unlock the function after the lock_time has expired
	setTimeout("unlock_func('"+name+"')", lock_time);
}

function unlock_func(name)
{
	lock_array[name] = false;
}


/* Slider functions */
function get_page_y_offset()
{
	if(IE)
	{
		if (document.documentElement && document.documentElement.scrollTop)
		{
			page_y_offset = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			page_y_offset = document.body.scrollTop;
		}
	}
	else
	{
		page_y_offset = window.pageYOffset;
	}
	return page_y_offset;
}

function slider(id, option)
{
	this.self = $('#'+id);

	if(option.margin_top) { this.margin_top = option.margin_top; }
	else { this.margin_top = 0; }

	if(option.type == 'fixed') { this.type = option.type; }
	else { this.type = 'normal'; }

	this.self.css('position', 'relative');

	var pos = find_pos(document.getElementById(id));
	this.orig_y_pos = pos.y;
	this.cur_y_pos = pos.y;

	this.self.css('top', 0);
	this.self.css('left', 0);
}
slider.prototype.check_y_pos = function()
{
	// Get page_y_offset
	var page_y_offset = get_page_y_offset()+this.margin_top;

	// If the type is fixed, once it reaches a scroll point, make it fixed
	if(this.type == 'fixed')
	{
		if(page_y_offset > this.orig_y_pos)
		{
			this.self.css('position', 'fixed');
			this.self.css('top', this.margin_top);
			return;
		}
		else
		{
			this.self.css('position', 'relative');
			this.self.css('top', 0);
			return;
		}
	}

	// Slow down as we reach the origin, or the top of the page
	if(page_y_offset < this.orig_y_pos)
	{
		var y_dif = Math.abs(this.orig_y_pos-this.cur_y_pos);
	}
	else
	{
		var y_dif = Math.abs(page_y_offset-this.cur_y_pos);
	}
	var y_move = parseInt(y_dif/10)+10;

	// Moving down
	if(page_y_offset > this.cur_y_pos)
	{
		if(this.cur_y_pos + y_move > page_y_offset)
		{
			this.self.css('top', (page_y_offset - this.orig_y_pos));
			this.cur_y_pos = page_y_offset;
		}
		else
		{
			this.self.css('top', (parseInt(this.self.css('top')) + y_move));
			this.cur_y_pos += y_move;
		}
	}
	// Moving up
	else if(page_y_offset < this.cur_y_pos && this.cur_y_pos > this.orig_y_pos)
	{
		if(this.cur_y_pos - y_move < this.orig_y_pos)
		{
			this.self.css('top', 0);
			this.cur_y_pos = this.orig_y_pos;
		}
		else
		{
			this.self.css('top', (parseInt(this.self.css('top')) - y_move));
			this.cur_y_pos -= y_move;
		}
	}
}
/* END Slider functions */

String.prototype.stripScripts = function() {
	return this.replace(new RegExp('<script[^>]*>([\\S\\s]*?)<\/script>', 'img'), '');
};

String.prototype.extractScripts = function() {
	var matchAll = new RegExp('<script[^>]*>([\\S\\s]*?)<\/script>', 'img');
	var matchOne = new RegExp('<script[^>]*>([\\S\\s]*?)<\/script>', 'im');
	return (this.match(matchAll) || []).map(function(scriptTag) {
		return (scriptTag.match(matchOne) || ['', ''])[1];
	});
};

String.prototype.evalScripts = function() {
	return this.extractScripts().map(function(script) { return eval(script) });
};

/* prompt */
function prompt_obj() {
	this.is_created = false;
	this.is_open = false;
}
prompt_obj.prototype.initialize = function() {
	$(document.body).bind('keydown', function(e) {
		if(IE) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
			deleteKey = 46;
		} else { // mozilla
			keycode = e.keyCode;
			escapeKey = e.DOM_VK_ESCAPE;
			deleteKey = e.DOM_VK_DELETE;
		}

		key = String.fromCharCode(keycode).toLowerCase();

		if(my_prompt.is_open == true)
		{
			if(keycode == escapeKey) { // close prompt
				my_prompt.close();
			}
		}
	});
}
prompt_obj.prototype.open = function(settings) {
	var default_settings = {
		width: 300
	};
	settings = $.extend({}, default_settings, settings);
	if(!this.is_created) this.create(settings.style);

	// Subtract the size of the left and right sides of the header
	var header_width = settings.width-12;
	var content_width = settings.width - 22;
	var footer_width = settings.width-12;

	$('#prompt_window').css('width', settings.width);

	$('#prompt_window_header_container').css('width', settings.width);
	$('#prompt_window_header_title_container').css('width', header_width);
	$('#prompt_window_content').css('width', content_width);
	$('#prompt_window_footer_container').css('width', settings.width);
	$('#prompt_window_footer_title_container').css('width', footer_width);

	if(settings.content != null)
	{
		if(settings.close_button == true)
		{
			settings.content += '<div style="margin-top: 10px;"><input type="button" value="Close" onclick="my_prompt.close();" /></div>';
		}
		this.finalize({content: settings.content, title: settings.title});
	}
	else
	{
		ajax_request(settings.data, {success: my_prompt.finalize});
	}
}
prompt_obj.prototype.close = function() {
	// Hide overlay
	$('#prompt_container').hide();
	my_overlay.close();

	// Show flash and select boxes
	show_selects();
	show_flash();
	this.is_open = false;
}
prompt_obj.prototype.create = function(style) {
	if(this.is_created) return;
	
	if(!style) style = 'home';

	if(IE)
	{
		$(document.body).append('<div id="prompt_container" style="display: none;"><div id="prompt_window"><div id="prompt_window_content"></div><!-- end prompt window content --></div><!-- end prompt window --></div><!-- end prompt container -->');

		$('#prompt_window').prepend('<div id="prompt_window_header_container"><div style="width: 6px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../../images/'+style+'/prompt/prompt_header_left.png\', sizingMethod=\'image\');"></div><div id="prompt_window_header_title_container" style="background: #000000 url(\'../../images/'+style+'/prompt/prompt_header_bg.png\') repeat-x center; height: 25px; float: left;"><div class="clickable" onclick="my_prompt.close();" style="width: 29px; height: 25px; float: right; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../../images/'+style+'/prompt/prompt_header_close.png\', sizingMethod=\'image\');"></div><span id="prompt_window_title" style="margin-left: 29px;"></span></div><!-- end prompt window header title --><div style="width: 6px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../../images/'+style+'/prompt/prompt_header_right.png\', sizingMethod=\'image\');"></div></div><!-- end prompt window header container -->');

		$('#prompt_window').append('<div id="prompt_window_footer_container"><div style="width: 6px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../../images/'+style+'/prompt/prompt_footer_left.png\', sizingMethod=\'image\');"></div><div id="prompt_window_footer_title_container" style="background: #000000 url(\'../../images/'+style+'/prompt/prompt_footer_bg.png\') repeat-x center; height: 25px; float: left;"><div style="width: 63px; height: 25px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../../images/'+style+'/prompt/prompt_footer_title.png\', sizingMethod=\'image\');"></div></div><!-- end prompt window footer title --><div style="width: 6px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../../images/'+style+'/prompt/prompt_footer_right.png\', sizingMethod=\'image\');"></div></div><!-- end prompt window footer container -->');
	}
	else
	{
		$(document.body).append('<div id="prompt_container" style="display: none;"><div id="prompt_window"><div id="prompt_window_content"></div><!-- end prompt window content --></div><!-- end prompt window --></div><!-- end prompt container -->');

		$('#prompt_window').prepend('<div id="prompt_window_header_container"><div style="width: 6px; height: 25px; float: left;"><img src="../../images/'+style+'/prompt/prompt_header_left.png" /></div><div id="prompt_window_header_title_container" style="background: #000000 url(\'../../images/'+style+'/prompt/prompt_header_bg.png\') repeat-x center; height: 25px; float: left;"><div style="float: right;" class="clickable" onclick="my_prompt.close();"><img src="../../images/'+style+'/prompt/prompt_header_close.png" /></div><span id="prompt_window_title" style="margin-left: 29px;"></span></div><!-- end prompt window header title --><div style="width: 6px; height: 25px; float: left;"><img src="../../images/'+style+'/prompt/prompt_header_right.png" /></div></div><!-- end prompt window header container -->');

		$('#prompt_window').append('<div id="prompt_window_footer_container"><div style="width: 6px; height: 25px; float: left;"><img src="../../images/'+style+'/prompt/prompt_footer_left.png" /></div><div id="prompt_window_footer_title_container" style="background: #000000 url(\'../../images/'+style+'/prompt/prompt_footer_bg.png\') repeat-x center; height: 25px; float: left;"><img src="../../images/'+style+'/prompt/prompt_footer_title.png" /></div><!-- end prompt window footer title --><div style="width: 6px; height: 25px; float: left;"><img src="../../images/'+style+'/prompt/prompt_footer_right.png" /></div></div><!-- end prompt window footer container -->');
	}

	$('#prompt_window').draggable({handle: $('#prompt_window_header_container')});
	this.is_created = true;
}
prompt_obj.prototype.finalize = function(data) {
	var auto_submit = data.auto_submit;
	var width = data.width;
	var title = data.title;
	var error = data.error;
	var content = data.content;
	var focus_id = data.focus_id;

	$('#prompt_window_title').html(title);
	$('#prompt_window_content').html(content);

	if(auto_submit != null)
	{
		auto_submit.evalScripts();
		return;
	}

	content.evalScripts();

	// Hide flash and select boxes
	hide_selects();
	hide_flash();

	// Set the prompt window width
	if(width != null)
	{
		$('#prompt_window').css('width', width);
	}

	// Calculate top and left offset for the prompt
	var arrayPageScroll = getPageScroll();
	var arrayPageSize = getPageSize();
	var promptTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var promptLeft = arrayPageScroll[0];
	$('#prompt_container').css('top', promptTop);
	$('#prompt_container').css('left', promptLeft);

	my_overlay.open();
	$('#prompt_container').show();

	// Focus on the default focus input
	if($('#'+focus_id) != null)
	{
		$('#'+focus_id).focus();
	}
	my_prompt.is_open = true;
}
my_prompt = new prompt_obj();
my_prompt.initialize();

/* overlay */
function overlay_obj() {
	this.is_created = false;
	this.is_open = false;
	this.id = 'overlay';
}
overlay_obj.prototype.initialize = function() {
	my_overlay.create();
}
overlay_obj.prototype.open = function(settings) {
	var default_settings = {
		duration: 300,
		opacity: .5
	};
	settings = $.extend({}, default_settings, settings);
	if(!this.is_created) this.create();

	// Hide flash and select boxes
	hide_selects();
	hide_flash();

	$('#'+this.id).css('opacity', 0);
	$('#'+this.id).show();
	$('#'+this.id).fadeTo(settings.duration, settings.opacity);

	my_overlay.is_open = true;
}
overlay_obj.prototype.close = function() {
	// Hide overlay
	$('#'+this.id).css('opacity', 0.0);
	$('#'+this.id).hide();

	// Show flash and select boxes
	show_selects();
	show_flash();
	this.is_open = false;
}
overlay_obj.prototype.create = function() {
	if(this.is_created) return;
	
	$(document.body).append('<div id="'+this.id+'" style="display: none;"></div>');

	// Set overlay width and height
	var arrayPageSize = getPageSize();
	$('#'+this.id).css('width', arrayPageSize[0]);
	$('#'+this.id).css('height', arrayPageSize[1]);

	// Set overlay opacity to 0
	$('#'+this.id).css('opacity', 0);

	this.is_created = true;
}
my_overlay = new overlay_obj();
$(window).bind('load', my_overlay.initialize);

function show_selects()
{
	var selects = document.getElementsByTagName("select");
	for(i = 0; i != selects.length; i++)
	{
		selects[i].style.visibility = "visible";
	}
}

function hide_selects()
{
	var selects = document.getElementsByTagName("select");
	for(i = 0; i != selects.length; i++)
	{
		var node = selects[i];
		var keep = false;
		
		while(node.id != null)
		{
			if(node.id == 'prompt_window_content') keep = true;
			node = node.parentNode;
		}
		// Don't remove the selects that are IN the prompt window!
		if(!keep)
			selects[i].style.visibility = "hidden";
	}
}

function show_flash()
{
	var flashObjects = document.getElementsByTagName("object");
	for(i = 0; i < flashObjects.length; i++)
	{
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for(i = 0; i < flashEmbeds.length; i++)
	{
		flashEmbeds[i].style.visibility = "visible";
	}
}

function hide_flash(){
	var flashObjects = document.getElementsByTagName("object");
	for(i = 0; i < flashObjects.length; i++)
	{
		var node = flashObjects[i];
		var keep = false;
		
		while(node.id != null)
		{
			if(node.id == 'prompt_window_content') keep = true;
			node = node.parentNode;
		}
		// Don't remove the flash objects that are IN the prompt window!
		if(!keep)
			flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for(i = 0; i < flashEmbeds.length; i++)
	{
		var node = flashEmbeds[i];
		var keep = false;
		
		while(node.id != null)
		{
			if(node.id == 'prompt_window_content') keep = true;
			node = node.parentNode;
		}
		// Don't remove the flash objects that are IN the prompt window!
		if(!keep)
		flashEmbeds[i].style.visibility = "hidden";
	}

}
/* end prompt */

/* upload progress */
var upload_url  = '';
var upload_pars = '';
var upload_id   = '';
// record last time and last amount to help record upload rate
var last_time = 0;
var last_amt = 0;
var upload_fin_js = '';
var fails_in_a_row = 0;
function init_progress_bar(server, id, success_js)
{
	upload_fin_js = success_js;

	upload_url  = site_url + '/scripts/retrieve_upload_progress.php';
	upload_pars = {id:id, server:server};

	setTimeout("click_progress_bar()", 250);
}

function click_progress_bar()
{
	var pars = upload_pars;
	pars['last_time'] = last_time;
	pars['last_amt'] = last_amt;
	ajax_request(pars, {success:update_progress_bar, url:upload_url});
}

function update_progress_bar(response)
{
	var amount_uploaded  = response.amount_uploaded;
	var size             = response.size;
	var percent_uploaded = response.percent_uploaded;
	var speed            = response.speed;
	var eta              = response.eta;
	// helps record download rate
	last_time            = response.last_time;
	last_amt             = response.last_amt;

	if(last_time < 0)
	{
		last_time = 0;
	}

	if(amount_uploaded == 'no_stat')
	{
		amount_uploaded = 0;
		fails_in_a_row++;
	}
	else
	{
		fails_in_a_row = 0;
	}
	if(fails_in_a_row >= 5)
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign','center');
		$('#upload_progress').html('Unknown error: unable to track upload.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
//		document.location=document.location;
	}
	
	if(amount_uploaded == 'fin')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign', 'center');
		$('#upload_progress').html('Upload Complete!');
		$('#cancel_upload').hide();
		$('#upload_again').show();
		if(typeof upload_fin_js == 'string')
		{
			eval(upload_fin_js);
		}
		else if(typeof upload_fin_js == 'function')
		{
			upload_fin_js.apply(this,[]);
		}
	}
	else if(amount_uploaded == 'too_big')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign','center');
		$('#upload_progress').html('Upload failed: File exceeds maximum size.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
	}
	else if(amount_uploaded == 'no_file')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign', 'center');
		$('#upload_progress').html('Upload failed: No file uploaded.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
	}
	else if(amount_uploaded == 'no_space')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign', 'center');
		$('#upload_progress').html('Upload failed: File too large. Not enough space available.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
	}
	else if(amount_uploaded == 'not_image')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign', 'center');
		$('#upload_progress').html('Upload failed: File was not an image file.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
	}
	else
	{
		$('#upload_progress').height(50);
		$('#upload_progress').css('textAlign', 'left');
		if(parseFloat(size) > 0 || $('#upload_progress').html() == '')
		{
			var margin = 10;
			var width = $('#prompt_window').width()-20-4-margin;
			var block_width = 1;
			var total_blocks = width/block_width;
			var bar = '<div style="margin-top: 5px; margin-left: '+(margin/2)+'px; padding: 1px; border: 1px solid #000000; background: #FFFFFF; overflow: hidden; height: 14px; width: '+width+'px;">';
			var num_blocks = parseInt(percent_uploaded/(100/total_blocks));
			if(!num_blocks || num_blocks == null || num_blocks < 1)
			{
				num_blocks = 1;
			}
			for(var i = 0; i < num_blocks; i++)
			{
				bar += '<div style="float: left; width: '+block_width+'px; height: 14px; background: #4d83ad;"></div>';
			}
			bar += '<div class="spacer">&nbsp;</div></div><div style="margin-left: '+(margin/2)+'px; overflow: hidden; width: '+width+'px; font-size: 10px;">';
			bar += amount_uploaded + '/' + size + '(' + percent_uploaded + '%) @ ' + speed + ' ETA: ' + eta;
			bar += '</div>';

			$('#upload_progress').html(bar);
		}
//		$('#upload_progress').html(amount_uploaded+','+size+','+percent_uploaded+','+speed);
		setTimeout("click_progress_bar()", 250);
	}
}
/* end upload progress */

/* ajax methods */
function ajax_update(data, container, options)
{
	data.tb = 1;
	data.tb_secure = get_cookie('tb_secure');

	var this_url = url;
	if(typeof options == "object" && typeof options.url == "string")
	{
		this_url = options.url;
	}

	$.ajax({
		url: this_url,
		data: data,
		type: 'post',
		dataType: 'json',
		success: function(response, textStatus) {
			if(typeof options == "object" && typeof options.form == "function") { options.form.apply(this, [response]); }
			var s = response.success;
			var e = response.error;
			var c = response.content;
			var rt = response.results_text;
			var rf = response.results_fade;

			if(typeof data.form_id == "string" && typeof rt == "string")
			{
				$('#'+data.form_id+'_results_area').stop();
				$('#'+data.form_id+'_results_area').css('opacity',1);
				$('#'+data.form_id+'_results_area').html(rt).show().animate({'marginLeft':'+=0px'},2000,'',function(){
					if(rf == true)
					{
						$('#'+data.form_id+'_results_area').fadeOut(3000);
					}
				});
			}

			if(e!=null) { post_new_event('error', e, data.form_id); return; }
			if(s!=null && s != 1) post_new_event('success', s);
			if(typeof options == "object" && typeof options.success == "function") { options.success.apply(this, [response]); return; }
			if(c==null) return;
			if(container.indexOf(':') != -1)
			{
				updater = container.split(":");
				if(updater[0] == 'prepend')
				{
					$('#'+updater[1]).prepend(c.stripScripts());
				}
			}
			else
			{
				$('#'+container).html(c.stripScripts());
			}
			c.evalScripts();
		},
		complete: function(response, textStatus) {
			if(typeof DEBUG == 'boolean' && DEBUG == true)
			{
				if(textStatus == 'error')
				{
					var the_url = this_url;
					for(i in data)
					{
						the_url += i+'='+data[i]+',';
					}
	//				alert('Parser Error: '+response.responseText);
					alert('Incorrect ajax call: check URL.'+the_url);
				}
				else if(textStatus == 'parsererror')
				{
					alert('Parser Error: '+response.responseText);
				}
			}

			if(typeof data.form_id == "string")
			{
				$('#'+data.form_id).enable();
			}
		}
	});
}

function ajax_request(data, options)
{
	if(typeof data == "string")
	{
		data += '&tb=1&tb_secure='+get_cookie('tb_secure');
	}
	else
	{
		data.tb = 1;
		data.tb_secure = get_cookie('tb_secure');
	}

	var this_url = url;
	if(typeof options == "object" && typeof options.url == "string")
	{
		this_url = options.url;
	}

	$.ajax({
		url: this_url,
		data: data,
		type: 'post',
		dataType: 'json',
		success: function(response, textStatus) {
			if(typeof options == "object" && typeof options.form == "function") { options.form.apply(this, [response]); }
			var s = response.success;
			var e = response.error;
			var c = response.content;
			var f = response.field;
			var rt = response.results_text;
			var rf = response.results_fade;

			if(typeof data.form_id == "string" && typeof rt == "string")
			{
				$('#'+data.form_id+'_results_area').stop();
				$('#'+data.form_id+'_results_area').css('opacity',1);
				$('#'+data.form_id+'_results_area').html(rt).show().animate({'marginLeft':'+=0px'},2000,'',function(){
					if(rf == true)
					{
						$('#'+data.form_id+'_results_area').fadeOut(3000);
					}
				});
			}

			if(e!=null) { post_new_event('error', e, data.form_id); return; }
			if(s!=null && s != 1) post_new_event('success', s);
			if(typeof options == "object" && typeof options.success == "function") { options.success.apply(this, [response]); return; }
			if(c==null) return;
			c.evalScripts();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
//			alert('XMLHttpRequest: '+XMLHttpRequest);
//			alert('textStatus: '+textStatus);
//			alert("Error: " + errorThrown);
		},
		complete: function(response, textStatus) {
/*			var blah = '';
			for(key in response)
			{
				if(key != 'channel')
					blah += key+': '+response[key]+'\n';
			}
			alert(blah);
*/
			DEBUG = true;
			if(typeof DEBUG == 'boolean' && DEBUG == true)
			{
				if(textStatus == 'error')
				{
					var the_url = this_url;
					for(i in data)
					{
						the_url += i+'='+data[i]+',';
					}
	//				alert('Parser Error: '+response.responseText);
					alert('Incorrect ajax call: check URL.'+the_url);
				}
				else if(textStatus == 'parsererror')
				{
					alert('Parser Error: '+response.responseText);
				}
			}

			if(typeof data.form_id == "string")
			{
				$('#'+data.form_id).enable();
			}
		}
	});
}
/* end ajax methods */
