/*
 * 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') && !$(tabLink).hasClass('hide')) {
					_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(){
	$('div.questions').tabSlide({
		btPrev: 'a.prev',
		btNext: 'a.next',
		tabs: 'ul.menu a',
		holderList: 'div.gallery2 div.holder',
		scrollElParent: '> ul',
		scrollEl: '> li',
		duration: 800,
		autoSlide: false,
		startAfterClick:10000,
		otherLinks:false,
		animateContentHeight:false
	});
	$('div.freeq-box').tabSlide({
		btPrev: 'div.arrow a.prev',
		btNext: 'a.next',
		tabs: 'div.coins-box ul a',
		holderList: 'div.freeq div.holder',
		scrollElParent: '> ul',
		scrollEl: '> li',
		duration: 800,
		autoSlide: false,
		startAfterClick:10000,
		otherLinks:false,
		animateContentHeight:false
	});	
})
