/*
 * Copyrights to webcoders.eu
 */

function bannerRotator(settings) {
	this.settings = settings;
	this.elements = new Array();
	this.currentElement = 0;
	this.slideshowInterval = 6000;
	this.startedSlideShow = false;
	this.lastSlideChange = 0;
	this.onPage = 0;
	this.currentPage = 0;
	this.showInterval = 2000;
	if (jQuery) {  
		jQuery.noConflict();
		jQuery.ajaxSetup({cache : false});
		this.startedSlideShow = true;
		this.fetchElements();
		this.show(0);
	}
};

bannerRotator.prototype.next = function()
{
	if (this.currentElement == (this.elements.length - 1)) {
		this.show(0);
	} else {
		this.show(this.currentElement+1);
	}
};

bannerRotator.prototype.prev = function()
{
	if (this.currentElement == 0) {
		this.show(this.elements.length - 1);
	} else {
		this.show(this.currentElement-1);
	}
};

bannerRotator.prototype.startSlideShow = function()
{
	this.startedSlideShow = true;
	this.slideShow();
};

bannerRotator.prototype.slideShowToggle = function()
{
	if (this.startedSlideShow) {
		this.stopSlideShow();
	} else {
		this.startSlideShow();
	}
	
};

bannerRotator.prototype.stopSlideShow = function()
{
	this.startedSlideShow = false;
};

bannerRotator.prototype.slideShow = function()
{
	var sliderObj = this;
	if (this.startedSlideShow) {
		jQuery(this.elements[this.currentElement]).hide();
		if (this.currentElement == (this.elements.length - 1)) {
			this.show(0);
		} else {
			this.next();
		}
	}
};

bannerRotator.prototype.show = function(i)
{
	var sliderObj = this;

	sliderObj.currentElement = i;
	jQuery(sliderObj.elements[sliderObj.currentElement]).show();
	jQuery(this.settings.selector+' ul.pagination li').removeClass('active');
	liIndex = 0;
	jQuery(this.settings.selector+' ul.pagination li').each(function(){
		if (parseInt(liIndex) == parseInt(i)) {
			jQuery(this).addClass('active');
		}
		liIndex++;
	});
	this.timeOutObj = setTimeout(function() {runTimeoutSlideshow(sliderObj);},sliderObj.slideshowInterval);

};



bannerRotator.prototype.fetchElements = function()
{
	if (this.settings.selector == null) {
		this.settings.selector = '#bannersContainer';
	}
	var elements = new Array(); 
	jQuery(this.settings.selector+' ul li').each(function(){
		index = elements.length;
		
		elements[index] = new Array();
		elements[index] = this;
		jQuery(this).hide();
	});
	this.elements = elements;
	this.generatePagination();
};


bannerRotator.prototype.generatePagination = function ()
{
	if (!this.elements || this.elements.length == 1) {
		return false;
	}
	var sliderObj = this;
	var elements = this.elements;
	ul = jQuery('<ul class="pagination"></ul>');
	for (var i = 0; i < this.elements.length; i++) {
		ul.append(jQuery('<li></li>').append(jQuery('<a href="javascript:void(0);">'+(i+1)+'</a>').bind('click', {index: i}, function(event) {
			clearTimeout(sliderObj.timeOutObj);
			jQuery(sliderObj.elements[sliderObj.currentElement]).hide();
			sliderObj.show(event.data.index);
		})));
	}
	jQuery(this.settings.selector).append(ul);

};
function runTimeoutSlideshow(sliderObject)
{
	sliderObject.slideShow();
}
