$( document ).ready(function(){
	
	$('.tabs').each(function(){
		var i=0;
		var links = $(this).find('li a');
		links.each(function(){
			this._index = i;
			i++;
		});
	});
	
	$('.tabs a').click(function(){
		
		var index = this._index;
		
		var ul = $(this).parent().parent();
		ul.find('a').removeClass('active');
		
		var container = ul.parent().parent().parent();
		
		container.find('.tab-content').hide();
		container.find('.tab-content:eq('+index+')').show();
		
		$(this).addClass('active');
		
		return false;
	});
	
	
	/*
	$('.nav a').click(function(){ return false; })
	*/
	
	start_slides('#slides .slide', 4000, function(i){
		$('#featured-cnt .nav').find('a').removeClass('active');
		$('#featured-cnt .nav').find('a:eq('+i+')').addClass('active');
		//alert(i);
	});
});

function start_slides( item, time, callback ) {
	var all_items = $( item ).length;
	if ( all_items < 1 ) return;
	var zindex = all_items;
	var i=0;
	$( item ).each( function(){
		$( this ).css( { 'z-index': zindex } );
		if ( i > 0 ) $( this ).hide();
		zindex--;
		i++;
		var img = new Image();
		img.src = $( this ).attr('src');
	});
	
	var index = 0;
	var next;
	var time = time || 5000;
	var callback = callback || function(){};
	
	var interval = window.setInterval( function(){
		if ( index == all_items ) index = 0;
		next = index + 1;
		
		if ( next == all_items ) next = 0;
		
		$( item + ":eq(" + index + ")" ).fadeOut(1200);
		$( item + ":eq(" + next + ")" ).fadeIn(1200, function(){
			callback( next );
		});
		
		index++;
	}, time);
}
