var header={
	active:null,
	interval:7000,
	duration:500,
	z:1,
	pos:0,
	wait:false
};

$(document).ready(function() {

	$('#spotlight header img').hover(function() {
		$(this).stop().fadeTo('fast',1);
		return false;
	},function() {
		$(this).not('.active').stop().fadeTo('fast',0.5);
		return false;
	}).click(function() {
		if (!$(this).hasClass('active') && !header.wait) {
			var ix=$(this).index()
			header.articles.eq(ix).slideIn(header.articles.filter(':visible'),'right');
		}
	});

	$('#spotlight header img').eq(0).addClass('active').trigger('mouseenter');

	$('.chromeless>a').chromeless({
		width:420,
		height:236
	});


	header.articles=$('#spotlight article').each(function() {
		$(this).find('.image:empty').remove();
	});
	header.active=header.articles.eq(0).css({left:0}).show();



	// slide nav bar
	header.slider=$('#slider');
	for (var i=0;i<header.articles.length;i++) {
		$('<strong></strong>').appendTo(header.slider).click($.selectSlide);
	}

	header.slider.find('strong').css({
		width:header.slider.width()/header.articles.length-2
	}).parent().show();

	header.slider.find('strong').eq(0).addClass('active');



	if (header.articles.length) {
		$('#spotlight').mouseenter(function() {
			$(this).addClass('over').find('header').headerShow();
			clearTimeout(header.timer);
			$('#spotlight>span').stop().width(0);
			return false;
		}).mouseover(function() {
			return false;
		}).mouseleave();

		$(document).mouseover(function() {
			var $t=$('#spotlight');
			$t.removeClass('over').find('header').headerHide();
			if (!$t.find('.chromeless.playing').length) {
				$('#spotlight>span').stop().animate({width:908},header.interval,'linear');
				clearTimeout(header.timer);
				header.timer=setTimeout(function() {$.nextSlide('right');},header.interval);
			}
		});

		$(document).keyup(function(e) {
			if (e.which==37 || e.which==39) {
				clearTimeout(header.timer);
				var dir={37:'left',39:'right'};
				dir=dir[e.which];
				$.nextSlide(dir);
			}
		});

		$('#spotlight a.arrow').click(function() {$.nextSlide($(this).attr('rel')=='prev'?'left':'right');});

	}

});

$.fn.headerShow=function() {
	$(this).stop().animate({top:0},125);
}

$.fn.headerHide=function() {
	$(this).stop().animate({top:-72},125);
}

$.nextSlide=function(direction) {
	if (header.wait) return false;
	var $current=header.active;

	if (direction=='right') {
		$next=$current.next('article');
		if (!$next.length) $next=header.articles.eq(0);
	} else {
		$next=$current.prev('article');
		if (!$next.length) $next=header.articles.eq(-1);
	}


	if ($next==$current) {
		clearTimeout(header.timer);
		return false;
	}

	$next.slideIn($current,direction);
	return true;
}

$.fn.slideIn=function($current,direction) {
	$('#spotlight>span').animate({width:0},1);

	var $next=$(this);
	header.z++;
	header.pos=$next.index();
	header.slider.find('strong').removeClass('active').eq(header.pos-1).addClass('active');

	$('#spotlight header img').eq(header.pos).addClass('active').trigger('mouseenter').siblings().removeClass('active').trigger('mouseleave');

	header.wait=true;
	header.active.animate({
		left:(direction=='left'?1:-1)*960
	},header.duration,function() {
		$(this).hide();
	});

	header.active=$next;
	$next.show().css({zIndex:header.z,left:(direction=='left'?-1:1)*960}).animate({
		left:0
	},header.duration,function() {
		header.wait=false;
		if (!$('#spotlight').hasClass('over')) {
			$('#spotlight>span').width(0).animate({width:958},header.interval,'linear');
			header.timer=setTimeout(function() {$.nextSlide('right');},header.interval);
		}
		$current.hide();

	});
}


$.selectSlide=function() {
	var id=$(this).index();
	if (id!=header.pos-1 && !header.wait) {
		var $current=header.active;
		var $next=header.articles.eq(id);
		$next.slideIn($current,id<(header.pos-1)?'left':'right');
	}
}
