// 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 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
    setupSliderControls();
});

function resetSlider() {
    if (timerObject == null)
        setupSlider();

    setupSliderControls();    
}

function setupSliderControls() {
    $('#' + elControllerId + ' a, #' + elControllerId + '2 a').click(function() {
        switchImage($(this).attr('id'));
        clearInterval(timerObject);
        timerObject = null;
        //	stop = true;
    });
    $('#' + elControllerId + ' .next, #' + elControllerId + '2 .next').click(function() {
        switchImage('');
        clearInterval(timerObject);
        timerObject = null;
    });
}

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));
  	});
	
	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');
	
	if (iNext)
	    next = $('#' + elParentId + ' #slide_' + iNext + ', #' + elParentId + '2 #slide_' + iNext);
	else
		var next = active.next().attr('id') ? active.next() : $('#'+elParentId+' '+elToSlide+':first, #'+elParentId+'2 '+elToSlide+':first');

    active.fadeOut(fadeOutTime, function() {
        active.removeClass('active').addClass('hidden');
        active.hide();
        changeButton(next.attr('sid'));
        next.fadeIn(fadeInTime, function() {
            next.removeClass('hidden').addClass('active');
        });
    });

}

function changeButton(iId) {

    $('#' + elControllerId + ' a#' + iId + ', #' + elControllerId + '2 a#' + iId)
		.css('background-image', 'url(/images/btn_console_dot_selected.png)')
		.siblings('a')
		.css('background-image', 'url(/images/btn_console_dot.png)');
			
	return false;
	
}
