// Class that implements queue.
// (c) David Delibasic

// Function declaration
function get_current_element() {
	if (this.queue_length == 0) {
		return false;
	} else {
		return this.data[this.queue_position];
	}
}
function add_to_tail(obj) {
	this.data[this.queue_length] = obj;
	this.queue_length++;
}
function add_to_head(obj) {
	if (this.queue_length == 0) {
		// We are adding first object which is the same
		// as adding to tail
		this.add_to_tail(obj);
	} else {
		// Shift all objects to make room for new one
		var i;
		for(i=this.queue_length;i>0;i--) {
			this.data[i] = this.data[i-1];
		}
		// Add a new object
		this.data[0] = obj;
		this.queue_length++;
	}
}
function get_element_at(num) {
	// We dont have so many elements
	if (num+1 > this.queue_length) {
		return false;
	}
	return this.data[num];
}
function add_to(num,obj) {
	if (num == 0) {
		this.add_to_head(obj);
	} else if (num+1 > this.queue_length) {
		this.add_to_tail(obj);
	} else {
		var i;
		for(i=this.queue_length;i>num;i--) {
			this.data[i] = this.data[i-1];
		}
		this.data[num] = obj;
		this.queue_length++;
	}
	
}
function delete_at(num) {
	// We dont have so many elements
	if (num+1 > this.queue_length) {
		return false;
	}
	// Wa want to delete first
	var i;
	for(i=num;i<this.queue_length-1;i++) {
		this.data[i] = this.data[i+1];
	}
	//alert('DELETE: '+this.data);
	delete this.data[i];
	this.queue_length--;
}

function queue() {


	this.queue_length = 0;   // Current length of queue
	this.queue_position = 0; // Current position in queue
	this.loader_position = 0; // Current loader position
	this.data = new Array(); // Storage for queue data
	
	// Function adds object named "obj" to the end of the queue
	this.add_to_tail = add_to_tail;
	// Function adds object named "obj" to the start of the queue
	this.add_to_head = add_to_head;
	// Function adds object named "obj" to specified position
	this.add_to = add_to;
	// Function deletes object at specified location
	this.delete_at = delete_at;
	// Function returns object on current position in queue
	this.get_current_element = get_current_element;
	// Function returns object on a specified in queue
	this.get_element_at = get_element_at;
}

