﻿
var timeID = null;
var maxX = 0;
var maxY = 0;
var ScrollSpeed = 20;
var saveScPos;

var testServerFlag = false;
var indexTOP = "index.html";
//var indexTOP = "index_tmp.html";

if(testServerFlag){
	// ローカルテスト用
	var xmlPath = "http://www.geometry.ne.jp/users/drbank/www";
	var defPath = "http://www.geometry.ne.jp/users/drbank/www";
	var secPath = "http://www.geometry.ne.jp/users/drbank/www";
	var secProt = "http://";
}else{
	// 本番用
	var xmlPath = "";
	var defPath = "http://www.dp-net.co.jp";
	var secPath = "https://www.dp-net.co.jp";
	var secProt = "https://";
}


// 間違いのないように全文字列を小文字に変換
var agt = navigator.userAgent.toLowerCase();
var is_gecko = (agt.indexOf('gecko') != -1);

function clearTime()
{
	clearInterval(timeID);
	timeID = null;
}
/*
String.prototype.camelize = function(){
	return this.replace( /-([a-z])/g, function($0,$1){return $1.toUpperCase()});
}
*/
//スタイル属性を読み取る
function getCSSValue(selector, property, cssindex)
{
	var p = (cssindex == undefined ) ? 0 : cssindex;

	property = (property.match(/-/)) ? property.camelize() : property;
	var stylesheets = document.styleSheets[p].rules || document.styleSheets[p].cssRules;
	var len = stylesheets.length;

	for(var i=0; i<len; i++){
		var css = stylesheets[i];
		if(css.style != undefined){
			if(css.selectorText.toLowerCase() == selector.toLowerCase()){
				if(css.style[property]){
					return css.style[property];
				}
			}
		}
	}
	return null;
}

//要素のスタイル属性を取得する関数
function getElementStyle(targetElem,IEStyleProp,CSSStyleProp)
{
	var elm = targetElem;

	if(elm.currentStyle){
		return elm.currentStyle[IEStyleProp];

	}else if(window.getComputedStyle){
		var compStyle = window.getComputedStyle(elm,"");
		return compStyle.getPropertyValue(CSSStyleProp);
	}
}


function getInnerSize()
{
	var obj = self;
	if(window.opera || document.layers){	//n4 o6用
		var w = obj.innerWidth;
		var h = obj.innerHeight;
		var sl = pageXOffset;
		var st = pageYOffset;

	} else if(document.all){		//ie用
		if(is.ie7up){
			var w = document.documentElement.clientWidth;
			var h = document.documentElement.clientHeight;
	      		var sl = document.documentElement.scrollLeft;
	      		var st = document.documentElement.scrollTop;
		}else{
			var w = obj.document.body.clientWidth;
			var h = obj.document.body.clientHeight;
	      		var sl = document.body.scrollLeft;
	      		var st = document.body.scrollTop;
		}

	}else if(document.getElementById){	//n6,n7,m1用
		var w = obj.innerWidth;
		var h = obj.innerHeight;
		var sl = pageXOffset;
		var st = pageYOffset;

	}
	return [w,h,sl,st];
}

function getClinetSize()
{
	var cw, hw;

	if(is.ie7up){
		cw = document.documentElement.scrollWidth;
		if(document.documentElement.offsetWidth > cw){
			cw = document.documentElement.offsetWidth;
		}
		ch = document.documentElement.scrollHeight;
		if(document.documentElement.offsetHeight > ch){
			ch = document.documentElement.offsetHeight;
		}

	}else if(is.ie){
		cw = document.body.scrollWidth;
		if(document.body.clientWidth > cw){
			cw = document.body.clientWidth;
		}
		ch = document.body.scrollHeight;
		if(document.body.clientHeight > ch){
			ch = document.body.clientHeight;
		}

	}else if(is.safari || is.opera){
		cw = document.body.scrollWidth;
		ch = document.body.scrollHeight;

	}else if(is.firefox || is.ns){
		cw = document.documentElement.scrollWidth;
		ch = document.documentElement.scrollHeight;

	}
	return [cw,ch];
}


function getPosition(that)
{
	var targetElem = that;
	var pos = new function(){this.x = 0; this.y = 0;}

	while(targetElem){
		pos.x += targetElem.offsetLeft; 
		pos.y += targetElem.offsetTop; 
		targetElem = targetElem.offsetParent;

		//IEの補正：上記計算で無視されてしまう各親要素のborder幅を加算
		if((targetElem) && (is.ie)){
			pos.x += (parseInt(getElementStyle(targetElem,"borderLeftWidth","border-left-width")) || 0);
			pos.y += (parseInt(getElementStyle(targetElem,"borderTopWidth","border-top-width")) || 0);
		}
	}

	//geckoの補正：カウントしないbody部border幅をマイナスしてしまうので２倍して加算
	if(is.gecko){
		//以下の部分でbody部を取得し、borderの減算を補正する。
		//var b = document.getElementsByTagName("body")[0];　　//body部を取得
		//pos.x += 2*(parseInt(getElementStyle(b, "borderLeftWidth","border-left-width")) || 0);
		//pos.y += 2*(parseInt(getElementStyle(b, "borderTopWidth","border-top-width")) || 0);
		pos.x += 2*(parseInt(getElementStyle(document.getElementsByTagName("body")[0],"borderLeftWidth","border-left-width")) || 0);
		pos.y += 2*(parseInt(getElementStyle(document.getElementsByTagName("body")[0],"borderTopWidth","border-top-width")) || 0);
	}
	return pos;
}

function getScrollNowPosition()
{
	var pos = new function(){this.x = 0; this.y = 0;}

	if(is.op || is.ie7up){
		pos.x = document.body.parentNode.scrollLeft;
		pos.y = document.body.parentNode.scrollTop;

	}else if(is.gecko){
		pos.x = window.pageXOffset;
		pos.y = window.pageYOffset;

	}else{
		pos.x = document.body.scrollLeft;
		pos.y = document.body.scrollTop;
	}
	return pos;
}

function softScrollTarget(yp)
{
	var pos = getScrollNowPosition();
	var scdistX = pos.x;
	var scdistY = pos.y;

	scdistY = yp - scdistY;

	if(maxY < 50 && scdistY){
		var gv = scdistX / scdistY;
		scdistY = (Math.abs(scdistY) > 2) ? Math.round(scdistY * 0.2) : ((scdistY > 0) ? 1 : -1);
		maxY ++;
		window.scrollBy(Math.round(-scdistY*gv), scdistY);

		if(pos.y == saveScPos.y){
			saveScPos = getScrollNowPosition();
			if(timeID == null){

				timeID = setInterval("softScrollTarget("+yp+")",ScrollSpeed);
			}
		}else{
			maxX = maxY = 0;
			clearTime();
		}

	}else if(maxX < 50 && scdistX){
		scdistX = (scdistX > 2) ? Math.round(scdistX * 0.2) : 1;
		maxX ++;
		window.scrollBy(-scdistX, scdistY);

		if(pos.x == saveScPos.x){
			saveScPos = getScrollNowPosition();
			if(timeID == null){
				timeID = setInterval("softScrollTarget("+yp+")",ScrollSpeed);
			}
		}else{
			maxX = maxY = 0;
			clearTime();
		}
	}else{
		window.scrollTo(scdistX,yp);
		maxX = maxY = 0;
		clearTime();
	}
}

function goPosition(id)
{
	var dn = (goPosition.arguments.length > 1) ? parseInt(goPosition.arguments[1],10) : 0;

	saveScPos = getScrollNowPosition();

	clearTime();

	var targetpoint = getPosition(document.getElementById(id)).y + dn;

	var inner = 0;
	var ctshi = document.getElementById("container").offsetHeight;
	var inner = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	if(is.ie7up){
		//ctshi += 19;
	}else if(is.op || is.gecko){
		ctshi += 27;
	}

	if(ctshi - inner < targetpoint){
		targetpoint = ctshi - inner;
	}

	softScrollTarget((targetpoint<0) ? 0 : targetpoint);

	return false;
}

function goTop()
{
	return goPosition("container",-10);
}

function goTarget(url)
{
	location.href=url;
	return false;
}

function goPos()
{
	if(targetID != ""){
		setTimeout('goPosition("'+targetID+'",'+targetPS+')',300);
	}
}

function addResizeEvent(func)
{
	if(typeof(window.addEventListener) == 'function'){
		// Firefox、Safari、Opera、Chromeなど
		window.addEventListener('resize', func, false);

	}else if(typeof(window.attachEvent) == 'object'){
		// IEなど
		window.attachEvent('onresize', func);

	}else{
		var oldfunc = window.onresize;
		if(typeof(window.onresize) != 'function'){
			window.onresize = func;
		}else{
			window.onresize = function(){
				oldfunc();
				func();
			}
		}
	}
}

function addLoadEvent(func)
{
	if(typeof(window.addEventListener) == 'function'){
		// Firefox、Safari、Opera、Chromeなど
		window.addEventListener('load', func, false);

	}else if(typeof(window.attachEvent) == 'object'){
		// IEなど
		window.attachEvent('onload', func);

	}else{
		var oldfunc = window.onload;
		if(typeof(window.onload) != 'function'){
			window.onload = func;
		}else{
			window.onload = function(){
				oldfunc();
				func();
			}
		}
	}
}

function setTopAnchors()
{
	var p = 0;
	var c = 0;
	while(document.getElementsByTagName("a").item(p) != null){
		var obj = document.getElementsByTagName("a").item(p);
		var anchorname = obj.href.substr(obj.href.lastIndexOf("#",obj.href.length));
		var classname = obj.getAttribute((is.ie7under) ? 'className' : 'class');
		var idname = obj.getAttribute('id');

//alert("["+anchorname+"]["+classname+"]["+idname+"]");

		if(anchorname == "#container"){
			if(is.ie7under)
				obj.setAttribute('onclick',new Function('return goTop()'));
			else	obj.setAttribute('onclick','return goTop()');
			//eval("document.getElementsByTagName('A').item("+p+").onclick = function(){return goTop();}");

		}
		p++;
	}
}

function initCorection()
{
	// ターゲット位置に移動する
	goPos();

	setTopAnchors();

	if(typeof(cinit) == 'function'){
		cinit();
	}
	if(is.ie6under){
		if(typeof(setIE6PNG) == 'function'){
			setIE6PNG();
		}
	}
	if(typeof(initLoginoutBox) == 'function'){
		initLoginoutBox();
	}
	if(typeof(initCoverMasked) == 'function'){
		initCoverMasked();
	}
}

/*
if(document.URL.indexOf("https://") == -1){
	var urlary = document.URL.split(":");
	var url = "https:"+urlary[1];
	location.href = url;
}else{
*/
	var targetID = "";
	var targetPS = 0;
	var str = location.search + "";
	var pt = str.indexOf("?");
	if(pt >= 0){
		if(str.indexOf("&") != -1){
			if(str.split("&")[0].split("=")[0] == "?id" && str.split("&")[1].split("=")[0] == "ps"){
				targetID = str.split("&")[0].split("=")[1];
				targetPS = str.split("&")[1].split("=")[1];
			}
		}
	}
	addLoadEvent(initCorection);
/*
}
*/
