	/*
		
		JQuery photo slider
		
		Copyright (c) 2010 Alex Silaev (http://alex.twimp.ru/) chrome.alex@gmail.com
		Licensed under the GPL (http://www.opensource.org/licenses/gpl-license.php) license.
		
		Created at 30 Nov 2010
		
		=================================
		* Use:
		=================================
			var slider = new Slider({
							'selector': ELEMENT,			// put here the jquery selector
							'images': IMAGES_ARRAY, 		// like [0]=>"src", [1]=>"src"...
							'fadetime': TIME_TO_FADE_IN_MS,	// default = 1000
							'time': TIME_TO_SLIDE_IN_MS,	// default = 500
							'autoplay': true/false			// default = true
						});
		
		=================================
		* Javascript control:
		=================================
			slider.stop();	// stop moving and slide to 0 slide
			slider.play();	// start moving
			slider.next();	// slide to next image
			slider.prev();	// slide to previous image
		
	*/
	var Slider = function(options) {
		
		if (typeof(options['selector']) !== 'undefined') {
			this.selector = options['selector'];
		} else {
			alert('Slider: The selector must be set!');
			return;
		}
		if (typeof(options['images']) !== 'undefined' && options['images'].length > 0) {
			for (src in options['images']) {
				var img = new Image();
				img.src = options['images'][src];
				this.sources.push(img);
			}
		} else {
			alert('Slider: The array images must be set and can not be empty!');
			return;
		}
		if (typeof(options['fadetime']) !== 'undefined') this.slideFadeTime = options['fadetime'];
		if (typeof(options['time']) !== 'undefined') this.slideTime = options['time'];
		if (typeof(options['autoplay']) !== 'undefined') this.autoPlay = options['autoplay'];
		
		var table = "\
			<table style='width:100%;height:270px;border:0px;' cellpadding='0' cellspacing='0' border='0'>\
				<tr><td align='center' valign='middle' class='slide'></td></tr>\
			</table>";
		
		$(table).appendTo(this.selector);
		
		$('<img>').appendTo($(this.selector).find('td:eq(0)'));
		$(this.selector).find('td.slide').find('img:eq(0)')[0].src = this.sources[0].src;
		$(this.selector).css('overflow', 'hidden');
		
		if (this.sources.length > 1) {
			if (this.autoPlay) {
				this.startPlay();
			}
		}
		
	};
	Slider.prototype = {
		
		status: 'stop',
		autoPlay: true,
		selector: null,
		timer: null,
		stimer: null,
		isbusy: false,
		busytimer: null,
		current: 0,
		sources: new Array(),
		slideTime: 1000,
		slideFadeTime: 500,
		
		startPlay: function() {
			
			var a = this;
			this.timer = setTimeout(function() { a.startPlayA(); }, this.slideTime);
			
		},
		
		startPlayA: function() {
			
			this.moveNext();
			
			var a = this;
			this.timer = setTimeout(function() { a.startPlay(); }, this.slideTime);
			
		},
		
		setPhoto: function(dir) {
			
			var a = this;
			
			var obj = $(this.selector).find('td.slide').find('img:eq(0)');
			
			var first = 'right';
			var second = 'left';
			if (dir == 'right') {
				first = 'left';
				second = 'right';
			}
			
			var options = {'mode':'hide', 'direction':first};
			$(obj).effect("drop", options, a.slideFadeTime);
			
			clearTimeout(a.busytimer);
			clearTimeout(a.stimer);
			
			a.isbusy = true;
			a.stimer = setTimeout(function() {
				$(obj)[0].src = a.sources[a.current].src;
				var options = {'mode':'show', 'direction':second};
				$(obj).effect("drop", options, a.slideFadeTime);
			}, a.slideFadeTime);
			
			a.busytimer = setTimeout(function() { a.isbusy = false; }, parseInt(a.slideFadeTime * 2));
			
		},
		
		movePrev: function() {
			
			var a = this;
			var cur = a.current;
			
			if (cur > 0) {
				a.current--;
			} else {
				a.current = a.sources.length - 1;
			}
			
			a.setPhoto('right');
			
		},
		
		moveNext: function() {
			
			var a = this;
			var cur = a.current;
			
			if (a.sources.length - 1 > cur) {
				a.current++;
			} else {
				a.current = 0;
			}
			
			a.setPhoto('left');
			
		},
		
		clear: function(clearcurrent) {
			
			if (this.status != 'manual') {
				clearTimeout(this.stimer);
			} else {
				if (this.isbusy === true) {
					return;
				}
			}
			clearTimeout(this.timer);
			if (clearcurrent) this.current = 0;
			return true;
			
		},
		
		play: function() {
			
			if (this.status == 'play') return;
			
			if (this.clear(false)) {
				this.status = 'play';
				this.startPlayA();
			}
			
		},
		
		stop: function() {
			
			if (this.status == 'stop') return;
			
			if (this.clear(true)) {
				this.status = 'stop';
				this.setPhoto();
			}
			
		},
		
		prev: function() {
			
			if (this.clear(false)) {
				this.status = 'manual';
				this.movePrev();
			}
			
		},
		
		next: function() {
			
			if (this.clear(false)) {
				this.status = 'manual';
				this.moveNext();
			}
			
		}
		
	}
	

