/*
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() });    
}

/* 
    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) {

					// Evento segnalato dopo il caricamento della singola immagine
					if (thumbImgs.onLoadImgComplete)
						thumbImgs.onLoadImgComplete();
						
					setTimeout("runLoading()", _DEBUGTIME);
				} else {
					
					// Evento segnalato al termine del caricamento di tutte le immagini
					if (thumbImgs.onLoadComplete)
						thumbImgs.onLoadComplete();
				}
                  
    }        
    thumbImgs[index].imLoader.src = thumbImgs[index].src;         
}

/*
Gestione dei menù a tendina
*/
function mainContainerOnClickHandler() {	
	Pinko.MenuHandler.chiudiTutti();
}

var Pinko = {
	FooterMenuHandler: {
		idStagione: 200266
		, divCategoria: 'prodotti-categorie'
		, divLook: 'prodotti-look'
		, divSpecialCorner: 'prodotti-specialCorner'
		, divStagione: 'prodotti-stagioni'
		, getIdDivCategoria: function() {
			return this.divCategoria + '-' + this.idStagione;
		}
		, getIdDivLook: function() {
			return this.divLook + '-' + this.idStagione;
		}
		, getIdDivSpecialCorner: function() {
			return this.divSpecialCorner;
		}
		, getIdDivStagione: function() {
			return this.divStagione;
		}
		, setStagione: function(_id, _descr) {
			document.getElementById('footer_lblCurrentSeason').innerHTML = _descr;
			this.idStagione = _id;
			Pinko.MenuHandler.toggle(Pinko.FooterMenuHandler.getIdDivCategoria());			
		}
	}
	, MenuHandler: {
		durataTransAperturaChiusura: 1000
		, durataTransOpacita: 1
		, gradoDiOpacita: 0.6
		, bloccaOpacita: false
		, sfondoOpaco : false
		, mainLayerId: 'main'
		
		, menuAttivi: new Array()
		
		, chiudiTutti: function() {
			// Chiusura di tutti i menù
			if (this.menuAttivi.length > 0) {
				for (var x = 0; x < this.menuAttivi.length; x++) {
					jQuery('#' + this.menuAttivi[x]).slideUp(this.durataTransAperturaChiusura);
				}
				this.menuAttivi = new Array();
				
				// Elimino l'opacità del layer principale
				if (! this.bloccaOpacita && this.sfondoOpaco) {
					jQuery('#' + Pinko.MenuHandler.mainLayerId).fadeTo(Pinko.MenuHandler.durataTransOpacita, 1);
					this.sfondoOpaco = false;
				}
			}
		}
		, attiva: function(_menuId) {
			// Attiva il singolo menù
			var lockOpacita = this.bloccaOpacita;
			this.bloccaOpacita = true;
			this.chiudiTutti();
			this.bloccaOpacita = lockOpacita;
					
			this.menuAttivi.push(_menuId);
			if (! this.bloccaOpacita && ! this.sfondoOpaco) {
				jQuery('#' + _menuId).slideDown(this.durataTransAperturaChiusura, function() {
					jQuery('#' + Pinko.MenuHandler.mainLayerId).fadeTo(Pinko.MenuHandler.durataTransOpacita, Pinko.MenuHandler.gradoDiOpacita);
				});
				this.sfondoOpaco = true;			
			} else {
				jQuery('#' + _menuId).slideDown(this.durataTransAperturaChiusura);
			}
		}
		, isAttivo: function(_menuId) {
			// Indica se il menù è attivo
			var attivo = false;
			for (var x = 0; x < this.menuAttivi.length; x++) {
				if (_menuId == this.menuAttivi[x]) {
					attivo = true;
					break;
				}
			}
			return attivo;
		}
		, toggle: function(_menuId) {
			if (this.isAttivo(_menuId)) {
				this.chiudiTutti();
			} else {
				this.attiva(_menuId);
			}
		}
	}
};

/*
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);
}