;(function($) {

	$.fn.slideGallery = function(options) {
		var $this = $(this);
		var totWidth = 0;
		var positions = new Array();
		
		
		$('.slide', $this).each(function(i) {
			positions[i]= totWidth;
			totWidth += $(this).width();
			
			if(!$(this).width()) {
				alert("Please, fill in width & height for all your images!");
				return false;
			}
		});
		
		$('.slides', $this).width(totWidth);
		
		$('.menu ul li a', $this).each(function() {
			$(this).addClass('active');
		});
		
		$('.menu ul li', $this).each(function() {
			$('<a></a>').addClass('noactive').ifixpng().appendTo($(this));
		});
		
		$('.menu ul li img, .menu ul li a', $this).click(function(e,keepScroll) {
				change($(this).parent());
				var pos = $(this).parent().prevAll('.menuItem').length;
				
				$('.slides', $this).stop().animate({
					marginLeft: -positions[pos] + 'px'
				}, 450);
				
				e.preventDefault();
				
				if(!keepScroll) 
					clearInterval(itvl);
		});
		
		function change(elem) {
			$('.menu ul li.menuItem', $this).each(function() {
				$('a.noactive', $(this)).css('display', 'block');
				$('a.active', $(this)).hide();
				$('a.active', elem).css('display', 'block');
				$('a.noactive', elem).hide();
			});
		}
		
		change($('.menu ul li.menuItem:first', $this));
		
		$('.menu ul li.menuItem a', $this).ifixpng();
		
		var current = 1;
		function autoAdvance() {
			if(current == -1) 
				return false;
			
			$('#menu ul li a', $this).eq(current%$('#menu ul li a').length).trigger('click',[true]);
			current++;
		}
		
		var changeEvery = 10;

		var itvl = setInterval(function(){
			autoAdvance()
		}, changeEvery*1000);
	}

})(jQuery);
