//depends upon jquery.js

var dynamic_args;//really ugly global variable, but it's needed to allow function-in-function parameter passing

function strip_px(value)
{
	if (value!=null)
		return (value.replace("px","")*1);
	else
		return 10;
}

//import titles & toolbar information from component view
function set_titles()
{
	if(document.getElementById("rw2_parent_app"))
	{
		if(document.getElementById("main_menu_" + $("#rw2_parent_app").html()))
		{
			$("#main_menu_" + $("#rw2_parent_app").html()).siblings().removeClass("highlighted_" + currentModule);
			$("#main_menu_" + $("#rw2_parent_app").html()).addClass("highlighted_" + currentModule);
		}
	}
	if(document.getElementById("rw2_dynamicsubmenu"))
		$("#application_sub_menu .tool_bar").html($("#rw2_dynamicsubmenu").html());
	else
		$("#application_sub_menu .tool_bar").html($("#rw2_dynamicsubmenu_default").html());
	if(document.getElementById("rw2_dynamictitle"))
		$("#application_title_bar h1").html($("#rw2_dynamictitle").html());
	else
		$("#application_title_bar h1").html($("#rw2_dynamictitle_default").html());
	if(document.getElementById("rw2_dynamicmenu_right"))
		$("#application_title_bar div").html($("#rw2_dynamicmenu_right").html());
	else
		$("#application_title_bar div").html($("#rw2_dynamicmenu_right_default").html());
	if(document.getElementById("rw2_dynamictitle_right"))
		$("#application_tool_bar h2").html($("#rw2_dynamictitle_right").html());
	else
		$("#application_tool_bar h2").html($("#rw2_dynamictitle_right_default").html());
	if(document.getElementById("rw2_dynamicmenu"))
		$("#application_tool_bar div").html($("#rw2_dynamicmenu").html());
	else
		$("#application_tool_bar div").html($("#rw2_dynamicmenu_default").html());
	$("#application_dialog").dialog("destroy");
	if(!keep_overlay)
		$("#application_overlay").hide();
	keep_overlay=false;
}

//dynamically load content into a html element, & display a throbber until it's loaded
//arguments = array(container,href,failure,isBig,custom_function,content)
function load_content()
{
	var style="application_loading";
	hide_map();
	dynamic_args=arguments;
	if(!dynamic_args[2])
		dynamic_args[2]=loadingError;
	if(dynamic_args[1].search("/?")<0)
		dynamic_args[1] = dynamic_args[1] + "/?";
	else
		dynamic_args[1] = dynamic_args[1] + "&";
	if(dynamic_args[0]=="#application_content"&&dynamic_args[1].search("/nohistory:1")<0)
		dynamic_args[1] = dynamic_args[1] + "/history=1&";
	dynamic_args[1] = dynamic_args[1] + "rand=" + rand();
	if(dynamic_args[3])
		style = style + "_big";
	$(dynamic_args[0]).html("<div class=\"" + style + "\">" + $("#application_loading").html() + "</div>" + $(dynamic_args[0]).html());
	if(!dynamic_args[6])
	{
		$("#application_overlay").show();
		$("#application_overlay").bgiframe();
		//reset_tasklist_timer();
	}
	$.ajax(
	{
		type: "GET",
		url: dynamic_args[1],
		success: function(content)
		{
			$(dynamic_args[0]).html(content);
			dynamic_args[5]=content;
			dynamic_args[4](dynamic_args);
		},
		error: function()
		{
			if(dynamic_args[0]=="#application_content")
			{
				$("#application_title_bar h1").html("Loading Error");
				$("#application_tool_bar h2").html("Error");
			}
			$(dynamic_args[0]).html(dynamic_args[2]);
			$("#application_overlay").hide();
		}
	});
}
function is_string(input)
{
	return typeof(input)=='string' && isNaN(input);
}
function content_area_links()
{
	var container=arguments[0];
	/*
	if(container.toString()!=container)
		container=container[0];
	if(container.toString()!=container)
		container=container[0];
	*/
	if(!is_string(container))
		container=container[0];
	$(container).find("form").ajaxForm(
	{
		beforeSubmit: function()
		{
			$("#application_overlay").show();
			$("#application_overlay").bgiframe();
			$("#application_content").html("<div class=\"application_loading_big\">" + $("#application_loading").html() + "</div>" + $("#application_content").html());
		},
		success: function()
		{
			content_links();
		},
		target: "#application_content"
	});
	build_table(container + " table.tablesorter");
	$("#application_overlay").hide();
}

function content_links()
{
	set_titles();
	$("#application_content form").ajaxForm(
	{
		beforeSubmit: function()
		{
			$("#application_overlay").show();
			$("#application_overlay").bgiframe();
			$("#application_content").html("<div class=\"application_loading_big\">" + $("#application_loading").html() + "</div>" + $("#application_content").html());
		},
		success: function()
		{
			content_links();
		},
		target: "#application_content"
	});
	build_table("#application_content table.tablesorter");
	//resize();
	$("#application_content").trigger("click");
	if(dynamic_args)
		dynamic_args[5]=null;
	if(!keep_overlay)
		$("#application_overlay").hide();
	keep_overlay=false;
	if(show_terms)
		$("#terms_link").trigger("click");
}
function popup_links()
{
	$("#application_dialog").html("<div class=\"ui-dialog-content-container\">" + dynamic_args[5] + "</div>");
	dynamic_args[5]=null;
	if($("#application_dialog .main_title").length>0)
		$(".ui-dialog-title").html($("#application_dialog .main_title").html());
	$("#application_dialog .ui-dialog-content-container").css("height",strip_px($(".ui-dialog-content").css("height"))-70);
	$("#application_dialog a").click(function()
	{
		if($(this).hasClass("overlay"))
		{
			$("#application_dialog").dialog("destroy");
			$("#application_overlay").show();
			return(true);
		}
		if($(this).hasClass("full"))
			return true;
		if($(this).hasClass("jquery_link"))
			return true;
		if($(this).hasClass("download"))
			return true;
		if($(this).hasClass("application"))
		{
			$("#application_dialog").dialog("destroy");
			reload_page(this.href);
			return false;
		}
		load_content("#application_dialog",this.href,dynamic_args[2],false,popup_links);
		return false;
	});
}
function form_links()
{
	if(is_string(arguments[0]))
		dynamic_args[5]=arguments[0]+"";
	$("#application_dialog").html("<div class=\"ui-dialog-content-container\">" + dynamic_args[5] + "</div>");
	dynamic_args[5]=null;
	$(".ui-dialog-title").html($("#application_dialog .main_title").html());
	$("#application_dialog .ui-dialog-content-container").css("height",strip_px($(".ui-dialog-content").css("height"))-70);
	$("#application_dialog a").click(function()
	{
		if($(this).hasClass("overlay"))
		{
			$("#application_dialog").dialog("destroy");
			$("#application_overlay").show();
			return(true);
		}
		if($(this).hasClass("full"))
			return true;
		if($(this).hasClass("jquery_link"))
			return true;
		if($(this).hasClass("download"))
			return true;
		if($(this).hasClass("application"))
		{
			$("#application_dialog").dialog("destroy");
			reload_page(this.href);
			return false;
		}
		load_content("#application_dialog",this.href,dynamic_args[2],false,form_links);
		return false;
	});
	format_form(dynamic_args);
	build_table("#application_dialog table.tablesorter");
	$("#application_overlay").hide();
}
