/**
	Please note:
	This script was written by bebel.
	If you want to use it in your projects, please ask for permission. You can contact us through http://thebebel.com

	This script is an easy to use tabs script without the overhead due by jquery.ui. see default options to know how to use it.

*/
(function($) {
	$.fn.btabs = function(options) {

		var
			tabBox = $(this),
			defaults = {
				'count': 3,
				'active': 1,
				'effect': 'slide',
				'effectTime': 600,
                'autoSlide' : false,
                'autoSlideTime': 1000,
                'autoSlideStartTab': 1,
				'debug': false, //if you want to debug your output, set to true or activate via options
				'linkActiveStateClass': 'active', //without dot. (not ".active", but "active")
				'tabWrapperClass': '.tabWrapper'
			},
			settings = $.extend({}, defaults, options),
			a = $('#'+tabBox.attr('id')+'Nav').children().find('a');

            
		//get and count the links, so we know how many tabs we have to loop.
		var countTabs = a.length;
        
		if(countTabs != settings.count && settings.debug === true) {
			alert('You told me there were '+settings.count+' Tabs, but I found '+countTabs);
		}
        
		//only display the first tab, if not already done with css.

		toggleTabs('#'+tabBox.attr('id')+"-"+1);

		//remove unwanted css class
		initCss();

		//define what happens on click
		a.click(function() {
			//on click, activate the desired tab and hide every other tab.
			toggleTabs($(this).attr('href'), settings.effect);
			//now activate settings.linkActiveStateClass class
			$(this).addClass(settings.linkActiveStateClass);
            stop_rotating = true;


			return false;
		});

        //settings.autoslide is enabled, scroll automatically every settings.autoslideTime seconds
        if(settings.autoSlide) {
            var stop_rotating = false;
            startTab = settings.autoSlideStartTab;
            //check id
            if(countTabs < startTab) {
                alert('You cannot start with a tab that does not exist! You have declared '+ countTabs +'tabs, but you tried to start with '+startTab+'.');
            }else {
                //大丈夫だった
                var scrolled = startTab+1;
                moveToNext = $.doTimeout(settings.autoSlideTime, function() {
                   if(!stop_rotating) {
                       toggleTabs('#'+tabBox.attr('id')+"-"+scrolled, settings.effect);
                       $('a[href$="#'+tabBox.attr('id')+'-'+scrolled+'"]').addClass(settings.linkActiveStateClass);
                       if(scrolled < countTabs){
                           scrolled = scrolled + 1;
                       }else {
                           scrolled = 1;
                       }
                       return true;
                   }
                });
                return false;
            }
            
        }


		function toggleTabs(tab, effect) {

			//toggle display, hide all tabs except the one we want to display.
			for(var i=1;i<=countTabs;i++) {

				switch(effect) {
					case 'slide':
						(tab == '#'+tabBox.attr('id')+"-"+i) ?
							$('#'+tabBox.attr('id')+"-"+i).stop(true, true).slideDown(settings.effectTime) :
							$('#'+tabBox.attr('id')+"-"+i).stop(true, true).slideUp(settings.effectTime);
						break;
					case 'fade':
						(tab == '#'+tabBox.attr('id')+"-"+i) ?
							$('#'+tabBox.attr('id')+"-"+i).stop(true, true).fadeIn(settings.effectTime) :
							$('#'+tabBox.attr('id')+"-"+i).hide();
						break;
					default:

						(tab == '#'+tabBox.attr('id')+"-"+i) ?
							$('#'+tabBox.attr('id')+"-"+i).show() :
							$('#'+tabBox.attr('id')+"-"+i).hide();
						break;
				}
			}
			//remove active state class.

			a.each(function() {
				$(this).removeClass(settings.linkActiveStateClass)
			});

		}

		function initCss() {
			//remove overflow
			$(settings.tabWrapperClass).css('overflow-y', 'none');
			//set position to absolute for nicer fading effect.
			if(settings.effect == 'fade') {
				for(var i=1;i<=countTabs;i++) {
					//$('#'+tabBox.attr('id')+"-"+i).css('position', 'absolute');
				}
			}
			//activate first tab navigation element
			a.first().addClass(settings.linkActiveStateClass);
            
		}

	}

})(jQuery);

/*
 * jQuery doTimeout: Like setTimeout, but better! - v1.0 - 3/3/2010
 * http://benalman.com/projects/jquery-dotimeout-plugin/
 *
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var f=d.call(arguments),e=b.apply(this,[c+f[0]].concat(f));return typeof f[0]==="number"||typeof f[1]==="number"?this:e};function b(l){var m=this,h,k={},g=l?$.fn:$,n=arguments,i=4,f=n[1],j=n[2],p=n[3];if(typeof f!=="string"){i--;f=l=0;j=n[1];p=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(f){k=a[f]||(a[f]={})}}k.id&&clearTimeout(k.id);delete k.id;function e(){if(l){h.removeData(l)}else{if(f){delete a[f]}}}function o(){k.id=setTimeout(function(){k.fn()},j)}if(p){k.fn=function(q){if(typeof p==="string"){p=g[p]}p.apply(m,d.call(n,i))===true&&!q?o():e()};o()}else{if(k.fn){j===undefined?e():k.fn(j===false);return true}else{e()}}}})(jQuery);
