var userClicked = false;

jQuery.fn.slowEach = function( interval, callback ) {
	var array = this;
	if( ! array.length ) return;
	var i = 0;
	next();
	function next() {
		if( callback.call( array[i], i, array[i] ) !== false ) 
			if( ++i < array.length )
				setTimeout( next, interval );
	}
};

jQuery.fn.resetActive = function(){
    if (userClicked == true){
       return;
    }
    else{
        $('.playlist .active-item').removeClass('active-item'); // Fix classes
    	$(this).addClass('active-item');
	
    	$('#playlist .active-detail a').attr('href', $('a', this).attr('name')); // Replace h3 a href with a.active-item name
    	$('#playlist .active-detail a').removeAttr('name');
    	$('#playlist .active-detail a').text($('a', this).text());	// Replace content of active detail
		
    	$('#lead-story img').attr('src', $('img', this).attr('src')); // Replace image 
    	$('#lead-story img').attr('alt', $('img', this).attr('alt')); // Replace image alt
    	$('#lead-story p.caption').text($('img', this).attr('alt')); // Make image alt == caption
	
	$('#playlist .active-detail p').text($('p', this).text());
    }
}

function autoMakeMeActive(x){
    if (userClicked == true){
       return;
    }
    else{
		$('.playlist .active-item').removeClass('active-item'); // Fix classes
		$(this).addClass('active-item');
		
		$('#playlist .active-detail a').attr('href', $('a', this).attr('name')); // Replace h3 a href with a.active-item name
		$('#playlist .active-detail a').removeAttr('name');
		$('#playlist .active-detail a').text($('a', this).text());	// Replace content of active detail
			
		$('#lead-story img').attr('src', $('img', this).attr('src')); // Replace image 
		$('#lead-story img').attr('alt', $('img', this).attr('alt')); // Replace image alt
		$('#lead-story p.caption').text($('img', this).attr('alt')); // Make image alt == caption
	
	$('#playlist .active-detail p').text($('p', this).text());
    }
}

function makeMeActive(x){
	$('.playlist .active-item').removeClass('active-item'); // Fix classes
	$(this).addClass('active-item');
	
	$('#playlist .active-detail a').attr('href', $('a', this).attr('name')); // Replace h3 a href with a.active-item name
	$('#playlist .active-detail a').removeAttr('name');
	$('#playlist .active-detail a').text($('a', this).text());	// Replace content of active detail
		
	$('#lead-story img').attr('src', $('img', this).attr('src')); // Replace image 
	$('#lead-story img').attr('alt', $('img', this).attr('alt')); // Replace image alt
	$('#lead-story p.caption').text($('img', this).attr('alt')); // Make image alt == caption
	
	$('#playlist .active-detail p').text($('p', this).text());
}

function setUserClicked(){
    userClicked = true;
}
		
$(function (){  
	$('#playlist li.option').click(makeMeActive);
	$('#playlist li.option').mouseup(setUserClicked);
	$('#playlist li.option').slowEach(5000,autoMakeMeActive);
	setTimeout("$('#playlist li.option:eq(0)').resetActive()",25000);
});
