/*--------------------------------------------------------------------------
  default Settings
--------------------------------------------------------------------------*/
	htmlIdName = "MoveDiv"

	timerIDtiming = 300   //移動検知時間軸（静止状態での検知） [数値]ns
	moveIDtiming  = 30    //移動タイミング時間軸               [数値]ns
	drawCount = 0.15      //移動動作速度指定(0 静止 0.1>移動 1=完全追従 (1以上の設定は不可) )
//	contents_size = 770
	divBlocksize_x = 150;
	divBlocksize_y = 30;

	scrollSpeed = 5;
/*--------------------------------------------------------------------------
  Smooth Scroller Script
--------------------------------------------------------------------------*/

Scroller = {
	// control the speed of the scroller.
	// dont change it here directly, please use Scroller.speed=50;
	speed: scrollSpeed,

	// returns the Y position of the div
	gy: function (d) {
		gy = d.offsetTop
		if (d.offsetParent) while (d = d.offsetParent) gy += d.offsetTop
		return gy
	},

	// returns the current scroll position
	scrollTop: function (){
		body=document.body
	    d=document.documentElement
	    if (body && body.scrollTop) return body.scrollTop
	    if (d && d.scrollTop) return d.scrollTop
	    if (window.pageYOffset) return window.pageYOffset
	    return 0
	},

	// attach an event for an element
	// (element, type, function)
	add: function(event, body, d) {
	    if (event.addEventListener) return event.addEventListener(body, d,false)
	    if (event.attachEvent) return event.attachEvent('on'+body, d)
	},

	// kill an event of an element
	end: function(e){
		if (window.event) {
			window.event.cancelBubble = true
			window.event.returnValue = false
      		return;
    	}
	    if (e.preventDefault && e.stopPropagation) {
	      e.preventDefault()
	      e.stopPropagation()
	    }
	},
	
	// move the scroll bar to the particular div.
	scroll: function(d){
		i = window.innerHeight || document.documentElement.clientHeight;
		h=document.body.scrollHeight;
		a = Scroller.scrollTop()
		if(d>a)
			if(h-d>i)
				a+=Math.ceil((d-a)/Scroller.speed)
			else
				a+=Math.ceil((d-a-(h-d))/Scroller.speed)
		else
			a = a+(d-a)/Scroller.speed;
		window.scrollTo(0,a)
	  	if(a==d || Scroller.offsetTop==a)clearInterval(Scroller.interval)
	  	Scroller.offsetTop=a
	},
	// initializer that adds the renderer to the onload function of the window
	init: function(){
		Scroller.add(window,'load', Scroller.render)
	},

	// this method extracts all the anchors and validates then as # and attaches the events.
	render: function(){
		a = document.getElementsByTagName('a');
		Scroller.end(this);
		window.onscroll
	    for (i=0;i<a.length;i++) {
	      l = a[i];
	      if(l.href && l.href.indexOf('#') != -1 && ((l.pathname==location.pathname) || ('/'+l.pathname==location.pathname)) ){
	      	Scroller.add(l,'click',Scroller.end)
	      		l.onclick = function(){
	      			Scroller.end(this);
		        	l=this.hash.substr(1);
		        	 a = document.getElementsByTagName('a');
				     for (i=0;i<a.length;i++) {
				     	if(a[i].name == l){
				     		clearInterval(Scroller.interval);
				     		Scroller.interval=setInterval('Scroller.scroll('+Scroller.gy(a[i])+')',10);
						}
					}
				}
	      	}
		}
	}
}
// invoke the initializer of the scroller
Scroller.init();

/*--------------------------------------------------------------------------
  Smooth Scroller Script
--------------------------------------------------------------------------*/
	LRmode =1;  UDmode = 1;
	XEnable = YEnable = 1;
	DivLocationX = DivLocationY = 0;
	alphaS      = false;
	alphaLast   = 65;
	alphaOver   = 100;


//ブラウザチェック (is[ブラウザ] OK=true / NG= false )
	var VarUsrAgt = navigator.userAgent.toLowerCase();
	isMac = (navigator.appVersion.indexOf("Mac") != -1);
	isWin = (navigator.appVersion.indexOf("Win") != -1);
	isX11 = (navigator.appVersion.indexOf("x11") != -1);
	isNS4 = (document.layers) ? 1 : 0;
	isNS6 = VarUsrAgt.indexOf("netscape6") != -1;
	isNS7 = VarUsrAgt.indexOf("netscape/7.") != -1;
	isIE5 = VarUsrAgt.indexOf("msie 5.") != -1;
	isIE6 = VarUsrAgt.indexOf("msie 6.") != -1;
	isOP6 = VarUsrAgt.indexOf("opera 6.") != -1 || VarUsrAgt.indexOf("opera/6.") != -1;
	isOP7 = VarUsrAgt.indexOf("opera 7.") != -1 || VarUsrAgt.indexOf("opera/7.") != -1;
	isMZ1 = navigator.appName.indexOf("Netscape") != -1 && navigator.appVersion.charAt(0)   == 5;

	isJS = ( (isIE5 && !isMac) || (isIE6 && !isMac) || isNS6 || isNS7 || isMZ1 || isOP7)

	//Operaのなりきりブラウザ対策
	if (isOP7) isNS4 = isNS6 = isNS7 = isIE5 = isIE6 = isMZ1 = false

var div_data = "" , div_mini="" , divcheck=false
var divW , divH , MouseToDiv
var ScriptStart = false

//Mozillaブラウザは、スクロールバー数値分を加算します（右寄せ時）
var MozillaState =0
if (isNS6 || isNS7 || isMZ1) MozillaState = 15

function divPreset(){
	alphaState() //アルファチャンネルの適応

	//座標移動を検出した場合に発動するルーチン
	if ( (DivLocat("X") - DivNow("X")) !=0 && XEnable || (DivLocat("Y") - DivNow("Y")) !=0 && YEnable){
		clearInterval(timerID); timerID = 0;
		timerID = setInterval("moveDiv()",moveIDtiming);
	}
}

function moveDiv(){
	alphaState() //アルファチャンネルの適応

	//移動方向チェックルーチン
	tempX = DivLocat("X") - DivNow("X"); tempY = DivLocat("Y") - DivNow("Y"); 
	tx = parseInt((tempX - Math.abs(tempX)) / (Math.abs(tempX) * 2))
	ty = parseInt((tempY - Math.abs(tempY)) / (Math.abs(tempY) * 2))
	if (tx==0) tx=1;    if (ty==0) ty=1     //正方向移動時
	if (isNaN(tx) || drawCount==1) tx=0;if (isNaN(ty) || drawCount==1) ty=0 //DIV基準位置とイコール

	//DIV移動量計算代入ルーチン
	document.getElementById(htmlIdName).style.top  = DivNow("Y") + Math.ceil(tempY * drawCount) + ty +"px";
	document.getElementById(htmlIdName).style.left = DivNow("X") + Math.ceil(tempX * drawCount) + tx + "px";

	if (tx == 0 && ty == 0){ //DIV位置の移動が完了
		clearInterval(timerID); timerID = 0;
		timerID = setInterval("divPreset()",timerIDtiming);
	}
}

function DivLocat(ordinate){
	var LRstate=0,UDstate=0;
	if (isIE5 || isIE6 || isOP7){ // mouseX ,Y 絶対座標
		mouseXm = document.body.scrollLeft || document.documentElement.scrollLeft;
		mouseYm = document.body.scrollTop  || document.documentElement.scrollTop;
	} else if (isNS6 || isNS7 || isMZ1) {
		mouseXm = self.pageXOffset;
		mouseYm = self.pageYOffset;
	}
// test
	else {
		mouseXm = document.body.scrollLeft || document.documentElement.scrollLeft;
		mouseYm = document.body.scrollTop  || document.documentElement.scrollTop;
	}

/*
	if (ordinate.toUpperCase() == "X"){        // X座標
		if(LRmode==1){
//			return Math.round(WinSize("X")/2+contents_size/2) - parseInt(WinSize("X") - DivSize("W") - DivLocationX + mouseXm - MozillaState)
			return Math.round(WinSize("X")/2-contents_size/2) + parseInt(WinSize("X") + DivSize("W") + DivLocationX - mouseXm + MozillaState))
		}
		if ( WinSize("X") > contents_size ) {
			return Math.round(WinSize("X")/2+contents_size/2) - ( divBlocksize_x + mouseXm );
		} else {
			return contents_size - divBlocksize_x;
		}
	} else if (ordinate.toUpperCase() == "Y"){ // Y座標
		if (UDmode==1){
			return (WinSize("Y") -  DivSize("H")) - parseInt(WinSize("Y") - DivSize("H") - DivLocationY + mouseYm - MozillaState)
		}   return (WinSize("Y") -  DivSize("H")) - divBlocksize_y + mouseYm;
	}
*/

	if (ordinate.toUpperCase() == "X"){        // X座標
		if(LRmode==1){
//			alert(parseInt(WinSize("X")));
			return parseInt(WinSize("X")-34);
//			return parseInt(WinSize("X") - DivSize("W") - DivLocationX + mouseXm - MozillaState)
		}   return contents_size
	} else if (ordinate.toUpperCase() == "Y"){ // Y座標
		if (UDmode==1){
//			return parseInt(WinSize("Y") - DivSize("H") - DivLocationY + mouseYm - MozillaState)
return (WinSize("Y") -  DivSize("H")) - divBlocksize_y + mouseYm;
		}   return DivLocationY + mouseYm
	}


}

function DivNow(ordinate){
	if(!ScriptStart) return;
	if (ordinate.toUpperCase()=="X"){
		return parseInt(div_data.style.left)
	} else if (ordinate.toUpperCase()=="Y"){
		return parseInt(div_data.style.top)
	}
}

function WinSize(ordinate){
	if (ordinate.toUpperCase()=="X"){        //X座標
		if(isIE5 || isIE6 || isOP7){
			return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		} else if (isNS6 || isNS7 || isMZ1) {
			return innerWidth
		}
		// test
		else {
			return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		}
	} else if (ordinate.toUpperCase()=="Y"){ //Y座標
		if(isIE5 || isIE6 || isOP7){
			return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		} else if (isNS6 || isNS7 || isMZ1) {
			return innerHeight
		}
		// test
		else {
			return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		}
	}
}

function DivSize(ordinate){
	if (ordinate.toUpperCase()=="W"){        //X座標
		return div_data.offsetWidth
	} else if (ordinate.toUpperCase()=="H"){ //Y座標
		return div_data.offsetHeight;
	}
}

function alphaState(){
	if(!alphaS) return;
	a_state = alphaLast
	if(MouseToDiv) a_state=alphaOver
	if (isIE5 || isIE6){
		div_data.style.filter = "alpha(opacity="+a_state+",style=0)"
	} else if (isNS6 || isNS7 || isMZ1){
		div_data.style.MozOpacity= a_state/100
	} else if (isOP7){

	}
}


var LastDivMode = false
function mousemove(myEvents){
	if(!ScriptStart) return;
	if (isIE5 || isIE6 || isOP7){ // mouseX ,Y 絶対座標
		mouseX  = window.event.clientX + document.body.scrollLeft; 
		mouseY  = window.event.clientY + document.body.scrollTop; 
	} else if (isNS6 || isNS7 || isMZ1) {
		mouseX  = myEvents.pageX; mouseY  = myEvents.pageY;
	}

	DivSize("W");DivSize("H")
	if ( mouseX >= DivNow("X") && ( DivNow("X") + DivSize("W") ) >= mouseX && mouseY >= DivNow("Y") && ( DivNow("Y")+DivSize("H") ) >= mouseY){
		if (LastDivMode == false) DivContents(maxMenu)
		MouseToDiv = LastDivMode = true
	} else {
		if (LastDivMode == true) DivContents(minMenu)
		MouseToDiv = LastDivMode = false
	}
}

test1=0
function DivContents(menu_mode){
	if(!div_mini) return;
	if (menu_mode != div_data.innerHTML){
		div_data.innerHTML = menu_mode
		test1++
	}
	status = test1
}

var minMenu,maxMenu
function startwindow(){
//	if ( !document.getElementById(htmlIdName) || !isJS ) return;
	if ( !document.getElementById(htmlIdName) ) return;

	//グローバルで使用するDIV情報取得「div_data」の作成
	div_data = document.getElementById(htmlIdName);
	div_data.style.position="absolute"
	divcheck=true; //DIVの準備が完了

	if (document.getElementById("MoveDiv_min")){
		div_mini = document.getElementById("MoveDiv_min")
		maxMenu = div_data.innerHTML
		minMenu = div_mini.innerHTML
		div_mini.style.display = "none";
		div_mini.style.position= "absolute";
		div_data.innerHTML = minMenu
	}

	//DIVサイズの取得
	DivSize("W");
	DivSize("H");

	//ユーザーステータス読込
for (i = 0 ; i <= document.getElementsByTagName('div').length; i ++){
		div_check = document.getElementsByTagName('div')[i];
		if (div_check){
			if(div_check.id.substr(0,8) == htmlIdName){
				ComponentsDivData(div_check.id) //ステータス読み込み
			}
		} else {
			break; //FOR終了
		}
	}

document.getElementById(htmlIdName).style.top  = DivLocat("Y")+"px";
document.getElementById(htmlIdName).style.left = DivLocat("X")+"px";

	alphaState()       //アルファフィルタ適応
	ScriptStart = true //スクリプト動作の許可

	timerID = setInterval("divPreset()",timerIDtiming);	//基幹時間軸の生成

	//マウスの動作を追従
	if (isJS) document.onmousemove = mousemove;
}

checkData=""
function ComponentsDivData(data){
	for (di = 0 ; di <= data.length ; di++){
		if ( data.substr(di,1) == "@" || data.substr(di,1) == "," || data.substr(di,1) == ";"){
			checkData = data.substr(di+1,2)
			for (dk = di+1 ; dk <= data.length ; dk++){ //識別子チェッカー
				if ( data.substr(dk,1) == "=" || data.substr(dk,1) == ":" ) break;
				if ( data.substr(dk,1) == "@" || data.substr(dk,1) == "," || data.substr(dk,1) == ";"){
					 dk=0;break;
				}
			}
			numberData = parseInt(data.substr(dk+1,8))
			if (!isNaN(numberData)){
				switch (checkData){
					case "aS":alphaS       = numberData;break
					case "aL":alphaLast   = numberData;break
					case "aO":alphaOver   = numberData;break
					case "Dx":DivLocationX = numberData;break
					case "Dy":DivLocationY = numberData;break
					case "T1":timerIDtiming= numberData;break
					case "T2":moveIDtiming = numberData;break
					case "Xe":XEnable     = numberData;break
					case "Ye":YEnable     = numberData;break
					case "LR":LRmode      = numberData;break
					case "UD":UDmode      = numberData;break
					case "cD":
						drawCount   = numberData/10;
						if (drawCount>1) drawCount=1
						break
					default:break;
				}
			}
		}
	}
}
