
function dot_offset(x, y, op)
{
	x -= 20; y -= 22;
	if(Browser.Engine.trident)
	{
		return { 'left': x, 'top': y/* , 'opacity': op */};
	} else {
		return { 'left': x, 'top': y , 'opacity': op};
	}
}

var keyframes = $A(
[
	[14, 546, 10], [52, 475, 20], [108, 414, 40], [172, 366, 60], [246, 324, 80], 
	[333, 287, 100], [410, 265, 100], 
	[490, 245, 100], [571, 236, 100],
	[648, 230, 100], [730, 229, 100],
	[805, 232, 100]
]);

var keyframes_per_dot = 2;
var keyframes_intro = 5;

var menu_a;
var logo_li;
var logo_a;
var copy;

var top_pictures;
var tp_top = 0;
var tp_bottom = 13;

var slide_pictures = function() {
	top_pictures.each( function(item) {
		var r = Math.random();
		if(r < 0.2) {
			item.tween('top',tp_top);
		} else if(r < 0.4) {
			item.tween('top',tp_bottom);
		}
	} );
};

var init_picture_slides = function() {
	top_pictures = $$('#picture_strip img');
	top_pictures.set('tween', {
					 fps: 40,
					 duration: 1000
					 });	
};

var init_menu_fadeins = function() {
	menu_a = $$('#menu_list li a');
	menu_a.setStyle('opacity', 0);

	copy = $('copy');
	copy.setStyle('opacity', 0);
	
	menu_a.each( function(item) {
		var orig_top = item.getPosition().y;
		var new_top = orig_top + (orig_top < 200 ? -2 : 2);
		item.store('orig_top', orig_top);
		item.store('mo_top', new_top);
		
		item.set('tween', {
					 fps: 40,
					 duration: 100,
					 transition: 'linear'
					 });
		
		item.addEvents({
			'mouseenter': function() {
				this.tween('top', this.retrieve('mo_top'));
			},
			'mouseleave': function() {
				this.tween('top', this.retrieve('orig_top'));
			}
		});									  
	});
};

var init_logo_mouseovers = function() {
	logo_li = $$('#logo_list li', '#logo_zncs');
	logo_a = $$('#logo_list li a');
	
	logo_li.setStyle('opacity', 0);
	
	var tween_options = {
		fps: 40,
		duration: 150,
		transition: 'sine:out'
	};
	
	logo_a.each( function(item) {
		var parent = item.getParent();
		item.set('tween', tween_options);		
		parent.set('tween', tween_options);
		
		item.addEvents({
			'mouseenter': function() {				
				this.tween('top', -8);
				this.getParent().tween('top', 2);
				},
			'mouseleave': function() {
				this.tween('top', 0);
				this.getParent().tween('top', 0);
				}
		});
	});

};

var start_dot_animation = function() {
	var dots = $A( [$('menu_dot_1'), $('menu_dot_2'), $('menu_dot_3'), $('menu_dot_4')] );	
	dots.each(function(item, index)
	   {
			var slideFX = item.retrieve('slideFX');
			for(i = 0; i < (dots.length - index); ++i)
			{
			   slideFX.start( dot_offset(0,615,0) );
			   slideFX.start( dot_offset(0,615,0) );
			   slideFX.start( dot_offset(0,615,0) );
			}
			
			keyframes.each(function(kf, kfindex)
				{
					if(kfindex <= (index * keyframes_per_dot) + keyframes_intro)
					{
						slideFX.start( dot_offset(kf[0], kf[1], kf[2]));
					}
			} );
		}
	);

};

var init_dot_animation = function() {	
	var dots = $A( [$('menu_dot_1'), $('menu_dot_2'), $('menu_dot_3'), $('menu_dot_4')] );	
	dots.each(function(item, index)
	   {
		   var slideFX = new Fx.Morph( item,
				{
					fps: 40,
					unit: 'px',
					link: 'chain',
					duration: 100,
					transition: 'linear'
				} ); 
		   
		   slideFX.set(dot_offset(0,615,0));
		   
		   item.store('slideFX', slideFX);
		   
		}
	);
	
	var menu_options =
		{
			fps: 40,
			unit: 'px',
			link: 'ignore',
			duration: 50,
			transition: 'linear'
		}
		
	var do_fadein = function(el, delay) {
		fadeFX = new Fx.Tween(el.getElement('a'), menu_options);
		el.store('fadeFX', fadeFX);
		return function() {
			el.retrieve('fadeFX').start('opacity',1);
		}
	}
	
	$('menu_dot_1').retrieve('slideFX').addEvent('chainComplete', do_fadein($('menu_partnerships')) );
	$('menu_dot_1').retrieve('slideFX').addEvent('chainComplete', function() { logo_li.tween('opacity', 1); copy.tween('opacity', 1); } );
	$('menu_dot_1').retrieve('slideFX').addEvent('chainComplete', function() { slide_pictures.periodical(1500);	} );
	
	$('menu_dot_2').retrieve('slideFX').addEvent('chainComplete', do_fadein($('menu_education')) );
		
	$('menu_dot_3').retrieve('slideFX').addEvent('chainComplete', do_fadein($('menu_sustainability')) );
	$('menu_dot_3').retrieve('slideFX').addEvent('chainComplete', do_fadein($('menu_collaboration')) );
	
	$('menu_dot_4').retrieve('slideFX').addEvent('chainComplete', do_fadein($('menu_contact')) );
	$('menu_dot_4').retrieve('slideFX').addEvent('chainComplete', do_fadein($('menu_brochure')) );
	$('menu_dot_4').retrieve('slideFX').addEvent('chainComplete', do_fadein($('menu_resources')) );
	
	
	
	
};

window.addEvent('domready', init_dot_animation);
window.addEvent('domready', init_menu_fadeins);
window.addEvent('domready', init_logo_mouseovers);
window.addEvent('domready', init_picture_slides);
window.addEvent('load', start_dot_animation);