/**
 *	Ad rotation component of gGAS that allows for the automatic rotation of ads through Ajax
 *	This library assumes swfObject and jQuery are also included
 *	(Aspects of this script are dynamically generated)
 *
 *	@author		gordongroup marketing + communications
 *	@link		http://gordongroup.com
 *	@copyright	gordongroup marketing + communications
 *	@package	com.gordongroup.ggas
 *	@version	2.0
 */
var AdRotation = {
	
	/**
	 *	Holds all the rotation timeouts
	 *	
	 *	@access	public
	 *	@var	array
	 */
	theTimeouts : new Array(),
	
	
	/**
	 *	Preloads the new ad into a hidden element
	 *
	 *	@access	public
	 *	@param	string	p_id
	 *	@param	string	p_zone
	 *	@param	string	p_timeoutId
	 *	@param	int	p_timeoutLength
	 *	@return	void
	 */
	preloadNewAd : function( p_id, p_zone, p_timeoutId, p_timeoutLength ){
		
		clearTimeout( AdRotation.theTimeouts[ p_timeoutId ] );
		
		if( $('#'+p_id+'-preload').length <= 0 ){
			$('body').prepend( '<div id="'+p_id+'-preload" style="display:none;"><!-- --></div>' );
		}
		
		$('#'+p_id+'-preload').load( '/common/ggas/libs/advertisements.php?ajax='+p_zone );
		AdRotation.theTimeouts[ p_timeoutId ] = setTimeout( "AdRotation.swapAds( '"+p_id+"', '"+p_zone+"', "+p_timeoutId+", "+p_timeoutLength+" )", 2000 );
		
	}, // END PROPERTY: preloadNewAd
	
	
	/**
	 *	Swaps the newly preloaded ad with the old ad
	 *
	 *	@access	public
	 *	@param	string	p_id
	 *	@param	string	p_zone
	 *	@param	string	p_timeoutId
	 *	@param	int	p_timeoutLength
	 *	@return	void
	 */
	swapAds : function( p_id, p_zone, p_timeoutId, p_timeoutLength ){
	
		clearTimeout( AdRotation.theTimeouts[ p_timeoutId ] );
		
		$('#'+p_id).html( $('#'+p_id+'-preload').html() );
		
		var timoutLength = p_timeoutLength - 2000;
		AdRotation.theTimeouts[ p_timeoutId ] = setTimeout( "AdRotation.preloadNewAd( '"+p_id+"', '"+p_zone+"', "+p_timeoutId+", "+p_timeoutLength+" )", timoutLength );
		
	} // END PROPERTY: swapAds
	
} // END OBJECT: AdRotation


/**
 *	Initialise the Dom load events and ad rotation timeouts
 */
if( document.getElementById && jQuery ){
	
	$(document).ready(
		function(){
			
			if( document.getElementById( 'ad-banner-en' ) ){
				AdRotation.theTimeouts[0] = setTimeout( "AdRotation.preloadNewAd( 'ad-banner-en', 'banner-en', 0, 9000 )", 9000-2000 );
			}
			
			if( document.getElementById( 'ad-skyscraper-en' ) ){
				AdRotation.theTimeouts[1] = setTimeout( "AdRotation.preloadNewAd( 'ad-skyscraper-en', 'skyscraper-en', 1, 7000 )", 7000-2000 );
			}
			
			if( document.getElementById( 'ad-miniscraper-en' ) ){
				AdRotation.theTimeouts[2] = setTimeout( "AdRotation.preloadNewAd( 'ad-miniscraper-en', 'miniscraper-en', 2, 7000 )", 7000-2000 );
			}
			
			if( document.getElementById( 'ad-banner-fr' ) ){
				AdRotation.theTimeouts[4] = setTimeout( "AdRotation.preloadNewAd( 'ad-banner-fr', 'banner-fr', 4, 9000 )", 9000-2000 );
			}
			
			if( document.getElementById( 'ad-miniscraper-fr' ) ){
				AdRotation.theTimeouts[6] = setTimeout( "AdRotation.preloadNewAd( 'ad-miniscraper-fr', 'miniscraper-fr', 6, 7000 )", 7000-2000 );
			}
			
		}
	);
	
}