/***
 * qDataContainer
 * Copyright 2010 QForma
 */


/**
 * Constructor
 */
function qDataContainer(data)
{
	this.data 		= new Array();
	this.images 	= new Array();
	this.pointer 	= 0;

	if (data != undefined)
		this.setData(data);
};




/**************************************
 * Data functions
 */

/**
 * Set the data
 */
qDataContainer.prototype.setData = function(data)
{
	this.data 		= data;
	this.pointer 	= 0;
}


/**
 * Get the data
 */
qDataContainer.prototype.getData = function()
{
	return this.data;
}


/**
 * Get an element from the data
 */
qDataContainer.prototype.get = function(index)
{
	return this.data[ index ];
}




/**************************************
 * Iterator functions
 */

/**
 * Return the current data-entry
 */
qDataContainer.prototype.current = function()
{
	return this.get( this.pointer );
}


/**
 * Return the next data-entry with a distance
 * Redirects to first, so always a valid entry
 */
qDataContainer.prototype.determineNext = function(distance)
{
	if (distance == undefined)
		distance = 1;
	
	var newIndex = this.pointer + distance;
	if (newIndex > (this.data.length-1))
		newIndex = this.pointer + distance - this.data.length;
	
	return newIndex;
}

/**
 * Return the next data-entry and sets the entry as active
 */
qDataContainer.prototype.setNext = function()
{
	this.pointer = this.determineNext(1);
	return this.get( this.pointer );
}

/**
 * Return the next data-entry
 */
qDataContainer.prototype.next = function(distance)
{
	return this.get( this.determineNext(distance) );
}


/**
 * Return the next data-entry with a distance
 * Redirects to first, so always a valid entry
 */
qDataContainer.prototype.determinePrevious = function(distance)
{
	if (distance == undefined)
		distance = 1;
	
	var newIndex = this.pointer - distance;
	if (newIndex < 0)
		newIndex = this.data.length + newIndex;
	
	return newIndex;
}

/**
 * Return the next data-entry and sets the entry as active
 */
qDataContainer.prototype.setPrevious = function()
{
	this.pointer = this.determinePrevious(1);
	return this.get( this.pointer );
}

/**
 * Return the next data-entry
 */
qDataContainer.prototype.previous = function(distance)
{
	return this.get( this.determinePrevious(distance) );
}




/**************************************
 * Image functions
 */

/**
 * Get image corresponding to row
 */
qDataContainer.prototype.getImage = function(row)
{
	// TODO: add check whether image already created
	return this.createImage(row);
}

/**
 * Create an image object from a row
 */
qDataContainer.prototype.createImage = function(row)
{
	return jQuery(document.createElement('img'))
		.attr('src', row.src)
		.attr('alt', row.alt)
		.attr('id',  row.id);
}



