﻿/**
 *	@filename		home.js
 *	@charset		utf-8
 *	@modified		September 14, 2010
 *	@description	トップページ（主にチラシスクロール処理）
 */
(function(){
		  
////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	DOMツリー構築完了後に実行（onload前）
//
////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){
		   
//
//	チラシスクロール部分を天地中央に表示
//
var MIN_WIDTH = Number($("div#chishiContainer").width());

//リストの長さ
var chirashiList = $("#chishiContainer ul");
chirashiList.each(function(){
	$(this).css("margin-top","-"+$(this).height()/2+"px");
});

//各リスト子要素の幅を指定(IE7only)
if(!jQuery.support.opacity){
    if(!jQuery.support.style){
        if (typeof document.documentElement.style.maxHeight != "undefined") {
			$("li",chirashiList).each(function(){
				$(this).css({"min-width":$("img",this).width()+"px","width":$("a",this).text().length - 2 +"em"});
			});
		}
	}
}

var listWidth = 0;
$("li",chirashiList).each(function(){
	listWidth += $(this).width() + 24;
});
listWidth = listWidth + Number(chirashiList.css("left").replace(/px/,''));

//リストの長さが最小値よりも大きければスクロール処理
if(listWidth - 12 > MIN_WIDTH){

	var chirashiList2 = chirashiList.clone();
	chirashiList.width(listWidth + "px");
	chirashiList2.width(listWidth + "px");
	chirashiList2.css("left",listWidth + "px");
	chirashiList.after(chirashiList2);
	var prevButton = $("#prevChirashiButton");
	var nextButton = $("#nextChirashiButton");
	
	//タイマー
	var timer;
	var dir = -1;
	var prev_dir = dir;
	
	timer = setInterval(scrollList,20);
	
	chirashiList.hover(
		function(){
			//clearInterval(timer);
			prev_dir = dir;
			dir = 0;
		},
		function(){
			//timer = setInterval(scrollList,10);
			dir = prev_dir;
		}
	);
	
	chirashiList2.hover(
		function(){
			//clearInterval(timer);
			prev_dir = dir;
			dir = 0;
		},
		function(){
			//timer = setInterval(scrollList,10);
			dir = prev_dir;
		}
	);
	
	prevButton.hover(
		function(){
			//clearInterval(timer);
			dir = -3;
			//timer = setInterval(scrollList,10);
		},
		function(){
			//clearInterval(timer);
			dir = -1;
			//timer = setInterval(scrollList,10);
		}
	);
	
	prevButton.click(function(){return false;});
	
	nextButton.hover(
		function(){
			//clearInterval(timer);
			dir = 3;
			//timer = setInterval(scrollList,10);
		},
		function(){
			//clearInterval(timer);
			dir = 1;
			//timer = setInterval(scrollList,10);
		}
	);
	
	nextButton.click(function(){return false;});
	
	//ページトップへボタンクリック時に処理を一時停止
	var disableButtons = $("<div id=\"disableButtons\" style=\"background-color: #cccccc\"></div>");
	disableButtons.css("opacity",0.1);
	/*$("#anchorTop a").click(function(){
		clearInterval(timer);
		$("div#sectionLatestChirashi").append(disableButtons);
		setTimeout(redumeScroll,1100);
	});
	
	function redumeScroll(){
		disableButtons.remove();
		timer = setInterval(scrollList,10);
	}*/
	
	$("#anchorTop a").click(function(){
		clearInterval(timer);
		$("div#sectionLatestChirashi").append(disableButtons);
		window.scroller.addEventListener(window.scroller.END, redumeScroll);
	});
	function redumeScroll(){
		window.scroller.removeEventListener(window.scroller.END, redumeScroll);
		disableButtons.remove();
		clearInterval(timer);
		timer = setInterval(scrollList,20);
	};
	
}


function scrollList(){
	var left1 = Number(chirashiList.css("left").replace(/px/,'')) + dir;
	var left2 = Number(chirashiList2.css("left").replace(/px/,'')) + dir;
	chirashiList.css("left",left1 + "px");
	chirashiList2.css("left",left2 + "px");
	if(dir < 0){
		if((left1) < -listWidth){
			chirashiList.css("left",left2 + listWidth - 12 + "px");
		}
		if((left2) < -listWidth){
			chirashiList2.css("left",left1 + listWidth - 12 + "px");
		}
	}else{
		if((left1) > listWidth){
			chirashiList.css("left",left2 - listWidth + 12 + "px");
		}
		if((left2) > listWidth){
			chirashiList2.css("left",left1 - listWidth + 12 + "px");
		}
	}
	return 0;
}


});
	
})();
