/***********************************************************
@
@     LaCo - Late Content v1.0
@     or AJAX for the non-programmer
@     
@     written by Gal Ori Steinitz
@     
@     See http://blog.spotty.us for usage instructions
@     
@     Requires prototype.js (see http://prototype.conio.net/)
@     
@     8/13/2006
@     								
************************************************************/

function LaCoAddLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function LaCoAssignBehaviors()
{

	$A(document.getElementsByTagName('div')).each
	(
		function(divTarget)
		{
			var attrLaCoSrc = divTarget.attributes["laco_src"];
			if (attrLaCoSrc)
			{
				var strUrl = attrLaCoSrc.value;
				var ajax = new Ajax.Updater
				(
					{success: divTarget},
					strUrl,
					{method: 'get'}
				);
			}
		}
	);

	$A(document.getElementsByTagName('a')).each
	(
		function(aHref)
		{
			var attrLaCoTarget = aHref.attributes["laco_target"];
			var attrHref = aHref.attributes["href"];
			var attrLaCoMouseOver = aHref.attributes["laco_mouseover"];

			if (attrLaCoTarget && attrHref)
			{
				var strTargetDivName = attrLaCoTarget.value;
				var divTarget = $(strTargetDivName);
				var strUrl = attrHref.value;
				
				if (divTarget)
				{
				
					if (attrLaCoMouseOver && attrLaCoMouseOver.value == "true")
					{
						// the event will happen onmouseover
						aHref.onmouseover = function()
						{
							LaCoUpdateHref(strUrl,strTargetDivName,aHref);
							return false;
						};
						
						aHref.onmouseout = function()
						{
							new Element.hide(divTarget);
							return false;
						};
						
						//disable click
						aHref.onclick = function()
						{
							return false;
						};
					}
					else
					{
						// the event will happen onclick
						aHref.onclick = function()
						{
							LaCoUpdateHref(strUrl,strTargetDivName,aHref);
							return false;
						};
					}
				}
			}
		}
	);
}

function LaCoUpdateHref(strUrl,strTargetDivName,aHref)
{
	// check if the content was already previously loaded (2nd click or 2nd mouseover)
	if (aHref.IsLaCoLoaded != true)
	{
		var ajax = new Ajax.Updater
		(
			{success: strTargetDivName},
			strUrl,
			{method: 'get'}
		);

		aHref.IsLaCoLoaded = true;
	}
	else
	{
		var attrLaCoToggle = aHref.attributes["laco_toggle"];
		if (attrLaCoToggle && attrLaCoToggle.value == "true")
		{
			new Element.toggle(strTargetDivName);
		}
	}
	
	return false;
}

LaCoAddLoadEvent(LaCoAssignBehaviors);