(function($) {
	$.fn.jqGalScroll = function(options){
		return this.each(function(i){
			var el = this, $this = $(this).css({position:'relative'}), $children = $this.children();
			el.opts = $.extend({}, $.jqGalScroll, options);
			$this.css({height:el.opts.height,width:el.opts.width})
			el.index = i;
			el.container = $('<div id="jqGS'+i+'" class="jqGSContainer">').css({position:'relative'});
			el.ImgContainer = $('<div class="jqGSImgContainer" style="height:'+el.opts.height+'px;position:relative;overflow:hidden;">');
			$this.wrap(el.container);
			$this.wrap(el.ImgContainer);
			el.pagination = $('<div class="jqGSPagination">');
			$this.parent().parent().append(el.pagination);

			$children.each(function(){
				var $child = $(this).css({height:el.opts.height,width:el.opts.width});
				var $img = $child.children('img');
				var $loader = $('<div class="jqGSLoader">').appendTo($child);
				var $titleHolder = $('<div class="jqGSTitle">').appendTo($child).css({opacity:el.opts.titleOpacity}).hide();
				var image = new Image();
				$img.hide()
				image.onload = function(){
					image.onload = null;
					$loader.fadeOut();
					$img.css({marginLeft:-image.width*.5,marginTop:-image.height*.5}).fadeIn();
					var alt = $img.attr('alt');
					if(typeof alt != 'undefined'){
						$titleHolder.text(alt).fadeIn();
					}
				};
				image.src = $img.attr('src');
			});
			
			var $ul = $('<ul>');
			
			for(var i = 0; i < $children.size(); i++){
				var selected = '';
				if(i == 0) selected = 'selected';
				
				var $a = $('<a href="#'+(i)+'" class="'+selected+'">'+(i+1)+'</a>').click(function(){
					var href = this.href.replace(/^.*#/, '');
					el.pagination.filter('.selected').removeClass('selected');
					$(this).addClass('selected');
					$this.stop().animate({top:-($children.height()*href)},el.opts.speed, el.opts.ease);
					index = href;
				});
				$('<li>').appendTo($ul).append($a);
			};
			el.pagination.append($ul);
		}); // end : this.each(function(){
	};
	$.jqGalScroll = {
		ease: null,
		speed:0,
		height: 'auto',
		width: 'auto',
		titleOpacity : .40
	};
})(jQuery);