var ggCaroussel = new Class({
	initialize:function(options){
		this.options = options || {};
		var imgs = options.imgList || [];
		//initialize some values
		this.leftPos = 0;
		this.currentSpeed = 0;//by default it don't run o_O
		if ((!options.slideSpeed) && (options.slideSpeed!=0)){options.slideSpeed = 1;}
		if ((!options.slideWidth) && (options.slideWidth!=0)){options.slideWidth = 600;}
		if ((!options.blocksWidth) && (options.blocksWidth!=0)){options.blocksWidth = 100;}
		if ((!options.slideHeight) && (options.slideHeight!=0)){options.slideHeight = 73;}					
		
		//generate an unused name
		if (!options.name) {
			options.name = 'car';var uid = 0
			while($(options.name+uid)){uid+=1}
			options.name+=uid;
		}
		
		if ((options.blocksWidth * imgs.length) < (options.slideWidth - options.blocksWidth)){
			options.blocksWidth = options.slideWidth / (imgs.length - 1);
			if (options.blocksWidth==Infinity)options.blocksWidth = options.slideWidth;
		}
		//if not enough picture! we don't need to move
		if (imgs.length<=1){options.slideSpeed=0;}
		
		document.write('<div id="'+options.name+'"></div>');
		var car = $(options.name);
		this.imgBox = new Element('div',{"style":"left:0;width:"+((options.blocksWidth+1) * imgs.length)+"px;position:absolute;left:=0px;"});
		
		var loop = function(img){
			this.imgBox.adopt(
				new Element('span').adopt(
					new Element('div',{'style':'float:left;width:'+options.blocksWidth+'px;','align':'center'}).adopt(
						new Element('img',{'src':img})
					)
				)
			)
		}
		$each(imgs,loop.bind(this));
		car.adopt(
			new Element('div',{'style':"height:"+options.slideHeight+"px;margin-left:12px;overflow:hidden;padding-top:5px;position:relative;width:"+(options.slideWidth)+"px;"}).adopt(
				this.imgBox
			)
		)
		
		//star the main loop
		this.run(this);
		return this;
	},
	run:function(instance){
		instance.leftPos -= instance.currentSpeed;
		if (instance.leftPos < -instance.options.blocksWidth){
			instance.imgBox.adopt(instance.imgBox.childNodes[0]);
			instance.leftPos = 0;
		}
		instance.imgBox.setStyle('left',instance.leftPos+"px");
		setTimeout(function(){instance.run(instance)},29);
		return this;
	},
	play:function(){
		this.currentSpeed = this.options.slideSpeed;
		return this;
	},
	pause:function(){
		this.currentSpeed = 0;
		return this;
	}
});

