// =============================================================================
// == Filename..........:	base-scripts.js
// ==
// == Used for..........:	Several usefull functions to make life easier
// ==
// == Author...........:	The real Orakel aka Wolverin
// ==
// == Copyright notice.:	This script is copyright protected
// ==                   	(c) 2008 - by T&Y Worldwide Customer Care
// ==                   	You are not allowed to use this script without our
// ==                   	agreement. If you are interested in drop me a line at
// ==                   	RealOrakel@gmx.net
// =============================================================================

// =============================================================================
// == Collect all event registering calls during the page load process. When
// == the page is loaded completely all call will be executed.
// =============================================================================
var cls_RegisterEvents = function( )
{
	this.arrObjectIDs	= new Array();
	this.arrEvents		= new Array();
	this.arrFunctions	= new Array();
	this.nCollectCalls	= -1;

	// ===========================================================================
	// == Public method to collect all eventhandler
	// ===========================================================================
	this.CollectEvent = function( szObjectID, szEvent, vFunction )
	{
		this.nCollectCalls += 1;
		this.arrObjectIDs[this.nCollectCalls] = szObjectID;
		this.arrEvents[this.nCollectCalls] = szEvent;
		this.arrFunctions[this.nCollectCalls] = vFunction;
	}

	// ===========================================================================
	// == Public method to install collected event handlers
	// ===========================================================================
	this.InstallEvents = function()
	{
		while( this.nCollectCalls >= 0 )
		{
			this.RegisterEvent(
				this.arrObjectIDs[this.nCollectCalls],
				this.arrEvents[this.nCollectCalls],
				this.arrFunctions[this.nCollectCalls]
			);
			this.nCollectCalls -= 1;
		}
		this.arrObjectIDs = null;
		this.arrEvents = null;
		this.arrFunctions = null;
	}

	// ===========================================================================
	// == When everything is loaded we will register all collected events
	// ===========================================================================
	this.RegisterEvent = function( szObjectID, szEvent, vFunction )
	{
		// =========================================================================
		// == Check if we can find desired object
		// =========================================================================
		if( document.getElementById( szObjectID ) )
		{
			oObject = document.getElementById( szObjectID );
			if( oObject.attachEvent )
			{
				szEvent = 'on' + szEvent;
				oObject.attachEvent( szEvent, vFunction );
//				oObject.szID = szObjectID;
			}
			else
			{
				oObject.addEventListener( szEvent, vFunction, false );
//				oObject.szID = szObjectID;
			}
		}
	}
}


// =============================================================================
// == This swap version will use preloaded images.
// == Comments:
// == 	Name the Pictures like this:
// == 		picture-1.jpg => If this picture should symbolise inactive part
// == 		picture-2.jpg => This is the MouseOver picture
// == 		picture-3.jpg => If this picture should symbolise active part
// ==
// ==		Call it like this:
// ==			var cSwapImages = new cls_SwapImages();
// ==			cSwapImages.LoadImages( 'img_1', 'images/pic-1-%1.jpg', true );
// ==			cSwapImages.LoadImages( 'img_2', 'images/pic-2-%1.jpg', false);
// ==
// ==		To register necessary event handler you can do this in two ways.
// ==			Place HTML-Code like this:
// ==				<img id="img_1" src="images/pic-1-1.jpg" alt="foo" 
// ==				onMouseOver="cSwapImages.MouseOver( this );"
// ==				onMouseOut="cSwapImages.MouseOut( this );">
// ==			Use a eventhandler registration function to do the stuff
// =============================================================================
var cls_SwapImages = function()
{
	// ===========================================================================
	// == Public method to load the necessary images and attach them to img object
	// ===========================================================================
	this.LoadImages = function( szObjectID, szSource, bActive )
	{
		if( document.getElementById( szObjectID ) )
		{
			nActivePicIndex = ((bActive == 1) ? 3 : 1);
			oObject = document.getElementById( szObjectID );
			oObject.ImageOver = new Image();
			oObject.ImageOver.src = szSource.replace( '%1', '2' );
			oObject.ImageOut = new Image();
			oObject.ImageOut.src = szSource.replace( '%1', nActivePicIndex );

			cRegisterEvents.CollectEvent( szObjectID, 'mouseover', this.MouseOver );
			cRegisterEvents.CollectEvent( szObjectID, 'mouseout', this.MouseOut );

		}
	}

	// ===========================================================================
	// == Public method simulate the mousover effect on images
	// ===========================================================================
	this.MouseOver = function( vParameter )
	{
		if( vParameter['ImageOver'] )
			var oObject = vParameter;
		else
			var oObject = vParameter['target'] ? vParameter['target'] : vParameter['srcElement'];
		oObject.src = oObject['ImageOver'].src;
	}

	// ===========================================================================
	// == Public method to simulate the mouseout effect on images
	// ===========================================================================
	this.MouseOut = function( vParameter )
	{
		if( vParameter['ImageOut'] )
			var oObject = vParameter;
		else
			var oObject = vParameter['target'] ? vParameter['target'] : vParameter['srcElement'];
		oObject.src = oObject['ImageOut'].src;
	}

	// ===========================================================================
	// == Public method simulate the mousover effect on images
	// ===========================================================================
	this.MouseOverStyle = function( vParameter )
	{
		if( vParameter['ImageOver'] )
			var oObject = vParameter;
		else
			var oObject = vParameter['target'] ? vParameter['target'] : vParameter['srcElement'];
		oObject.style.backgroundImage = 'url(' + oObject['ImageOver'].src + ')';
	}

	// ===========================================================================
	// == Public method to simulate the mouseout effect on images
	// ===========================================================================
	this.MouseOutStyle = function( vParameter )
	{
		if( vParameter['ImageOut'] )
			var oObject = vParameter;
		else
			var oObject = vParameter['target'] ? vParameter['target'] : vParameter['srcElement'];
		oObject.style.backgroundImage = 'url(' + oObject['ImageOut'].src + ')';
	}
}

// =============================================================================
// == Create an instance of the event registration class
// =============================================================================
var cRegisterEvents = new cls_RegisterEvents();
var cSwapImages = new cls_SwapImages();

window.onload = function()
{
	cRegisterEvents.InstallEvents();
}

