/*----------------------------------------
| mizutori.co.uk -> cmn.js
| Copyright (c)2006 Inter-Planning Inc.
| Written by Joel Millin (jomi@inter-garden.com) [06.01.16]
----------------------------------------*/

ua = navigator.userAgent.toLowerCase();
isOP = (ua.match(/opera/)) ? true : false;
isIE = (!isOP && ua.match(/msie/)) ? true : false;

/*----------------------------------------
| init common
----------------------------------------*/

function initCmn() {
	//-- init window name --
	if (window == top && !window.opener && !window.name) {
		window.name = 'win_main';
		window.onunload = function() { window.name = ''; };
	}
	//-- init link targets --
	for (var i = 0; i < document.links.length; i++) {
		if (document.links[i].target.match(/^win_(.+)$/)) {
			document.links[i].onclick = openSubWindow;
		}
	}
}

/*----------------------------------------
| item navigation
----------------------------------------*/

itemNav = {};

itemNav.init = function(rowCnt) {
	this.items = [];
	if (!rowCnt) { rowCnt = 4; }
	this.rowCnt = rowCnt;
	this.curRow = 0;
	this.obj = document.getElementById('item-list');
	this.btns = {}
	this.btns.prev = document.getElementById('btn-prevrow');
	this.btns.next = document.getElementById('btn-nextrow');
	this.btns.prev.onclick = function() { this.blur(); };
	this.btns.next.onclick = function() { this.blur(); };
	for (var i = 0; i < this.obj.childNodes.length; i++) {
		if (this.obj.childNodes[i].tagName) {
			this.items[this.items.length] = this.obj.childNodes[i];
			this.obj.childNodes[i].className = 'disp-n';
		}
	}
	this.lastRow = Math.ceil(this.items.length / this.rowCnt) - 1;
	this.obj.className = '';
	this.showRow();
};

itemNav.showRow = function(row) {
	if (row == null) { row = this.curRow; }
	row += this.curRow;
	if (row < 0) { row = 0; }
	if (row > this.lastRow) { row = this.lastRow; }
	var first = row * this.rowCnt;
	var last = first + this.rowCnt;
	for (var i = 0; i < this.items.length; i++) {
		this.items[i].className = (i >= first && i < last) ? '' : 'disp-n';
	}
	this.btns.prev.className = (row > 0) ? '' : 'x';
	this.btns.next.className = (row < this.lastRow) ? '' : 'x';
	this.curRow = row;
};

/*----------------------------------------
| info navigation
----------------------------------------*/

infoNav = {};

infoNav.init = function(ids,aIndex) {
	if (!ids) { return; }
	this.items = {};
	this.curId = null;
	ids = ids.split(',');
	for (var i = 0; i < ids.length; i++) {
		var item = {};
		item.btn = document.getElementById('btn_info-' + ids[i]);
		item.btn.onclick = infoNav.showInfo;
		item.info = document.getElementById('info-' + ids[i]);
		this.items[ids[i]] = item;
		if (aIndex != null && aIndex == i) { item.btn.onclick(); }
	}
};

infoNav.showInfo = function(id) {
	if (infoNav.curId) {
		infoNav.items[infoNav.curId].btn.className = '';
		infoNav.items[infoNav.curId].info.className = 'disp-n';
	}
	if (!id || typeof id != "string") { id = this.id.match(/-(.+$)/)[1]; }
	infoNav.items[id].btn.className = 'a';
	infoNav.items[id].info.className = '';
	infoNav.curId = id;
}

/*----------------------------------------
| window functions
----------------------------------------*/

function openSubWindow(winName,link) {
	if (typeof winName != "string" && this.target) {
		winName = this.target.split('win_');
		winName = winName[winName.length - 1];
	}
	var w,h,x,y,p1,p2,params;
	if (link == null) { link = '' }
	switch (winName) {
		case 'pop' : w = 695; h = 410; p1 = 'resizable=1'; break;
		case 'size' : w = 705; h = 400; p1 = 'resizable=1'; break;
		case 'hinoki' : w = 750; h = Math.round(screen.height*0.8); p1 = 'scrollbars,resizable=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) {
		if (x == null) { x = (screen.width - w) / 2; }
		if (y == null) {
			y = (screen.height - h) / 2;
			if (isIE) { 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();
	}
}

function openInOpener(link){
	if(window.opener != null) {
		opener.location.href = link;
		opener.focus();
		return true;
	} else { return false }
}
