var t;

function switchTo(idx) {
	window.clearTimeout(t);
	var navFeatureLIs = $('#nav-feature > li');
	var idxListItem = navFeatureLIs.eq(idx);
	var curListItem = idxListItem.siblings('li.current');
	idxListItem.children('img').stop().fadeIn(750);
	curListItem.children('img').stop().fadeOut(750);
	curListItem.removeClass('current');
	var pos = idxListItem.position();
	var theHeight = idxListItem.height();
	var btm = theHeight + pos.top;
	$('#tab-active').stop().animate({'top': pos.top,'height':theHeight}, 750);
	$('#nav-feature2').stop().animate({'clip': 'rect('+pos.top+'px 550px '+btm+'px 0px);'}, 750);
	idxListItem.addClass('current');
	idx = idx + 1;
	var nextIdx = (idx >= navFeatureLIs.length) ? 0 : idx;
	t = window.setTimeout(function() { switchTo(nextIdx) }, 4000);
}

$(function() {
	$('#feature').prepend('<div id="tab-active"><div>&nbsp;</div></div>');
	var position = $('#nav-feature li:first-child').position();
	var height = $('#nav-feature li:first-child').height();
	var btm = height + position.top;
	$('#tab-active').animate({'height':height}, 750);


	$('#feature').prepend('<ul id="nav-feature2">'+$('#nav-feature').html()+'</ul>');
	$('#nav-feature2 li img').remove();
	$('#nav-feature2').css({'clip':'rect('+position.top+'px 550px '+btm+'px 0px)'});
	$('#nav-feature li.current').children('img').hide();
	$('#nav-feature li.current').children('img').fadeIn(750);
	$('#nav-feature > li').each(function(intIndex){
		$(this).bind('click', function() {
			switchTo(intIndex);
		});
	});
	t = window.setTimeout(function() { switchTo(0); }, 4000);
});
