// VARS

var FADING_SPEED = 2000;
var INTERVAL = 20000;

var id = undefined;
var previousItem = undefined;
var nextItem = undefined;
var totalItems = undefined;

// FUNCTIONS

function setBackground( id ) {

//	console.log( "TEST = " + id + " - " + previousItem + " - " + nextItem + " - " + totalItems );
	
	/*  */
	
	nextItem = id;

	/*  */
	
	updateBackground();

}

function setBackgroundNavigation( id ) {
	
	if( $("#background_navigation ul li").length > 0 ) {
		
		$("#background_navigation ul li").attr("class", "inactive");
		
		//
		
		$("#background_navigation ul li:eq(" + id + ")").attr("class", "active");

	}

}

function updateBackground() {

	$(document).stopTime("updateBackground");
	
	/*  */
	
	$("#background ul li:eq(" + previousItem + ")").delay(FADING_SPEED / 2).fadeOut(FADING_SPEED);
		
	$("#background ul li:eq(" + nextItem + ")").fadeIn(FADING_SPEED);
	
	/*  */
	
	setBackgroundNavigation( nextItem );
	
	/*  */
	
	if( nextItem < totalItems ) {
				
		nextItem += 1;
		previousItem = nextItem - 1;
			
	} else {
	
		nextItem = 0;
		previousItem = totalItems;
	
	}
	
	/*  */
	
//	console.log( "TEST = " + previousItem + " - " + nextItem + " - " + totalItems );
//	console.log( $("#background ul li:eq(" + nextItem + ")") );
//	console.log( $("#background ul li:eq(" + previousItem + ")") );
	
	/*  */
	
	$(document).oneTime(INTERVAL, "updateBackground", function() {updateBackground();} );

}

/* INIT */

$(document).ready( function() {

	

} );

$(document).ready( function() {

	if( $("#background ul li").length > 0 ) {
	
		totalItems = $("#background ul li").length - 1;
		
	}
	
	/*  */
	
	if( $("#background_navigation ul li").length > 0 ) {
		
		$("#background_navigation ul li a").click( function() {
			
			id = parseInt( $(this).attr("rel") ) - 1;
			
			//
			
			setBackground( id );
			
			setBackgroundNavigation( id );
		
		} );

	}
	
	/*  */
	
	setBackground( 0 );
	
	setBackgroundNavigation( 0 );
	
} );

$(window).resize( function() {

	
	
} );
