/*
 * jQuery tabSlide v1.2.0 
 */
jQuery.fn.tabSlide = function(_options){
	// defaults options
	var _options = jQuery.extend({
			btPrev: 'a.prev',
			btNext: 'a.next',
			tabs: 'ul.tabset a',
			holderList: 'div',
			scrollElParent: 'ul',
			scrollEl: 'li',
			duration: 1000,
			autoSlide: false,
			startAfterClick:10000,
			otherLinks:false,
			animateContentHeight:false
	},_options)

	return this.each(function(){
		var _this = $(this);
		
		var _holder = $(_options.holderList, _this);
		var _mover = $(_options.scrollElParent, _holder);
		var _scrollEl = $(_options.scrollEl, _mover);
		var _next = $(_options.btNext,  _this);
		var _prev = $(_options.btPrev,  _this);
		var _tabs = $(_options.tabs,  _this);
		var _otherLinks = $(_options.otherLinks,  _this);
		
		var _margin = 0;
		var _duration = _options.duration;
		var _current = 0;
		var _step = _holder.innerWidth();
		var _length = _scrollEl.length;
		var _afterTimer = false, _contentHeight, slideHeight = [];

		var _slideTimer = false;
		if (_options.autoSlide) _slideTimer = setInterval(function(){nextSlides()}, _options.autoSlide);
		_scrollEl.each(function(i, slide){
			if (i==0) _contentHeight = $(slide).outerHeight(true);
			slideHeight[i] = $(slide).outerHeight(true);
		});
		if (_options.animateContentHeight)
			_holder.css({'overflow':'hidden','height':_contentHeight});
		
		if (_options.btNext) {
			_next.click(function(){
				checkAutoScroll();
				nextSlides();
				return false;
			});
		}
		if (_options.btPrev) {
			_prev.click(function(){
				checkAutoScroll();
				_current -= 1;
				if (_current < 0) _current = _length-1;
				_margin = _step*_current;
				animateTab();
				animateHeight(_current);
				setActive();
				return false;
			});
		}
		if (_options.tabs) {
			if (!_tabs.parent().filter('.active').length) {
				_tabs.eq(0).parent().addClass('active');
			}
			_tabs.each(function(i, tabLink){
				if ($(tabLink).parent().hasClass('active')) {
					_margin = _step*i;
					_mover.css({'marginLeft': -_margin})
					_current = i;
				}
			});
			_tabs.click(function(){
				var i = _tabs.index(this);
				checkAutoScroll();
				_tabs.parent().removeClass('active');
				_margin = _step*i;
				animateTab();
				animateHeight(i);
				_current = i;
				$(this).parent().addClass('active');
				return false;
			});
		}
		if (_options.otherLinks) {
			_otherLinks.click(function(){
				checkAutoScroll();
				_current = parseInt(this.href.substr(this.href.indexOf('#')+1))-1;
				_margin = _step*_current;
				animateTab();
				animateHeight(_current);
				setActive();
				return false;
			});
		}
		function checkAutoScroll(){
			if (_slideTimer) clearInterval(_slideTimer);
			if (_afterTimer) clearTimeout(_afterTimer);
			if (_options.autoSlide && _options.startAfterClick) {
				_afterTimer = setTimeout(function(){
					_slideTimer = setInterval(function(){nextSlides()},_options.autoSlide);
				},_options.startAfterClick-_options.autoSlide)
			}
		}
		function nextSlides(){
			_current += 1;
			if (_current >= _length) _current = 0;
			_margin = _step*_current;
			setActive();
			animateTab();
			animateHeight(_current);
			return false;
		}
		function animateTab(){
			_mover.animate({'marginLeft': -_margin}, {duration:_duration, queue:false});
		}
		function animateHeight(_index){
			if (_options.animateContentHeight) {
				_holder.animate({'height': slideHeight[_index]}, {duration:_duration, queue:false});
			}
		}
		function setActive () {
			if (_options.tabs) {
				_tabs.parent().removeClass('active');
				_tabs.eq(_current).parent().addClass('active');
			}
		}
	});
}

$(document).ready(function(){
	var _slider = $('div.slider-mover');
	$('> ul',_slider).css({'float':'left', 'width':$('> ul',_slider).width()});
	_slider.css('width',31999);
	
	$('div.aside2, div.slider, div.article-holder').tabSlide({
		tabs: 'div.post-swicher:first ul a',
		holderList: 'div.slide-holder',
		scrollElParent: '> div.slider-mover',
		scrollEl: '> ul, > div.post',
		duration: 1000,
		animateContentHeight:true
	});
	
	$('div.carusel-holder').tabSlide({
		tabs: 'ul.swicher a',
		holderList: '#carusel',
		scrollElParent: 'ul',
		scrollEl: '> li',
		duration: 300,
		autoSlide: 6000
	});
	
	var _aside2 = $('div.aside2');
	var _mainTabs = $('div.post-swicher:first ul a', _aside2);
	var _subTabs = $('div.side-social div.post-swicher ul a', _aside2);
	_subTabs.eq(0).parent().addClass('active');
	
	_mainTabs.each(function(i){
		$(this).mouseup(function(){
			_subTabs.parent().removeClass('active')
			_subTabs.eq(i).parent().addClass('active');
		});
	});
	_subTabs.each(function(i){
		$(this).click(function(){
			_subTabs.parent().removeClass('active')
			$(this).parent().addClass('active');
			_mainTabs.eq(i).trigger('click');
			return false;
		});
	})
});
