/* mainTopics Carousel
================================================================ */
jQuery(function ($) {

	ReadFontSize();

	//動作設定
	var o = {
		speed   : 500,
		interval: 10000
	};

	//基本設定
	var $slider     = $('#mainTopics'),
		$container  = $slider.find('#carouselInner'),
		$contents   = $container.children(),
		$firstChild = $contents.filter(':first-child'),
		$lastChild  = $contents.filter(':last-child');

	var size = {
		width : $container.width()
	};

	var count = {
		min    : 0,
		max    : $contents.length,
		current: 0
	};

	$container.css({
		width      : size.width * ($contents.length + 2),
		marginLeft : -size.width,
		paddingLeft: size.width
	});

	//自動スライド設定
	var play, start;

	play = function () {
		start = setInterval(function () {
			slide.next();
		}, o.interval);
	};

	//hover時に自動スライドを止める
	$contents.hover(
		function () {
		clearInterval(start);
	},
	function () {
		play();
	}
	);

	//次へ・前へボタン設定
	$('#carouselPrev').click(function (e) {
		fnc.pager('negative', e);
	});

	$('#carouselNext').click(function (e) {
		fnc.pager('positive', e);
	});

	//ヘッドライン表示切替設定
	$('#mainTopicslClose').click(function () {
		$slider.slideUp("slow",function(){
			$('#mainTopicslClose').hide();
			$('#mainTopicslOpen').show();
		});
	});

	$('#mainTopicslOpen').click(function () {
		$slider.slideDown("slow",function(){
			$('#mainTopicslOpen').hide();
			$('#mainTopicslClose').show();
		});
	});

	//カルーセル設定
	var distance;
	var slide = {

		next: function (index) {
			fnc.range(index, 'positive');
			if(count.current < count.max - 1) {
				fnc.scroll(distance);
			} else {
				$firstChild.css('left', size.width * $contents.length);
				$container.stop(true, false)
				.animate({left: -distance}, o.speed,
				function () {
				$firstChild.css('left', 0);
				$container.css('left', 0);
			}
			);
			count.current = -1;
			}
			fnc.counter(index, 'increment');
		},

		prev: function (index) {
			fnc.range(index, 'negative');
			if(count.current > count.min) {
				fnc.scroll(distance);
			} else {
				$lastChild.css('left', -(size.width * $contents.length));
				$container.stop(true, false)
				.animate({left: -distance}, o.speed,
				function () {
				$lastChild.css('left', '');
				$container.css('left', -(size.width * ($contents.length - 1)));
			}
			);
			count.current = count.max;
			}
			fnc.counter(index, 'decrement');
		}

	};

	//各関数設定
	var fnc = {

		range  : function (n, d) {
			if(n >= 0) {
				distance = size.width * n;
			} else {
				var addNum;
				if(d === 'negative') addNum = -1;
				if(d === 'positive') addNum = +1;
				distance = size.width * (count.current + addNum);
			}
		},

		scroll : function (d) {
			$container.stop(true, false).animate({left: -d}, o.speed);
		},

		counter: function (n, c) {
			if(n >= 0) {
				count.current = n;
			} else {
				if(c === 'increment') count.current++;
				if(c === 'decrement') count.current--;
			}
		},

		pager  : function (d, e) {
			if(!$container.is(':animated')) {
				clearInterval(start);
				if(d === 'positive') slide.next();
				if(d === 'negative') slide.prev();
				play();
			}
			e.preventDefault();
		}

	};

	//自動スタート
	play();

});

/* Articles Tab
================================================================ */

$(function(){
	//初期設定
	$("div.articlesInner").hide();
	
	//タブ切替設定
	$("#tabList li a").click(function(){
		//selectedをはずす
		$("#tabList li a").removeClass("selected");
		//タブの画像を切り替える
		$("#tabList li a img").each(function(){
			$(this).attr("src",$(this).attr("src").replace("_on.gif",".gif"));
		});
		$("img",this).attr("src","/img/top/tab_"+$("img",this).attr("className")+"_on.gif");
		//コンテンツを切り替える
		$("div.articlesInner").hide();
		$("#"+$("img",this).attr("className")).show();
		//クッキー設定
		var tabno = $("#tabList li a").index(this);
		SetTabNo(tabno);
		return false;
	});
});

$(document).ready(function(){
	tabno = ReadCookie('TabNo');
	if (tabno != '') {
		$("#tabList li a").eq(tabno).trigger('click');
	} else {
		$("#subTopics").show();
		$("img.subTopics").attr("src",$("img.subTopics").attr("src").replace(".gif","_on.gif"));
	}
});

//ページタブ記録
function SetTabNo(no)  {
	WriteCookie('TabNo', no, days);
}


