$j(document).ready(function () {													
	
	setupSlider();
	
	$j(document).everyTime(5000, "features", function(i) {
		if (sliderAnimate) {
			nextSlider();
		}
		
	});
	
	$j(document).everyTime(5000, "features", function(i) {
		if (sliderAnimate2) {
			nextSubSlider();
		}
		
	});
	

});


setupSlider = function() {
	
	// constants
	
	mainWidth = 960;
	subWidth = 320;

	sliderAnimate = true;
	sliderAnimate2 = true;
	
	mainShow = 1;
	subShow = 1;

	mainCount = $j('#slider > li').length;
	subCount = $j('#subslider > li').length;
	
	//alert(subCount);

	updateControls();
	
	$j('.slider_controls').each(function () {
		$j(this).find('li').click( function() {
			sliderAnimate = false;
			idOfClickedItem = $j(this).attr('id').split('_')[0];
			sliderPanel = idOfClickedItem.split('panel')[0];
			
			//alert("sliderPanel " + sliderPanel);
			
			if (sliderPanel != 'sub') { sliderPanel = 'main' };
			sliderNumber = parseFloat(idOfClickedItem.split('panel')[1]);
			bringIntoView(sliderPanel, sliderNumber);
		 });
	});
	
	$j('.slider_controls2').each(function () {
		$j(this).find('li').click( function() {
			sliderAnimate = false;
			idOfClickedItem = $j(this).attr('id').split('_')[0];
			sliderPanel = idOfClickedItem.split('panel')[0];
			if (sliderPanel != 'sub') { sliderPanel = 'main' };
			sliderNumber = parseFloat(idOfClickedItem.split('panel')[1]);
			bringIntoView(sliderPanel, sliderNumber);
		 });
	});
	
	$j('#home_bottom').click( function() {
		//nextSubSlider();
	});
}


bringIntoView = function(panel, num) {
	
	// make num an index
	//alert("this is the panel " + panel);
	
	if (panel == 'sub') {
		subShow = num;
		slideAmount = (num - 1) * subWidth * -1 + 'px';
		// alert(slideAmount)
		$j('#subslider').animate({'left':slideAmount});
	} else {
		mainShow = num;
		slideAmount = (num - 1) * mainWidth * -1 + 'px';
		// alert(slideAmount)
		$j('#slider').animate({'left':slideAmount});
	}
	
	updateControls();
}

nextSlider = function() {
	
	if (mainShow < mainCount) {
		bringIntoView('main', (mainShow + 1) );
	} 
	else{
		bringIntoView('main', (1) );
		sliderAnimate = false;
	}
	

	//console.log(mainShow);
	//console.log(subShow);

	updateControls();
}


nextSubSlider = function() {
	
	if (subShow < subCount) {
		bringIntoView('sub', (subShow + 1) );
	} else {
		
		bringIntoView('sub', (1) );
		sliderAnimate2 = false;
		// alert('at the end');
	}

	//console.log(mainShow);
	//console.log(subShow);

	updateControls();
}

updateControls = function() {
	$j('.slider_controls li').removeClass('active');
	$j('.slider_controls2 li').removeClass('active');
	$j('#subfeature_controls li:eq(' + (subShow - 1) + ')').addClass('active');
	$j('#feature_controls li:eq(' + (mainShow - 1) + ')').addClass('active');
}





