var fotoShow = Class.create({
	hasVideo: false,
	videoControl: null,
	
	initialize: function(container, foto) {
		this.container = $(container);
		this.foto = foto;
		this.pos = { current:1, totali:1 };
		
		this.pos.totali = foto.length;
		this.imgControl = this.container.getElementsBySelector(".img-control").first();
		
		if (Object.isUndefined(this.foto[0])) {
			this.imgControl.hide();						
		} else {				
			if (this.pos.totali > 0) {
				this.show(1);
				this.pos.current = 1;
				this.container.show();
			} else {
				this.container.hide();	
			}
		
			this.prevControl = this.imgControl.getElementsBySelector(".prev").first();
			this.nextControl = this.imgControl.getElementsBySelector(".next").first();
			this.cursorLabel = this.imgControl.getElementsBySelector(".cursor-label").first();

			this.cursorLabelTemplate = new Template(this.cursorLabel.innerHTML);
			this.cursorLabel.update();
			this.setCursorLabel();
			
			this.prevControl.observe("click", this.prev.bind(this));
			this.nextControl.observe("click", this.next.bind(this));
			
			this.imgControl.show();
		}
				
		// Video:
		this.playVideo = this.container.getElementsBySelector(".play-video").first();
		if (this.playVideo != null) {
			this.hasVideo = true;
			this.videoControl = this.container.getElementsBySelector(".video-control").first();		
			this.playVideo.observe("click", this.playVideoClick.bind(this));
			
			// Pulsante di chiusura:
			this.closeVideoControl = this.videoControl.getElementsBySelector(".close").first();	
			if (this.closeVideoControl != null) {
				this.closeVideoControl.observe("click", this.videoControlHide.bind(this));
			}			
		}
	},
	
	setCursorLabel: function() {
		this.cursorLabel.update(this.cursorLabelTemplate.evaluate(this.pos));
	},
	
	next: function() {
		if (this.pos.current < this.pos.totali) {
			this.pos.current++;						
		} else {
			this.pos.current = 1;
		}
		
		this.show(this.pos.current);
		this.setCursorLabel();
	},
	
	prev: function() {
		if (this.pos.current > 1) {
			this.pos.current--;						
		} else {
			this.pos.current = this.pos.totali;
		}
		
		this.show(this.pos.current);			
		this.setCursorLabel();
	},
	
	show: function(num) {
		this.container.setStyle({ "background": "url(" + this.foto[parseInt(num)-1].src + ") center center no-repeat" });
		this.videoControlHide();
	},
	
	playVideoClick: function() {
		this.videoControl.show();	
	},
	
	videoControlHide: function() {
		if (this.videoControl != null) {
			this.videoControl.hide();
		}
	}
});