/*********************************************************************
 *
 *	</js/index.js>
 *	Created 2011/10/17
 *
*********************************************************************/


$(function(){

	// リンクの点線を消す (IE)
	$('a').focus(function(){
		this.blur();
	});

	/* 文字サイズ
	------------------------------------------------------------------- */
	$('ul.fontChanger li.large').click(function(){
		$(this).find('img').attr('src', '/img/btn_font_size_large_current.gif');
		$(this).parent().find('li.normal img').attr('src', '/img/btn_font_size_normal_off.gif');
		$('body').addClass('large')
	});
	$('ul.fontChanger li.normal').click(function(){
		$(this).find('img').attr('src', '/img/btn_font_size_normal_current.gif');
		$(this).parent().find('li.large img').attr('src', '/img/btn_font_size_large_off.gif');
		$('body').removeClass('large')
	});

	$('ul.fontChangerEn li.large').click(function(){
		$(this).find('img').attr('src', '/e/img/btn_font_size_large_current.gif');
		$(this).parent().find('li.normal img').attr('src', '/e/img/btn_font_size_normal_off.gif');
		$('body').addClass('large')
	});
	$('ul.fontChangerEn li.normal').click(function(){
		$(this).find('img').attr('src', '/e/img/btn_font_size_normal_current.gif');
		$(this).parent().find('li.large img').attr('src', '/e/img/btn_font_size_large_off.gif');
		$('body').removeClass('large')
	});

	/* トピックス
	------------------------------------------------------------------- */
	if ($('ul#carousel li').length == 2) {
		jQuery('ul#carousel').jcarousel({
			scroll: 1,
			auto: 5,
			wrap: 'last',
			initCallback: carousel_initCallback,
			buttonPrevHTML: '<div class="disabled"><img src="img/btn_carousel_disabled.gif" alt="" /></div>',
			buttonNextHTML: '<div class="disabled"><img src="img/btn_carousel_disabled.gif" alt="" /></div>'
		});
	} else {
		jQuery('ul#carousel').jcarousel({
			scroll: 1,
			auto: 5,
			wrap: 'circular',
			initCallback: carousel_initCallback,
			buttonPrevHTML: '<div><img src="img/btn_carousel_prev_off.gif" alt="" /></div>',
			buttonNextHTML: '<div><img src="img/btn_carousel_next_off.gif" alt="" /></div>'
		});
	}

	if ($('ul#carousel-en li').length == 2) {
		jQuery('ul#carousel-en').jcarousel({
			scroll: 1,
			auto: 5,
			wrap: 'last',
			initCallback: carousel_initCallback,
			buttonPrevHTML: '<div class="disabled"><img src="../img/btn_carousel_disabled.gif" alt="" /></div>',
			buttonNextHTML: '<div class="disabled"><img src="../img/btn_carousel_disabled.gif" alt="" /></div>'
		});
	} else {
		jQuery('ul#carousel-en').jcarousel({
			scroll: 1,
			auto: 5,
			wrap: 'circular',
			initCallback: carousel_initCallback,
			buttonPrevHTML: '<div><img src="../img/btn_carousel_prev_off.gif" alt="" /></div>',
			buttonNextHTML: '<div><img src="../img/btn_carousel_next_off.gif" alt="" /></div>'
		});
	}

	function carousel_initCallback(carousel) {
		// アイテムにマウスポインタを乗せるとストップ、外すとスタート
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};

	/* ティッカー
	------------------------------------------------------------------- */
	// 初期スピード（タイマー当たりの移動 px）	
	var speed = 1;

	// マウスをオーバーしたとき（タイマー当たりの移動 px）	
	var hoverSpeed = 3;

	// タイマーの設定
	var timeInterval = 25;

	// 進行方向（right、left）
	var scrollDirection = "left";

	// リストの横幅を配列に格納
	var listWidthArray = [];
	var listWidth = 0;
	$('#ticker ul li').each(function(i){
		listWidthArray.push($('#ticker ul li').eq(i).outerWidth());
		listWidth += listWidthArray[i];
	});

	// 表示エリアの幅
	$('#ticker ul').css('width',listWidth + 400 + 'px');

	// 表示エリアの位置
	$('#ticker ul').css('left', '-100px');

	var x = 0;
	var s = speed;
	var first = 120;

	function set_timer(){
		timerID = setInterval(function(){
			if(scrollDirection == 'left'){
				slideLeft();
			}else if(scrollDirection == 'right'){
				slideRight();
			}else{
				slideLeft();
			}
		},timeInterval);
	}
	
	function clear_timer(){
		clearInterval(timerID);
	}
	
	// 左スクロール
	function slideLeft(){
		if (first > 0) {
			first = first - s;
			$('#ticker ul li').css('left',first + 'px');
		} else {
			if (x <= 0) {
				$('#ticker ul li:last').after($('#ticker ul li:first').clone());
				x = $('#ticker ul li:eq(0)').width(); // リストの横幅を指定
				$('#ticker ul li:first').remove();
			}
			x = x - s;
			$('#ticker ul li').css('left', x + 'px');
		}
	}

	// 右スクロール
	function slideRight(){
		if ((x || first) >= $('#ticker ul li:last').width()){
			$('#ticker ul li:first').before($('#ticker ul li:last').clone());
			$('#ticker ul li:last').remove();
			x = 0;
			first = 0;
		}
		x = x + s;
		$('#ticker ul li').css('left', x + 'px');
	}

	// 左のボタン制御
	$('#ticker div.prev div.btn').hover(function(){
		clear_timer();
		s= hoverSpeed;
		scrollDirection = 'right';
		set_timer();
	},
	function(){
		clear_timer();
		s= speed;
		scrollDirection = 'left';
		set_timer();
	});

	// 右ボタン制御
	$('#ticker div.next div.btn').hover(function(){
		clear_timer();
		s= hoverSpeed;
		scrollDirection = 'left';
		set_timer();
	},
	function(){
		clear_timer();
		s= speed;
		scrollDirection = 'left';
		set_timer();
	});

	// スクロールエリアをマウスオーバーしたとき
	$('#ticker ul').hover(function(){
		clear_timer();
	},
	function(){
		s= speed;		
		set_timer();			
	});

	// 移動開始
	set_timer();

	//画像の高さを配列に格納して画像位置を中央に指定
	var listHeight = $('#ticker ul li').outerHeight();
	$('#ticker ul li img').each(function(){
		$(this).css('padding', (listHeight - $(this).height())/2 + 'px 0');
	});

	/* ロールオーバー
	------------------------------------------------------------------- */
	$('img')
	.live({
		mouseenter: function() {
			$(this).attr('src',$(this).attr('src').replace(/^(.+)_off(\.[a-z]+)$/,'$1_on$2'));
		},
		mouseleave: function() {
			$(this).attr('src',$(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/,'$1_off$2'));
		}
	})
	.each(function(init){
		$('').attr('src',$(this).attr('src').replace(/^(.+)_off(\.[a-z]+)$/,'$1_on$2'));
	});

});
