var Gallery = function(_id){	
	this.id= _id;
	this.max_length = 6000 ; 
	this.pos = 0 ; 
	this.offset = 150 + 2 + 10 + 10  ; 
	this.fx ; 
	this.timer_delay = 1000 * 8; 
	this.max_elem = 30 ;
					
};

Gallery.prototype = {
	caricamento : function(){
		
		var array_foto_t = $(this.id).getElements("img.thumbnail");
		var array = $(this.id).getElements("div.GalleryVJS");
		var array_foto = Array(); 
		for (i = 0; i < array_foto_t.length; i++){
			array_foto[i] = array_foto_t[i].get("src"); 
		}


		var load_div = $(this.id); 
		load_div.innerHTML = ""; 
		load_div.setStyles({
			'text-align' : 'center'
		});
		var img = new Element("img"); 
		img.set("src","template/Immagini/loader.gif"); 
		img.set("alt",""); 
		img.setStyles({
			'margin' : '50px 0'
		}); 
		img.inject(load_div);
		var myImages = new Asset.images(array_foto, {
		 	'onComplete': function(){
			this.avvia(array);
			}.bind(this),
		 	'onProgress': function(counter,index){
				//$(this.id).innerHTML = counter ; 
			}.bind(this)
		});
	},
	avvia : function (_array) {
		this.array = _array;
		var contenitor = new Element("div");
		var gallery = new Element("div");
		//gallery.id = "jsgallery"+this.id; 
		//contenitor.id = "jscon"+this.id; 
		gallery.id = "JSgallery";
		contenitor.id = "contenitor";
		/*contenitor.setStyles({
			'background' : 'green' 
		}); */
		var galleryphp = $(this.id);
		galleryphp.parentNode.appendChild(contenitor); 
		galleryphp.dispose();
		this.arrowDesign(contenitor); 
		gallery.set("class","gallery_gallery")
		gallery.inject(contenitor);
		var items = new Element('div').inject(gallery);
		items.setStyles({
			'margin':'0px',
			'width':   this.max_length+'px',
			'position': 'relative'
		});
		items.set("id","items");
		
		this.inserite = 0; 
		
		for(i = 0 ; i<this.array.length;i++){
			if(this.imgDesing(this.array[i],items)){
				this.inserite++;
			//	this.array[i].getElement('p').innerHTML += inserite ; 
			}
			if(this.inserite>=this.max_elem){break;}
		}
				
		var max_lenght_temp = (this.inserite + 1 )*(this.offset) ;
		items.setStyle('width',max_lenght_temp +'px'); 
		//$('test').innerHTML += this.max_elem +"-"+ this.max_length+ '-'+inserite;
		this.scroll = new Fx.Scroll("JSgallery", {
			offset:{'x':0, 'y':0}, 
			transition: Fx.Transitions.Elastic.easeOut
		});
		
		
		this.scroll.toLeft();
		this.assignArrow(scroll);
		this.fx = new Fx.Morph($("items"),{
				duration: 800, 
				transition: Fx.Transitions.linear
			});
		this.timer = setInterval (
					this.getTimerFunction(),
					this.timer_delay
				);
	},
	getTimerFunction : function () {
	var temp = this ; 
	return function() { 
			var pos_v = temp.pos; 
			temp.pos += temp.offset;
			if(temp.pos>= ((temp.inserite-2 )*(temp.offset)) ){
				temp.pos =0; 
			}
			var scroll = temp.scroll; 
			temp.fx.start({ 
				'opacity': .3 
				}).chain(function(){
					this.start.delay(800, this, { 'opacity': 1 });
					scroll.start(temp.pos);
			});
			
		}; 
	},
	imgDesing : function (img_d,div){
		img_d.inject(div);
		var img = img_d.getElement('img'); 
		var l = img.getWidth() ; 
		var h = img.getHeight() ; 
		var ln  = 160;
		var hn =  ( h*ln)/l;
		var inc_h  = hn-h ; 
		inc_h = Math.round(inc_h) ;
		//$('test').innerHTML += "<br>"+  img.get('src')+ " "+h+  " "+l+" <br>" ;
		//alert( img.get('src')+ " "+h+  " "+l);
		/*if(l<150&&h<60){
		//	$('test').innerHTML += "<br>"+  img.get('src') + " "+l+"<br>" ;  
			img_d.dispose();
			return false; 	
		}*/
		img.set("class","gallery_img_leave"); 
		img.setStyles({
				'width': l,
				'height': h,
				'border-width'	: 	'thin',
				'margin-top'	: 	'0px',
				'margin-left'	: 	'0px',
				'border-color'	: 	'#ff7f00'
			});
		var temp = this; 
		img.addEvent('mouseover', function(event) {
				var fx2 = new Fx.Morph(img,{
					duration: 200,
					transition: Fx.Transitions.linear
				});
				fx2.start({
					'width'		: 	ln,
					'height'	:	hn,
					'margin-top'	: 	-inc_h+'px',
					'margin-left'	:	'-5 px',
					'border-width'	: 	'medium'

				});
				clearInterval(temp.timer);

			});
		img.addEvent('mouseleave', function(event) {
				var fx2 = new Fx.Morph(img, {
					duration: 200,
					transition: Fx.Transitions.linear
				});
				fx2.start({
					'margin-top': '0px',
					'margin-left': '0px',
					'border-width': 'thin',
					'width': l,
					'height':h
				});
				temp.timer = setInterval (
					temp.getTimerFunction(),
					temp.timer_delay
				);
			});
		
		return true; 
	} ,
	arrowDesign : function (contenitor){
		var left = new Element("a"); 
		left.set("id","moveleft" +  this.id); 
		left.set("class", "gallery_moveleft"); 	
		var right = new Element("a");
		right.set("id","moveright" +this.id);
		right.set("class", "gallery_moveright"); 	 
		
		left.inject(contenitor);
		right.inject(contenitor);
	},
	assignArrow : function(scroll) {
		var fx = new Fx.Morph($("items"),{
				duration: 800, 
				transition: Fx.Transitions.linear
			});
		var temp = this; 
		$('moveleft'+this.id).addEvent('click', function(event) {
			event = new Event(event).stop();
			temp.pos += -(temp.offset);
			if(temp.pos<0){
				temp.pos= 0;
				return;
			}
			scroll = temp.scroll
			fx.start({ 
				'opacity': .3 
			}).chain(function(){
				this.start.delay(800, this, { 'opacity': 1 });
				scroll.start(temp.pos);
			});			
		});
		$('moveright'+this.id).addEvent('click', function(event) { 
			event = new Event(event).stop();
			var pos_v = temp.pos; 
			temp.pos += temp.offset;
			if(temp.pos>= ((temp.inserite-2 )*(temp.offset)) ){
				temp.pos =0; 
			}
			scroll = temp.scroll
			fx.start({ 
				'opacity': .3 
				}).chain(function(){
					this.start.delay(800, this, { 'opacity': 1 });
					scroll.start(temp.pos);
			});
		});
	}
}

