(function($){
	$.fn.rowgallery = function(options){
		var defaults = {
			thumbs_to_move: 2
		}
		var options = $.extend(defaults, options);
		/*debug*/
		var debug = false;
		trace('rowgallery');
		// ==============================
		// = formattazione della pagina =
		// ==============================
	
		/* attivo gli stili solo se js è attivo*/
		this.each(function(){
			var o = options;
			var obj = $(this);	
			
			//formattazione html e css della gallery
			obj.addClass('on');
			obj.prepend('<div class="content_module_rowgallery_prev"></div>');
			obj.prepend('<div class="content_module_rowgallery_next"></div>');
			//obj.prepend('<div class="content_module_rowgallery_tooltip"></div>');
			
			
			//contenitore per lo stato dell'oggetto
			obj.attributes = {
				thumbs_number: getThumbsNumber(),
				viewport_width: getRowgalleryViewportWidth(),
				thumbs_total_width: getThumbsTotalWidth()
			};
			
			/* gestione delle didascalie */ 
			/*
			$('.content_module_rowgallery_thumbs p', obj).hide();
			$('.content_module_rowgallery_thumbs a', obj).mouseover(function(){
				$(this).parents('.content_module_rowgallery').find('.content_module_rowgallery_tooltip').html($(this).attr('title'));
			}).mouseleave(function(){
				$(this).parents('.content_module_rowgallery').find('.content_module_rowgallery_tooltip').html('');
			});
			*/
			
			//assegno all'ul la larghezza calcolata per mantenere le miniature su una sola riga
			obj.find('.content_module_rowgallery_thumbs ul').css('width',obj.attributes.thumbs_total_width);
			
			// ==============================
			// = gestione dei comportamenti =
			// ==============================
			
			//verifico se devo attivare i comportamenti 
			if(obj.attributes.thumbs_total_width > obj.attributes.viewport_width && obj.attributes.thumbs_number > 0){
				checkButtons();
			}
			
			/*gestisco lo stato dei bottoni*/
			function checkButtons(){
				
				//rimuov la classe che dice che l'oggetto si sta muovendo
				obj.removeClass('ismoving');
				var rowgallery_btn_prev = obj.find('.content_module_rowgallery_prev');
				var rowgallery_btn_next = obj.find('.content_module_rowgallery_next');
				var rowgallery_thumbs_position = obj.find('.content_module_rowgallery_thumbs').position();

				// bottone prev
				if(rowgallery_thumbs_position.left >= 0){
					rowgallery_btn_prev.css('background-position', '0px 50%');
					rowgallery_btn_prev.unbind('click');
					rowgallery_btn_prev.unbind('mouseenter');
					rowgallery_btn_prev.unbind('mouseleave');
					rowgallery_btn_prev.removeClass('btnOn');
					trace('disattiva prev');
				}else{
					if(! rowgallery_btn_prev.hasClass('btnOn')){
						rowgallery_btn_prev.css('background-position', '-' + rowgallery_btn_prev.width() + 'px 50%');
						rowgallery_btn_prev.bind('click', {dir:'prev', obj: obj}, move);
						rowgallery_btn_prev.bind('mouseenter', onBtnRollOver);
						rowgallery_btn_prev.bind('mouseleave', onBtnRollOut);	
						rowgallery_btn_prev.addClass('btnOn');
					}          
				}

				// bottone next                                                                                                                                  
				if(rowgallery_thumbs_position.left < 0 && rowgallery_thumbs_position.left <=  obj.attributes.viewport_width - obj.attributes.thumbs_total_width){
					rowgallery_btn_next.css('background-position', '0px 50%');
					rowgallery_btn_next.unbind('click');
					rowgallery_btn_next.unbind('mouseenter');
					rowgallery_btn_next.unbind('mouseleave');
					rowgallery_btn_next.removeClass('btnOn');
				}else{
					if(! rowgallery_btn_next.hasClass('btnOn')){
						rowgallery_btn_next.css('background-position', '-' + rowgallery_btn_next.width() + 'px 50%');
						rowgallery_btn_next.bind('click', {dir:'next', obj: obj}, move);
						rowgallery_btn_next.bind('mouseenter', onBtnRollOver);
						rowgallery_btn_next.bind('mouseleave', onBtnRollOut);	
						rowgallery_btn_next.addClass('btnOn');			
					}

				}
			}
			
			function move(evt){
				var obj = evt.data.obj;
				if(obj.hasClass('ismoving')) return;

				obj.addClass('ismoving');

				var rowgallery_thumbs_position = obj.find('.content_module_rowgallery_thumbs').position();
				if(evt.data.dir == 'next'){
					target_pos = rowgallery_thumbs_position.left - getThumbTotalWidth() * o.thumbs_to_move;
					trace('target_pos: ' + target_pos);
				}
				if(evt.data.dir == 'prev'){
					target_pos = rowgallery_thumbs_position.left + getThumbTotalWidth() * o.thumbs_to_move;
				}
				obj.find('.content_module_rowgallery_thumbs').animate({
				    left: target_pos
				  }, 1000);
				setTimeout(checkButtons,1200,obj); //da controllare
			}

			function onBtnRollOver(){
				$(this).css('background-position', '-' + $(this).width()*2 + 'px 50%');
			}

			function onBtnRollOut(){
				$(this).css('background-position', '-' + $(this).width() + 'px 50%');
			}
			
			// =============
			// = utilities =
			// =============

			function getThumbsTotalWidth(){
				//trace('getThumbTotalWidth: ' + getThumbTotalWidth());
				//trace('getThumbsNumber: ' + getThumbsNumber());
				//trace('getThumbRightSpace: ' + getThumbRightSpace());
				return parseInt(getThumbTotalWidth()) * parseInt(getThumbsNumber()) + parseInt(getThumbRightSpace());
			}
			function getThumbTotalWidth(){
				//trace('getThumbWidth: ' + getThumbWidth());
				//trace('getThumbRightSpace: ' + getThumbRightSpace());
				return parseInt(getThumbWidth()) + parseInt(getThumbRightSpace());
			}
			function getThumbWidth(){
				return obj.find('.content_module_rowgallery_thumbs li:first').css('width');
			}
			function getThumbRightSpace(){
				var rowgallery_thumb_space_temp = obj.find('.content_module_rowgallery_thumbs li:first').css('margin-right');
				if (rowgallery_thumb_space_temp == undefined) return;
				return rowgallery_thumb_space_temp.substr(0, rowgallery_thumb_space_temp.length - 2);
			}
			function getRowgalleryViewportWidth(){
				return parseInt(obj.find('.content_module_rowgallery_viewport').css('width'));
			}
			function getThumbsNumber(){
				return obj.find('.content_module_rowgallery_thumbs').find('li').length;
			}
			
		});
		function trace(s){
			//console.log(s);
		}
	}
})(jQuery);

