
/* 
 * 
 * userAgent.js
 * @ v 1.0
 * 
 */

function UserAgent() {
	
	var ua = navigator.userAgent.toLowerCase();
	var os;
	var browser;
	var version;
	var device;
	
	// os
	if (ua.indexOf("win") > 0) {
		this.os = "win";
		
	} else if (ua.indexOf("mac") > 0) {
		this.os = "mac";
		
	} else if (ua.indexOf("linux") > 0) {
		this.os = "linux";
		
	} else {
		this.os = "other";
	}
	
	// browser and version
	if (ua.indexOf("msie") > 0) {
		this.browser = "msie";
		this.version = parseInt(ua.substring(ua.indexOf("msie") + 5));
		
	} else if (ua.indexOf("firefox") > 0) {
		this.browser = "firefox";
		this.version = parseInt(ua.substring(ua.indexOf("firefox") + 8));
		
	} else if (ua.indexOf("safari") > 0 && ua.indexOf("chrome") == -1) {
		this.browser = "safari";
		this.version = parseInt(ua.substring(ua.indexOf("version") + 8));
		
	} else if (ua.indexOf("chrome") > 0) {
		this.browser = "chrome";
		this.version = parseInt(ua.substring(ua.indexOf("chrome") + 7));
		
	} else {
		this.browser = "other";
		this.version = undefined;
	}
	
	// device
	if (ua.indexOf("iphone") > 0) {
		this.device = "iphone";
		
	} else if (ua.indexOf("ipod") > 0) {
		this.device = "ipod";
		
	} else if (ua.indexOf("ipad") > 0) {
		this.device = "ipad";
		
	} else if (ua.indexOf("android") > 0) {
		
		if (ua.indexOf("mobile") > 0) {
			this.device = "android_mobile";
		} else {
			this.device = "android_tablet";
		}
		
	} else {
		this.device = "other";
	}
	
};


var UA = new UserAgent();



function createTopOfPage() {
	
	var doc = document;
	var btnT = doc.createElement("p");
	var btnB = doc.createElement("p");
	
	btnT.innerHTML = "&uarr;";
	btnB.innerHTML = "&uarr;";
	btnT.setAttribute("id","topOfPageTop");
	btnB.setAttribute("id","topOfPageBottom");
	btnT.setAttribute("title","ページの先頭へ戻る");
	btnB.setAttribute("title","ページの先頭へ戻る");
	
	doc.getElementById("globalFooter").appendChild(btnT);
	doc.getElementById("globalFooter").appendChild(btnB);
	doc.getElementById("topOfPageTop").onclick = onClickHandler;
	doc.getElementById("topOfPageBottom").onclick = onClickHandler;
	
	function onClickHandler() {
		pageScrollY(0);
		return false;
	}
	
	if (window.addEventListener) {
		window.addEventListener("scroll", onScroll, false);
	} else if (window.attachEvent) {
		//window.scroll = onScroll;
		window.attachEvent("onscroll", onScroll);
	}
	
	function onScroll(event) {
		var y = 0;
		
		// Webkit, Gecko
		if (typeof(window.pageXOffset) == "number") {
			y = window.pageYOffset;
			
		// IE(Standards mode)
		} else if (typeof(document.documentElement.scrollLeft) == "number") {
			y = document.documentElement.scrollTop;
			
		// Other IE
		} else if (typeof(document.body.scrollLeft) == "number") {
			y = document.body.scrollTop;
		}
		
		if(y === 0) {
			btnT.style.visibility = "hidden";
			btnB.style.visibility = "hidden";
		} else {
			btnT.style.visibility = "visible";
			btnB.style.visibility = "visible";
		}
	}
	
	onScroll();
}



/* 
 * 
 * scrollto.js
 * @ v 0.4
 * 
 */



var G_PAGE_SCROLL_ID;

function pageScrollY(y) {
	
	ResetPageScroll();
	
	var ypos     = y;
	var yp       = 0;
	var xp       = 0;
	var millisec = 16;
	var speed    = 5;
	var size         = getPageSize();
	var pageHeight   = size[1];
	var windowHeight = size[3];
	var scrollMaxY   = pageHeight - windowHeight;
	
	if (typeof y == "number") {
		ypos = y;
		
	} else if (typeof y == "object") {
		ypos = getElementPosition(y)[1];
		
	} else if (typeof y == "string") {
		if (y.indexOf("#") >= 0) {
			ypos = getElementPosition(y.match(/#.*/i)[0].substring(1, y.match(/#.*/i)[0].length))[1];
		} else {
			ypos = getElementPosition(y)[1];
		}
	}
	
	// Webkit, Gecko
	if (typeof(window.pageXOffset) == "number") {
		xp = window.pageXOffset;
		yp = window.pageYOffset;
		loop();
		
	// IE(Standards mode)
	} else if (typeof(document.documentElement.scrollLeft) == "number") {
		xp = document.documentElement.scrollLeft;
		yp = document.documentElement.scrollTop;
		loopIEStandards();
		
	// Other IE
	} else if (typeof(document.body.scrollLeft) == "number") {
		xp = document.body.scrollLeft;
		yp = document.body.scrollTop;
		loopIE();
	}
	
	if (ypos >= scrollMaxY) ypos = scrollMaxY;
	
	function loop() {
		var y = window.pageYOffset;
		result(y, loop);
	}
	
	function loopIEStandards() {
		var y = document.documentElement.scrollTop;
		result(y, loopIEStandards);
	}
	
	function loopIE() {
		var y = document.body.scrollTop;
		result(y, loopIE);
	}
	
	function result(y, func) {
		G_PAGE_SCROLL_ID = window.setTimeout(func, millisec);
		yp += (ypos - y) / speed;
		window.scrollTo(xp, yp);
		
		if(y < ypos + 2 && y > ypos - 2) {
			window.clearTimeout(G_PAGE_SCROLL_ID);
			window.scrollTo(xp, ypos);
		}
	}
}

function ResetPageScroll() {
	if(G_PAGE_SCROLL_ID) {
		window.clearTimeout(G_PAGE_SCROLL_ID);
	}
}

function getPageSize() {
	
	var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageHeight, arraySizes;
	
	// Gecko
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
		
	// Webkit, IE(Standards mode)
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
		
	// Other IE
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	// not IE
	if (window.innerHeight) {
		windowWidth  = window.innerWidth;
		windowHeight = window.innerHeight;
		
	// IE(Standards mode)
	} else if (document.documentElement.clientHeight && document.documentElement) {
		windowWidth  = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
		
	// Other IE
	} else if (document.body) {
		windowWidth  = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	yScroll < windowHeight ? pageHeight = windowHeight : pageHeight = yScroll;
	xScroll < windowWidth  ? pageWidth  = windowWidth  : pageWidth  = xScroll;
	
	arraySizes = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	
	return arraySizes;
}

function getElementPosition(element) {
	
	var target = typeof element == "string" ? document.getElementById(element) : element;
	var left   = 0;
	var top    = 0;
	
	while (target) {
		left  += target.offsetLeft;
		top   += target.offsetTop;
		target = target.offsetParent;
	}
	
	return new Array(left, top);
}



/* 
 * 
 * Page Highlight
 * 
 */



function currentPageHighlight() {
	
	var URL = document.URL;
	var dirName = URL.match(".+/(.+?)$")[1];
	var dirAry = URL.split("/");
	
	var nav = document.getElementById("mainContents");
	var a = nav.getElementsByTagName("a");
	var al = a.length;
	
	for (var i = 0; i < al; i++) {
		var str = a[i].getAttribute("href");
		if (str.indexOf(dirAry[2]) > 0 || str.indexOf(dirAry[3]) > 0 || str.indexOf(dirAry[4]) > 0) {
			a[i].setAttribute("class","current");
		}
	}
}



/* 
 * 
 * Navigation position style.
 * 
 */



function iOSFixPosition() {
	
	var URL = "http://" + document.domain + "/";
	var navigation = document.getElementById("navigation");
	
	if (document.URL.indexOf("t.") > 0) URL = "http://" + document.domain + "/glide/deploy/";
	
	//iOS position fixed
	if (document.URL === URL || document.URL === URL + "index.html" || document.URL === URL + "index.php" ) {
		navigation.style.position = "relative";
	} else {
		navigation.style.position = "absolute";
	}
}



/* 
 * 
 * Access Analysis
 * 
 */



// Google Analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-645359-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();



/* 
 * 
 * DOM Content Ready
 * 
 */



function DOMContentLoaded() {
	
	currentPageHighlight();
	if (UA.device === "other") {
		createTopOfPage();
	} else {
		iOSFixPosition();
	}
}


(function (){
	
	/* DOMContentLoaded */
	
	// Safari, Chrome, Firefox
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", DOMContentLoaded, false);
		
	// IE
	} else if (UA.browser === "msie") {
		try {
			if (document.readyState == "interactive" || document.readyState == "complete") {
				DOMContentLoaded();
			} else {
				setTimeout(arguments.callee, 0);
				return;
			}
			
		} catch (error) {
			setTimeout(arguments.callee, 0);
			return;
		}
	}
})();






