var cur_banner = 0;
var num_banner = 0;
var rotlock = 0;
var clicklock = 0;
$(document).ready(function() {
	num_banner = $('.featureSlide').size();
	$('div.featureSlide').hover(function() {
			clicklock = 2;
		}, function() {
			clicklock = 0;
		}
	);
	
	$('div.featureSlide div.text').each(function() {
		$(this).click(function() {
			if ($(this).siblings('a').size())
				window.location=$(this).siblings('a').attr('href');
		});
	});
	
	$('#homeFeaturesList li').each(function() {
		$(this).click(function() {
			if ($(this).hasClass('active')) {
				if ($('div.featureSlide.active a').size()) {
					window.location=$('div.featureSlide.active a').attr('href');
				}
			} else {
				if ((cur_banner != parseInt($(this).attr('idx')))&&(rotlock == 0)) {
					clicklock = 2;
					showBanner($(this).attr('idx'));
				}
			}
			return false;
		});
		
		$(this).hover(function() {}, 
			function() {
				if (cur_banner == parseInt($(this).attr('idx'))) {
					clicklock = 0;
				}				
			}
		);
	});
	// setup auto rotate
	rInt = setInterval('autoRotate()', 7000);
});

function autoRotate()
{
	var next_banner = (cur_banner + 1) % num_banner;
	//alert(next_banner);
	if (clicklock == 2)
		return;
	if (clicklock == 1) {
		clicklock = 0;
		return;
	}
	if (rotlock == 0)
		showBanner(next_banner);
}

function showBanner(n)
{
	rotlock = 1;
	$('#banner-slide-'+n).css({'z-index':20}).addClass('active').fadeIn('slow', function() {
		$('#banner-slide-'+cur_banner).removeClass('active');
		$('#banner-slide-'+n).css({'z-index':10});
		rotlock = 0;			
		cur_banner = parseInt(n);	
	});
	$('#homeFeaturesList li.active').removeClass('active');
	$('#banner-nav-'+n).addClass('active');
}
