
function appendLabel( str )
{
	var sep = $("#labelText").val()=="" ? "" : ", ";
	$("#labelText").val( $("#labelText").val() + sep + str );
};

var dataToMap = function(data)
{
	var result = {};
	jQuery.each
	(
		data, 
		function(index, item)
		{
			var existing = result[item.name];
			if( existing !== undefined ) 
			{
				if( (typeof existing) === "object" )
				{
					existing[existing.length] = item.value;
				}
				else 
				{
					result[item.name] = new Array( existing, item.value );
				}
			} 
			else 
			{
				result[item.name] = item.value;
			}
		}
	);
	return result;
};

var instrumentInputs = function(content)
{
	var inputs = $("input.titlehint, textarea.titlehint", content);
	inputs.focus(function(event) {
		$this = $(this);
		if( $this.attr("title") == $this.val() )
		{
			$this.val("");
		}
	});
	
	inputs.blur(function(event) {
		$this = $(this);
		if( $this.val() == "" )
		{
			$this.val( $this.attr("title") );
		}
	});
	
	inputs.each( function( index, element ) {
		$element = $(element);
		if( $element.val() == "" )
		{
			$element.val( $element.attr("title") );
		}
	});
	
};

var postLoad = function(content)
{
	//should really check that this has actually been loaded first
	swfUploadInit(content);
	instrumentInputs(content);
	curriculumTree(content);
};

var instrumentLinksAndForms = function( loading, loaded, error, content, selector )
{
	log( "Instrumenting container #" + content.parent().get(0).id );
	
	$("a", loaded).unbind("click");
	$("a", error).unbind("click");
	
	$("a", content.parent()).click
	(
		function(event)
		{
			var loc = $(this).attr("href");
			// in IE, this attribute will be the full interpreted url, 
			// so we need to strip off the current document location
			// if that is the start of the url (in cases where the url 
			// started with #) so that our logic still works as expected.
			var winLoc = window.location.href;
			
			// if loc starts with winLoc (no startsWith in javascript)
			// this will break if the relative url contains .. so don't do that
			if( loc.match("^"+winLoc)==winLoc )
			{
				loc = loc.substring( winLoc.length );
			}
			
			if( loc.charAt(0) == '#' )
			{
				return;
			}
			
			if( $(this).attr("target") != "")
			{
				return;
			}
			
			if( $(this).hasClass("notab") || $(this).closest("span").hasClass("notab") )
			{
				return;
			}

			event.preventDefault();

			if( $(this).hasClass("confirm-link") )
			{
				var result = confirm("Are you sure? This action cannot be undone.");
				if( result === false )
				{
					return;
				}	
			}
			
			content.hide();
			error.hide();
			loaded.hide();
			loading.show();
			
			log( "Loading content for url " + loc + " in container #" + content.parent().get(0).id );
			content.load
			(
				loc + (selector == "" ? "" : " " + selector),
				{'decorator': 'ajax'},
				function( responseText, textStatus, XMLHttpRequest )
				{
					loading.hide();
					if( textStatus == "success" || textStatus == "notmodified" )
					{
						loaded.show();
						content.show();
						
						instrumentLinksAndForms(loading, loaded, error, content, selector);
						postLoad(content);
					}
					else
					{
						error.show();
					}
				}
			);
		}
	);
	
	$("form", content).submit
	(
		function(event)
		{
			var action = $(this).attr("action");
			
			var winLoc = window.location.href;
			
			// see above
			if( action.match("^" + winLoc) == winLoc ) 
			{
				action = action.substring( winLoc.length );
			}
			
			if( action.charAt(0) == "#" )
			{
				return;
			}
			
			if( $(this).hasClass("notab") )
			{
				return;
			}
			
			var inputs = $("input.titlehint, textarea.titlehint");
			inputs.each( function( index, inputElement) {
				$input = $(inputElement);
				if( $input.val() == $input.attr("title") )
				{
					$input.val("");
				}
			});
			
			event.preventDefault();
			
			var data = $(this).serializeArray();
			
			data = dataToMap(data);
			data['decorator'] = 'ajax';
			
			content.hide();
			error.hide();
			loaded.hide();
			loading.show();
			
			log( "Submitting form to url " + action + " in container #" + content.parent().get(0).id );
			content.load
			(
				action + (selector == "" ? "" : " " + selector),
				data,
				function()
				{
					loading.hide();
					loaded.show();
					content.show();
					
					instrumentLinksAndForms(loading, loaded, error, content, selector);
					postLoad(content);
				}
			);
		}
	);
};


var instrument = function(hash)
{
	var panel = $( hash );
	if( panel.length > 0 )
	{
		instrumentLinksAndForms
		(
				panel.find(".loading"), //loading element 
				panel.find(".loaded"),  //loaded element (might be empty)
				panel.find(".error"),   //the error indicator
				panel.find(".content"), //container for content
				".ajinc",				//selector
				""						//current page, N/A
		);
		if( window.location.hash == hash )
		{
			panel.find("a[rel='load']").click();
		}
	}
	
};

$(document).ready
(
	function() 
	{
		instrument("#dashboard");
		
		if( window.location.hash == '' )
		{
			$("#dashboard").find("a[rel='load']").click();
		}
		
		instrument("#my-content");
		instrument("#my-comments");
		instrument("#upload-content");
		instrument("#help");
		
		var helpLoaded = false;
		var tabIndexPreHelp = 0;
		
		var tabs = $(".yk-ui-tabs");
		if( tabs.length > 0)
		{
			tabs.tabs({
				select: function(event, ui) 
				{
					var id = $(ui.panel).attr("id");
					if( id == "help" )
					{
						if( !helpLoaded )
						{
							$(ui.panel).find("a[rel='load']").click();
							helpLoaded = true;
						}
					} 
					else
					{
						if( tabIndexPreHelp != ui.index )
						{
							$(ui.panel).find("a[rel='load']").click();
						}
						tabIndexPreHelp = ui.index;
					}
				}
			});
			tabIndexPreHelp = tabs.tabs('option', 'selected');
		}
		
	}
);

