
function DataManager(options) 
{ 

	var _aItems = [];
	
	var index = 0;
    var percent = 0;
	var nbItems = 0;
	
	var indexCurrent;
	
	var rel;
	var subRel;
	var isDiez;
	
	var _options = 
	{
		loading : options.loading || null,
		
		onComplete : options.onComplete,
		onCompleteParams : options.onCompleteParams || null
	};
	
    this.addItem = function(item) { 
        _aItems.push(item);
    };
	
	this.add_aItems = function(aItems) { 
        _aItems = aItems;
    };
	
	var _compareIndex = function (a, b) {
		return a.index - b.index;
	};
	
	this.start = function(_rel, _subRel, _isDiez)
	{
		rel = _rel;
		subRel = _subRel;
		isDiez = _isDiez;
		
		var div = document.createElement('div');
		div.setAttribute("id", "content-load");
		div.style.display = "none";
		document.body.appendChild(div);
		
		nbItems = _aItems.length;
		
		//Re-order
		_aItems.sort(_compareIndex);
		
		//Reset
		$(_options.loading).width(0);
		
		indexCurrent = 3; //Plus grand index

		_loadContent (0);
		
	};
	
	var _loadContent = function(i)
	{
		//console.log("i", i);
		var item = _aItems[i];
		
		if(item.rel == rel)
		{
			indexCurrent = item.index;
			
			//console.log(item.rel, " == ", rel);
			
			if (subRel == null)
			{
				//console.log("continue");
				var nextI = i+1;
				_loadContent(nextI);
				return;
			}
			else
			{
				if(item.sub == 1 || isDiez)
				{
					var nextI = i+1;
					_loadContent(nextI);
					return;
				}
					
			}
			
		}
		
		
		$.ajax({
			  url: item.partial,
			  success: function(data)
			  {
				    $("#content-load").append(data);
				  
				    index += 1;
					percent = parseInt(  (parseInt(index) / parseInt(nbItems - 1) * 100 ) );
					
					if (percent == 100 && _options.onComplete != null)
					{
						_end();
						return;
					}
					else
					{
						$(_options.loading).animate(
							{
								width:percent+'%'
							},
							{
								duration: 300, 
								easing : 'easeOutCubic'
							}
						);
						
						var nextI = i+1;
						_loadContent(nextI);
						return;
					}
					
			  }
			});
		
	}
	
	var _end = function()
	{
		
		//get current index of the section
		var current;
		
		if (subRel == null)
			current = $("#container section.page[rel="+rel+"]");
		else
		{
			var sectionprojet = $("#content-load").find("section.page[rel=projets]");
			sectionprojet.append($("#projet"));
			$("#footer").before(sectionprojet);
			current = $("#container section.page[rel=projets]");
		}
			
		
		//console.log("current", current);
		
		//console.log("$(#content-load).children()", $("#content-load").children());
		
		//Order content correctly
		$.each($("#content-load").children(), function (idx, child)
		{
			child = $(child);
			
			var indexItem;
			
			$.each(_aItems, function (i, obj)
			{
				if(obj.rel == child.attr("rel"))
				{
					indexItem = i;
				}
					
			});
			
			//console.log("indexItem", indexItem);
			//console.log("indexCurrent", indexCurrent);
			
			if (indexItem < indexCurrent)
			{
				//console.log(">> before", current);
				current.before(child);
			}
			else if (indexItem > indexCurrent)
			{
				//console.log(">> after");
				$("#footer").before(child);
			}
			
			
		});
		
		$(_options.loading).animate(
			{
				width:percent+'%'
			},
			{
				duration: 2000, 
				easing : 'easeOutCubic',
				complete:_options.onComplete
			}
		);
	}
} 
