var Global = {};
Global.instances = 0;

// jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
// t: current time, b: beginning value, c: change in value, d: duration

jQuery.extend( jQuery.easing, {
easeInOutExpo: function (x, t, b, c, d) {
if (t==0) return b;
if (t==d) return b+c;
if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
}
});

jQuery.fn.portfolio = function() {
	
	return this.each(function() {
		
		Global.instances++;
	
		var $polaroid = $(this);
		$polaroid.parent().attr('id','portfolio'+Global.instances);
		$polaroid.parent().find('ul.sequence').attr('id', 'sequence'+Global.instances);
		$polaroid.parent().find('ul.gallery').attr('id', 'gallery'+Global.instances);
		$polaroid.parent().find("img.loading").remove();
		$polaroid.show();
		
		var imgWidth = $polaroid.children("li").width();
		var imgNum = $polaroid.children("li").length;
		var galleryWidth = imgWidth * imgNum;
		$polaroid.css("width", galleryWidth);
		$polaroid.children('li').css("width", imgWidth);
		
		$("ul#sequence"+Global.instances+" li").each(function(i) {
			$(this).bind("mouseover", function(){
				var margin = - (imgWidth * i);
				$(this).find('a').addClass("current").parent().find("li:not($(this)) a").removeClass("current");
			
				$polaroid.animate({left: margin}, 500, "easeInOutExpo");
			}).bind("mouseout", function(){$(this).find('a').removeClass('current')});
		
			$(this).click(function() {return false});
		});
	});

};
