/* Gallery (slide-on-click, auto-slide-left) */
jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('div.carousel > ul');
		var _el = _hold.find('div.carousel > ul > li');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		var _count = _el.index(_el.filter(':last'))+1;
		var _w = _el.outerWidth(true);
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _t;
		var _active = 0;
		
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		runTimer();
		_next.click(function(){
			_active++;
			if(_t) clearTimeout(_t);
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			runTimer();
			return false;
		});
		_prev.click(function(){
			_active--;
			if(_t) clearTimeout(_t);
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			runTimer();
			return false;
		});
	});
}
/*--- tabs ---*/
function initTabs(){
	$('ul.tabset').each(function(){
		var btn_h = $(this);
		var _btn = $(this).find('a.tab');
		var _a = _btn.parent().index(_btn.parent().filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.parents('div.tweet-box').removeClass('active').eq(_a).addClass('active');
		_btn.each(function(_i){
			this._box = this.href.substr(this.href.indexOf("#") + 1);
			if(this._box){
				this._box = $('#'+this._box);
				if(_i == _a) this._box.show();
				else this._box.hide();
			}
			this.onclick = function(){
				changeTab(_i);
				return false;
			}
		});
		function changeTab(_ind){
			if(_ind != _a){
				if(_btn.get(_a)._box) _btn.get(_a)._box.hide();
				if(_btn.get(_ind)._box) _btn.get(_ind)._box.show();
				_btn.parent().eq(_a).removeClass('active');
				_btn.parent().eq(_ind).addClass('active');
				_a = _ind;
			}
		}
	});
}
$(document).ready(function(){
	$('div.carousel01').gallSlide({
		duration: 700,
		autoSlide: 6000
	});
	$('div.carousel02').gallSlide({
		duration: 1000,
		autoSlide: 5000
	});
	initTabs();
});

