Event.observe(window,'load',
function()
{
	$("viewport").addClassName("is-loaded");
	
	if (Prototype.Browser.IE)
	{
		showNavigation.delay(0);
	}
});

Event.observe(window,"unload",function()
{
	destroyCache();
});

destroyCache = function()
{
	$A(document.getElementsByTagName("*")).each(function(element)
	{
    	if (!element._prototypeEventID) return;
		Event.stopObserving(element);
  });
}

showNavigation = function()
{		
	$("navigation").setStyle
	(
		{
			visibility: "visible"
		}
	);
	$("search").setStyle
	(
		{
			visibility: "visible"
		}
	);
}

// ----------------------------------------------
anchorBehaviour = 
{	
	attachBehaviour: function()
	{
		$$("a[rel]").each(function(anchor)
		{ 
			switch(anchor.rel)
			{
				case 'nofollow':
				
				anchor.onclick = function()
				{
					anchorBehaviour.openWindow(this.href);
					return false;
				}
				
				break;
				
				case 'external':
				
				anchor.writeAttribute("title","Link launches in new window");
				anchor.insert
				(
					{
						'after': '<img src="/images/icons/external-faux.gif" class="external" alt="" />'
					}
				);
				anchor.onclick = function()
				{
					anchorBehaviour.openWindow(this.href);
					return false;
				}

				break;
				
				case 'within-page':
				
				anchor.onclick = function()
				{
					anchorBehaviour.scrollTo(this.href);
					return false;
				}
				break;
			}
		});
		
		$$("a[href$='.pdf'], a[href$='.doc'], a[href$='.xls'], a[href$='.ppt'], a[href$='.zip']").each(function(anchor)
		{
			var hrefParts = anchor.href.split('.');
			var fileType = hrefParts[hrefParts.length -1].toUpperCase();
			
			var titleValue = "Click to download " + fileType;
			
			anchor.writeAttribute("title",titleValue);
			anchor.onclick = function()
			{
				anchorBehaviour.openWindow(this.href);
				return false;
			}
		});
	},
	
	openWindow: function(href)
	{
		var attributes = "scrollbars=yes,toolbar=yes,menubar=yes,status=yes,directories=no,location=yes,resizable=yes";
		var windowOrTab = window.open(href,"",attributes);
		
		windowOrTab.focus();
	},
	
	scrollTo: function(href)
	{
		var modifiedHref = href.substring(href.indexOf('#')+1);
			
		Effect.ScrollTo(modifiedHref,
		{
			//afterFinish:function()
			//{	
				//document.location.hash = modifiedHref;
			//}
		});
	}
}

document.observe("dom:loaded",anchorBehaviour.attachBehaviour);

// ----------------------------------------------
enableSearch = function()
{	
	cleanBackground();
	$("cse-search-box").writeAttribute("target","_blank");
	
	$("txtKeyword").observe
	(
		"blur",function()
		{
			cleanBackground();	
		}
	);
}

cleanBackground = function()
{
	$("txtKeyword").setStyle
	(
		{
			backgroundImage: "none"
		}
	);
}

Event.observe(window,"load",enableSearch);