/**
 * jQuery jqGalView Plugin
 * Examples and documentation at: http://benjaminsterling.com/2007/08/24/jquery-jqgalscroll-photo-gallery/
 *
 * @author: Benjamin Sterling
 * @version: 1.0
 * @requires jQuery v1.1.3.1 or later
 * @optional jQuery Easing v1.1.1
 *
 * @name jqGalScroll
 * @type jQuery
 * @param Hash	options				additional options
 * @param String	options[ease]				refer to http://gsgd.co.uk/sandbox/jquery.easing.php for values
 * @param String	options[speed]				fast, slow, 1000, ext..
 * @param String	options[height]				the default height of your wrapper
 * @param String	options[navArrowOpacity]	the opacity of your up and down links
 * @param String	options[titleOpacity]		the opacity of your title bar (if present)
 * @return jQuery Object (chainable)
 */
(function($) {
	$.fn.jqGalScroll = function(options){
		/*	Set up our options */
		var o = $.extend({}, $.fn.jqGalScroll.defaults, options);
		return this.each(function() {
			var $parent = $(this), $children = $parent.children(), index=0;
			var $wrap = $('<div class="jqgsContainer"></div>').css({position:'relative',height:o.height,overflow:'hidden'});
			var $navContainer = $('<div class="jqgsNav">');
			var $jqgstitle = $('<div class="jqgstitle">').css({opacity:o.titleOpacity}).hide();
			var $navUp = $('<a href="#up" class="jqgsUp">Avanti</a>').css({opacity:o.navArrowOpacity, width:75});
			var $navDown = $('<a href="#down" class="jqgsDown">Precedente</a>').css({opacity:o.navArrowOpacity, width:75});
			var $jqgsPagination = $('<div class="jqgsPagination">');
			
			$navUp
			.click(function(){
						
				if($children.size()==(index+1)) return false;	
				$.fn.jqGalScroll.getTitle($children[++index],$jqgstitle);
				$parent.animate({marginTop:-($children.height()*index)},o.speed, o.ease);
				$jqgsPaginationLinks.filter('.selected').removeClass('selected');
				//$jqgsPaginationLinks.slice(index,index+1).addClass('selected');				
				$jqgsPaginationLinks.eq(index).addClass('selected');
				return false;
			});
			
			$navDown
			.click(function(){
				if(index==0) return false;
				$.fn.jqGalScroll.getTitle($children[--index],$jqgstitle);
				$parent.animate({marginTop:-($children.height()*index)},o.speed, o.ease);
				$jqgsPaginationLinks.filter('.selected').removeClass('selected');
				//$jqgsPaginationLinks.slice(index,index+1).addClass('selected');	
				$jqgsPaginationLinks.eq(index).addClass('selected');	
				return false;
			});
			
			//$parent.css({padding:0,margin:0,listStyle:'none'}).wrap($wrap).parent().append($navContainer).append($jqgstitle).append($jqgsPagination);
			$parent.css({padding:0,margin:0,listStyle:'none'}).wrap($wrap).parent().append($jqgstitle).append($jqgsPagination);
			$navContainer.append($navUp).append($navDown);			
			$children.css({height:o.height});
			$.fn.jqGalScroll.getTitle($children[index],$jqgstitle);

			var $ul = $('<ul>');
			
			for(var i = 0; i < $children.size(); i++){
				var selected = '';
				if(i == 0) selected = 'selected';
				
				var $a = $('<li><a href="#'+(i)+'" class="'+selected+'">'+(i+1)+'</a></li>').css({opacity:.70});
				$ul.append($a);
			};
			
			$jqgsPagination.append($ul)
			var $jqgsPaginationLinks = $jqgsPagination.find('a').click(function(){
				var href = this.href.replace(/^.*#/, '');
				$jqgsPaginationLinks.filter('.selected').removeClass('selected');
				$(this).addClass('selected');
				$parent.animate({marginTop:-($children.height()*href)},o.speed,o.ease);
				$.fn.jqGalScroll.getTitle($children[href],$jqgstitle);
				index = href;
			});	
			var $prev = $('<li>');
			$prev.append($navDown);
			$ul.prepend($prev);
			var $nxt = $('<li>');
			$nxt.append($navUp);
			$ul.append($nxt);
		});
	};
	$.fn.jqGalScroll.getTitle = function(obj, $textTo){
		var alt = $(obj).children('img').attr('alt');
		if(alt) $textTo.text(alt).show();
		else $textTo.hide();
	};
	$.fn.jqGalScroll.defaults = {
		ease: 0,
		speed: 10,
		height: 'auto',
		width: 'auto',
		navArrowOpacity : .70,
		titleOpacity : .60
	};
})(jQuery);

$(document).ready(function(){
		$("ul.jqGalScroll").jqGalScroll({height:530,width:530});
	});
