$(document).ready(function()
{
	$("A.messagebox_link").live("click",function(event)
	{
		event.preventDefault();
		
		jLoad($(this).attr("href"));
	});
	
	$("INPUT.jump_input").focus(function()
	{
		if ($(this).val() == $(this).attr("svalue"))
			$(this).val("");
	});
	
	$("INPUT.jump_input").blur(function()
	{
		if ($(this).val() == "")
			$(this).val($(this).attr("svalue"));
	});

	$("INPUT.jump_input").parent("FORM").submit(function(e)
	{
		el = $(this).contents("INPUT.jump_input");
		if (el.val() == el.attr("svalue"))
			el.val("");
	});
	
	$("DIV.logged_info *").mouseenter(function(e)
	{
		e.preventDefault();
		
		$(this).blur();
		$("DIV.logged_info DIV.dropdown_menu").show();
	});
	
	$("DIV.logged_info DIV.dropdown_menu").mouseleave(function(e)
	{
		$(this).hide();
	});
	
	$("DIV.dashboard_module_small DIV.ds_title A").click(function(e)
	{
		e.preventDefault();
		$(this).toggleClass("closed");
		$(this).closest("DIV.dashboard_module_small").find("DIV.ds_content").slideToggle();
	});
	
	$("DIV.dashboard_module_recentsearches A.clear").click(function(e)
	{
		e.preventDefault();
		var content = $(this).closest("DIV.dashboard_module_recentsearches").find("DIV.recent_searches");
		$.getJSON("/secure/subscription/papers/search/clear_recent_searches",function(data) { content.html("No saved searches"); });		
	});
	
	$("DIV.ds_alerts A.alert_close").click(function(e)
	{
		e.preventDefault();
		var alert = $(this).closest("DIV.alert_container");
		alerts_close(alert);
	});
	
	alerts_closer_start();
});

function pleaseWait()
{
	showMessage("<img src=\"/i/loading.gif\" align=\"absmiddle\"> Please wait&#133;",{close: false});
}

function noneed2Wait()
{
	hideMessage();
}

function showMessage(message,options)
{
	if (!options)
		options = { close: true };
	
	if ($("#mb_container").length)
	{
		$("#mb_container DIV.mb_inner_content").html(message);
	} else
	{
		$("body").append("<div id=\"general_over\"></div>");
		$("#general_over").height($("#general").height());

		$("body").addClass("darkshadow");
		$("#general").fadeTo(10,0.50,function()
		{
			$("#general").after("<div class=\"messagebox mb_pleasewait\" id=\"mb_container\"><div class=\"mb_inner_content\">"+message+"</div></div>");
			centerMessage();	
			
			$("#mb_close_button, .mb_close_button").live("click",function(event)
			{
				event.preventDefault();
				hideMessage();
			});
		});
	}
	
	if (!options.close)
		$("#mb_close_button").remove();
	else if (!$("#mb_close_button").length)
	{
		$("#mb_container").append("<a href=\"#\" id=\"mb_close_button\"></a>");
	}

}

function centerMessage()
{
	var pos_top = Math.floor(($("BODY").height() - $("#mb_container").height())/2) + $(document).scrollTop();
	var pos_left = Math.floor(($("BODY").width() - $("#mb_container").width())/2);

	$("#mb_container").css("top",pos_top);
	$("#mb_container").css("left",pos_left);
}

function hideMessage()
{
	$("#mb_container").remove();
	$("#general").fadeTo(300,1, function() 
	{ 
		$("body").removeClass("darkshadow"); 
		$("#general_over").remove();
	});	
	
}

function checkForm(options)
{
	pleaseWait();
	$("DIV[id^='errors_']").html("");
	
	$.ajax({
		type: "POST",
		url: options.url,
		dataType: "json",
		data: options.form.serialize(),
		formObject: options.form,
		success: function (data,status) {
			if (data.errors_fields && data.errors_fields.length)
			{
				noneed2Wait();
				$(data.errors_fields).each(function(idx) {
					$("#errors_"+this).html(data.errors_messages[idx]);
				});

				if (options.errors)
					options.errors();
			} else {
				if (options.success)
				{
					this.formObject._response_data = data;
					options.success(this.formObject);
				}
			}
		},
		error: function (data,status) {
			showMessage("Processing error");
		}
	});	
}

function jLoad(url)
{
	if ($("#mb_container").data("loading"))
		return false;
		
	pleaseWait();
	$("#mb_container").data("loading",true);
		
	$.ajax({
		type: "GET",
		dataType: "json",
		url: url,
		success: function(data,status) {
			if (!data.error)
			{
				showMessage("");
				$("#mb_container").removeClass("mb_pleasewait").addClass("mb_data");
				$("#mb_container DIV.mb_inner_content").before("<div class=\"mb_title\"></div>");
				$("#mb_container DIV.mb_inner_content").html(data.results);
				$("#mb_container DIV.mb_title").html($("#mb_container DIV.mb_inner_content H3.title").html());
				$("#mb_container").draggable({cursor: "move", handle: "DIV.mb_title", containment: "document" });
				$("#mb_container").resizable({minHeight: 250, maxHeight: 550, minWidth: 100});
				
				centerMessage();
				
				$("#mb_container").data("loading",false);
			}
		}
	});
}

function alerts_close(alert)
{
	if (!alert)
		return false;
		
	alert.remove();
}

function alerts_closer_start()
{
	if ($("DIV.ds_alerts DIV.alert_container[temporary='1']").size())
		var cta = setTimeout("alerts_close_temporary()",60000);
}

function alerts_close_temporary()
{
	$("DIV.ds_alerts DIV.alert_container[temporary='1']").each(function(i)
	{
		$(this).slideUp("normal",function()
		{
			$(this).remove();
		});
	});
}