/*
 * jQuery ajaxTabSlide v1.1.0 
 */
jQuery.fn.ajaxTabSlide = function(_options){
	// defaults options
	var _options = jQuery.extend({
			btPrev: 'a.prev',
			btNext: 'a.next',
			tabs: 'ul.tabset a',
			holder: 'div',
			scrollEl: 'li',
			loadIco: '.load',
			duration: 2000,
			tabLoadCallback : function(){},
			tabSlideCallback : function(){}
	},_options);

	return this.each(function(){
		var _this = $(this);
		
		var _holder = $(_options.holder, _this);
		var _scrollEl = $('> '+_options.scrollEl, _holder);
		var _next = $(_options.btNext,  _this);
		var _prev = $(_options.btPrev,  _this);
		var _tabs = $(_options.tabs,  _this);
		var _loadIco = $(_options.loadIco,  _this);
		
		var _margin = 0, _first = false,
			_duration = _options.duration;
			_current = 0,
			_step = _holder.innerWidth(),
			_length = _tabs.length,
			_activeSlide = _scrollEl,
			_activeIndex = _current;

		if (_options.btNext) {
			_next.click(function(){
				nextSlides();
				return false;
			});
		}
		if (_options.btPrev) {
			_prev.click(function(){
				if (!$('> '+_options.scrollEl, _holder).is(':animated')) {
					_current -= 1;
					if (_current < 0) _current = _length-1;
					animateTab();
					setActive();
				}
				return false;
			});
		}
		
		if (!_tabs.filter('.active').length) {
			_tabs.eq(0).addClass('active');
		}
		if (_holder.is(':empty')) {
			_current = _tabs.index(_tabs.filter('.active'));
			_first = true;
			animateTab();
		}
		_scrollEl.addClass('loadTab').attr('rel',_tabs.filter('.active').attr('href'));
		
		_tabs.click(function(){
			if (!$(this).hasClass('active') && !$('> '+_options.scrollEl, _holder).is(':animated')) {
				var i = _tabs.index(this);
				if (!$.browser.msie) {
					_tabs.filter('.active').removeClass('active').find('span.corner').fadeOut(_duration);
					$(this).addClass('active').find('span.corner').fadeIn(_duration);
				} else {
					_tabs.filter('.active').removeClass('active').find('span.corner').hide();
					$(this).addClass('active').find('span.corner').show();
				}
				_current = i;
				animateTab();
			}
			return false;
		});
		
		function nextSlides(){
			if (!$('> '+_options.scrollEl, _holder).is(':animated')) {
				_current += 1;
				if (_current >= _length) _current = 0;
				setActive();
				animateTab();
			}
			return false;
		}
		function animateTab(){
			var _rel = _tabs.eq(_current).attr('href');
			var _el = $('.loadTab[rel='+_rel+']');
			if (_el.length) {
				slideThis();
			} else {
				_loadIco.show();
				$.ajax({
					url: _rel,
					success: function(msg){
						var _loadSlide = $(msg);
						_loadSlide.addClass('loadTab').attr('rel',_rel);
						_holder.append(_loadSlide);
						_loadIco.hide();
						_el = _loadSlide;
						_options.tabLoadCallback(_loadSlide);
						if (!_first) {
							_loadSlide.css({'left':-_step});
							slideThis();
						} else {
							_loadSlide.show().css({'left':0}).addClass('activeSlide');
							_activeSlide = _tempEl = _loadSlide;
							_activeIndex = _current;
							_first = false;
						}
					}
				});
			}
			function slideThis(){
				var _tempEl;
				_activeSlide.removeClass('activeSlide');
				_el.addClass('activeSlide');
				_activeSlide.animate({'opacity':0}, {duration:_duration,queue:false, easing:'easeOutExpo'});
				if (_activeIndex < _current) {
					_el.css({'left':_step});
					_activeSlide.animate({'left':-_step}, {duration:_duration, easing:'easeOutExpo', step:function(u,_opt){
						var _tempEnd = _opt.end,
							_tempNow = _opt.now;
						if (_tempEnd < 0) {
							_tempNow = (_tempEnd - _tempNow)* -1;
						}
						if (_tempEl)
							_tempEl.css({'left':_tempNow});
					}});
				} else {
					_el.css({'left':-_step});
					_activeSlide.animate({'left':_step}, {duration:_duration, easing:'easeOutExpo', step:function(u,_opt){
						var _tempNow = _opt.end - _opt.now;
						if (_tempEl)
							_tempEl.css({'left':-_tempNow});
					}});
				}
				_el.css({'opacity':0}).animate({'opacity':1}, {duration:_duration,queue:false, easing:'easeOutExpo', complete:function(){_el.css({'opacity':'auto'})}});
				_options.tabSlideCallback(_el,_activeSlide);
				_activeSlide = _tempEl = _el;
				_activeIndex = _current;
			}
		}
		function setActive () {
			if (_options.tabs) {
				_tabs.removeClass('active');
				_tabs.eq(_current).addClass('active');
			}
		}
	});
}

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});


