(function($){
	$.fn.hGallery = function(options) {

		var defaults = {
			myImgs: ".hGal_img",
			widthRiq: 900,
			heightRiq: 300
		};

		var options = $.extend(defaults, options);

		var scrLeft = 0; // variabile per lo scroll ad ogni click
		var maxScrLeft = 0; // variabile per il massimo scroll totale

		indexCont = 1;

		return this.each(function() {
			globalCont = $(this);

			idCont = globalCont.attr('id');

			setMisure();
			setNav();

			// determino le misure del contenitore per lo scroll massimo
			function setMisure() {

				realImg = options.myImgs + indexCont;

				var tmpW = 0;
				var tmpImgWidth = 0;

				//$(realImg).each(function(i) {
				$('#'+idCont + ' img').each(function(i) {

					tmpW = $(this).width();

					if(tmpW == 0 || tmpW == undefined)
						tmpW = 470;

					tmpImgWidth += tmpW;

				});

				maxScrLeft = tmpImgWidth;

				if(maxScrLeft < 0)
					maxScrLeft = 0;

			}

			// navigazione a scroll
			function setNav() {

				var widthBt = options.widthRiq/20;

				widthBt = ( widthBt < 30 ) ? 30 :  widthBt;

				var widthArrow = widthBt/2;

				var left = options.widthRiq-widthBt;

				var mt = (options.heightRiq/2)-50;

				var myIdCont = idCont;
				var myIndexCont = indexCont;
				var myMaxScrLeft = maxScrLeft;

				// bottone navigazione DX
				$('<div/>')
					.attr('id', 'btNavDx_'+myIndexCont)
					.css('background-color', '#333333')
					.css('width', widthBt)
					.css('height', options.heightRiq+'px')
					.css('opacity', '.5')
					.css('position', 'absolute')
					.css('top', '16px')
					.css('left', left + 'px')
					.css('z-index', '200')
					.hover( function() {$(this).css('opacity', '.8')}, function() {$(this).css('opacity', '.5')} )
					.appendTo('#'+idCont);

				$('<img/>')
					.attr('src', 'img/navDx.gif')
					.attr('alt', '>')
					.css('width', widthArrow)
					.css('margin', mt + 'px 0 0 10px')
					.css('cursor', 'pointer')
					.click(function() { setScroll(450,myMaxScrLeft); $('#'+myIdCont).animate({ scrollLeft: scrLeft }, "slow"); })
					.appendTo('#btNavDx_'+indexCont);

				// bottone navigazione SX
				$('<div/>')
					.attr('id', 'btNavSx_'+myIndexCont)
					.css('background-color', '#333333')
					.css('width', widthBt)
					.css('height', options.heightRiq+'px')
					.css('opacity', '.5')
					.css('position', 'absolute')
					.css('top', '16px')
					.css('left', '0')
					.css('z-index', '200')
					.hover( function() {$(this).css('opacity', '.8')}, function() { $(this).css('opacity', '.5'); } )
					.appendTo('#'+idCont);

				$('<img/>')
					.attr('src', 'img/navSx.gif')
					.attr('alt', '<')
					.css('width', widthArrow)
					.css('margin', mt + 'px 0 0 10px')
					.css('cursor', 'pointer')
					.click(function() { setScroll(-450,myMaxScrLeft); $('#'+myIdCont).animate({ scrollLeft: scrLeft }, "slow"); })
					.appendTo('#btNavSx_'+myIndexCont);

				if($.browser.msie) {
					$('#btNavDx_'+myIndexCont).css('background-color', '#DDDDDD');
					$('#btNavSx_'+myIndexCont).css('background-color', '#DDDDDD');
				}

			} // function setNav()

			// controllo sullo scroll
			function setScroll(pVal, pMaxScrLeft) {
				scrLeft += pVal;

				if(scrLeft < 0)
					scrLeft = 0;

				if(scrLeft > pMaxScrLeft)
					scrLeft = pMaxScrLeft;
			}

			indexCont++;

		}); // return this.each(function()

	};
})(jQuery);

