(function ($) {
	var methods = {
		context: Object,
		container: 'slider-container',
		slideSpeed: 2000,
		slideInterval: 4000,
		centerImage: {},
		rightImage: {},
		leftImage: {},
		rightImageStorage: {},
		galleryArr: [],
		curIndex: {},
		animating: false,
		animate1: false,
		animate2: false,
		animate3: false,
		animate4: false,
		init: function(options) {
			if(options) {
				$.extend(methods, options);
			}
			methods.container = $('#' + methods.container);
			methods.container.children().each( function () {
				$(this).css("display", "none");
				methods.galleryArr.push($(this));
			});
			if(methods.galleryArr.length > 0)
			{
				methods.start();
				setInterval(methods.slideLeft, methods.slideInterval);
			}
		},
		start : function() {
			methods.curIndex = 1;
			methods.updateImages();
			methods.centerImage.css("display", "block");
			methods.rightImage.css("display", "block");
			methods.leftImage.css("display", "block");
			methods.leftImage.css("left", 0);	
			methods.leftImage.load(function() {
				methods.centerImage.css("left", methods.leftImage.width() + 10 + "px");
			});
			methods.centerImage.load( function () {
				methods.rightImage.css("left", methods.leftImage.width() + methods.centerImage.width() + 20 + "px");
			});	
			methods.rightImage.load( function () {
				methods.rightImageStorage.css("left", methods.leftImage.width() + methods.centerImage.width() + methods.rightImage.width() + 30 + 'px');
				methods.rightImageStorage.css("display", "block");
			});
		},
		slideLeft : function() {
			if(methods.animating == false)
			{
				methods.animating = true;
				methods.animate1 = true;
				methods.animate2 = true;
				methods.animate3 = true;
				methods.animate4 = true;
				methods.rightImageStorage.css("left", methods.leftImage.width() + methods.centerImage.width() + methods.rightImage.width() + 30 + 'px');
				methods.rightImageStorage.css("display", "block");
				methods.leftImage.animate({
					opacity: 0,
					left: '-' + methods.leftImage.width() + "px"
				}, methods.slideSpeed, function() {
					$(this).css("display", "none");
					methods.animate1 = false;
				});
				
				methods.centerImage.animate({
					left: 0
				}, methods.slideSpeed, function() {
					methods.animate2 = false;
				});
				
				methods.rightImage.animate({
					left: methods.centerImage.width() + 10 + 'px'
				}, methods.slideSpeed, function() {
					methods.animate3 = false;
				});
				
				methods.rightImageStorage.animate({
					opacity:1,
					left: methods.centerImage.width() + methods.rightImage.width() + 20 + "px"
				}, methods.slideSpeed, function() {
					methods.animate4 = false;
				});
				
				if(!methods.animate1 && !methods.animate2 && !methods.animate3 && !methods.animate4)
				{
					methods.animating = false;
					// Update index and then update the image variables
					if(methods.curIndex + 1 == methods.galleryArr.length)
					{
						methods.curIndex = 0;
					} else methods.curIndex++;
					methods.updateImages();
				}
			}
			else if(!methods.animate1 && !methods.animate2 && !methods.animate3 && !methods.animate4)
			{
				methods.animating = false;
				// Update index and then update the image variables
				if(methods.curIndex + 1 == methods.galleryArr.length)
				{
					methods.curIndex = 0;
				} else methods.curIndex++;
				methods.updateImages();
			}
		},
		updateImages : function() {
			methods.centerImage = methods.galleryArr[methods.curIndex];
			methods.leftImage = methods.getLeft();
			methods.rightImage = methods.getRight();
			methods.rightImageStorage = methods.getRightStorage();
		},
		getRight : function() {
			if(methods.curIndex + 1 == methods.galleryArr.length)
			{
				return methods.galleryArr[0];
			} else return methods.galleryArr[methods.curIndex+1];
		},
		getLeft : function() {
			if(methods.curIndex == 0)
			{
				return methods.galleryArr[methods.galleryArr.length - 1];
			} else return methods.galleryArr[methods.curIndex-1];
		},
		getRightStorage : function () {
			if(methods.curIndex + 2 == methods.galleryArr.length)
			{
				return methods.galleryArr[0];
			} 
			else if(methods.curIndex + 2 > methods.galleryArr.length)
			{
				return methods.galleryArr[1];
			} else return methods.galleryArr[methods.curIndex+2];
		}
	}
	
    $.fn.slidingGallery = function (method) {
		methods.context = this;
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this.arguments);
        } else {
            $.error('Method ' + method + ' does not exist');
        }
	};
})(jQuery);
