/* 
-----------------------------------------------------------------
	MOE Corporate
	common.js
	
	Reusable CT functions
	
	Created 21.12.2007 by DS
	Last Updated: See SVN	
	
	Functions:
	- activate_faux_links()
	- emulate_link_hover()
	- do_inline_javascript_test()
	- do_inline_flash_test()
	- add_event()
	- remove_event()
	- add_load_event()
-----------------------------------------------------------------
*/

	function activate_faux_links()
	{
	//	fakin' it ~ updated and compressed by using getElementsByTagName wildcard
		
		var all_elements = document.getElementsByTagName('*'); // BPA P39
		
		for (var i=0; i<all_elements.length; i++)
		{
			if (all_elements[i].className.match('a blur'))
			{
				// Mouse-dependent handlers
				// Because these are not real links...
				add_event(all_elements[i], 'mouseover', emulate_link_hover, false);
				add_event(all_elements[i], 'mouseout', emulate_link_hover, false);	
			}
		}		
	}

	function emulate_link_hover(e)
	{
		// updated ~ compressed
		
		if (!e) 
		{
			var e = window.event; // http://www.quirksmode.org/js/events_properties.html
		}
		
		// 
		
		if (e.target) 
		{
			targ = e.target;
		}
		else if (e.srcElement) 
		{
			targ = e.srcElement;
		}
		if (targ.nodeType == 3) 
		{	
			// defeat Safari bug
			targ = targ.parentNode;
		}
		
		// IE
		
		if (whichbrowser.isIE6up)	
		{
			_this = targ;
		}
		else
		{
			_this = this;	
		}
		
		// interaction behaviours
		
		faux_link_classname = (_this.className ? _this.className : '');	
	
		if (e.type == 'mouseover')
		{					
			if ((faux_link_classname.indexOf('current') == -1) && (faux_link_classname.indexOf('blur') != -1)) 
			{				
				//	If className already contains 'blur' (ie if it has been hovered over already), just change this part of the className
				_this.className = faux_link_classname.replace(/blur/, 'hover');
			}					
		}							
		else if (e.type == 'mouseout')
		{				
			if (faux_link_classname.indexOf('current') == -1)
			{			
				_this.className = faux_link_classname.replace(/hover/, 'blur');
			}		
		}	
	}
	
	function do_inline_javascript_test()
	{	
		if (document.getElementById('inline-js-test'))
		{
			document.getElementById('inline-js-test').innerHTML = 'enabled';
		}
	}
	
	function do_inline_flash_test()
	{
		if (document.getElementById('inline-flash-test'))
		{
			document.getElementById('inline-flash-test').innerHTML = '(you appear to have version <strong>' + global_flash_version + '</strong> installed)';
		}
	}
	
	function add_event(obj, evType, fn, useCapture)
	{
		// http://www.scottandrew.com/weblog/articles/cbs-events
		
		if (obj.addEventListener)
		{
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} 
		else if (obj.attachEvent)
		{
			var r = obj.attachEvent('on' + evType, fn);
			return r;
		} 
		else 
		{
			alert("Handler could not be attached");
		}
	}	
	
	function remove_event(obj, evType, fn, useCapture)
	{
		// http://www.scottandrew.com/weblog/articles/cbs-events		
		
		if (obj.removeEventListener)
		{
			obj.removeEventListener(evType, fn, useCapture);
			return true;
		} 
		else if (obj.detachEvent)
		{
			var r = obj.detachEvent('on' + evType, fn);
			return r;
		} 
		else 
		{
			alert("Handler could not be removed");
		}
	}		
	
	function add_load_event(func)
	{
		// BPA P98
		
		var oldonload = window.onload;
		
		if (typeof window.onload != 'function')
		{
			window.onload = func;	
		}
		else
		{
			window.onload = function()
			{
				if (oldonload)
				{
					oldonload();	
				}
				func();
			}
		}
	}	
	