// default image switcher vars  YOU CAN CHANGE THESE
var switchImageTime = 5000; // time between image switches in miliseconds
var fadeOutTime = 300; // time it takes for one image to fade to another in miliseconds
var fadeInTime = 800;
var elParentId = 'console';
var elParentIdCta = 'cta';
var elToSlide = 'div'; // element inside of parent div to change.  i.e. a, div, img, etc.
var elControllerId = 'console_controls';
var timerObject = ''; // make global

// rewrite to JQuery from Ext...
$(document).ready(function(){

	setupSlider(); // add necessary id's if not included
	
	$('#'+elControllerId+' a, #'+elControllerId+'2 a').click( function() {
		switchImage($(this).attr('id'));
		clearInterval(timerObject);
	//	stop = true;
	} );
	$('#' + elControllerId + ' .next, #' + elControllerId + '2 .next').click(function() {
		switchImage('');
		clearInterval(timerObject);
	});
});

function setupSlider() {

	// get group and set ids if they don't have them
	$('#' + elParentId + ' ' + elToSlide + ',#' + elParentId + '2 ' + elToSlide).each(function(index) {
	
		// add classes to items
		if (index == 0) // add active class to first div
			$(this).addClass('active');
		else {
			// add hidden class to others
			if (!$(this).hasClass('hidden'))
				$(this).addClass('hidden');
	}
			
		$(this).attr('id', 'slide_'+(index+1));
  	});
	
	
	$('#' + elParentIdCta + ' ' + elToSlide).each(function(index) {
	
		// add classes to items
		if (index == 0) // add active class to first div
			$(this).addClass('active');
		else {
			// add hidden class to others
			if (!$(this).hasClass('hidden'))
				$(this).addClass('hidden');
	}
			
		$(this).attr('id', 'slide_2'+(index+1));
  	});
	
	timerObject = setInterval( function() {
		switchImage('');
	}, switchImageTime);
}

function switchImage(iNext) {
	
	// get active div
	var active = $('#'+elParentId+ ' .active, #' + elParentId + '2 .active');
	if (!active) active = $('#' + elParentId + ' div:first, #' + elParentId + '2 div:first');
	
	var activeCta = $('#'+elParentIdCta+ ' .active');
	if (!activeCta) activeCta = $('#' + elParentIdCta + ' div:first');
	
	if (iNext)
	{
	    next = $('#' + elParentId + ' #slide_' + iNext + ', #' + elParentId + '2 #slide_' + iNext);
		nextCta = $('#' + elParentIdCta + ' #slide_2' + iNext);
	}
	else
	{
		var next = active.next().attr('id') ? active.next() : $('#'+elParentId+' '+elToSlide+':first, #'+elParentId+'2 '+elToSlide+':first');
		var nextCta = activeCta.next().attr('id') ? activeCta.next() : $('#' + elParentIdCta + ' div:first');
	}
		
	
		activeCta.fadeOut(fadeOutTime, function() {
		activeCta.removeClass('active').addClass('hidden')});
		
	active.fadeOut(fadeOutTime, function() {
		active.removeClass('active').addClass('hidden');
		changeButton(next.attr('sid'));
		

		nextCta.fadeIn(fadeInTime, function() {
		nextCta.removeClass('hidden').addClass('active');
		});
		
	next.fadeIn(fadeInTime, function() {
		next.removeClass('hidden').addClass('active');
		});
		
	
	});

}

function changeButton(iId) {

    $('#' + elControllerId + ' a#' + iId + ', #' + elControllerId + '2 a#' + iId)
		.css('background-image', 'url(http://www.ncu.edu/images/btn_console_dot_selected.png)')
		.siblings('a')
		.css('background-image', 'url(http://www.ncu.edu/images/btn_console_dot.png)');
			
	return false;
	
}
