

(function($) {

	/* for iPhone & iPad ============================= */
	var iphone = /(iPhone)|(iPad)/i.test(navigator.userAgent);

	var _ua = (function(){
	return {
	ltIE6:typeof window.addEventListener == "undefined" && typeof document.documentElement.style.maxHeight == "undefined",
	ltIE7:typeof window.addEventListener == "undefined" && typeof document.querySelectorAll == "undefined",
	ltIE8:typeof window.addEventListener == "undefined" && typeof document.getElementsByClassName == "undefined",
	ie:document.uniqueID,
	firefox:window.globalStorage,
	opera:window.opera,
	webkit:!document.uniqueID && !window.opera && !window.globalStorage && window.localStorage,
	mobile:/android|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())
	}
	})();

	if (!_ua.ltIE6) {
	
		/* prcSlider */
		$.fn.prcSlider = function(param){
			return this.each(function(){

					// 初期値
					var def = {
						time: 20/1.3,			// 1px動かすのにかける時間。
						speed: 2,			// 早送りや遅送りの比率指定。
						direction: "left",	// 方向の初期値は「左」
						auto: true,			// ループの初期値は「自動」
						easing: "linear",	// イージングの初期値は「linear」
						build: true,		// 暗黙のオプション（幅の初期設定）
						opacity: 0.8,		// ホバー時の透過値
						cellSelector: ".slideCell",	// セル用クラス名
						ctrlSelector: ".slideCtrl",	// コントローラー用クラス名
						event: { click: false, hover: true },	// イベントハンドラ
						sp: 1,				// 基本速度
						guide: $(this)
					};
					
					// プライベート用のプロパティを上書きさせない
					if(param) delete param.guide;
					if(param) delete param.sp;
					
					// パラメータ受け渡し
					$.extend(def, param);
					
					// 内部処理用
					def.d = def.direction; // 方向の初期設定を記憶
					def.cell = def.cellSelector;
					def.ctrl = def.ctrlSelector;
					cell = def.guide.find(def.cell);
					
					// セレクターが不正または無効な場合は次へ
					if(!cell.length) return true;
					
					// slideFrame.id
					def.frame = def.guide.parent();
					def.frame.id = def.frame[0].id;
					// iPhone, iPad用
					def.handlerMousedown = iphone ? "touchstart" : "mousedown";
					def.handlerMouseup = iphone ? "touchend" : "mouseup";

	//
					switch(def.guide.parent()[0].id){
						case "topSlide":
						case "newsSlide":
							var margin = def.guide.parent()[0].id == "newsSlide" ? 10 : 0;
							var height = def.guide.parent()[0].id == "newsSlide" ? 270 : 420;
							var length = cell.length;
							// スライドのセルサイズ設定 for webkit
							cell.each(function(i){
								var pasttime = 0;
								var $img = $(this).find("img:first");
								// webkit以外
								if($img.width()){
									$(this).width($img.width() + margin);
									if(i == length - 1) onLoad();
									return true;
								}
								$(this).width(140); // デフォルト設定
								var img = new Image;
								img.src = $img.attr("src");
								if(img.src){
									var cellidx = $(this);
									var timerId = setInterval(function(){
										if(img.complete){
											clearInterval(timerId);
											if(img.height){
												cellidx.width(Math.floor(img.width * height / img.height));
											}
											if(i == length - 1){
												onLoad();
											}
										}else{
											if(pasttime > 2000 && i == length - 1){
												clearInterval(timerId);
												onLoad();
											}else if(pasttime > 4000){
												clearInterval(timerId);
											}
										}
										pasttime += 10;
									}, 10);
								}
							});
							
							break;
						case "bannerSlide":
						case "shopSlide":
						default:
							onLoad();
					}
					
	// 

					// ホバーでセルをグレースケール化
					def.guide.find(def.cell).hover(
						function(){ $(this).css("opacity", def.opacity); },
						function(){ $(this).css("opacity", 1); }
					);
					
					// ホバーイベント
					if(def.event.hover){
						// for iPhone & iPad
						if (iphone){
							def.frame.find(def.ctrl)
								.bind(def.handlerMousedown, function(event){
									event.preventDefault();
									def.guide.find(def.cell).stop(true);
									def.sp = def.speed;
									def.direction = $(this).hasClass("right") ? "left" : "right";
									slider(def);
								}).bind(def.handlerMouseup, function(){
									def.guide.find(def.cell).stop(true);
									def.sp = 1;
									def.direction = def.d;
									if(def.auto) slider(def);
								});
						} else {
							// 早送り・巻き戻し
							def.frame.find(def.ctrl).hover(
								function(){
									def.guide.find(def.cell).stop(true);
									def.sp = def.speed;
									def.direction = $(this).hasClass("right") ? "left" : "right";
									slider(def);
									console.log("tw");
								},
								function(){
									def.guide.find(def.cell).stop(true);
									def.sp = 1;
									def.direction = def.d;
									if(def.auto) slider(def);
								}
							);
						}
						
					}
					// クリックイベント
					if(def.event.click){
						def.guide.parent().find(def.ctrl).mousedown(function(event){
							def.guide.find(def.cell).stop(true, true);
							// 初期動作の中止
							event.preventDefault();
							// 方向設定
							def.direction = $(this).hasClass("right") ? "left" : "right";
							// ループ指定解除
							def.once = true;
							
							slider(def);
						});
					}


					/* 実行関数 定義 ========================== */
					function onLoad(){
						// ニュース＆イベントのセルサイズ設定 
						if(def.guide.parent()[0].id == "newsSlide"){
							$("#newsSlide .slideCell").each(function(){
								var p = $(this).find("p").eq(0);
								var size = $(this).width() / 12 * 3;
								var str = p.text().length > size ? p.text().substr(0, size) + "…" : p.text();
								p.text(str);
							});
						}

						// 幅の初期設定
						if(def.build){
							init();
							//$(window).resize(init);
						}

						// 自動起動
						if(def.auto){
							slider(def);

							// for iPhone & iPad
							if (iphone){
								def.guide.bind(def.handlerMousedown, function(){ $(this).find(def.cell).stop(true); });
								def.guide.bind(def.handlerMouseup, function(){ slider(def); });
							} else {
								def.guide.hover(
									function(){	$(this).find(def.cell).stop(true); },
									function(){ slider(def); }
								);
							}
						
						}

					}

					/* slider 定義 ========================== */
					// ｢先頭セルを左に移動させたあと、最終セルの後ろに移動させる｣を繰り返す。
					function slider(par){
						var first = par.guide.find(def.cell).filter(":first");
						var last = par.guide.find(def.cell).filter(":last");

						if(par.direction.toLowerCase() === "left"){
							var t = ~~Math.abs(par.time / par.sp * first.width() * 
								(first.offset().left - par.guide.offset().left < 0 ? 
									(first.width() + first.offset().left - par.guide.offset().left) / first.width() : 1
								)
							);
							first.animate({
								marginLeft: -1 * first.width() - 5
							}, {
								// 途中で止まっていた場合は残りを調整してanimateし、次以降は再ループさせる。
								duration: t,
								easing: par.easing,
								complete: function(){
									first.appendTo(par.guide).css("marginLeft", 0);
									if(!par.once) slider(par);
								}
							});
						}else if(par.direction.toLowerCase() === "right"){
							if(first.offset().left - par.guide.offset().left < 0){
								var t = ~~Math.abs(par.time / par.sp * first.width() * 
									(first.offset().left - par.guide.offset().left < 0 ? 
										(par.guide.offset().left - first.offset().left) / first.width() : 1
									)
								);
								first.animate({
									marginLeft: 0
								},{
									duration: t,
									easing: par.easing,
									complete: function(){ if(!par.once) slider(par); }
								});
							}else{
								last.prependTo(par.guide).css("marginLeft", -1 * last.width() - 5);
								if(!par.once) slider(par);
							}
						}else{ return false; }
					}
					
					/* init 定義 ============================= */
					// スライド枠の幅設定
					function init(){
						// slideCellの数調整。足りない時にコピーして増やす。
						var max = 0;
						var size = 0;

						cell.each(function(){
							max = max > $(this).width() ? max : $(this).width();
							size += $(this).width();
						});

						//while(0 < size && size < (def.guide.width + max)){
						while(0 < size && size < (1920 + max)){
							def.guide.find(def.cell).clone(true).appendTo(def.guide);
							size *= 2;
						}

						// slideGuideのサイズをドキュメントサイズに合わせる。
						def.guide.width($("body").width() + max);
					}

			});
		}; // prcSlider end


		/* easing */
		$.easing.quart = function (x, t, b, c, d) { return -c * ((t=t/d-1)*t*t*t - 1) + b; };
	}


	$(function(){
		if (_ua.ltIE6) {
			// IE6以下
			$(".slideFrame").css("background-color", "#fff");
			$(".slideFrame").css("width", "1020px").css("overflow", "hidden");
		} else {
			// ロールオーバー
			(function rollover() {
				var preLoad = new Object();
				$('.rollover').not("[src*='_on.']").each(function(){
					var imgSrc = this.src;
					var src = /^(.*)\.([^.]+)$/.exec(imgSrc);
					var imgOver = src[1] + "_on." + src[2];
					preLoad[this.src] = new Image();
					preLoad[this.src].src = imgOver;
					$(this).hover(
						function (){ this.src = imgOver; },
						function (){ this.src = imgSrc; }
					);
				});
				// スライドコントローラ背景
				$(".slideCtrl").hover(
					function(){ $(this).addClass("over"); }, 
					function(){ $(this).removeClass("over"); }
				);
			})();

			// ページ内リンク
			$("a[href^=#]").click(function(){
				$(this).blur();
				$("html, body").animate({ scrollTop: $(this.hash).position() ? $(this.hash).position().top : 0 }, 500);
				return false;
			});

			// スライダーのコントローラ表示
			// （JavaScriptオフの場合はコントローラ非表示でスクロールバーを表示される）
			$(".slideCtrl").show();
			$(".slideFrame").css("overflow", "hidden");
		}
	});

})(jQuery);

