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

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)
{
	return $.param(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) == 'undefined')
					{
						return false;
					}
					if (typeof(function_to_fire_after) == 'function')
					{
						function_to_fire_after = new Array(function_to_fire_after);
					}
					if ((typeof(function_to_fire_after).toLowerCase == 'array') || (typeof(function_to_fire_after) == 'object'))
					{
						for (var i=0;i<function_to_fire_after.length;i++)
						{
							var func=function_to_fire_after[i];
							if ((typeof func) == 'function')
							{
								func.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
	{
		if (rejax_location)
		{
			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();
});
$(document).ready(function(){
$(".toggle").unbind();
$(".toggle").bind("click", function() {
	hide = $(this).parents('div.module').find(".closeable, .closable").slideToggle();
	hide.parents('div.module').find(".toggle").toggleClass("closed");
	
	//state = hide.parent().attr("class").indexOf("closed") >= 0 ? "closed" : "open";
	var state='open';
	if ($(this).attr('class').indexOf("closed") > -1)
	{
		state = 'closed';
	}
	
	// hide/show any buttons within the header with class of module_header_button
	if(state == 'closed') hide.parent().find(".module_header_button").hide();
	else hide.parent().find(".module_header_button").show();
	
	if(hide.parent().attr("report_name") != undefined)
	{
		$.ajax({
			type: "POST",	url: "/store_closed_report_state.php",
			data: "report=" + hide.parent().attr("report_name") + "&state=" + state
		});
	}
});
});
