
var featured_user_changed = false;
var featured_position = 0;
var featured_offset = 1000;
var featured_total_num = 0;
var timer_delay = 6000;
var timer;

function homepage_slideshow_init(){
	featured_total_num = $('#promotion_stage div.slide').size();
	setupSlides();
	setupSlideshowNav();
}

function setupSlideshowNav() {
	var target = $('#promotion_stage_nav');
	var caption = $('#promotion_stage .slide').eq(0).find('img').attr('alt');
	
	$('#promotion_stage div.slide').each(function(index) {
		target.append('<span>' + (index+1) + '</span>');
		if(index == featured_total_num-1){
			target.append('<div class="caption">caption</div>');
		}
  	});
  	
  	target.children('span').eq(0).addClass('curr');
  	target.find('.caption').text(caption);
  	target.children('span').hover(
  		function(){
  			$(this).css({'cursor' : 'pointer'});
  		},
  		function(){
  		
  		}
  	);
  
  	target.children('span').click(function(){
  		featured_show($(this).index());
		try{ clearInterval(timer)} catch (err){}
  	});
  	
	
	if ($("#promotion_stage .slide").length > 1){
		try{ clearInterval(timer)} catch (err){}
		timer = setInterval('advance_slideshow()', timer_delay);
	}
	
}

function advance_slideshow(){
	/*
	featured_position++;
	if (featured_position > $('#promotion_stage div.slide').size()-1){
		featured_position = 0;
	}
	$('#promotion_stage_nav span:eq('+featured_position+')').trigger('click');
	*/
	featured_next();
}

function setupSlides() {
	$('#promotion_stage div.slide').css({ 'opacity' : 0 }).hide().filter(':eq(0)').show().css({ 'opacity' : 1.0 });
}



/*
function featured(){
	if(!featured_user_changed){
		if((featured_position + 1) == $('#promotion_stage div.slide').size()){
			featured_position = 0;
		}else{featured_position++;}
		featured_show(featured_position);
	}
	setTimeout('featured()', 5000);
}
*/

function featured_next(){
	featured_user_changed = true;
	if((featured_position + 1) == $('#promotion_stage div.slide').size()){
		featured_position = 0;
	}else{featured_position++;}
	featured_show(featured_position);
}
function featured_previous(){
	featured_user_changed = true;
	if(featured_position == 0){
		featured_position = $('#promotion_stage div.slide').size() - 1;
	}else{
		featured_position--;
	}
	featured_show(featured_position);
}
/*
function featured_click(num){
	featured_position = num;
	log(featured_position);
	featured_show(num);
	featured_user_changed = true;
}
*/
function featured_show(num){
	featured_position = num;
	$('#promotion_stage div.slide').not(':eq(' + num + ')').stop(true, false).animate({
		'opacity' : 0
		}, 500, function(){
			$(this).hide();
	});

	$('#promotion_stage div.slide').eq(num).stop(true, false).show().animate({
		'opacity' : 1.0
	}, 500);
	
	var caption = $('#promotion_stage div.slide').eq(num).find('img').attr('alt');

 	$('#promotion_stage_nav span').removeClass('curr').filter(':eq(' + num + ')').addClass('curr');
 	$('#promotion_stage_nav .caption').text(caption);

}

