// -----------------------------------------------------------------------------------------------
//
//	Copyright 2009, Orbital Guidance Industries, Ltd. (orbitalguidance.com), All rights reserved.
//
//	This work is provided under a royalty-free, non-exclusive license in perpetuity to
//	duncan pollard photography (duncanpollard.com).
//
// -----------------------------------------------------------------------------------------------

var OGIsplash = new Class(
{
	options:
	{
		datafile: null,
		images: []
	},
	
	initialize: function(options)
	{
		this.setOptions(options);
		this.loadManifest();
	},
	
	redirect: function()
	{
		window.location = 'portfolio';
	},
	
	loadManifest: function()
	{
		this.newMoo = !MooTools.version.contains('1.1');
		this.curLength = this.curIndex = this.loadIndex = 0;
		this.loadOrder = [];
		this.loadComplete = [];

		var request = null;
		
		if (this.newMoo)
		{
			request = new Request.JSON({ url: this.options.datafile, onComplete: function(response) { this.gotJSON(response); }.bind(this) }).send();
		}
		else
		{
			request = new Json.Remote(this.options.datafile, { method: 'get', onComplete: function(response) { this.gotJSON(response); }.bind(this) }).send();
		}
	},
	
	gotJSON: function(response)
	{
		this.curLength = response.manifest.length;
		
		var	i = 0;
		
		while (i < this.curLength)
		{
			this.options.images[i] = response.manifest[i].image;
			
			var	j = ((i / 2) | 0);
			
			this.loadOrder[i] = ((i % 2) ? (j + 1) : (this.curLength - j));
			this.loadComplete[i++] = false;
		}
		
		this.loadOrder[0] = 0;
		
		this.loadImages();
	},
	
	loadImages: function()
	{
		Asset.image(this.options.images[this.loadOrder[this.loadIndex]], { onload: function()
		{
			this.loadComplete[this.loadOrder[this.loadIndex]] = true;
			
			if (++this.loadIndex < this.curLength)
			{
				this.loadImages();
			}
			else
			{
				this.redirect.delay(1500, this);
			}
		}.bind(this) });
	}
});

OGIsplash.implement(new Options, new Events);
