/**
 * Creative Networks
 */

// Tabs
(function() {
	
	var fx, pages = [], currentPage = 0;
	
	window.addEvent('domready', function() {
		fx = new Fx.Tween('tab_overlay', {duration:400, transition: Fx.Transitions.Quint.easeOut});
		$('tabbed').set('tween', {transition: Fx.Transitions.Quint.easeOut});
		pages = $('tabbed').getElements('.page');
		
		$$('#nav a[rel^=tab]').each(function(a) {
			var img = a.getElement('img');
			var index = a.rel.match(/^tab\[(-?\d)\]/i)[1];
			
			a.addEvents({
				'mouseover': img.fade.pass(0, img),
				'mouseleave': img.fade.pass(1, img)
			});
			
			if (index >= 0) {
				a.addEvent('click', function(ev) {
					ev.stop();
					show_page(index);
					$('nav').getElement('.active').removeClass('active');
					a.getParent('li').addClass('active');
				});
			}
		});
		
		show_page(0);
	});
	
	function show_page(index) {
		fx.cancel();
		
		var current = pages[currentPage].removeClass('active');
		var next = pages[index].addClass('active');
		
		fx.start('opacity', 1).chain(function () {
			current.setStyle('display','none');
			next.setStyle('display','block');
			$('tabbed').tween('height', 30+next.getStyle('height').toInt());
			(function () {fx.start('opacity', 0);}).delay(500);
		});
		
		currentPage = index;
	}
	
})();

// Videos
(function() {
	
	var scroller = '', current = 0, count = 0;
	
	window.addEvent('domready', function() {
		scroller = $('videos').getElement('ul');
		count = scroller.getElements('li').length;
		
		scroller.setStyle('width', count * 450);
		scroller.set('tween', {duration:800, transition:Fx.Transitions.Quint.easeOut});
		
		$$('#videos .button').each(function(a) {
			var img = a.getElement('img');
			var adj = a.rel.match(/^video\[([+-]?\d)\]$/i)[1];
			
			a.addEvents({
				'mouseover': img.fade.pass(0, img),
				'mouseleave': img.fade.pass(1, img),
				'click': function(ev) {
					ev.stop();
					if (adj == '+1') {if (current < count-1) {current += 1; scroll();}}
					if (adj == '-1') {if (current > 0) {current -= 1; scroll();}}
				}
			});
		});
	});
	
	function scroll() {
		scroller.tween('margin-left', current * -450);
	}
	
})();

// Testimonials
(function() {
	
	var testimonials = [], current = 0, count = 0;
	
	function loop() {
		var next = (current == (count-1))? 0 : current + 1;
		
		testimonials[current].fade(0);
		testimonials[next].fade(1);
		
		current = next;
		
		loop.delay(12000);
	}
	
	window.addEvent('domready', function() {
		testimonials = $('testimonials').getElements('li');
		count = testimonials.length;
		
		testimonials.each(function(li, index) {
			if (index > 0) {li.setStyle('opacity', 0);}
		});
		
		loop.delay(12000);
	});
	
})();
