/*
Preload delle immagini
*/
var _DEBUGTIME = 0;
var thumbImgs = new Array();

var loadImageCounter = 0;
var completed = 0;

function scrollAddImage(_id, _src, _width, _height){  
	thumbImgs.push({id:_id, src:_src, width: _width, height: _height, status:0, obj:$(_id), imLoader:new Image() });    
	//thumbImgs.push({id:'immagine1', src:'/images/test/forcella1.jpg', width: 175, height: 295, status:0, obj:$('immagine1'), imLoader:new Image() });    
}

/* 
    Image status: 
    0 - abort/stopped
    1 - loading
    2 - complete
*/

function runLoading(){
    
	if(thumbImgs.length==0) return false;

    var index = loadImageCounter % thumbImgs.length;        
    if(index<0)index=0;    

    if(thumbImgs[index].status!=0){
        loadImageCounter++;
        setTimeout("runLoading()", _DEBUGTIME);   
        return false;
    }
		
		thumbImgs[index].status = 1; 
				
    thumbImgs[index].imLoader.onload = function()        
    {                       
        thumbImgs[index].status = 2;
        if (thumbImgs[index].width > 0) thumbImgs[index].obj.style.width = thumbImgs[index].width + 'px';
        if (thumbImgs[index].height > 0) thumbImgs[index].obj.style.height = thumbImgs[index].height +'px';                
        thumbImgs[index].obj.src = thumbImgs[index].src                
        thumbImgs[index].obj.style.margin = 0;                
        completed++;                                                                                  
        loadImageCounter++;
                      
        if(completed<thumbImgs.length) {
					setTimeout("runLoading()", _DEBUGTIME);
				} else {
					if (thumbImgs.onLoadComplete)
						thumbImgs.onLoadComplete();
				}
                  
    }        
    thumbImgs[index].imLoader.src = thumbImgs[index].src;         
}

/*
Pager lato client
*/
ClientPager = function(fgElementCollection, fgPositionCollection, onIndexChenged) {
	this.ElementCollection = fgElementCollection;
	this.PositionCollection = fgPositionCollection;
	this.Index = 0;
	this.Count = this.ElementCollection.length;
	
	this.OnIndexChenged = onIndexChenged;
	
	// numero di elementi nella pagina
	this.PageSize = 1;
}

// Restituisce l'elemento corrente della gallery
ClientPager.prototype.Current = function() {
		if (this.Index < this.Count) {
			return this.ElementCollection[this.Index];
		} else {
			return null;
		}
}

ClientPager.prototype.MoveNext = function() {
	return this.SetIndex(this.Index + this.PageSize);
}

ClientPager.prototype.MovePrevious = function() {
	return this.SetIndex(this.Index - this.PageSize);
}

ClientPager.prototype.SetIndex = function(index) {
	if ((index < this.Count) && (index >= 0)) {
		this.Index = index;
		this.OnIndexChenged(this);
		return true;
	} else {
		return false;
	}
}

ClientPager.prototype.IsFirst = function() {
	return (this.Index == 0);
}

ClientPager.prototype.IsLast = function() {
	var y = (Math.ceil(this.Count / this.PageSize) - 1) * this.PageSize;
	return (this.Index == y);
}

ClientPager.prototype.Init = function() {
	this.Index = 0;
	this.OnIndexChenged(this);
}