
(function($){

	$(document).ready(function(){
		// open link as '_blank'.
		$('#contents a[rel=external]').click(function(){					
			window.open(this.href, '_blank');
			return false;
		});

		initTab();
	});

	// initialize tab.
	function initTab()
	{
		// set tab item behavior.
		$('#tab li a')
			// store images.
			.find('img')
				.each(function(){
					var img = new Image();
					img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
					$(this).data('image', {
						defaultSrc: this.src,
						activeSrc: img.src
					});
				})
			.end()
			// click handler.
			.click(function(){
				var contents = $('#tab').siblings('div.section');

				// is selected index eq current index?
				var tabIndex = $('#tab li a').index(this);
				var currentIndex = $(contents).index($(contents).filter(':visible'));
				if (tabIndex != currentIndex) {
					switchTabTo(tabIndex);
				}
				return false;
			})
		;
		switchTabTo(0);
	}

	// tab switch function.
	function switchTabTo(index)
	{
		// show only matched.
		$('#tab').siblings('div.section')
			.fadeOut('fast')
			.eq(index).fadeIn('fast')
		;

		// switch tab image.
		$('#tab ul img').each(function(i){
			if (i == index) {
				this.src = $(this).data('image').activeSrc;
			}
			else {
				this.src = $(this).data('image').defaultSrc;
			}
		});
	}

})(jQuery);
