/**
 * Qball Carrousel
 * 
 * Class QballCarrousel
 */

var QballCarrousel = function(options)
{
	this.options = options;
	
	if(!$(options.wrapper))
		throw 'Er is geen of foute "wrapper" aangegeven in de constructor.';
	this.wrapper = $(options.wrapper);
	
	if(!$$(options.elements) || $$(options.elements).length <= 0)
		throw 'Er is geen of foute "elements" aangegeven in de constructor.';
	this.elements = $$(options.elements);
	
	if($type(options.widthElement)!='number')
		throw 'Er is geen element breedte aangegeven in de constructor.';
	this.widthElement = options.widthElement;
	
	this.speed 		= options.speed ? options.speed : 40;
	this.cssprop	= options.cssprop ? options.cssprop : 'margin-left';
	this.steps		= options.steps ? options.steps : 2;
	
	this.timeInterval = null;
	this.elementIndex = 0;
	
	this.init();
};

/**
 * Start de Carrousel
 */
QballCarrousel.prototype.init = function()
{
	this.runningEvent();
	
	this.wrapper.addEvent('mouseover', function(){
		$clear(this.timeInterval);
		
	}.bind(this));
	
	this.wrapper.addEvent('mouseout', function(){
		this.runningEvent();
		
	}.bind(this));
};

QballCarrousel.prototype.runningEvent = function()
{
	if( (this.wrapper.getStyle(this.cssprop).toInt()*-1) > this.widthElement )
	{
		this.switchElement();
		this.wrapper.setStyle(this.cssprop, ( this.widthElement - (this.wrapper.getStyle(this.cssprop).toInt()*-1) ) - this.steps );
	}
	else
		this.wrapper.setStyle(this.cssprop, this.wrapper.getStyle(this.cssprop).toInt()-this.steps);
	
	this.timeInterval = this.runningEvent.delay(this.speed, this);
};

/**
 * Zet eerste element aan het eind
 */
QballCarrousel.prototype.switchElement = function()
{
	if (!this.elements[this.elementIndex])
		this.elementIndex = 0;
	
	this.elements[this.elementIndex].inject(this.wrapper);
	this.elementIndex++;
};







/**
 * Log handler
 */
var log = function(log)
{
	try{
		console.log(log);
	} catch(err){
		try{
			//alert(log);
		}catch(err){}
	}
};
