function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}

function _rejax_rebindPagers()
{
	$('.ajax_pager_holder a').unbind('click');
	$('.ajax_pager_holder a[page]').bind('click',function() {
		var ob = {
			page				:$(this).attr('page'),
			rejax_name  		:$(this).attr('rejax_name')
		};
		ajaxUrlParms = rejaxGenerateUrlParam(ob);
		loadInRejaxTable($(this).attr('rejax_name'),ajaxUrlParms);
		$(this).parent().parent().find('div').removeClass('ajax_pager_pagesel_selected');
		$(this).parent().addClass('ajax_pager_pagesel_selected');
		return false;
	});
	
	$('.ajax_pager_holder a[pager_offset]').bind('click',function() {
		var replaceDiv = '#rejax_pager__'+$(this).attr('rejax_name');
		
		$(replaceDiv).load('_rejax_pager.php',{
			rejax_name  		:$(this).attr('rejax_name'),
			pager_offset		:$(this).attr('pager_offset'),
			record_count		:$(this).attr('record_count')
			},_rejax_rebindPagers);
	});
}


function rejaxGenerateUrlParam(kv_pairs_in_obj)
{
	url = ''
	first = true;
	for (k in kv_pairs_in_obj)
	{
		if (!first)
		{
			url = url+'&';
		}
		url = url + urlencode(k) + '='+urlencode(kv_pairs_in_obj[k])+'';
		first = false;
	}
	return url;
}

function loadInRejaxTable(rejax_name, url)
{
		$.ajax(
		{
			type: "GET",
			url: './_rejax_ajax.php?rejax_name='+rejax_name+'&'+url,
			dataType: "html",
			success: function(success_html)
				{
					clearLoadingImage(rejax_name);
					$('#data_'+rejax_name).children().remove();
					$('#data_'+rejax_name).replaceWith(success_html);
					rebindColumnKeys();
					
					if ((typeof function_to_fire_after) == 'function')
					{
						function_to_fire_after.call();
					}
				},
			beforeSend: function()
				{
					$('#data_'+rejax_name).fadeTo('fast', 0.66);
					
					setLoadingImage(rejax_name);
				},
			error: function()
				{
					$('#data_'+rejax_name).replaceWith('ERROR');
				}
		});
		return false;
}

var previous_inner_height = 0;
var previous_rejax_location = 0;
function setLoadingImage(rejax_name)
{
	// position and set 'loading' image.
	rejax_location = $('#data_'+rejax_name).offset();
	
	// widths
	calc_width = $(window).width();
	rejax_width = $('#data_'+rejax_name).innerWidth();
	if(calc_width < rejax_width)
	{
		calc_width = rejax_width;
	}
	image_x = (calc_width/2) - 75;
	
	// heights
	rejax_height = $('#data_'+rejax_name).innerHeight();

	// failover since it occasionally doesn't read the height in FF
	if(rejax_height == 0)
	{
		rejax_height = previous_inner_height;
		rejax_location = previous_rejax_location;
	}
	else
	{
		previous_inner_height = rejax_height;
		previous_rejax_location = rejax_location;
	}

	if(rejax_height > 300)
	{
		rejax_height = 300;
	}
	if(rejax_height < 65)
	{
		image_y = rejax_location.top + rejax_height - 18;
	}
	else
	{
		image_y = rejax_location.top + Math.round(rejax_height/2) - 18;
	}
	
	$('#holder').append("<img src='images/loading_animation.gif' style='position:absolute; left:"+image_x+"px; top:"+image_y+"px;' class='process_rejax_image_"+rejax_name+"' width='150' height='14'>");
}

function clearLoadingImage(rejax_name)
{
	$('.process_rejax_image_'+rejax_name).fadeOut();
	$('.process_rejax_image_'+rejax_name).remove();
}

function loadPagerAndItemCount(location, url)
{
	var selector = '#'+location;
	$(selector).load(url);
	return false;
}

function rebindColumnKeys()
{
	$(".table_key").unbind('mouseup');
	$(".table_key").mouseup(
		function()
		{
			removed = 0;
			$(".table_key_list").each(function()
			{
				removed = 1;
				$('.table_key_list').remove();
			});
			
			if (removed == 0)
			{
				keyarea = $(this);
				url = $(this).attr('href');
				offset = $(this).offset();
				$.ajax({
					type: 'GET',
					url: url,
					success: function(html)
					{
						keyarea.after(html);
						$('.table_key_list').css({
							"top":offset.top+20,
							"left":offset.left+20
						});
					}
				});
			}
			return false;
		}
	);
}

$(document).ready(function(){
	rebindColumnKeys();
});