/*
v 1.0
**
	animateFunc:  	1: - Fade
				2: - FromLeft
				3: - FromRight
				4: - FromTop
				5: - FromBottom
				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',
		btPrev: 'a.prev',
		btNext: 'a.next',
		numShow: true,
		numHolder: false,
		numCreate: false,
		animateFunc: 'Random',
		autoStart: false,
		special: false,
		switchTime: 2000,
		duration : 1000
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _next = jQuery(_options.btNext, _this).length ? jQuery(_options.btNext, _this) : false;
		var _prev = jQuery(_options.btPrev, _this).length ? jQuery(_options.btPrev, _this) : false;
		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 + _options.duration;
		var _animateFunc = _options.animateFunc;
		

		//////********************** animation function
		setFunc();
		function setFunc(){
			if(_animateFunc=='Fade') 			_animateFunc = 1;
			else if(_animateFunc=='FromLeft') 	_animateFunc = 2;
			else if(_animateFunc=='FromRight')	_animateFunc = 3;
			else if(_animateFunc=='FromTop') 	_animateFunc = 4;
			else if(_animateFunc=='FromBottom') _animateFunc = 5;
			else if(_animateFunc=='All') 		_animateFunc = 6;
			else if(_animateFunc=='Random') 	_animateFunc = 7;
		}
		/////*************** 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>');
		};


		//***************** prev click 
		
		_prev.click(function(){
							 
			if(_list.is('animated')) _list.stop(true,true);
			if(_t) clearTimeout(_t);
			if(_options.special){
				if(_a == 0) {
					_animateFunc = 'Fade';
					setFunc();
					method(_list.length-1);
				}else{
					_animateFunc = 'FromLeft';
					setFunc();
					method(_a -1);
				}
			}else{
				if(_a == 0) {
					method(_list.length-1);
				}else {
					method(_a -1);
				}
			}
			return false;
		});

		_next.click(function(){
			if(_list.is('animated')) _list.stop(true,true);
			if(_t) clearTimeout(_t);
			if(_options.special){
				if(_a < _list.length-1) {
					_animateFunc = _options.animateFunc;
					setFunc();
					method(_a + 1);
				}
				else {
					_animateFunc = 'Fade';
					setFunc();
					method(0);
				}
			}else{
				if(_a < _list.length-1) {
					method(_a + 1);
				}
				else {
					method(0);
				}
			}
			return false;
		})


		//////**************** autoslide
		if(_options.autoStart) setTimeout(function(){
				autoRotate();
			},_options.autoStart);
		function autoRotate(){
			if(_t) clearTimeout(_t);
			if (_autoslide){
				_t = setTimeout(function(){
					if(_options.special){
						if(_a < _list.length-1) {
							_animateFunc = _options.animateFunc;
							setFunc();
							method(_a + 1);
						}
						else {
							_animateFunc = 'Fade';
							setFunc();
							method(0);
						}
					}else{
						if(_a < _list.length-1) {
							method(_a + 1);
						}
						else {
							method(0);
						}
					}
				}, _autoslide);
			}
		}

		//////**************** 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;
				default: 
					randomAnimateFunc(_ind);
					break;
			}
		}
		/////**************** thumbs click
		function numClick(){
			var _thumbs = _numHolder.find('a');
			_thumbs.live('click',function(){
				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
				}).animate({opacity: 0}, {queue:false, duration:_d});
				_list.eq(_ind).css({
					display:'block',
					opacity: 0,
					left: 0,
					zIndex: 3,
					top: 0
				}).addClass('active').animate({opacity: 1}, {queue:false, duration:_d,complete: function(){
					autoRotate();
				}});
				_a = _ind;
			}
		}

		/////////*************** Scroll From Left to Right
		function ScrollFromLeft(_ind){
			var _left = _list.eq(_a).outerWidth(true);
			_list.css({zIndex: 1});
			_list.eq(_ind).css({
				display:'block',
				opacity: '',
				zIndex: 3,
				left: -_left,
				top: 0
			}).addClass('active').animate({left: 0},{queue:false, duration:_d,complete: function(){
				autoRotate();
			}});
			_list.eq(_a).removeClass('active').css({
				display:'block',
				opacity: '',
				zIndex: 2,
				left: 0,
				top: 0
			}).animate({left: _left},{queue:false, duration:_d});
			_a = _ind;
		}

		/////////*************** Scroll From Right to Left
		function ScrollFromRight(_ind){
			var _right = _list.eq(_a).outerWidth(true);
			_list.css({zIndex: 1});
			_list.eq(_ind).css({
				display:'block',
				opacity: '',
				zIndex: 3,
				left: _right,
				top: 0
			}).addClass('active').animate({left: 0},{queue:false, duration:_d,complete: function(){
				autoRotate();
			}});
			_list.eq(_a).removeClass('active').css({
				display:'block',
				opacity: '',
				zIndex: 2,
				left: 0,
				top: 0
			}).animate({left: -_right}, {queue:false, duration:_d});
			_a = _ind;
		}

		/////////*************** Scroll From Top to Bottom
		function ScrollFromTop(_ind){
			var _top = _list.eq(_ind).outerHeight(true);
			_list.css({zIndex: 1});
			_list.eq(_ind).css({
				display:'block',
				opacity: '',
				zIndex: 3,
				left: 0,
				top: -_top
			}).addClass('active').animate({top: 0},{queue:false, duration:_d,complete: function(){
				autoRotate();
			}});
			_list.eq(_a).removeClass('active').css({
				display:'block',
				opacity: '',
				zIndex: 2,
				left: 0,
				top:0
			}).animate({left: top}, {queue:false, duration:_d});
			_a = _ind;
		}

		/////////*************** Scroll From Bottom to Top
		function ScrollFromBottom(_ind){
			var _bottom = _list.eq(_ind).outerHeight(true);
			_list.css({zIndex: 1});
			_list.eq(_ind).css({
				display:'block',
				opacity: '',
				zIndex: 3,
				left: 0,
				top: _bottom
			}).addClass('active').animate({top: 0},{queue:false, duration:_d,complete: function(){
				autoRotate();
			}});
			_list.eq(_a).removeClass('active').css({
				display:'block',
				opacity: '',
				zIndex: 2,
				left: 0,
				top:0
			}).animate({left: -_bottom}, {queue:false, duration:_d});
			_a = _ind;
		}

		///////**************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;
				}
		}
	})
}

