// Carousel script
// Travis Valdes

	var theShow, isPlaying = false, mainCarousel, triggers, playControl,
	mcItems, mcDots, mcWrapper, mcImage, mcData = [], mcPanel,
	carouselSpeed = 4500,	
	current = null, play;
	
	function getNext() {
		return current +  1 === mcData.length ? 0 : current + 1;
	}
	
	function getPrevious() {
		return current - 1 < 0 ? mcData.length - 1 : current - 1;
	}
	
	function startCarousel() {
		theShow = window.setInterval(function () {
			if (isPlaying) {
				play();
				/*
					if (playControl.hasClass('stopped')) { playControl.removeClass('stopped');}	
					triggers.eq(getNext()).find('a').parent().addClass('selected').siblings().removeClass('selected');					
					move(getNext(),1);*/
				//move(getNext(),1);
				//triggers.eq(getNext()).find('a').trigger('click');
				//triggers.eq(getNext()).find('a').click();//trigger('click');
				
			} else {
				window.clearInterval(theShow);
			}			
		}, carouselSpeed);
	}	
	function move(index, play) {
		//$('#mainTabWrapper');
	//	mcWrapper = mainCarousel.find('#'+mcData[0].id);
		mcWrapper.hide();		
		mcWrapper.html(mcData[index].body).fadeIn('slow');
		current = index;		
		if (!isPlaying && mcData.length > 1 && play==1) {
			isPlaying = true;
			startCarousel();
		}				
	}
	
	function play(){
			if (playControl.hasClass('stopped')) { playControl.removeClass('stopped');}	
			triggers.eq(getNext()).find('a').parent().addClass('selected').siblings().removeClass('selected');					
			move(getNext(),1);
	}

	function initCarousel() {		
		triggers = mainCarousel.find('#maintab').find('li');
		
		triggers.find('a').each(function (i) {
			var index = i;			
			$(this).click(function () {
				//alert(tester);
				//if (playControl.hasClass('stopped')) { playControl.removeClass('stopped');}	
				playControl.addClass('stopped');
				//$(this).addClass('selected').parent().siblings().find('a').removeClass('selected');
				$(this).parent().addClass('selected').siblings().removeClass('selected');
				
				if (current === null || current !== index) {
					isPlaying = false;
					window.clearInterval(theShow);
					move(index, 0);								
				}
				return false;
			
			/* SWITCHED to stop when clicking a trigger
				if (playControl.hasClass('stopped')) { playControl.removeClass('stopped');}				
				//$(this).addClass('selected').parent().siblings().find('a').removeClass('selected');
				$(this).parent().addClass('selected').siblings().removeClass('selected');
				
				if (current === null || current !== index) {
					isPlaying = false;
					window.clearInterval(theShow);
					move(index);								
				}
				return false;*/
			});
		});			
		playControl.click(function () {
			if ($(this).hasClass('stopped')) {
				$(this).removeClass('stopped');
				play();
				//triggers.eq(getNext()).find('a').trigger('click');
			} else {
				$(this).addClass('stopped')
				isPlaying = false;
			}
		});		
		move(0,1);
		//triggers.eq(0).find('a').trigger('click');
	}
	$(document).ready(function () {
		mainCarousel = $('#mainTabWrapper');		
		mcWrapper = mainCarousel.find('#mainContentWrapper');
		playControl = mainCarousel.find('.pause');
		mcItems = mainCarousel.find('.tabcontentstyle').find('.tabcontent');//.find('.tabcontent');			
		mcItems.each(function (i) {
			mcData.push({
				//link : $(this).find('a').attr('href'),
				//image : $(this).find('img').attr('src'),
				//title : $(this).find('h3').text(),
				id  : $(this).attr('id'),
				body  : $(this).html()
 			});
			//mcDots.append(createDot(i));
		});
		if (mcData.length) {
			//alert(mcData.length);
			mcWrapper.fadeIn();
			initCarousel();
		}		
		//setupMainCarousel();
	});




