document.observe ('dom:loaded', function () 
{
	// Setup feature image switch
	switchFeatureImage.delay (10);
	
	// Fetch provider logos
	fetchProviderLogos();
});

function switchFeatureImage ()
{
	// Get the feature that is currently hidden
	var current = $$('.home-feature-image').find (function (feature_image) { return feature_image.visible(); });
	var target = $$('.home-feature-image').find (function (feature_image) { return !feature_image.visible(); });
	current.setStyle({ zIndex:-1 });
	target.setStyle ({ zIndex:0 });
	
	target.appear({ duration:1, afterFinish:function () { this.hide() }.bind (current) });
	
	switchFeatureImage.delay (10);
}

function fetchProviderLogos ()
{
	// Get the data
	new Ajax.Request(broadcast.public_url() + '/ajax/generate-our-providers', 
	{
		onSuccess: function (transport)
		{
			var file_stack = transport.responseJSON;
			
			var array = file_stack;
			for(var j, x, i = array.length; i; j = parseInt(Math.random() * i), x = array[--i], array[i] = array[j], array[j] = x);	//Fisher-Yates shuffle algorithm (jsfromhell.com/array/shuffle)
			data_array = array;

			var providers = data_array.collect(function(data)
			{
				var container = new Element('div').setStyle({width:'90px', height:'30px', display:'none'});
				var link_tag = new Element('a', { 'href':data.uri }).addClassName('provider-logo');
				link_tag.insert(new Element('img', { 'src':data.image_uri, 'alt':data.name }).setStyle({width:((data.ratio >= 3)? 90: 30*data.ratio)+'px', height:((data.ratio < 3)? 30: 90/data.ratio)+'px'}));
				container.insert(link_tag);
				return container;
			});
			var targets = $('main').down('.provider-logos').select('.logo-target');
			
			if(providers.length > 0) 
			{
				targets.first().insert(providers[0]);
				providers[0].appear({ duration:1, queue:{ scope:'providers:logo_panel', position:'end' } });
			}
			if(providers.length > 1) 
			{
				targets.last().insert(providers[1]);
				providers[1].appear({ duration:1, queue:{ scope:'providers:logo_panel', position:'end' } });
			}
			
			switchProviderLogo.delay(8, providers);
		}
	});
}

function delaySwitch(element_array)
{
	switchProviderLogo.delay(5, element_array);
}

function switchProviderLogo (element_array)
{
	try{
	var _transition_delay = 10; _effect_duration = 1, _effect_scope = 'providers:logo_panel';
	var targets = $('main').select('.logo-target');
	// Switch the first image
	var queued_element = (element_array.indexOf(targets.last().down()) + 1 < element_array.length)? element_array[element_array.indexOf(targets.last().down()) + 1]: element_array[0];
	targets.first().down().fade({ duration:_effect_duration, queue:{ scope:_effect_scope, position:'end' }, afterFinish:newProviderLogo.curry(targets.first(), queued_element, element_array, false) });

	// Switch the second image
	var queued_element_2 = (element_array.indexOf(queued_element) + 1 < element_array.length)? element_array[element_array.indexOf(queued_element) + 1]: element_array[0];
	targets.last().down().fade({ duration:_effect_duration, queue:{ scope:_effect_scope, position:'end' }, afterFinish:newProviderLogo.curry(targets.last(), queued_element_2, element_array, true) });
	} catch(e) {broadcast.log(e);}
}

function newProviderLogo(target, element, element_array, is_last)
{
	target.down().remove();
	target.insert(element);
	
	if(!is_last) element.appear({ duration:_effect_duration, queue:{ scope:_effect_scope, position:'end' } });
	else element.appear({ duration:_effect_duration, queue:{ scope:_effect_scope, position:'end' }, afterFinish:delaySwitch.curry(element_array) });
}
