/*
v 1.0
**
	animateFunc:  	1: - Fade
				2: - ScrollLeft
				3: - ScrollRight
				4: - ScrollTop
				5: - ScrollBottom
				6: - All
				6: - Random
	switchTime: 2000,  false or number 1000 = 1sec time between switch slides
	duration : 1000			 animation (integer)
	numHolder: false,			conteiner of thumbs list (true or false)
	numCreate: false,			create thumbs list automaticaly (true or false)
	scrollElParent: '> ul.g-items',	conteiner of slides
	scrollEl: '> li',			conteiner of 1 slide
	autoStart: true,			can be true , false  or some integer ( delay before start autoRotation 1000 = 1 sec )
	numShow: true,			show thumbs list (true or false)
**
*/


jQuery.fn.uniShow = function(_options){
	// defaults options
	var _options = jQuery.extend({
		scrollElParent: '> ul.g-items',
		scrollEl: '> li',
		numShow: true,
		numHolder: false,
		numCreate: false,
		animateFunc: 'Random',
		autoStart: true,
		switchTime: 2000,
		duration : 1000
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _list = jQuery(_options.scrollEl, _this);
		var _a = _list.index(_list.filter('.active:eq(0)'));
		
		if(_a == -1) _a = 0;
		_list.hide().eq(_a).show();
		var _d = _options.duration;
		var _t = null;
		var _key = 1;  // for  all func animate
		var _autoslide = _options.switchTime;
		var _animateFunc = _options.animateFunc;
		var _isFirstSlide = true;
		
		//////********************** animation function
		if(_animateFunc=='Fade') 				_animateFunc = 1;
		else if(_animateFunc=='ScrollRight') 	_animateFunc = 2;
		else if(_animateFunc=='ScrollLeft')		_animateFunc = 3;
		else if(_animateFunc=='ScrollBottom') 	_animateFunc = 4;
		else if(_animateFunc=='ScrollTop') 		_animateFunc = 5;
		else if(_animateFunc=='All') 			_animateFunc = 6;
		else if(_animateFunc=='None') 			_animateFunc = 7;
		else if(_animateFunc=='Random') 		_animateFunc = 8;

		/////*************** Create NumList 
		jQuery.fn.uniShow.numListCreate = function(numHolder, _list){
			for(var i=0; i<_list.length; i++) {
				_numListElC += '<li><a href="">'+i+'</a></li>';
			}
			jQuery(numHolder).html('<ul>'+_numListElC+'</ul>');
		};

		//////**************** autoslide
		if(_options.autoStart) setTimeout(function(){
				autoRotate();
			},_options.autoStart);
		function autoRotate(){
			
			currentLi = $(".switcher ul li").eq(_a);
			jQuery("[class^=thumbnailItem]").removeClass("thumbnailItemActive").addClass("thumbnailItem");
			jQuery(currentLi).removeClass("thumbnailItem").addClass("thumbnailItemActive");
			
			if(_t) clearTimeout(_t);
			if (_autoslide){
				
				// Check if it's the first slide 
				// If it is - only delay for _autoslide seconds
				// if not - delay for _autoslide+_d seconds
				if (!_isFirstSlide) { _st = _autoslide + _d; } 
				else { _st = _autoslide; }
				
				_t = setTimeout(function(){
					if(_a < _list.length - 1) method(_a + 1);
					else method(0);
				}, _st);
				
				_isFirstSlide = false;
			}
		}

		//////**************** init NumClick
		if(_options.numHolder){
			var _numHolder = jQuery(_options.numHolder, _this);
			if(!_options.numShow) {
				_numHolder.hide();
				_this.hover(function(){
					if(_numHolder.is(':animated')) _numHolder.stop(true,true);
					_numHolder.slideDown(_d/2);
				},function(){
					_numHolder.slideUp(_d/2);
				})
			}
			numClick();
			if(_options.numCreate){
				jQuery.fn.uniShow.numListCreate(_numHolder, _list);
			}
		}

		////****************** select animation method
		function method(_ind){
			
			switch(_animateFunc){
				case 1: 
					changeEl(_ind);
					break;
				case 2: 
					ScrollFromLeft(_ind);
					break;
				case 3: 
					ScrollFromRight(_ind);
					break;
				case 4: 
					ScrollFromTop(_ind);
					break;
				case 5: 
					ScrollFromBottom(_ind);
					break;
				case 6: 
					allAnimateFunc(_ind);
					break;
				case 7: 
					switchEffect(_ind);
					break;
				default: 
					randomAnimateFunc(_ind);
					break;
			}
		}
		/////**************** thumbs click
		function numClick(){
			var _thumbs = _numHolder.find('a.thumbnail');
			_thumbs.live('click',function(){
				thumbsContainer = jQuery(this).parent().parent();
				jQuery("[class^=thumbnailItem]").removeClass("thumbnailItemActive").addClass("thumbnailItem");
				jQuery(this).parent().removeClass("thumbnailItem").addClass("thumbnailItemActive");
				var _ind = _thumbs.index(jQuery(this));
				method(_ind);
				return false;
			})
			
		}

		/////************* fade Change 
		function changeEl(_ind){
			
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.css({zIndex: 1});
				_list.eq(_a).removeClass('active').css({
					display:'block',
					opacity: 1,
					zIndex: 2,
					left: 0,
					top: 0
				});
				_list.eq(_ind).css({
					display:'block',
					opacity: 0,
					left: 0,
					zIndex: 3,
					top: 0
				}).addClass('active').animate({opacity: 1}, {queue:false, duration:_d});
				
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d});
				
				_a = _ind;
			}
			if (_options.autoStart) autoRotate();
		}
		
		/////************* None effect
		function switchEffect(_ind){
			
			if(_t) clearTimeout(_t);
			
			if(_ind != _a){
				
				// Hide all slides
				$.each(_list, function() {
					$(this).css('display', 'none');
				});
				
				// Display only current slide
				_list.eq(_ind).css('display', 'block');
				
				// Remember new current slide index
				_a = _ind;
			}
			if (_options.autoStart) autoRotate();
		}

		/////////*************** Scroll From Left to Right
		function ScrollFromLeft(_ind){
			var _left = _list.eq(_a).outerWidth(true);
			_list.css({zIndex: 1});
			_list.eq(_a).removeClass('active').css({
				display:'block',
				opacity: '',
				zIndex: 2,
				left: 0,
				top: 0
			});
			_list.eq(_ind).css({
				display:'block',
				opacity: '',
				zIndex: 3,
				left: -_left,
				top: 0
			}).addClass('active').animate({left: 0},{queue:false, duration:_d});
			
			_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d});
			
			_a = _ind;
			if (_options.autoStart) autoRotate();
		}

		/////////*************** Scroll From Right to Left
		function ScrollFromRight(_ind){
			var _right = _list.eq(_a).outerWidth(true);
			_list.css({zIndex: 1});
			_list.eq(_a).removeClass('active').css({
				display:'block',
				opacity: '',
				zIndex: 2,
				left: 0,
				top: 0
			});
			_list.eq(_ind).css({
				display:'block',
				opacity: '',
				zIndex: 3,
				left: _right,
				top: 0
			}).addClass('active').animate({left: 0},{queue:false, duration:_d});
			
			_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d});
			
			_a = _ind;
			if (_options.autoStart) autoRotate();
		}

		/////////*************** Scroll From Top to Bottom
		function ScrollFromTop(_ind){
			var _top = _list.eq(_ind).outerHeight(true);
			_list.css({zIndex: 1});
			_list.eq(_a).removeClass('active').css({
				display:'block',
				opacity: '',
				zIndex: 2,
				left: 0,
				top:0
			});
			_list.eq(_ind).css({
				display:'block',
				opacity: '',
				zIndex: 3,
				left: 0,
				top: -_top
			}).addClass('active').animate({top: 0},{queue:false, duration:_d});
			
			_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d});
			
			_a = _ind;
			if (_options.autoStart) autoRotate();
		}

		/////////*************** Scroll From Bottom to Top
		function ScrollFromBottom(_ind){
			var _bottom = _list.eq(_ind).outerHeight(true);
			_list.css({zIndex: 1});
			_list.eq(_a).removeClass('active').css({
				display:'block',
				opacity: '',
				zIndex: 2,
				left: 0,
				top:0
			});
			_list.eq(_ind).css({
				display:'block',
				opacity: '',
				zIndex: 3,
				left: 0,
				top: _bottom
			}).addClass('active').animate({top: 0},{queue:false, duration:_d});
			
			_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d});
			
			_a = _ind;
			if (_options.autoStart) autoRotate();
		}

		///////**************all Animate Function 
		function allAnimateFunc(_ind){
			switch(_key){
				case 1: 
					changeEl(_ind);
					break;
				case 2: 
					ScrollFromLeft(_ind);
					break;
				case 3: 
					ScrollFromRight(_ind);
					break;
				case 4: 
					ScrollFromTop(_ind);
					break;
				case 5: 
					ScrollFromBottom(_ind);
					break;
				default: alert("I'm a crazy program, Don't touch me. Go away!!!");
						break;
				}
				_key++;
				if(_key==6) _key=1;
		}

		function randomIntervalNumber(m,n){return Math.floor( Math.random() * (n - m + 1) ) + m;}
		///////**************random Animate Function 
		function randomAnimateFunc(_ind){
			_key = randomIntervalNumber(1, 5);
			switch(_key){
				case 1: 
					changeEl(_ind);
					break;
				case 2: 
					ScrollFromLeft(_ind);
					break;
				case 3: 
					ScrollFromRight(_ind);
					break;
				case 4: 
					ScrollFromTop(_ind);
					break;
				case 5: 
					ScrollFromBottom(_ind);
					break;
				default: alert("I'm a crazy program, Don't touch me. Go away!!!");
						break;
				}
		}
	})
}
