jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 900,
		autoSlide: 5000
	},_options);
	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var change_speed = 1200;
		var _wrap = _hold.find('ul.slider');
		var _el = _hold.find('ul.slider > li');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _t;
		var _active = 0;
		var _prevItem = 0;
		var _btn = $('<ul class="switch"></ul>');
		var _holder = $('div.gallery');
		_holder.append(_btn);
		var _list = $('ul.fader > li');
        var _h = _list.eq(0).outerHeight();
		_list.show();	
		_el.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = _btn.find('a');
		_btn.eq(_active).parent('li').addClass('active');
		function scrollEl(){
            _wrap.eq(0).animate({marginLeft: -(_w * _active) + "px"},{queue:false, duration: _speed});
			_list.eq(0).animate({marginTop: -(_h * _active) + "px"},{queue:false, duration: _speed});
			_btn.parent('li').removeClass('active');
			_btn.eq(_active).parent('li').addClass('active');
			_prevItem = _active;
		}
		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;
		});
		_btn.click(function(){
			_active = _btn.index($(this));
			if(_t) clearTimeout(_t);
			scrollEl();
			runTimer();
			return false;
		});
	});
}

$(document).ready(function(){
	$('div.intro').gallSlide({
		duration: 700,
		autoSlide: 10000
	});
    $("#tweet").tweet({
        username: "kitemobile",
        join_text: "auto",
        avatar_size: 44,
        count: 5,
        auto_join_text_default: "we said,",
        auto_join_text_ed: "we",
        auto_join_text_ing: "we were",
        auto_join_text_reply: "we replied to",
        auto_join_text_url: "we were checking out",
        loading_text: "<p>Laden Kitemobile tweets..."
    });
});
