var current_index = 0;
var timer;

var AUTO_INTERVAL = 5000;
var ANIMATION_TIME = 800;
function start_auto() {
	
	timer = setInterval( function() { next_promo(jQuery("div#frontarticles")) }, AUTO_INTERVAL );
	
}

function stop_auto() {
	if( timer != undefined ) {
		clearInterval( timer );
	}
}

function setPause( el ) {
	if(el.hasClass("play")) {
		el.removeClass("play");
		el.addClass("pause");
	}
}

function setPlay( el ) {
	
	if( el.hasClass("pause")) {
		el.removeClass("pause");
		el.addClass("play");
	}
}

function toggle_state( el ) {
	if(el.hasClass("play")) {
		//pause
		next_promo(jQuery("div#frontarticles"))
		start_auto();
		setPause( el );
	}else if( el.hasClass("pause")){
		stop_auto();		
		setPlay( el );
	}
}

function on_pulse_click() {
	if(!jQuery(this).parent("li").hasClass("active")) {		
		stop_auto();
		setPause( jQuery("div.play_pause") );
		
		var index = jQuery("div.dock ul.navigation li").index(jQuery(this).parent("li"));
		var image_well = jQuery("div#frontarticles");
		clean_up_old_promo( current_index , image_well  );
		make_promo_active( index , image_well );
	}
}

function next_promo( container ) {

	var len = jQuery( "ul.welcome_text li", container).length;
	var new_index = ( current_index >= len-1 ) ? 0:current_index+1
        //alert( new_index );

	clean_up_old_promo( current_index , container , "right" );
	make_promo_active( new_index , container, "left" );
	
	
}

function previous_promo( container ) {
	var len = jQuery( "ul.welcome_text li", container).length;
	var new_index = ( current_index <= 0 ) ? len-1:current_index-1
	
	clean_up_old_promo( current_index , container, "left" );
	make_promo_active( new_index , container , "right");
}

function make_promo_active( index , container , direction ) {
	direction = direction || "left";
	
//	jQuery("#debug").text( index );
	
	var promo = jQuery( "ul.welcome_text li" , container ).eq( index );
	promo.addClass("active");
	promo.css("zIndex", 10 );
 	jQuery( "div.dock ul.navigation li", container ).eq(index).addClass("active");
	//promo.show();
	promo.fadeIn( ANIMATION_TIME );
	current_index = index
	
}

function clean_up_old_promo( index , container, direction ) {
	
	direction = direction || "right";
	var promo = jQuery( "ul.welcome_text li.active" , container );
	promo.css("zIndex", 20 );
//	alert(promo.length);
	//promo.hide();
	promo.fadeOut(ANIMATION_TIME );
	promo.removeClass("active");
	jQuery( "div.dock ul.navigation li:active", container ).removeClass("active");
}
