var jQueryXB = jQuery.noConflict(true);

;(function(jQueryXB){
jQueryXB.fn.uniShow = function(_options){
	// defaults options
	var _options = jQueryXB.extend({
		width: 603,
		height: 90,
		bannerText: false,
		bgColor: false,
		scrollElParent: '> ul',
		scrollEl: '> li',
		onMouseOver: null,
		numHolderClass: false,
		thumsBG: '#f00',
		thumsBGActive: '#0f0',
		transition: 'fade',
		autoStart: true,
		switchTime: 5000,
		duration : 500,
		CwidthBG: '11px',
		CheightBG: '11px',
		NwidthBG: '11px',
		NheightBG: '11px'
	},_options);

	return this.each(function(){
		var _this = this;

		//options
		_this.opt = {
			width 			:				_options.width,
			height			:				_options.height,
			bannerText 		:				_options.bannerText,
			bgColor			:				_options.bgColor,
			scrollElParent 	:				_options.scrollElParent,
			scrollEl 		:				_options.scrollEl,
			numHolderClass 	:				_options.numHolderClass ? ((_options.numHolderClass==true) ? ' ' : _options.numHolderClass) : false,
			onMouseOver 	:				_options.onMouseOver,
			pause 			:				false,
			animated		:				false,
			thumbs 			:				null,
			thumsBG			:				_options.thumsBG,
			thumsBGActive 	:				_options.thumsBGActive,
            CwidthBG	:				_options.CwidthBG,
			CheightBG	:				_options.CheightBG,
			NwidthBG	:				_options.NwidthBG,
			NheightBG	:				_options.NheightBG,
			transition 		:				_options.transition,
			autoStart 		:				_options.autoStart,
			switchTime 		:				_options.switchTime,
			duration 		:				_options.duration,
			textColor	:				_options.textColor
		};

		//options

		//get element 
		_this.parent = jQueryXB(_this).find(_this.opt.scrollElParent);
		_this.els = jQueryXB(_this.opt.scrollEl, _this.parent);

		//def settings for holder 
		jQueryXB(_this).css({
			position: 'relative',
			overflow: 'hidden',
			width: _this.opt.width,
			height: _this.opt.height,
			'background-color': _this.opt.bgColor
		}).mouseenter(function(){
			if (_this.opt.onMouseOver) {
				if(_this.opt.timer) clearTimeout(_this.opt.timer);
				_this.opt.pause = true;
			} else {
				//jQueryXB.fn.uniShow.autoslide(_this, _this.els, _this.opt);
			};
		}).mouseleave(function(){
		        
			_this.opt.pause = false;
			if (!_this.opt.was_clicked && _this.opt.onMouseOver) {
				jQueryXB.fn.uniShow.autoslide(_this, _this.els, _this.opt);
			}
		});
		jQueryXB(_this.parent).css({
			position: 'relative',
			overflow: 'hidden',
			width: _this.opt.width,
			height: _this.opt.height
		});
		jQueryXB(_this.els).css({
			position: 'absolute',
			top:0,
			left:0,
			width: _this.opt.width,
			height: _this.opt.height
		});

		//create thumbs
		_this.opt.active = 0;
		_this.els.eq(_this.opt.active).css('zIndex',1);
		_this.opt.next = 0;
		_this.opt.prev = 0;
		_this.opt.is_nav_started = 0;

		if(_this.opt.numHolderClass && _this.els.length > 1){
						
			if (!_this.opt.is_nav_started) {
				
				var Cheight_num = _this.opt.CheightBG.replace("px", "");
				var Nheight_num = _this.opt.NheightBG.replace("px", "");
						
				var nav_padding = Math.max(Cheight_num, Nheight_num) + 8;
				
				jQueryXB(_this).css({
					padding: "0 0 "+nav_padding+"px"
				});
								
				var _html = '<ul class="'+_this.opt.numHolderClass+'">';
				for(var i=0; i<_this.els.length; i++) {
					_html+='<li><a href="#">'+(i+1)+'</a></li>';
				};
				_html += '</ul>';
				_this.opt.thumbs = jQueryXB(_html);
				jQueryXB(_this).append(_this.opt.thumbs);
				
			}
			_this.opt.thumbs = _this.opt.thumbs.find('li');
			jQueryXB.fn.uniShow.activatePager(_this, _this.opt);
			
			_this.opt.thumbs.click(function(){
				if(_this.opt.thumbs.index(this) != _this.opt.active && !_this.opt.animated){
					_this.opt.animated = true;
					_this.opt.prev = _this.opt.next;
					_this.opt.next = _this.opt.thumbs.index(this);
					
					jQueryXB.fn.uniShow.activatePager(_this, _this.opt);
					_this.opt.was_clicked = true;
					_this.opt.pause = false;
					var init = jQueryXB.fn.uniShow.transition[_this.opt.transition];
					if (jQueryXB.isFunction(init)) init(_this, _this.els, _this.opt);
					jQueryXB.fn.uniShow.autoslide(_this, _this.els, _this.opt);
				}
				return false;
			});
		};

		if(_this.opt.bannerText){
			_this.bText = jQueryXB('<p style="height:'+(nav_padding-6)+'px;">'+_this.opt.bannerText+'</p>');
			_this.bText.css({color: _this.opt.textColor});
			jQueryXB(_this).append(_this.bText);
		};
	
		jQueryXB.fn.uniShow.autoslide(_this, _this.els, _this.opt);
	});
};

jQueryXB.fn.uniShow.activatePager = function(_this, opt){
	if(_this.opt.thumbs){
		_this.opt.thumbs.eq(_this.opt.next).addClass('active');

		if (!_this.opt.is_nav_started) {
			_this.opt.thumbs.css({
			background: _this.opt.thumsBG,
			width: _this.opt.NwidthBG,
			height: _this.opt.NheightBG
			});
			_this.opt.is_nav_started = 1;
		}
		
		_this.opt.thumbs.eq(_this.opt.next).css({
			background: _this.opt.thumsBGActive,
			width: _this.opt.CwidthBG,
			height: _this.opt.CheightBG
		});
		if (!(_this.opt.prev == _this.opt.next)) {
		_this.opt.thumbs.eq(_this.opt.prev).css({
			background: _this.opt.thumsBG,
			width: _this.opt.NwidthBG,
			height: _this.opt.NheightBG

		});
		}
		
	};
}


//autorotation
jQueryXB.fn.uniShow.autoslide = function(_this, els, opt){
	if(opt.switchTime && !_this.opt.pause){
		var _delay = 0
		
		if(opt.autoStart && opt.autoStart!=true){
			_delay = _this.opt.autoStart;
		}

		setTimeout(function() {
			if (!_this.opt.was_clicked) {
			opt.timer = setTimeout(function(){
				
					if (!(!opt.next && !opt.prev)) {
						opt.prev = opt.next;
					}
					opt.next = opt.active;
			
					if(opt.active!= els.length-1) opt.next++;
					else opt.next =0;
					_this.opt.animated = true;
				
					jQueryXB.fn.uniShow.activatePager(_this, opt);
					
					var init = jQueryXB.fn.uniShow.transition[opt.transition];
									
					
					if (jQueryXB.isFunction(init)) { 
						init(_this, els, opt) 
					}
				
			},_this.opt.switchTime);
			} else {
				_this.opt.was_clicked = false;
			}
		},_delay);
	}
};

jQueryXB.fn.uniShow.transition = {};

})(jQueryXB);
