$(document).ready(function() {
	var source 	= 'all';
	var s1 		= ['1','4','7'];
	var s2 		= ['2','5','8'];
	var s3 		= ['3','6','9'];
	var delay	= 10000; // # of Seconds * 1000
	
	init();
	
	// Initialize
	function init() {
		$('div.slide').each(function() {
			var slide 	= this.id;
			//var cur		= getCurrent(slide);
			var cur		= 0;
			
			// Init play buttons
			$('div.controls a.play').each(function(i) {
				if ($(this).html() == 'Play') toggleButton($(this));
			});

			fetch(slide, cur); // Initial pulling of data
			
			initTimer(slide); // Start timed pulling of data
		});
	}
	
	// Button control
	$('div.controls a').click(function(event) {
		event.preventDefault();
		var info 	= this.id.split('-');
		var slide	= info[0];
		var action	= info[1];
		var cur 	= getCurrent(slide);
		//var play 	= $('a.play','#'+slide);
		
		switch (action) {
		  case 'back' :
		    cur = (cur == 0) ? 2 : cur - 1;
			fetch(slide, cur);
			//initTimer(slide);
			//if (play.html() == 'Play') toggleButton(play);
		    break;
		  case 'forward' :
		    cur = (cur == 2) ? 0 : cur + 1;
			fetch(slide, cur);
			//initTimer(slide);
			//if (play.html() == 'Play') toggleButton(play);
		    break;
		  default : // Play/Pause
			toggleButton($(this));
			if ($(this).html() == 'Play') {
				$('#'+slide).stopTime(slide);
			} else {
				initTimer(slide);
			}
		}
	});
	
	// Category selection
	$('#cats a').click(function(event) {
		event.preventDefault();
		source = this.id.split('-')[1];
		$('#cats a').each(function() {
			$(this).removeClass('current');
		});
		$(this).addClass('current');
		init();
	});
	
	$('#promotop').hover(function() {
		$('#s1 div.promo-content h4').children('a').eq(0).css({color:'#999'});
		$(this).css({cursor:'pointer'});
	}, function() {                                                        
		$('#s1 div.promo-content h4').children('a').eq(0).css({color:'#f60'});
		$(this).css({cursor:'default'});
	});
	
	$('#promotop').click(function(event) {
		var href = $('#s1 div.promo-content').children('a').eq(0).attr('href');
		window.location = href;
	});
	
	// Initialize slider timer
	function initTimer(slide) {
		$('#'+slide).stopTime(slide).everyTime(delay, slide, function() {
			cur = getCurrent(slide);
			cur = (cur == 2) ? 0 : cur + 1;
			fetch(slide, cur);
		});
	}
	
	// Content puller
	function fetch(slide, cur) {
		var img = $('img','#'+slide+' div.dots')[0];
		var str = $('#'+source+'-'+eval(slide+'['+cur+']')).html();
		
		$('div.promo-content','#'+slide).fadeOut('fast', function() {
			$(this).empty().append(str);
		}).fadeIn();
		
		img.src = '/.img/promo/dot-'+cur+'.gif';
	}
	
	// Get current story index
	function getCurrent(slide) {
		var img = $('img','#'+slide+' div.dots')[0];
		return Number(img.src.match(/dot-([0-2])/g)[0].split('-')[1]);
	}
	
	// Toggle play/pause button
	function toggleButton(button) {
		if (button.html() == 'Play') {
			button.css('backgroundImage','url(/.img/promo/pause.gif)').html('Pause');
		} else {
			button.css('backgroundImage','url(/.img/promo/play.gif)').html('Play');
		}
	}
	
	// Homepage article controls
	// $('#headline-controls a').click(function(event) {
// 		$('#headline-controls a').each(function(i) {
// 			$(this).removeClass('selected');
// 		});
// 		
// 		$(this).addClass('selected');
// 		
// 		if (this.id != '') {
// 			event.preventDefault();
// 			
// 			$('#news-results').fadeTo('fast', 0).load('/index.php/ilounge2/news-'+this.id, function() {
// 				$(this).fadeTo('fast', 1);
// 			});
// 		}
// 	});
});