/*
Sky.Preloader
copyright by MySign AG (http://www.mysign.ch), Dominik Richner
depencies: Prototype 1.6.1
*/

// creating namespace
var Sky;
if ( !Sky ) Sky = {};

Sky.Preloader = Class.create();
Sky.Preloader.Item = Class.create();

Sky.Preloader._REQUIRED_PROTOTYPE = '1.6.1';

Sky.Preloader.Item.TYPEID_IMG = 1;

Sky.Preloader.Item.prototype = {
	initialize: function( uri, opts )
	{
		this.uri = uri;
		this.preloaded = false;
		this.item = null;
		this._checkLoadState = null;
		
		this.options = {
			typeid:				Sky.Preloader.Item.TYPEID_IMG,
			handleAfterLoad:	null
		};
		
		Object.extend( this.options, opts || {} );
	},
	
	isPreloaded: function ()
	{
		return this.preloaded;
	},
	
	_loadItem: function ()
	{
		switch ( this.options.typeid )
		{
			case  Sky.Preloader.Item.TYPEID_IMG:
				this.item = new Image();
				this.item.src = this.uri;
				
				this._checkLoadState = new PeriodicalExecuter( function(){
					if ( this.item.complete )
					{
						this._handleAfterLoad();
						this._checkLoadState.stop();
					}
				}.bind( this ), 1 );
				break;
			default:
				throw( "Sky.Preloader.Item: typeid '" + this.options.typeid
							+ "' is not supported at the moment." );
				break;
		}
	},
	
	_handleAfterLoad: function ()
	{
		this.preloaded = true;
		
		if ( this.options.handleAfterLoad != null )
		{
			this.options.handleAfterLoad( this );
		}
	}
};

Sky.Preloader.prototype = {
	initialize: function( items, opts )
	{
		// checking Sky.PrototypeExtensions
		if( Object.isUndefined( Sky.PrototypeExtensions ) )
		{
			throw( "Sky.Preloader requires the Sky.PrototypeExtensions JavaScript framework" );
		}
		// checking Prototype
		if( !Sky.PrototypeExtensions.isPrototypeLoaded( Sky.Preloader._REQUIRED_PROTOTYPE ) )
		{
			throw( "Sky.Preloader requires the Prototype JavaScript framework >= " + Sky.Preloader._REQUIRED_PROTOTYPE );
		}
		
		this.items = items;
		this.preloaded = false;
		this._checkLoadState = null;
		
		this.options = {
			delay:				1000,
			handleAfterLoad:	null
		};
		
		Object.extend( this.options, opts || {} );
		
		if ( !document.loaded )
		{
			document.observe( 'dom:loaded', function() {
				setTimeout( this._loadItems.bind( this ), this.options.delay );
			}.bind( this ) );
		}
		else
		{
			this._loadItems();
		}
	},
	
	isPreloaded: function ()
	{
		return this.preloaded;
	},
	
	_loadItems: function ()
	{
		if ( this.items != null && this.items.length > 0 )
		{
			this.items.each( function ( item ) {
				item._loadItem();
			} );
		}
		
		this._checkLoadState = new PeriodicalExecuter( function(){
			var allLoaded = true;
			if ( this.items != null && this.items.length > 0 )
			{
				this.items.each( function ( item ) {
					if ( allLoaded && !item.isPreloaded() )
					{
						allLoaded = false;
					}
				} );
			}
			
			if ( allLoaded )
			{
				this.preloaded = true;
				
				if ( this.options.handleAfterLoad != null )
				{
					this.options.handleAfterLoad( this );
				}
				
				this._checkLoadState.stop();
			}
		}.bind( this ), 1 );
	}
};
