var tim;
(function($) {

	function SJGalery() {}

	var PROP_NAME = 'SJGalery';

	var html = {
		content: '<div class="sjg_back"></div><div class="sjg_ahead"></div><div class="sjg_panel"><div></div><div></div></div>'
	};

	var selectors = {
		images:	'.sjg_ahead, .sjg_back',
		ahead:	'.sjg_ahead',
		back:	'.sjg_back',
		panel:	'.sjg_panel',
		fwd:	'.sjg_panel div:eq(0)',
		bwd:	'.sjg_panel div:eq(1)'
	};

	var Instance = function(target, options) {
		this._defaults = {
			folder: '',
			list: [],
			class_fwd: 'fwd',
			class_bwd: 'bwd',
			interval: 8000
		};

		this.construct(target, options);
	};

	$.extend(Instance.prototype, {

		options: {},

		target: null,

		idx: 0,

		intervalInstance: null,

		images: [],

		construct: function(target, options) {
			this.options = $.extend({}, this._defaults, options);
			this.target = target;

			for (var i=0; i<this.options.list.length; i++) {
				if (typeof(this.options.list[i]) == 'string') {
					var img = $('<img src="' + this.options.folder + '/' + this.options.list[i] + '"/>').css({
						width: '100%'
					});
				} else {
					var img1 = $('<img src="' + this.options.folder + '/' + this.options.list[i][0] + '"/>').css({
						width: '50%',
						float: 'right'
					});
					var img2 = $('<img src="' + this.options.folder + '/' + this.options.list[i][1] + '"/>').css({
						width: '50%',
						float: 'right'
					});
					var img = $('<div></div>').append(img1).append(img2);
				}

				this.images.push(img);
			}
		},

		inc: function() {
			this.idx++;
			if (this.options.list[this.idx] == undefined) {
				this.idx = 0;
			}
		},

		dec: function() {
			this.idx--;
			if (this.options.list[this.idx] == undefined) {
				this.idx = this.options.list.length-1;
			}
		},

		/**
		 * Return next image and put it at the end
		 */
		imageGet: function() {
			this.inc();
			return this.images[this.idx];
		},

		run: function(callback, first_call) {
			var self = this;
            tim=this;
			if (first_call == undefined || first_call == true) {
				callback(self.imageGet());
			}
			this.intervalInstance = setInterval(function() {
				callback(self.imageGet());
			}, this.options.interval);
		},

		stop: function() {
			clearInterval(this.intervalInstance);
		},

		next: function(callback) {
			this.stop();
		//	this.inc();
			callback(this.imageGet());
			this.run(callback, false);
		},

		prev: function(callback) {
			this.stop();
			this.dec();
			this.dec();
			callback(this.imageGet());
			this.run(callback, false);
		}
	});

	$.extend(SJGalery.prototype, {

		_getInst: function(target)
		{
			try {
				return $.data(target, PROP_NAME);
			} catch (err) {
				throw 'Missing instance data for this SJGalery';
			}
		},

		_connectSJGalery: function(target, options)
		{
			// create new inst
			var inst = new Instance(target, options);
			var $target = $(target);

			$target.html(html.content);
			this.setStyles($target);

			//save
			$.data(target, PROP_NAME, inst);

			var self = this;
			$target.find(selectors.fwd).addClass(inst.options.class_fwd).click(function() {
				self.next(target);
			});
			$target.find(selectors.bwd).addClass(inst.options.class_bwd).click(function() {
				self.prev(target);
			});

			//run
			this._runSJGalery(target);
		},

		_runSJGalery: function(target) {
			var inst = this._getInst(target);
			var self = this;
			inst.run(function(image) {

				self.showImage($(target), image);
			});
		},

		//PRIVATE

		next: function(target) {

			var inst = this._getInst(target);
			var self = this;
			inst.next(function(image) {
				self.showImage($(target), image);
			});
		},

		prev: function(target) {
			var inst = this._getInst(target);
			var self = this;
			inst.prev(function(image) {
				self.showImage($(target), image);
			});
		},

		showImage: function($target, image) {
			if ($target.find(selectors.ahead).is(':visible')) {
				$target.find(selectors.back).html(image);
				$target.find(selectors.ahead).fadeOut(1500, function() {$(this).hide()});
			} else {
				$target.find(selectors.ahead).html(image);
				$target.find(selectors.ahead).fadeIn(1500, function() {$(this).show()});
			}
		},

		setStyles: function($target) {
			$target.css('position', 'relative');
			$target.find(selectors.images).css({
				position: 'absolute',
				width: '100%',
				height: '100%',
				top: 0,
				left: 0,
				background: '#ffffff',
				overflow: 'hidden'
			});
			$target.find(selectors.panel).css({
				position: 'absolute',
				right: '20px',
				bottom: '20px'
			});
			$target.find(selectors.fwd).css({
				float: 'right',
				margin: '5px'
			});
			$target.find(selectors.bwd).css({
				float: 'left',
				margin: '5px'
			});
		}
	});

	$.fn.sjgalery = function(options)
	{
        
		if (!$.SJGalery.initialized) {
			$.SJGalery.initialized = true;
		}
		var otherArgs = Array.prototype.slice.call(arguments, 1);
		if (typeof options == 'string' && this.length > 0) {
			return $.SJGalery['_' + options + 'SJGalery'].apply($.SJGalery, [this[0]].concat(otherArgs));
		}
		return this.each(function() {
			$.SJGalery._connectSJGalery(this, options);
		});
		
	};

	$.SJGalery = new SJGalery();
	$.SJGalery.initialized = false;
	$.SJGalery.uuid = new Date().getTime();

})(jQuery);

