/* ************************************************************************

Site: MEMPHIS
File: home.js
---
Last Update: 2011/11/15
Create Date: 2011/11/18
---
(c) MEMPHIS

************************************************************************ */


/* --------------------------------------------------------------------- */
/* setting */
/* --------------------------------------------------------------------- */
var homePhoto;
var homeNewsTicker;
$(function(){
	homePhoto = new HomePhoto($("div#homePhotoArea"));
	//下の方で重複起動してたから、バグってた
	homeNewsTicker = new HomeNewsTicker($("ul#homeNewsList"),$("ul#homeNewsNavi"));
});


/* --------------------------------------------------------------------- */
/* function */
/* --------------------------------------------------------------------- */

//[HomeNewsTicker]
var HomeNewsTicker = function(newsArea,controller){
	this.newsArea = newsArea;
	this.controller = controller;
	//
	this.newsObj = this.newsArea.find("li");
	this.newsNum = this.newsObj.length;
	this.loopCount = 0;
	this.loopZ = 1;
	this.changeTime = 800;
	this.waitTime = 3000;
	this.cntrlFlg = "next";
	this.loopTimer;
	//
	this.newsObj.css({visibility:"visible"});
	//
	for(var i=1;i<this.newsNum;i++){//先頭のみ表示
		this.newsObj.eq(i).css({display:"none",opacity:"0"});
	}
	//
	this.loop();
	this.controlButton();
	this.selectNone();
};
HomeNewsTicker.prototype = {
	loop:function(){
		clearInterval(this.loopTimer);
		this.cntrlFlg = "next";
		var inthis = this;
		this.loopTimer = setInterval(function(){
			inthis.fadeOut();
		},this.waitTime);
	},
	fadeOut:function(){
		clearInterval(this.loopTimer);
		//trace(this.loopTimer);
		var inthis = this;
		//trace("now:  "+this.loopCount);
		this.newsObj.eq(this.loopCount).stop().animate({opacity:"0"},this.changeTime,"easeOutCirc", function(){
			inthis.newsObj.eq(inthis.loopCount).css({display:"none",zIndex:inthis.loopZ});
			//inthis.newsObj.css({display:"none"});
			inthis.loopZ++;
			//
			
			if(inthis.cntrlFlg == "prev"){
				inthis.loopCount += (0 < inthis.loopCount)? -1:(inthis.newsNum-1);
			} else if(inthis.cntrlFlg == "next"){
				inthis.loopCount += (inthis.loopCount<inthis.newsNum-1)? 1:-(inthis.newsNum-1);
			}
			//trace("next: "+inthis.loopCount);
			inthis.fadeIn();
		});
	},
	fadeIn:function(){
		var inthis = this;
		this.newsObj.eq(this.loopCount).css({display:"block",zIndex:this.loopZ});
		this.newsObj.eq(this.loopCount).stop().animate({opacity:"1"},this.changeTime,"easeOutCirc", function(){
			inthis.loop();
		});
	},
	controlButton:function(){
		var inthis = this;
		this.controller.find("li a").each(function(e){
			$(this).click(function(){
				clearInterval(inthis.loopTimer);
				if(e == 0){//left[prev]
					inthis.cntrlFlg = "prev";
				} else if(e == 1){//right[next]
					inthis.cntrlFlg = "next";
				}
				inthis.fadeOut();
			});
		});
	},
	selectNone:function(){
		var obj  = $("div#homeNewsArea")[0];
		//trace(obj);
		addEvent(obj,"mousemove",eventCancel);
		addEvent(obj,"mouseup",eventCancel);
		addEvent(obj,"mousedown",eventCancel);
		addEvent(obj,"mouseout",eventCancel);
	}
}


//[HomePhoto]
var HomePhoto = function(area){
	this.area = area;
	this.areaBox = area.find("figure")
	this.areaPhoto = area.find("img");
	this.areaTxt = area.find("figcaption");
	this.sizeScale = [];//各画像初期サイズ（各際比率基準）
	this.sizePropo = [];
	this.winPropo;
	this.pictAreaH;
	//
	this.loopCount = 0;
	this.loopZ = 1;
	this.changeTime = 800;
	this.waitTime = 4500;
	this.loopTimer;
	//
	this.winHRimit = 680;
	//
	for(var i=0;i<this.areaBox.length;i++){
		this.sizeScale[i] = {
			width:  this.areaPhoto.eq(i).width(),
			height: this.areaPhoto.eq(i).height()
		};
		this.areaBox.eq(i).css({display:"none",width:"0px"});
		//this.areaBox[i].css({display:"none",width:"0px"});
	}
	//
	this.init();
	this.selectNone();
	//this.loaded();
	//this.loop();
};
HomePhoto.prototype = {
	init:function(){
		var inthis = this;
		//
		$(window).bind("load", function(){
			homePhoto.sizing();
			homePhoto.winHeightFix();
			homePhoto.loaded();
		});
		
		$(window).bind('resize', function() {
			homePhoto.sizing();
			homePhoto.winHeightFix();
		});
	},
	loaded:function(){
		inthis = this;
		$("p#loading").delay(1000).animate({opacity:"0"},1000,"easeOutCirc", function(){
			inthis.fadeIn();
		});
	},
	/*
	opening:function(){
		this.areaBox.eq(0).css({display:"block",opacity:"1",zIndex:this.loopZ});
		this.areaBox.eq(0).stop().animate({width:"100%"},this.changeTime,"easeInOutCirc", function(){
			inthis.loop();//オープニング終了後にループスタート
		});
	},
	*/
	sizing:function(){
		var inthis = this;
		var winW = $(window).width();
		var winH = $(window).height();
		this.pictAreaH = winH-$("div#homeMainArea").height();
		//
		/*
		body全体にoverflowをauto（高さ制限）にした際の処理。
		$("div#homePhotoArea")へoverflowかけて、
		ウィンドウサイズで高さ設定をつけないと
		bodyのoverflow: auto;の際に画像の高さ分はみ出る。
		
		だから、bodyがoverflow: hidden;の時は
		この処理は必要ないが、念のためと
		混同を避けるためにあえて割り当てる
		*/
		//this.area.css({height:this.pictAreaH});
		if(this.winHRimit < winH){
			this.area.css({height:this.pictAreaH});
		} else if(this.winHRimit >= winH){
			this.area.css({height:this.winHRimit-$("div#homeMainArea").height()});
		}
		//
		this.areaTxt.css({width:winW - this.areaTxt.css("left").replace("px","")});
		//
		/*---------------------------------
		要：仕組み解析
		---------------------------------*/
		for(var i=0;i<this.areaBox.length;i++){
			this.winPropo = winH/winW;
			this.sizePropo[i] = this.sizeScale[i].height/this.sizeScale[i].width;
			//
			if (this.winPropo > this.sizePropo[i]){
				//trace("ウィンドウ大");
				this.areaPhoto.eq(i).css({width:winH/this.sizePropo[i],height:winH});
			} else /*if (this.winPropo < this.sizePropo[i])*/{
					//trace("画像大");
				this.areaPhoto.eq(i).css({width:winW,height:winW*this.sizePropo[i]});
			}
			this.areaPhoto.eq(i).css({marginLeft:(winW-this.areaPhoto.eq(i).width())/2});
		};
		//trace("ウィンドウ比率："+this.winPropo+" / 画像比率："+this.sizePropo[0]);
	},
	loop:function(){
		var inthis = this;
		this.loopTimer = setInterval(function(){
			inthis.fadeOut();
		},this.waitTime);
	},
	fadeOut:function(){
		clearInterval(this.loopTimer);
		var inthis = this;
		this.areaBox.eq(this.loopCount).stop().animate({opacity:"0"},this.changeTime,"easeOutCirc", function(){
			inthis.areaBox.eq(inthis.loopCount).css({display:"none",width:"0px",zIndex:inthis.loopZ});
			inthis.loopZ++;
			//
			inthis.loopCount += (inthis.loopCount<inthis.areaBox.length-1)? 1:-(inthis.areaBox.length-1);
			inthis.fadeIn();
		});
	},
	fadeIn:function(){
		var inthis = this;
		this.areaBox.eq(this.loopCount).css({display:"block",opacity:"1",zIndex:this.loopZ});
		this.areaBox.eq(this.loopCount).stop().animate({width:"100%"},this.changeTime,"easeInOutCirc", function(){
			inthis.loop();
		});
	},
	selectNone:function(){
		//var obj  = $("div#homePhotoArea figure img");
		//var obj  = $("div#homePhotoArea figure img").eq(0);
		//配列のように[0]指定してあげないとObjectで認識され動かない
		//eqはやはりjqの関数でしかない
		//ID、classでもjqでセレクタ参照しているので、[0]などで
		//指定しないとタグとして戻り値が返らない
		//[任意の値]を指定することによりjqのメソッドチェーンが切れるため。
		var obj  = this.area[0];
		//trace(obj);
		addEvent(obj,"mousemove",eventCancel);
		addEvent(obj,"mouseup",eventCancel);
		addEvent(obj,"mousedown",eventCancel);
		addEvent(obj,"mouseout",eventCancel);
	},
	winHeightFix: function(){
		var winH =  $(window).height();
		if(this.winHRimit < winH){
			$("body").css({height:"auto",overflow:"hidden"});
		} else if(this.winHRimit >= winH){
			$("body").css({height:this.winHRimit,overflow:"auto"});
		}
	}
}


//[contetsMoveCancel]
function addEvent(obj,type,func){
	if(obj.addEventListener){
		obj.addEventListener(type,func,false);
	} else if(obj.attachEvent){
		obj.attachEvent("on"+type,func);
	}
}

function eventCancel(e) {
	//trace(e);
	//e.preventDefault();
	if(window.addEventListener){
		e.preventDefault();
	} else if(window.attachEvent){
		e.returnValue = false;
	}
}

/*
function contetsMoveCancel(){
	//var obj  = $("div#homePhotoArea figure img");
	//var obj  = $("div#homePhotoArea figure img").eq(0);
	//配列のように[0]指定してあげないとObjectで認識され動かない
	//eqはやはりjqの関数でしかない
	//ID、classでもjqでセレクタ参照しているので、[0]などで
	//指定しないとタグとして戻り値が返らない
	//[任意の値]を指定することによりjqのメソッドチェーンが切れるため。
	var obj  = $("div#homePhotoArea")[0];
	//trace(obj);
	addEvent(obj,"mousemove",roEvent);
	addEvent(obj,"mouseup",roEvent);
	addEvent(obj,"mousedown",roEvent);
	addEvent(obj,"mouseout",roEvent);
	
}
*/




/* --------------------------------------------------------------------- */
/* start */
/* --------------------------------------------------------------------- */


$(window).bind("load", function(){
	MouseOver();
   	slowScroll();
	//contetsMoveCancel();
});

