
var IMAGESLIDER = function( a_slider ) {

	//------------------------------------------------------------------------

	var _self = this;

	this.imageWidth = 475;
	this.btPrev = null;
	this.btNext = null;
	this.imageContainer = null;
	this.bulletContainer = null;
	this.slider = a_slider;
	this.images = null;
	this.activeImage = 0;
	this.fx = null;
	this.isRunning = false;
	
	//------------------------------------------------------------------------
	
	this.__construct = function() {
		this.init_elements();
	}
	
	//------------------------------------------------------------------------
	
	this.init_elements = function() {
		var divs = this.slider.getElementsByTagName('div');
		for( var i=0; i<divs.length; i++ ) {
			var div = divs[i];
			switch( div.className ) {
				case 'imageSliderPrevious':
					this.btPrev = div;
					this.btPrev.parent = this;
				break;
				case 'imageSliderNext':
					this.btNext = div;
					this.btNext.parent = this;
				break;
				case 'imageSliderContent': this.imageContainer = div; break;
				case 'imageSliderBullets': this.bulletContainer = div; break;
				default: continue; break;
			}
		}
		
		this.images = this.imageContainer.getElementsByTagName('img');
		for(var i=0;i<this.images.length; i++) {
			var bullet = document.createElement('div');
			bullet.imageId = i;
			bullet.className = ( i==0 ) ? 'imageSliderBulletsElement active' : 'imageSliderBulletsElement';
			bullet.parent = this;
			bullet.onclick = function() {
				this.parent.slide(this.imageId);
			}
			this.images[i].bullet = bullet;
			this.bulletContainer.appendChild(bullet);

		}

		this.bulletContainer.style.width = this.images.length * 20 + 'px';
		this.imageContainer.style.width = this.images.length * this.imageWidth + 'px';
		
		this.btPrev.onclick = function() {
			if(this.parent.isRunning === false) {
				this.parent.isRunning = true;
				this.parent.slide( false );
			}
		}
		this.btNext.onclick = function() {
			if(this.parent.isRunning === false) {
				this.parent.isRunning = true
				this.parent.slide( true );
			}
		}
	}

	//------------------------------------------------------------------------

	this.slide = function( next ) {

		this.images[this.activeImage].bullet.className = this.images[this.activeImage].bullet.className.replace(' active', '');

		if(	typeof(next) == 'number' ) {
			this.activeImage = next;
		} else if( next === true ) {
			if( (this.activeImage+1) >= this.images.length ) {
				this.activeImage = 0;
			} else {
				this.activeImage++;
			}
		} else {
			if( this.activeImage <= 0 ) {
				this.activeImage = this.images.length-1;
			} else {
				this.activeImage--;
			}
		}
		
		this.images[this.activeImage].bullet.className += ' active';

		var ml = (this.activeImage * this.imageWidth) * -1;
		this.fx = new Fx.Morph(this.imageContainer,{duration:500,onComplete:function(){
			_self.isRunning = false;
		}});
		this.fx.start({marginLeft:ml});
	}

	//------------------------------------------------------------------------

	this.set_bullet = function() {
		
	}

	//------------------------------------------------------------------------

	this.__construct();

	//------------------------------------------------------------------------
}

//----------------------------------------------------------------------------

window.addEvent('load',function(){
	if( IS_IFRAME === true ) {
		var sliders = $$('.imageSlider');
		if(sliders.length > 0) {
			sliders.each(function(slider) {
				var s = new IMAGESLIDER(slider);
			});
		}
	}
});

