﻿var LinkLayerData = new function(){
	if(! document.all){
		this.init = function(){}
		return false;
	}
	function registerLinkLayer(o){
	///// ここから /////
	// 1	展開できるかどうか
	// 2	ID
	// 3	表示文字
	// 4	コメント
	// 5	リンク先
	// Main
	o.makeChild(true, "Main", "Main", "メインページ。", "http://shoo.s20.xrea.com/shoo/index.html#PageTop");
	o.ch["Main"].makeChild(false, "Top", "Top", "トップ", "http://shoo.s20.xrea.com/shoo/index.html#Main");
	o.ch["Main"].makeChild(false, "Dialy", "Dialy", "日記", "http://shoo.s20.xrea.com/shoo/index.html#Dialy");
	// History
	o.makeChild(false, "History", "History", "更新履歴。", "http://shoo.s20.xrea.com/shoo/history.html#PageTop");
	// Diary
	//o.makeChild(false, "Diary", "Diary", "日記ログ。", "diary/index.html#PageTop");
	// BLOG
	o.makeChild(false, "BLOG", "BLOG", "ブログ。", "http://shoo.9.dtiblog.com/");
	// About
	o.makeChild(true, "About", "About", "このサイトについての注意事項やサイトマップなど。", "http://shoo.s20.xrea.com/shoo/about.html#PageTop");
	o.ch["About"].makeChild(false, "About", "About", "このサイトについて。", "http://shoo.s20.xrea.com/shoo/about.html#About");
	o.ch["About"].makeChild(false, "Rule", "Rule", "利用規約", "http://shoo.s20.xrea.com/shoo/about.html#Rule");
	o.ch["About"].makeChild(false, "Map", "Map", "サイトマップ", "http://shoo.s20.xrea.com/shoo/about.html#Map");
	// Illust
	o.makeChild(true, "Illust", "Illust", "過去に描いたイラストの展示場", "http://shoo.s20.xrea.com/shoo/illust.html#PageTop");
	o.ch["Illust"].makeChild(false, "東方", "東方", "東方シリーズの絵", "http://shoo.s20.xrea.com/shoo/illust.html#東方");
	o.ch["Illust"].makeChild(false, "その他版権", "その他版権", "その他版権モノの絵", "http://shoo.s20.xrea.com/shoo/illust.html#その他版権");
	o.ch["Illust"].makeChild(false, "オリジナル", "オリジナル", "オリジナルの絵", "http://shoo.s20.xrea.com/shoo/illust.html#オリジナル");
	//o.ch["Illust"].makeChild(false, "オリジナル", "オリジナル", "オリジナルの絵", "illust/index.html#オリジナル");
	// Program
	o.makeChild(true, "Programing", "Programing", "プログラミングのはなし", "http://shoo.s20.xrea.com/shoo/programing/index.html#PageTop");
	o.ch["Programing"].makeChild(false, "Primary", "D言語入門", "D言語の入門講座", "http://shoo.s20.xrea.com/shoo/programing/index.html#primary");
	o.ch["Programing"].makeChild(false, "ChirnoQuality", "⑨でも(ry", "⑨でもわかるパチュリーのD言語講座", "http://shoo.s20.xrea.com/shoo/programing/index.html#ChirnoQuality");
	o.ch["Programing"].makeChild(false, "DMemo", "Dメモ", "D言語の個人的メモ", "http://shoo.s20.xrea.com/shoo/programing/index.html#DMemo");
	o.ch["Programing"].makeChild(false, "Tools", "小物", "作ったことのあるツールの類", "http://shoo.s20.xrea.com/shoo/programing/index.html#Tools");
	// Other
//	o.makeChild(true, "Other", "Other", "そのほかのコンテンツ。", "other/index.html#PageTop");
//	o.ch["Other"].makeChild(false, "CG講座", "CG講座", "CG講座", "other/index.html#CG講座");
//	o.ch["Other"].ch["CG講座"].makeChild(false, "SAI講座", "SAI講座", "ペイントツールSAIについての講座", "other/index.html#SAI講座");
	// Link
	o.makeChild(true, "Link", "Link", "リンクページ", "http://shoo.s20.xrea.com/shoo/link.html#PageTop");
	o.ch["Link"].makeChild(false, "Favorite", "Favorite", "お気に入りの中身", "http://shoo.s20.xrea.com/shoo/link.html#Favorite");
	o.ch["Link"].makeChild(false, "Search", "Search", "サーチエンジン", "link.html#Search");
	o.ch["Link"].makeChild(false, "Other", "Other", "その他。素材やリファレンスなど。", "http://shoo.s20.xrea.com/shoo/link.html#Other");
	// WEB拍手
	o.makeChild(false, "WEB拍手", "WEB拍手", "WEB拍手です。ご用の際にはこれを使用してください。", "http://shoo.s20.xrea.com/shoo/cgi-bin/webclap/clap_your_hand.cgi");
	///// ここまで /////
	}
	function LinkLayerMover(){
		var self = this;
		var oLinkLayer = document.getElementById('LinkLayer');
		var oLinkHead= document.getElementById('LinkHead');
		var oLinkBody= document.getElementById('LinkBody');
		if(oLinkLayer.currentStyle){
			this.DefaultOffsetY = parseInt(oLinkLayer.currentStyle.top);
			this.DefaultOffsetX = parseInt(oLinkLayer.currentStyle.left);
		}else{
			this.DefaultOffsetY = parseInt(oLinkLayer.style.top);
			this.DefaultOffsetX = parseInt(oLinkLayer.style.left);
		}
		this.OffsetY = self.DefaultOffsetY;
		this.OffsetX = self.DefaultOffsetX;
		this.posX = self.OffsetX;
		this.posY = self.OffsetY;
		this.MouseOnX = 0;
		this.MouseOnY = 0;
		this.bMouseMoved = false;
		
		this.writeCookie = function(){
			var d = new Date();
			d.setTime(d.getTime()+( 300 * 1000 ));
			if(document.body)Page.setCookie("LinkLayerMover::OffsetY", Math.round(this.OffsetY), d);
			if(document.body)Page.setCookie("LinkLayerMover::OffsetX", Math.round(this.OffsetX), d);
		}
		
		this.readCookie = function(){
			var y = Page.getCookie("LinkLayerMover::OffsetY");
			var x = Page.getCookie("LinkLayerMover::OffsetX");
			if(x.length != 0 && y.length != 0){
				this.posY = this.OffsetY = Number(y);
				this.posX = this.OffsetX = Number(x);
				// ちょっと時間を置いて位置決めをしないと、
				// ページ読み込み直後の場合スクロールが0と認識される。
				setTimeout(function(){
					self.posY = self.OffsetY + Page.getScrollTop();
					self.setPosition(Math.round(self.posX),Math.round(self.posY));
				}, 200);
			}
		}
		
		/// リンクレイヤーのポジションを変更します。
		this.setPosition = function(x,y){
			if(document.layers){
				oLinkLayer.pageY = y;
				oLinkLayer.pageX = x;
			}else if(document.all){
				if(oLinkLayer.runtimeStyle){
					oLinkLayer.runtimeStyle.top = y.toString() + "px";
					oLinkLayer.runtimeStyle.left = x.toString() + "px";
				}else{
					oLinkLayer.style.top = y.toString() + "px";
					oLinkLayer.style.left = x.toString() + "px";
				}
			}else if(oLinkLayer.getElementsByTagName){
				oLinkLayer.style.top = y.toString() + "px";
				oLinkLayer.style.left = x.toString() + "px";
			}
		}
		/// リンクレイヤーの、時間で動的な動作をするためのインターバル関数です
		this.Stalke = function(){
			if(! this.bMouseOn){
				var dy;
				if(oLinkLayer.currentStyle){
					dy = (Page.getScrollTop()-(parseInt(oLinkLayer.currentStyle.top)-this.OffsetY));
				}else{
					dy = (Page.getScrollTop()-(parseInt(oLinkLayer.style.top)-this.OffsetY));
				}
				this.posY += dy/20;
				this.setPosition(Math.round(this.posX), Math.round(this.posY));
			}
		}
		/// 位置と状態の初期化
		this.Initialize = function(){
			this.OffsetY = this.DefaultOffsetY;
			this.OffsetX = this.DefaultOffsetX;
			this.posY = Page.getScrollTop() + this.OffsetY;
			this.posX = this.OffsetX;
			this.setPosition(Math.round(this.posX), Math.round(this.posY));
			if(LinkBody.runtimeStyle){
				oLinkBody.runtimeStyle.display = "";
			}else{
				oLinkBody.style.display = "";
			}
		}
		/// トグル動作
		this.Togle = function(){
			if(oLinkBody.runtimeStyle){
				if(oLinkBody.runtimeStyle.display == "none"){
					oLinkBody.runtimeStyle.display = "block";
				}else{
					LinkBody.runtimeStyle.display = "none";
				}
			}else{
				if(oLinkBody.style.display == "block"){
					oLinkBody.style.display = "none";
				}else{
					oLinkBody.style.display = "block";
				}
			}
		}
		/// ダブルクリック時の動作
		oLinkHead.ondblclick = function(e){ self.Initialize() };
		/// クリック時の動作
		oLinkHead.onclick = function(e){
			if(! self.bMouseMoved){
				self.Togle();
			}
			self.bMouseMoved = false;
			return true;
		}
		oLinkHead.oncontextmenu = function(e){
			//alert(0);
			return false;
		}
		/// マウスダウン時の動作
		oLinkHead.onmousedown = function(e){
			var MouseOnY;
			var MouseOnX;
			if(document.layers){
				MouseOnX = e.layerX;
				MouseOnY = e.layerY;
			}else if (document.all){
				MouseOnX = event.offsetX + 2;
				MouseOnY = event.offsetY + 2;
			}else if (obj.getElementsByTagName){
				MouseOnX = e.pageX - parseInt(obj.style.left);
				MouseOnY = e.pageY - parseInt(obj.style.top);
			}
			/// マウスムーブ時の動作の登録
			Page.setOnMouseMove("LinkLayerMover", function(e){
				if(document.layers){
					self.posX = self.OffsetX = e.pageX - MouseOnX;
					self.posY = self.OffsetY = e.pageY - MouseOnY;
				}else if (document.all){
					self.OffsetX = event.clientX - MouseOnX;
					self.OffsetY = event.clientY - MouseOnY;
					self.posX = self.OffsetX + Page.getScrollLeft();
					self.posY = self.OffsetY + Page.getScrollTop();
				}else if (LinkLayer.getElementsByTagName){
					self.posX = self.OffsetX = e.pageX - MouseOnX;
					self.posY = self.OffsetY = e.pageY - MouseOnY;
				}
				if(self.posX < 0)self.posX = self.OffsetX = 0;
				if(self.posY < 0)self.posY = self.OffsetY = 0;
				self.setPosition(Math.round(self.posX), Math.round(self.posY));
				self.bMouseMoved = true;
				return true;
			});
			/// マウスアップ時の動作の登録
			Page.setOnMouseUp("LinkLayerMover", function(e){
				// マウスムーブの登録解除
				Page.setOnMouseMove("LinkLayerMover", undefined);
				// マウスアップの登録解除
				Page.setOnMouseUp("LinkLayerMover", undefined);
				return true;
			});
			return true;
		}
		this.IntervalID = setInterval(function(){self.Stalke();},15);
		this.setPosition(this.DefaultOffsetX,this.DefaultOffsetY);
	}
	this.init = function(mylinkname){
	var oLinkLayer = document.createElement("div");
	function MakeExtendLink(parent, name, text, title, linkurl){
		var hoge;
		hoge = document.createElement("span");
		hoge.setAttribute("href", linkurl);
		hoge.Head = document.createElement("a");
		hoge.Head.setAttribute("title", title);
		//hoge.Head.setAttribute("href", Page.RootDir+"/"+linkurl);
		hoge.Head.setAttribute("href", linkurl);
		hoge.Body = document.createElement("div");
		hoge.Body.className = "MenuContents";
		hoge.appendChild(hoge.Head);
		hoge.appendChild(hoge.Body);
		hoge.Name = name;
		hoge.parent = parent;
		hoge.Head.innerText = text;
		hoge.UniqueID = parent.createID();
		hoge.isExtend = true;
		hoge.Range = new Range(hoge.UniqueID, hoge.UniqueID+1);
		hoge.ch = new Array();
		// public
		hoge.makeChild = function(bExtend,name,text,title,linkurl){
			var temp;
			if(bExtend) temp = MakeExtendLink(hoge, name, text, title, linkurl);
			else temp = MakeLinkItem(hoge, name, text, title, linkurl);
			hoge.Body.appendChild(temp);
			oLinkLayer.ItemList.push(temp);
		}
		// protected
		hoge.setItem = function(item){
			hoge.ch[item.Name] = item;
		}
		// protected
		hoge.createID = function(){
			hoge.Range.appendRange(1);
			return hoge.parent.createID();
		}
		// protected
		hoge.getLastSelection = function(){
			return hoge.parent.getLastSelection();
		}
		hoge.doShow = function(){
			if(hoge.Body.runtimeStyle){
				hoge.Body.runtimeStyle.display = "block";
			}else{
				hoge.Body.style.display = "block";
			}
		}
		hoge.doHide = function(){
			if(hoge.Body.runtimeStyle){
				hoge.Body.runtimeStyle.display = "none";
			}else{
				hoge.Body.style.display = "none";
			}
		}
		hoge.isVisible = function(){
			if(hoge.Body.currentStyle){
				return hoge.Body.currentStyle.display == "block";
			}else{
				return hoge.Body.style.display == "block";
			}
		}
		hoge.doTogle = function(){
			if(hoge.isVisible()){
				hoge.doHide();
			}else{
				hoge.doShow();
			}
		}
		hoge.doReset = function(){
			hoge.Body.runtimeStyle.display = "";
		}
		hoge.toString = function(){ return hoge.Range.toString(); }
		hoge.Head.onclick = function(){
			oLinkLayer.currentChange(hoge.UniqueID);
			return true;
		}
		// 自身の登録
		parent.setItem(hoge);
		return hoge;
	}
	function MakeLinkItem(parent, name, text, title, linkurl){
		var hoge;
		hoge = document.createElement("a");
		//hoge.setAttribute("href", Page.RootDir+"/"+linkurl);
		//hoge.setAttribute("href", "http://shoo.s20.xrea.com/shoo/"+linkurl);
		hoge.setAttribute("href", linkurl);
		hoge.setAttribute("title", title);
		hoge.Name = name;
		hoge.innerText = text;
		hoge.UniqueID = parent.createID();
		hoge.isExtend = false;
		hoge.onclick = function(){
			oLinkLayer.currentChange(hoge.UniqueID);
			return true;
		}
		// 自身の登録
		parent.setItem(hoge);
		return hoge;
	}
	// レイヤーの設定
	oLinkLayer.setAttribute("id", "LinkLayer");
	oLinkLayer.Head = document.createElement("div");
	oLinkLayer.Head.setAttribute("id", "LinkHead");
	oLinkLayer.Body = document.createElement("div");
	oLinkLayer.Body.setAttribute("id", "LinkBody");
	oLinkLayer.appendChild(oLinkLayer.Head);
	oLinkLayer.appendChild(oLinkLayer.Body);
	oLinkLayer.LastSelection = NaN;
	oLinkLayer.ItemList = new Array();
	oLinkLayer.ch = new Array();
	// public
	oLinkLayer.makeChild = function(bExtend,name,text,title,linkurl){
		var temp;
		if(bExtend) temp = MakeExtendLink(oLinkLayer, name, text, title, linkurl);
		else temp = MakeLinkItem(oLinkLayer, name, text, title, linkurl);
		oLinkLayer.Body.appendChild(temp);
		oLinkLayer.ItemList.push(temp);
	}
	// protected
	oLinkLayer.setItem = function(item){
		oLinkLayer.ch[item.Name] = item;
	}
	// protected
	oLinkLayer.createID = function(){
		return oLinkLayer.ItemList.length;
	}
	oLinkLayer.currentChange = function(id){
		for(i in oLinkLayer.ItemList){
			if(oLinkLayer.ItemList[i].isExtend){
				if(oLinkLayer.ItemList[i].UniqueID == id && oLinkLayer.LastSelection == id){
					oLinkLayer.ItemList[i].doTogle();
				}else if(oLinkLayer.ItemList[i].Range.isIn(id)){
					oLinkLayer.ItemList[i].doShow();
				}else{
					oLinkLayer.ItemList[i].doHide();
				}
			}
		}
		oLinkLayer.LastSelection = id;
	}
	registerLinkLayer(oLinkLayer);

	var linkpath = mylinkname.split("/");
	var linkcurrent = oLinkLayer;
	for(var i=0;i<linkpath.length;i++){
		linkcurrent = linkcurrent.ch[linkpath[i]];
	}
	oLinkLayer.currentChange(linkcurrent.UniqueID);
	document.body.appendChild(oLinkLayer);
	oLinkLayer.Mover = new LinkLayerMover();
	oLinkLayer.Mover.readCookie();
	Page.setOnUnLoad("LinkLayer", function(){oLinkLayer.Mover.writeCookie();});
	}
}
