$(document).ready(function(){
	//alert($('.featureshow').attr('class'));
	$('#Feature').cseSlide();
	$('#Preview').cseTabs();
/*var slide_count = 0;
$('.featureshow').addClass('js');
$('.featureshow .section:eq('+slide_count+')').addClass('current');

function nextSlide() {
	
}

*/

});

(function($){


	$.fn.cseSlide = function(){
		this.addClass('js');
		var element = this;
		var slide_count = $(this).children('.feature').length;
		//alert('Slide Count = ' + slide_count);
		var current = 0;
		
		
		goToSlide(current);
		//setInterval('$.fn.cseSlide.goToSlide('+ (current++) +');',3000);
		//$.delay(2000).goToSlide(1);
		
		
		function goToSlide(n, wait) {
			removeNavInteraction();
			$(document).stopTime("transition");
			if(n < slide_count) {
				current = n;
			} else {
				current = 0;
			}
			removeNav();
			$(element).children('.feature.current').fadeOut(1000, function() {
				
				$(element).children('.feature.current').removeClass('current');
			});
			
			$(element).children('.feature:eq('+current+')').fadeIn(1000, function() {
				$(element).children('.feature:eq('+current+')').addClass('current');
				addNavInteraction();
			});
			addNav();
			
			if(wait) {
				$(document).oneTime(10000, "transition", function(){ /*delay when slide is selected*/
					goToSlide(++current);
				});
			} else {
				$(document).oneTime(7000, "transition", function(){ /*standard rotation speed*/
					goToSlide(++current);
				});
			}
			
			
			
		
		}
		
		function addNav() {
			var navigation = '<ul class="nav">';
			for(var i = 0; i < slide_count; i++) {
				if(i == current) {
					navigation += '<li class="current"></li>';
				} else {
					navigation += '<li></li>';
				}
			}
			navigation += '</ul>'
			$(element).children('.feature:eq('+current+')').children('.text').append(navigation);	
		}
		
		function removeNav() {
			$(element).children('.feature:eq('+current+')').children('.text').children('.nav').remove();
		}
		
		function addNavInteraction() {
			$(element).children('.feature:eq('+current+')').children('.text').children('.nav').children('li:not(:eq('+current+'))').click(function(){
				goToSlide($(this).index(), true);
			});
			
		}
		
		function removeNavInteraction(){
			$(element).children('.feature:eq('+current+')').children('.text').children('.nav').children('li:not(:eq('+current+'))').unbind('click');
			
		}
		
	}
	
		

})(jQuery);


(function($){


	$.fn.cseTabs = function(){
		this.addClass('js');
		var element = this;
		var tab_count = $(this).children('.section').length;
		//alert('Slide Count = ' + slide_count);
		var current = 0;
		addNav();
		goToTab(0);
		//setInterval('$.fn.cseSlide.goToSlide('+ (current++) +');',3000);
		//$.delay(2000).goToSlide(1);
		
		
		function goToTab(n) {
			current = n;
			$(element).children('.section.current').removeClass('current');
			$(element).children('.nav').children('ul').children('li.current').removeClass('current');
			$(element).children('.section:eq('+current+')').addClass('current');
			$(element).children('.nav').children('ul').children('li:eq('+current+')').addClass('current');
			
		}
		
		function addNav() {
			var navigation = '<div class="nav"><ul>';
			for(var i = 0; i < tab_count; i++) {
				navigation += '<li>'+$(element).children('.section:eq('+i+')').children('h2').text()+'</li>';
			}
			navigation += '</ul></div>'
			$(element).prepend(navigation);
			
			$(element).children('.nav').children('ul').children('li').click(function(){
				goToTab($(this).index());
				
			});
		}
		
		
		
	}
	
		

})(jQuery);

