/*---< cmn.js >----------------------[05.07.17]
|
| [www.chooseikan.com]
| Common js components
|
+----< dependencies >----------------------------
|
| >> core.js
|
+----< notes >-----------------------------------
|
| Javascript 1.4 compliant
|
+------------------------------------------------
| (c)2003,2004,2005 Inter-Planning Inc.
| http://www.inter-garden.com
+----------------------------------------------*/

if (!top.opener) { window.name = "main_win"; }

//----- init links -----

function initCmn() {

	//-- init anchor sliding --
	var dPath = document.src;
	var a = document.links;
	for (var i = 0; i < a.length; i++) {
		var aPath = file.splitSrc(a[i]);
		if ((aPath.all == dPath.all) || (aPath.anchor && aPath.absPath == dPath.absPath)) {
			tasks.replace(a[i],'onclick','slideTo("' + aPath.anchor + '"); return false;');
		}
	}

	//-- init sub-window targets --
	for (var i = 0; i < a.length; i++) {
		if (a[i].target.match(/^win_/)) {
			var win = a[i].target.substring(a[i].target.lastIndexOf('_') + 1);
			tasks.replace(a[i],'onclick','openSubWindow("'+ win +'")','target');
		}
	}

	//-- init window resizing --
	window.onresize = checkWinSize;
	setWinSize();

	//-- anchor position fix --
	if (document.src.anchor) { window.scrollTo(0, getPos(document.src.anchor).y); }
}

function initPD(pd) {
	var pd = pd.split(',');
	for (var i = 0; i <pd.length; i++) {
		tasks.replace(getById(pd[i]),'onchange','verifyPDHasVal(this)','verify');
	}
}

//----- window sizing methods -----

winMinSize = { w:800,h:512 };
if (browser.is('ns,ff')) { winMinSize.w += 16; winMinSize.h += 16; }

function checkWinSize() {
	window.onmousemove = setWinSize;
}

function setWinSize() {
	var bodyW = (window.innerWidth > winMinSize.w) ? window.innerWidth : winMinSize.w;
	var bodyH = (window.innerHeight > winMinSize.h) ? window.innerHeight : winMinSize.h;
	if (window.innerWidth < winMinSize.w || window.innerHeight < winMinSize.h) {
		var dW = window.outerWidth - window.innerWidth;
		var dH = window.outerHeight - window.innerHeight;
		var wX = (window.screenX) ? screenX : screenLeft;
		var wY = (window.screenX) ? screenY : screenTop;		
		window.resizeTo(bodyW + dW, bodyH + dH);
		window.moveTo(wX,wY);
	}
	window.onmousemove = null;
}

//----- button/link methods -----

function initBtns(branch) {
	var btns = collectElements('this.isTag(obj,"img") && this.hasAttribute(obj,"id=/^btn_/")',null,null,branch);
	for (var i = 0; i < btns.length; i++) {
		var src = splitFilePath(btns[i]);
		var btnId = btns[i].id.substring(btns[i].id.indexOf('_') + 1);
		if (docPath.paths.length <= topDepth && btnId == topBtnId) { // if top page
			btns[i].src = src.btn[0] + 'a' + src.btn[1];
		} else {
			if (docPath.path.indexOf(btnId) < 0) { // if not current page
				tasks.add(btns[i],'onmouseover','this.src = "' + src.btn[0] + 'o' + src.btn[1] + '"','btn');
				tasks.add(btns[i],'onmouseout','this.src = "' + src.btn[0] + 'n' + src.btn[1] + '"','btn');
			} else {
				btns[i].src = src.btn[0] + 'a' + src.btn[1];
			}
		}
		plItems.add(src.btn[0] + 'o' + src.btn[1]);
	}
}

function setActiveLinks(active,normal) {
	active = splitFilePath(document.location.href).paths.join(',') + ((active) ? "," + active : '');
	if (normal) {
		normal = toRegExp('/,?(' + normal.replace(/,/g,'|') + ')/g');
		active = active.replace(normal,''); //-- remove "normal" items
	}
	active = active.replace(/^(,+)/,''); //-- remove head comma (,)
	active = toRegExp('/(' + active.replace(/,/g,'|') + ')/');
	var a = collectElements('this.isTag(obj,"a") && this.hasAttribute(obj,"id=/^a_/")');
	for (var i = 0; i < a.length; i++) {
		if (a[i].id && a[i].id.match(active)) { setClass(a[i],'a'); }
	}
}

//----- new linked object methods -----

function initLinkObj(obj,link,win) {
	obj = getById(obj);
	tasks.add(obj,'onmouseover','setClass(this,"o")');
	tasks.add(obj,'onmouseout','setClass(this,"n")');
	if (win) {
		tasks.add(obj,'onclick','openSubWindow("' + win + '","' + link + '")');
	} else {
		tasks.add(obj,'onclick','document.location.href = "' + link + '"');
	}
}

//----- new window methods -----
function openSubWindow(winName,link){
	var w,h,p1,p2,params;
	if (link == null) { link = '' }
	switch (winName) {
		case 'shisetsu' : w = 840; h = 680; p1 = 'scrollbars,resizable,status=1'; break;
		default : w = 0; h = 0; p = ''; p1='titlebar,toolbar,location,menubar,directories,copyhistory,status,resizable,scrollbars=1'; if (winName == null) { winName ='new' }
	}
	winName = 'win_' + winName;
	if (w > 0 && h > 0) {
		var x = (screen.width - w) / 2;
		var y = (screen.height - h) / 2;
		if (browser.ie) { y -= 32; }
		p2 = ',height=' + h + ',width=' + w + ',top=' + y + ',left=' + x;
	}
	if (window[winName] != null) {
		window[winName].focus();
	} else {
		w = window.open(link,winName,p1+p2)
		w.focus();
	}
}

//----- display methods -----
function toggleDisplay(who) {
	if (typeof who == "string") {
		who = getById(who);
	} else {
		who = who.nextSibling;
		while (who.tagName == null) { who = who.nextSibling }
	}
	who.style.display = (who.style.display == 'none') ? '' : 'none';
}

//---------- slider ----------[begin]
var slideTimer;
var sliding = false;

function slideTo(who){
	var sPos = getPageOffset();
	var ePos = getPos(who);
	if (ePos.y > 12) { ePos.y -= 12; }
	var steps = 5;
	sliding = true;
	ePos.y -= 4;
	goSlide(sPos.x, sPos.y, 0, ePos.y, steps);
}

function goSlide(curX, curY, endX, endY, steps){
	var newX, newY;
	if (slideTimer) { clearTimeout(slideTimer) }
	if (!endX || endX < 0) { endX = 0 }
	if (!endY || endY < 0) { endY = 0 }
	if (!curX) { curX = getPageOffset().x }
	if (!curY) { curY = getPageOffset().y }
	if (!steps) { steps = 6 }
	curX += (endX - curX) / steps;
	curY += (endY - curY) / steps;
	if (curX < 0) { curX = 0 }
	if (curY < 0) { curY = 0 }
	newX = Math.round(curX);
	newY = Math.round(curY);
	window.scrollTo(newX, newY);
	if (newX != endX || newY != endY) {
		slideTimer = setTimeout("goSlide("+curX+","+curY+","+endX+","+endY+","+steps+")",15);
	} else {
		sliding = false;
	}
}
function getPageOffset() {
	var x, y;
	x = (document.body.scrollLeft) ? document.body.scrollLeft : ((window.pageXOffset) ? window.pageXOffset : 0 );
	y = (document.body.scrollTop) ? document.body.scrollTop : ((window.pageYOffset) ? window.pageYOffset : 0 );
	return new vector(x,y);
}
//---------- slider ----------[end]

//---------- form methods ----------[begin]

function verifyPDHasVal(pd) {
	if (!pd) { pd = this; }
	if (!pd.value) { pd.selectedIndex = 0; }
}

//---------- form methods ----------[end]

