/**
 * @author Richard Mann
 * @version 0.1
 */
$.imageSlider = {};
$.imageSlider.index_array = new Array();
$.imageSlider.id_array = new Array();
$.imageSlider.selector_id_array = new Array();
$.imageSlider.selector_class_on_array = new Array();
$.imageSlider.selector_class_off_array = new Array();
$.imageSlider.timeout_array = new Array();
$.imageSlider.duration_array = new Array();

$.imageSlider.create = function(slider_name, options) {
	this.index_array[slider_name] = 0;
	this.id_array[slider_name] = options.banner_ids;
	try {
		// Remove any elements that do not exist
		count = 0;
		this.selector_id_array[slider_name] = new Array();
		for (selector_index in options.selector_ids) {
			if ($(options.selector_ids[selector_index]).css('display') != 'none') {
				this.selector_id_array[slider_name][count] = options.selector_ids[selector_index];
				count++;
			}
		}
	}
	catch (e) {}
	if (options.selector_class_on) {
		this.selector_class_on_array[slider_name] = options.selector_class_on;
	}
	if (options.selector_class_off) {
		this.selector_class_off_array[slider_name] = options.selector_class_off;
	}
}

$.imageSlider.start = function(slider_name, duration) {
	try {
		this.duration_array[slider_name] = duration * 1000;
	}
	catch (e) {
		this.duration_array[slider_name] = duration * 1000;
	}
	try {
		this.duration_array[slider_name] = duration * 1000;
		// A try catch has been added her to try to provide max possible stability
		// across all sites using Aurora, even when the elements for this code are
		// not present.
		this.timeout_array[slider_name] = setTimeout("try { $.imageSlider.next('"+slider_name+"'); } catch (e) {}", this.duration_array[slider_name]);
	}
	catch (e) {}
}

$.imageSlider.show = function(slider_name, element_id) {
	// Stop the current slider to pause it for viewing the new slide
	this.stop(slider_name);

	for (element_index in this.id_array[slider_name]) {
		if (this.id_array[slider_name][element_index] == element_id) {
			this.index_array[slider_name] = element_index;
			break;
		}
	}

	// Start the slider again to continue it on it's way
        this.start(slider_name, this.duration_array[slider_name] / 1000);
}

$.imageSlider.stop = function(slider_name) {
	try {
		clearTimeout(this.timeout_array[slider_name]);
	}
	catch (e) {
	}
}

$.imageSlider.next = function(slider_name) {

	// Incriment the index for this banner slider or initialize it if not already done
	prev_banner_index = this.index_array[slider_name];
	this.index_array[slider_name]++;

	// If we have passed the end of the array, begin again
	if (!this.id_array[slider_name][this.index_array[slider_name]]) {
		this.index_array[slider_name] = 0;
	}

	// Deal wiht the selector options
	if (this.selector_id_array[slider_name][this.index_array[slider_name]]) {
		if (this.selector_class_on_array[slider_name]) {
			$(this.selector_id_array[slider_name][this.index_array[slider_name]]).addClass(this.selector_class_on_array[slider_name]);
			$(this.selector_id_array[slider_name][prev_banner_index]).removeClass(this.selector_class_on_array[slider_name]);
		}
		if (this.selector_class_off_array[slider_name]) {
			$(this.selector_id_array[slider_name][this.index_array[slider_name]]).removeClass(this.selector_class_off_array[slider_name]);
			$(this.selector_id_array[slider_name][prev_banner_index]).addClass(this.selector_class_off_array[slider_name]);
		}
	}

	// Show the next banner and hide the one before
	$(this.id_array[slider_name][this.index_array[slider_name]]).css('position', 'absolute').css('z-index', '0').show();
	$(this.id_array[slider_name][prev_banner_index]).css('position', 'absolute').css('z-index', '1').fadeOut();

	// Set the timer again to activate the next slide
	this.timeout_array[slider_name] = setTimeout("$.imageSlider.next('"+slider_name+"');", this.duration_array[slider_name]);
}

