ImageSlider.Slider = function(div) {
	this._holdFor = 2000;
	this._transitionSpeed = 'slow';
	this._disableIndicator = false;
	
	if (
		div.id.match(/[a-zA-Z]/) && 
		typeof(imageSliderSettings) != 'undefined' &&
		typeof(imageSliderSettings[div.id]) != 'undefined'
	) {
		this._holdFor = imageSliderSettings[div.id].holdFor;
		this._transitionSpeed = imageSliderSettings[div.id].transitionSpeed;
		if (typeof(imageSliderSettings[div.id].disableIndicator) != 'undefined') this._disableIndicator = imageSliderSettings[div.id].disableIndicator;
	}
	
	this.width = $(div).width();
	this.height = $(div).height();
	
	this.currentImage = 0;
	this.indicators = [];
	
	this.setUp(div);
	if (this.numberOfImages > 1) {
		if (!this._disableIndicator) this.createIndicator(div);
		
		setTimeout(this.animate.bind(this), this._holdFor);
	}
}

ImageSlider.Slider.prototype.setUp = function(div) {
	var images = div.getElementsByTagName('img');
	var imagesArray = [];
	
	for (var i=0; i<images.length; i++) {
		imagesArray.push(images[i]);
	}
	
	this._holderDiv = document.createElement('div');
	this._holderDiv.className = 'holderDiv';
	this._holderDiv.style.width = (this.width * (images.length + 1)) + 'px';
	this._holderDiv.style.height = (this.height) + 'px';
	
	div.appendChild(this._holderDiv);
	
	for (var i=0; i<imagesArray.length; i++) {
		this._holderDiv.appendChild(imagesArray[i]);
	}
	//this._holderDiv.style.left = '-' + this.width + 'px';
	this._holderDiv.appendChild(imagesArray[0].cloneNode(false));
	
	this.numberOfImages = imagesArray.length;
}

ImageSlider.Slider.prototype.createIndicator = function(div) {
	var indicatorHolder = document.createElement('div');
	indicatorHolder.className = 'indicatorHolder';
	indicatorHolder.style.width = (36 + ((this.numberOfImages - 2) * 15)) + 'px';
	
	this.indicators[0] = document.createElement('div');
	this.indicators[0].className = 'indicator firstIndicator firstIndicatorActive'
	indicatorHolder.appendChild(this.indicators[0]);
	
	for (var i=1; i<(this.numberOfImages - 1); i++) {
		this.indicators[i] = document.createElement('div');
		this.indicators[i].className = 'indicator middleIndicator'
		indicatorHolder.appendChild(this.indicators[i]);
	}
	
	this.indicators[i] = document.createElement('div');
	this.indicators[i].className = 'indicator lastIndicator'
	indicatorHolder.appendChild(this.indicators[i]);
	
	div.appendChild(indicatorHolder);
}

ImageSlider.Slider.prototype.animate = function() {
	if (this.currentImage == 0) {
		this._holderDiv.style.left = '0px';
		if (!this._disableIndicator) $(this.indicators[this.currentImage]).removeClass('firstIndicatorActive');
	} else if (this.currentImage == (this.numberOfImages - 1)) {
		if (!this._disableIndicator) $(this.indicators[this.currentImage]).removeClass('lastIndicatorActive');
	} else {
		if (!this._disableIndicator) $(this.indicators[this.currentImage]).removeClass('middleIndicatorActive');
	}
	
	this.currentImage++;
	
	if (this.currentImage == this.numberOfImages) {
		$(this._holderDiv).animate({"left": "-=" + this.width + "px"}, this._transitionSpeed);
		this.currentImage = 0;
	} else {
		$(this._holderDiv).animate({"left": "-=" + this.width + "px"}, this._transitionSpeed);
	}
	
	if (!this._disableIndicator) {
		if (this.currentImage == 0) {
			$(this.indicators[this.currentImage]).addClass('firstIndicatorActive');
		} else if (this.currentImage == (this.numberOfImages - 1)) {
			$(this.indicators[this.currentImage]).addClass('lastIndicatorActive');
		} else {
			$(this.indicators[this.currentImage]).addClass('middleIndicatorActive');
		}
	}
	
	setTimeout(this.animate.bind(this), this._holdFor);
}
