var meta = [];

var gallery = (function(items,options){
	
	//$('#gallery a')[0].addClass("selected");
		
	return function(items,options){

		var instance = this;

		this.G_images = 0;
		this.G_current = 1;
		this.items = items;
		this.thumbCol = options.thumbCol;
		
		this.init = function(carousel){
			
			instance.carousel = carousel;
			
			jQuery('#galleryNext').bind('click', function(){
				instance.go_prev();
				return false;
			});
			
			jQuery('#galleryPrev').bind('click', function(){
				instance.go_next();
				return false;
			});
			
			$('#gallery a').each(function(intIndex){
				$(this).bind("click", function(){
					instance.G_current = (intIndex + 1);
					$('#IMAGE').attr('src', this.href);
					instance.updateMetaData();
					instance.go_update_selection();
					instance.go_update_btn();
					return false;
				});
			
				instance.G_images++;
			
			});
		};
		
		this.go_next = function(){
		
			if (instance.G_current < instance.G_images) {
				instance.G_current++;
				instance.go_update_btn();
				$('#IMAGE').attr('src', instance.items[instance.G_current - 1].href);
				instance.updateMetaData();
				instance.go_update_selection();			
				if (instance.G_current<=(instance.carousel.last / instance.thumbCol ) * 6 && instance.G_current>(instance.carousel.last / instance.thumbCol) * 6 - 6) {
				}
				else {
					instance.carousel.next();
				};
			}
			else {
			}
		};
		
		this.go_prev = function(){
			if (instance.G_current > 1) {
				instance.G_current--;
				instance.go_update_btn();
				document.getElementById("galleryPrevImg").src = rootPath + '/css/tools/images/gallery-next.gif';
				$('#IMAGE').attr('src', instance.items[instance.G_current - 1].href);
				instance.updateMetaData();
				instance.go_update_selection();
				if (instance.G_current <= (instance.carousel.last / instance.thumbCol) * 6 && instance.G_current > (instance.carousel.last / instance.thumbCol) * 6 - 6) {
				}
				else {
					instance.carousel.prev();
				};
			}
			else {
			}
		};
		
		this.go_update_btn = function(){
			if (instance.G_current == instance.G_images) {
				$("#galleryPrevImg").attr('src', rootPath + '/css/tools/images/gallery-next-disabled.gif');
			}
			else {
				$("#galleryPrevImg").attr('src', rootPath + '/css/tools/images/gallery-next.gif');
			};
			if (instance.G_current == 1) {
				$("#galleryNextImg").attr('src', rootPath + '/css/tools/images/gallery-prev-disabled.gif');
			}
			else {
				$("#galleryNextImg").attr('src', rootPath + '/css/tools/images/gallery-prev.gif');
			};
		};
		
		this.go_update_selection = function(){
			$('#gallery a').each(function(intIndex){
				if ((intIndex + 1) == instance.G_current) {
					$(this).find('img').addClass("selected");
				}
				else {
					$(this).find('img').removeClass("selected");
				};
			});
		},
		
		this.updateMetaData = function(){

			if ((meta.length >= instance.G_current - 1) && (meta[instance.G_current - 1])) {
				
				var m = meta[instance.G_current - 1];
				
				if (m.Linea) {
					$('#metaLinea').html(m.Linea);
				}
				else {
					$('#metaLinea').html('');
				};
				
				if (m.Serie) {
					$('#metaSerie').html(m.Serie);
				}
				else {
					$('#metaSerie').html('');
				};
				
				if (m.Colori) {
					$('#metaColori').html(m.Colori);
				}
				else {
					$('#metaColori').html('');
				};
				
				if (m.Formati) {
					$('#metaFormati').html(m.Formati);
				}
				else {
					$('#metaFormati').html('');
				};
				
			}


		}
	}	
})();
	



