/**
*
* @package ORANGEfps
* @author $Author: moltendorf@gmail.com $
* @version $Id: hook_orangefps.php 23 2009-11-08 03:46:19Z moltendorf@gmail.com $
* @latest $URL: https://free1.projectlocker.com/orangefps/svn/trunk/community/includes/hooks/hook_orangefps.php $
* @copyright (c) 2009 ORANGEfps
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

( function ( $ )
{
	/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
	// Name: slideshow.
	// Type: Singleton.
	// Title: ORANGEfps Slideshow Controls.
	// Description: Control the slideshow with this object.
	// Variables: viewport, size
	//	1. viewport: The CSS selector to find the viewport within the document.
	// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
	var slideshow = function ( viewport )
	{
		// Link elements to this object.
		this.viewport = $ ( viewport );

		return true;
	};

	// Prototype it!
	slideshow.prototype = {
		// jQuery!
		$: $,

		// Timers.
		delay: 3000,
		speed: 1250,

		// Viewport.
		viewport: undefined,

		// Switches.
		timeout: false,
		visible: false,
		type: 'top',

		// Counters.
		current: -1,
		location: 0,

		// Images.
		images: [ ],
		objects: [ ],
		preload: [ ],

		click: function ( error )
		{
			// Bind jQuery to $.
			var $ = this.$, current, image;

			/* Not used.
				if ( this.location >= ( this.images.length - 1 ) )
				{
					this.location = -1;
				}

				if ( this.location >= ( this.objects.length - 1 ) )
				{
					this.timeout = false;

					return;
				}

				this.timeout = true;

				++this.location;
			*/

			if ( error )
			{
				// Remove the broken image.
				this.images.splice ( this.location, 1 );

				if ( this.current > this.location )
				{
					--this.current;
				}
			}
			else
			{
				this.current = this.location;
			}

			this.location = Math.round ( Math.random ( ) * ( this.images.length - 1 ) );

			if ( this.location == this.current )
			{
				++this.location;

				if ( this.location >= this.images.length )
				{
					this.location = 0;
				}
			}

			//$ ( this.viewport ).attr ( 'src', this.images[ this.location ] );
			image = document.createElement ( 'img' );

			$ ( '.' + this.type + ' > img', this.viewport ).replaceWith ( image );

			$ ( image ).load ( function ( )
			{
				window.slideshow.fade ( this );
			} );

			$ ( image ).error ( function ( )
			{
				window.slideshow.click ( true );
			} );

			$ ( image ).hide ( );

			$ ( image ).attr ( 'src', this.images[ this.location ] );
		},

		initiate: function ( images )
		{
			var i;

			// Copy the array, instead of passing it by reference.
			for ( i in images )
			{
				this.images.push ( images[ i ] );
				this.preload.push ( images[ i ] );
			}

			this.click ( );
			//this.loaded ( );
		},

		loaded: function ( item )
		{
			// Bind jQuery to $.
			var $ = this.$, object;

			if ( item === false )
			{
				// Remove the broken image.
				this.images.splice ( ( this.images.length - this.preload.length - 1 ), 1 );
			}
			else if ( item )
			{
				this.objects.push ( true );
			}

			if ( this.preload.length <= 0 )
			{
				return;
			}

			object = new Image ( );

			// Create the load callback.
			$ ( object ).load ( function ( )
			{
				window.slideshow.loaded ( this );
			} );

			// Create the error callback.
			$ ( object ).error ( function ( )
			{
				window.slideshow.loaded ( false );
			} );

			// Set the src for it.
			$ ( object ).attr ( 'src', this.preload.shift ( ) );

			if ( ! this.timeout )
			{
				this.timeout = true;

				setTimeout ( function ( )
				{
					window.slideshow.click ( );
				}, this.delay );
			}
		},

		fade: function ( item )
		{
			// Bind jQuery to $.
			var $ = this.$, call;

			call = function ( )
			{
				setTimeout ( function ( )
				{
					window.slideshow.click ( );
				}, window.slideshow.delay );
			}

			if ( this.type == 'top' )
			{
				$ ( item ).fadeIn ( this.speed, call );

				this.type = 'bottom';
			}
			else
			{
				$ ( item ).show ( );
				$ ( '.top > img', this.viewport ).fadeOut ( this.speed, call );

				this.type = 'top';
			}
		}
	};

	$ ( document ).ready ( function ( )
	{
		window.slideshow = new slideshow ( '#slideshow' );

		var containers = $ ( '.front-ad' );

		$ ( containers [ 0 ] ).addClass ( 'front-ad-load' );
		$ ( containers [ 0 ] ).append ( '<iframe src="./community/ads.html"></iframe>' );
	} );
} ) ( jQuery );
