	/*
		
		JQuery text 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
							'items': ITEMS_SELECTOR, 		// selector
							'fadetime': TIME_TO_FADE_IN_MS,	// default = 1000
							'time': TIME_TO_SLIDE_IN_MS,	// default = 500
							'autoplay': true/false			// default = true
							'fadetimewait': TIME_TO_WAIT_BETWEEN_ANIMATIONS		// default = 500
						});
		
		=================================
		* 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 SliderText = function(options) {
		
		var current = 0;
		
		if (typeof(options['selector']) !== 'undefined') {
			this.selector = options['selector'];
		} else {
			alert('Slider: The selector must be set!');
			return;
		}
		if (typeof(options['items']) !== 'undefined' && options['items'].length > 0) {
			var a = this;
			$(options['items']).each(function(i) {
				$(this).attr('slideindex', i);
				a.sources.push($(this));
			});
		} else {
			alert('Slider: The array items must be set and can not be empty!');
			return;
		}
		if (typeof(options['fadetime']) !== 'undefined') this.slideFadeTime = options['fadetime'];
		if (typeof(options['fadetimewait']) !== 'undefined') this.slideFadeTimeWait = options['fadetimewait'];
		if (typeof(options['time']) !== 'undefined') this.slideTime = options['time'];
		if (typeof(options['autoplay']) !== 'undefined') this.autoPlay = options['autoplay'];
		
		$(options['items']).hide();
		
		$(options['items'] + ':eq('+current+')').show();
		
		this.items = options['items'];
		
		this.current = current;
		
		this.prevNextHide();
		
		if (this.sources.length > 1) {
			if (this.autoPlay) {
				this.startPlay();
			}
		}
		
	};
	SliderText.prototype = {
		
		status: 'stop',
		autoPlay: true,
		selector: null,
		items: null,
		timer: null,
		stimer: null,
		isbusy: false,
		busytimer: null,
		current: 0,
		pcurrent: 0,
		sources: new Array(),
		slideTime: 1000,
		slideFadeTime: 500,
		slideFadeTimeWait: 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 objc = $(this.items + '[slideindex=' + this.pcurrent + ']');
			
			if (dir == 'right') {
				var first = 'right';
				var second = 'left';
			} else if (dir == 'left') {
				first = 'left';
				second = 'right';
			} else {
				first = 'down';
				second = 'up';
			}
			
			$('.advice_icons li').removeClass('active');
			$('.advice_icons li:eq('+this.current+')').addClass('active');
			
			this.prevNextHide();
			
			var options = {'mode':'hide', 'direction':first};
			$(objc).effect("drop", options, a.slideFadeTime);
			
			clearTimeout(a.busytimer);
			clearTimeout(a.stimer);
			
			a.isbusy = true;
			a.stimer = setTimeout(function() {
				var obj = a.sources[a.current];
				var options = {'mode':'show', 'direction':second};
				$(obj).effect("drop", options, a.slideFadeTime);
			}, a.slideFadeTimeWait);
			
			a.busytimer = setTimeout(function() { a.isbusy = false; }, parseInt(a.slideFadeTime * 2));
			
		},
		
		prevNextHide: function() {
			
			if (this.current == 0) {
				$('.advice_stepper .prev').hide(700);
			} else {
				$('.advice_stepper .prev').show(700);
			}
			
			if (this.current == this.sources.length - 1) {
				$('.advice_stepper .next').hide(700);
			} else {
				$('.advice_stepper .next').show(700);
			}
			
		},
		
		set: function(index) {
			
			if (this.current != index && this.isbusy === false) {
				this.pcurrent = this.current;
				this.current = index;
				this.setPhoto();
			}
			
		},
		
		movePrev: function() {
			
			var a = this;
			var cur = a.current;
			this.pcurrent = cur;
			
			if (cur > 0) {
				a.current--;
			} else {
				a.current = a.sources.length - 1;
			}
			
			a.setPhoto('right');
			
		},
		
		moveNext: function() {
			
			var a = this;
			var cur = a.current;
			this.pcurrent = cur;
			
			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();
			}
			
		}
		
	}
	

