/*
Blue Shift Inc Website Javascript

Adds functions to event handlers

Copyright Max Johnson 2007
*/

var gEvtNum = 0;

function BSIEventAdd( obj, evtName, fun, def)
{
	//if multiple listeners can be attached //
	if ( obj.addEventListener )
	{
		obj.addEventListener( evtName, function(e){
														// if def false, cancel default actions//
														if( !def )
														{
															if (e.preventDefault){ e.preventDefault(); }
															e.returnValue = false;	
														}
														
														// kill fucntion reference if function handle removed //
														if( !obj[fun] )
														{
															obj.removeEventListener( evtName, arguments.callee, false);
														}
														else
														{
															obj[fun](e);
														}
													}
													, false); 
	}
	else if ( obj.attachEvent )
	{
		obj.attachEvent( "on" + evtName, function(e){
														// if def false, cancel default actions//
														if( !def )
														{
														    if (e.preventDefault){ e.preventDefault(); }
															e.returnValue = false;	
														}
														
														// kill fucntion reference if function handle removed //
														if( !obj[fun] )
														{
															obj.detachEvent( evtName, arguments.callee);				
														}
														else
														{
															obj[fun](e);
														}
													}
													);
	}
	// otherwise get previous functions attached to event and add new one //
	else
	{
		var Prev = obj[ "on" + evtName ];
		if (Prev)
		{
			obj[ "on" + evtName ] = function(e){
													// if def false, cancel default actions//
													if( !def )
													{
													    if (e.preventDefault){ e.preventDefault(); }
														e.returnValue = false;	
													}
													Prev(e);
													obj[fun](e);
												};
		}
		else
		{
			obj[ "on" + evtName ] = obj[fun];
		}
	}
}

function BSIAddEvt( obj, fun, evt, def)
{
	// test for function/event combo in object, if it already exists skip //
	dupTest = obj[ fun + evt ];
	
	if ( !dupTest )
	{
		// this is the property added to the object that will contain the function call //
		functionCall = fun + evt;
		
		// put the function into useable form //
		obj[ functionCall ] = window[fun];
		
		BSIEventAdd( obj, evt, functionCall, def);
		gEvtNum++;
	}
}

function BSIRemoveEvt( obj, fun, evt )
{
	if ( obj[ fun + evt ] )
	{
		obj[ fun + evt ] = null;
	}
}