initGradients();
initParallax();

function initFades() {
	var time = 500;
	
	$(function() {
		$('#nav .hover').css({
			'display': 'block',
			'opacity': 0
		});
		
		$('#nav .item').hover(function() {
			$('.hover', this).stop().animate({
				'opacity': 1
			}, time);
		}, function() {
			$('.hover', this).stop().animate({
				'opacity': 0
			}, time);
			;
		});
	});
}

function initGradients() {
	$(function() {
		$('#rainbow').each(function() {
			var el = this;
			// Parse the inputs
			var from = '#ffffff', to = '#000000';
			var
				fR = parseInt(from.substring(1, 3), 16),
				fG = parseInt(from.substring(3, 5), 16),
				fB = parseInt(from.substring(5, 7), 16),
				tR = parseInt(to.substring(1, 3), 16),
				tG = parseInt(to.substring(3, 5), 16),
				tB = parseInt(to.substring(5, 7), 16);

			
			var h = $(this).height() * 1.2;
			var html;
			
			if (this.initHTML)
				html = this.initHTML;
			else
				html = this.innerHTML;
			
			this.initHTML = html;
			this.innerHTML = '';
			
			for (var i = 0; i < h; i++) {
				var c = '#' +
					(Math.floor(fR * (h - i) / h + tR * (i / h))).toString(16) +
					(Math.floor(fG * (h - i) / h + tG * (i / h))).toString(16) +
					(Math.floor(fB * (h - i) / h + tB * (i / h))).toString(16);

				$('<span class="rainbow rainbow-' + i + '" style="color: ' + c + ';"><span style="top: ' + (-i - 1) + 'px;">' + html + '</span></span>').appendTo(this);
			}
			
			$('<span class="dolly">' + html + '</span>').appendTo(this);

		});
		
	});
}

function d(str) {
	//return;
	$('#d').append('<p>' + str + '</p>');
}

function initParallax() {
	var time = 50000;
	var stage = 800;
	var offset = 50;
	
	var parallax = function(o) {
		var el = o.el;
		var prop = o.prop;
		var pulsate = o.pulsate ? o.pulsate : false;
		var fadein = o.fadein ? o.fadein : false;
		var sine = o.sine ? o.sine : false;
		var sinepx = o.sinepx ? o.sinepx : false;
		var loop = o.loop ? o.loop : false;
		var delay = o.delay ? o.delay : 0;
		var movedelay = o.movedelay ? o.movedelay : 0;
		
		var f = function() {
			var z = $(el).css('z-index');
			var w = $(el).width() / 1;
			var pos =  -w /3  - (z - 1) * offset;
			
			// Image CSS
			$(el).css('display', 'block');
			
			
			if (sine)
				$(el).wrap('<div class="sine" style="position: relative; z-index: ' + z + ';"></div>');

			if (pulsate)
				$(el).wrap('<div class="pulsate" style="top: position: relative; z-index: ' + z + ';"></div>');

			switch(true) {
				case sine:
					$(el).parents('.sine').wrap('<div class="img-wrap"></div>');
					break;
					
				case pulsate:
					$(el).parents('.pulsate').wrap('<div class="img-wrap"></div>');
					break;
										
				default:
					$(el).wrap('<div class="img-wrap"></div>');
					break;
			}
			
			eval('var css = { "' + prop + '": "' + pos + 'px" }');
			$(el).parents('.img-wrap').css(css);
				
			$(el).parents('.pulsate').css('opacity', '0').animate({ opacity: 1 }, time / 20);

			setTimeout(function() {
				for(var i = 0; i < 10; i++) {
					$(el).parents('.pulsate')
						.animate({ opacity: 1 }, time / 20)
						.animate({ opacity: 0.6 }, time / 20);
					
					$(el).parents('.sine')
						.animate({ 'top': '+=' + sinepx + 'px' }, time / 20)
						.animate({ 'top': '-=' + sinepx + 'px' }, time / 20);
				}
			}, delay);
			
			// Movement animation
			eval('var anim = { "' + prop + '": "' + (stage + pos * -2) + 'px" }');			
			
			setTimeout(function() {
			
				$(el).animate(anim, time, function() {
					var clone = $(this).clone()[0];
					
					$(this).parents('.img-wrap').remove();
					
					
					$('#stage').append(clone);
					
					o.el = $('#' + clone.id)[0];
					
					if (loop) parallax(o);
				});
			}, movedelay);
			
		};
		
		if ($(el).width() > 0)
			f(el, prop);
		else
			$(el).load(function() {
				f(el, prop);
			});
	
	};
	
	
	$(function() {
		$('.blue').each(function() {
			parallax({
				el: this,
				prop: 'right',
				pulsate: false,
				sine: true,
				sinepx: 10,
				loop: true,
				delay: Math.random() * 700 + 200,
				movedelay: 0
			});
			
		});
		
		//return;
		
	})

	$(function() {
		$('.green').each(function() {
			parallax({
				el: this,
				prop: 'right',
				pulsate: false,
				sine: false,
				sinepx: 10,
				loop: true,
				delay: Math.random() * 700 + 300,
				movedelay: 0
			});
			
		});
		
		//return;

	})

	$(function() {
		$('.orange').each(function() {
			parallax({
				el: this,
				prop: 'left',
				pulsate: false,
				sine: true,
				sinepx: 10,
				loop: true,
				delay: Math.random() * 500 + 300,
				movedelay: 0
			});
			
		});
		
		//return;
		
		
		
	})
	
}
