// JavaScript Document
function QueueLoader ()
{
	var _queueList = [];
	var _callback;
	var _onFinish;
	var _index = 0;
	
	/* Public Methods
	***************************************************/
	this.addItem = function (file, id, container, rel)
	{
		var obj = {
			file: file,
			id: id, 
			container: container,
			rel: rel
		}
		_queueList.push(obj);
	}
	
	this.start = function (callback, onFinish)
	{
		// Params
		_callback = callback;
		_onFinish = onFinish == null ? false : onFinish;
		
		// validate length
		if(_queueList.length == 0) return false;
		
		// init
		loadImage ();
	}
	
	/* Private Methods
	***************************************************/
	function loadImage ()
	{
		$(_queueList[_index].container).append("<img rel='" + _queueList[_index].rel + "' id='" + _queueList[_index].id + "' src='" + _queueList[_index].file + "' />");
		$("#" + _queueList[_index].id).css("display", "none");
		$("#" + _queueList[_index].id).load(nextLoad);
	}
	
	function nextLoad ()
	{
		if(_index < _queueList.length - 1) {
			if(!_onFinish && _callback != undefined) _callback(_queueList[_index], false);
			_index++;
			loadImage ();
		} else {
			if(_callback == undefined) return;
			_onFinish ? _callback(_queueList) : _callback(_queueList[_index], true);
		}
	}
}
