/******************************************************
 *	@name			basement.js
 *	@type			get
 *	@facility		汎用的Javascript
 *	@created date	2007/02/27 (revised 2008/02/27)
 *	@author			so
 *	@version		1.0
 *	@copyright (c) 2007 COCONOE INC.
 ******************************************************/

var scrollTimer;
var obj = document.all ? (document.getElementById ? 3 : 2) : (document.getElementById ? 4 : (document.layers ? 1 : 0));

function prevButton()
{
	if(document.referrer.indexOf(document.domain) != -1)
	{
		var prevUrl = document.referrer;
		document.write('<p id="prev"><a href="' + prevUrl + '">前のページへ</a></p>');
	}
	else
	{
		document.write('<p id="noprev">前のページは存在しません</p>');
	}
}

function toA(idn)
{
	if(!idn) idn = "top";

	if(obj == 1 || obj == 2 || obj == 3 || obj == 4)
	{
		frameScroll(0);
	}
	else
	{
		location.hash = idn;
	}
	return false;
}

function frameScroll(toY)
{
	if(scrollTimer) clearTimeout(scrollTimer);
	if(!toY || toY < 0) toY = 0;
	nowY = 0 + getOffsetY();


	nowX = 0 + getOffsetX();
	nowY += (toY - nowY) * 0.2;
	if(Math.abs(toY - nowY) <= 0.5)
	{
		nowY = toY;
	}

	window.scroll(nowX, nowY);

	if(Math.abs(nowY - toY) < 0.5)
	{
		clearTimeout(scrollTimer);
	}
	else
	{
		scrollTimer = setTimeout("frameScroll("+toY+")", 30);
	}
}

function getOffsetX()
{
	if(obj == 2 || obj == 3) return document.documentElement.scrollLeft || document.body.scrollLeft;
	else if(obj == 1 || obj == 4) return window.pageXOffset;
	return 0;
}

function getOffsetY()
{
	if(obj == 2 || obj == 3) return document.documentElement.scrollTop || document.body.scrollTop;
	else if(obj == 1 || obj == 4) return window.pageYOffset;
	return 0;
}

var wobj = new Array();

window.onload = function()
{
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
//	if (!document.getElementById("totop")) return false;
	
	var ernal = document.getElementsByTagName('a');
	var length = ernal.length;
	
	// open with blank window
	for (var i = 0; i < length; i++)
	{
		if (ernal[i].className == 'ext')
		{
			ernal[i].onclick = function()
			{
				var w = window.open(this.href);
				w.focus();
				return false;
			}
		}
		else if (ernal[i].className == 'ext uno')
		{
			ernal[i].onclick = function()
			{
				var pIndex = this.href.indexOf('://')+3;
				var drIndex = this.href.indexOf('/', pIndex);
				var domain = this.href.substr(pIndex, drIndex-pIndex);
				domain = domain.replace(/\.|-/g, '');
				
				var w = window.open(this.href, domain);
				w.focus();
				
				wobj[domain] = w;
				
				return false;
			}
		}
	}
	
	// play movie (company/tvcm.html)
	if (document.URL.match(/company\/tvcm\.html/))
	{
		for (var i = 0; i < length; i++)
		{
			if (ernal[i].className == 'mov')
			{
				ernal[i].onclick = function()
				{
					play(this.href);
					return false;
				}
			}
		}
	}
	
	// internal anchor warp (company/tvcm.html, company/overview/offices.html)
	if (document.URL.match(/company\/tvcm\.html/) ||
		document.URL.match(/company\/overview\/offices\.html/))
	{
		for (var i = 0; i < length; i++)
		{
			if (ernal[i].href.match(/#(inav(\d{2}))/))
			{
				ernal[i].onclick = function()
				{
					warp(this);
					return false;
				}
			}
		}
	}
	
	// return to pagetop
	if(document.getElementById('totop'))
	{
		var ttp = document.getElementById('totop');
		var upto = ttp.getElementsByTagName('a');
		
		for (var j = 0; j < upto.length; j++)
		{
			upto[j].onclick = function()
			{
				toA();
				return false;
			}
		}
	}
	
	// open enquete
	for (var i = 0; i < length; i++)
	{
		if (ernal[i].className == 'enquete')
		{
			ernal[i].onclick = function()
			{
				var w = window.open(this.href, 'window', 'width=700,height=700,menubar=no,toolbar=no,scrollbars=yes,status=yes');
				w.focus();
				return false;
			}
		}
	}

	// open brand function
	for (var i = 0; i < length; i++)
	{
		if (ernal[i].className == 'bfunc')
		{
			ernal[i].onclick = function()
			{
				var w = window.open(this.href, 'brandfunc', 'status=yes,toolbar=yes,scrollbars=yes,width=575,height=575,location=no,menubar=yes,directories=no,resizable=yes');
				w.focus();
				return false;
			}
		}
	}
	
	// open brand function opener
	for (var i = 0; i < length; i++)
	{
		if (ernal[i].className == 'opozaki')
		{
			ernal[i].onclick = function()
			{
				window.opener.location.href = this.href;
				window.opener.focus();
				return false;
			}
		}
	}
	
	// open mama site
	for (var i = 0; i < length; i++)
	{
		if (ernal[i].className == 'opmama')
		{
			ernal[i].onclick = function()
			{
				var w = window.open(this.href, 'mama', 'status=yes,toolbar=yes,scrollbars=yes,width=auto,height=auto,location=yes,menubar=yes,directories=yes,resizable=yes');
				w.focus();
				return false;
			}
		}
	}
	
	// change font-size
	handleFontSize();
	
	// print this page
	handlePrint();
}

/** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * * **/

var cookieName = 'ozakiStyle';
var toRoot = document.URL.match(/\/ozaki_2\//) ? '/ozaki_2' : '';

function handlePrint()
{
	var fn02 = document.getElementById('fn02');
	var ptp = activateBtnOnHeader(fn02);
	
	if( ptp == null ) return;
	
	if (window.name == 'pw')
	{
		// create and insert link tag
		var style = document.getElementsByTagName('link');
		var length = style.length;
		
		var link = document.createElement('link');
		link.setAttribute('rel',	'stylesheet');
		link.setAttribute('type',	'text/css');
		link.setAttribute('href',	toRoot+'/css/printprint.css');
		link.setAttribute('media',	'all');
		
		insertAfter(link, style[length-1]);
		
		var header = document.getElementById('header');
		var footer = document.getElementById('footer');
		
		// create new header
		var headerNew = createHeaderNew();
		
		// create print button
		var pBtnH = createBtnOnPrintWindow('btn_print', 'javascript:void(window.print());', 'このページを印刷する');
		var pBtnF = pBtnH.cloneNode(true);
		
		// create close window button
		var cBtnH = createBtnOnPrintWindow('btn_close', 'javascript:void(window.close());', 'このウィンドウを閉じる');
		var cBtnF = cBtnH.cloneNode(true);
		
		// insert new header and buttons
		header.parentNode.insertBefore(cBtnH, header);
		footer.parentNode.insertBefore(pBtnF, footer);
		insertAfter(pBtnH, cBtnH);
		insertAfter(cBtnF, pBtnF);
		insertAfter(headerNew, pBtnH);
	}
	else
	{
		ptp.onclick = function()
		{
			var w = window.open(document.URL, 'pw', 'width=820, height=600, menubar=yes,toolbar=no,scrollbars=yes,status=yes');
			w.focus();
			return false;
		}
	}
}

// if childNodes[n] is refChild, than childNodes[n+1] is newChild
function insertAfter(newChild, refChild)
{
	var parent = refChild.parentNode;
	
	if (parent.lastChild == refChild) return parent.appendChild(newChild);
	else return parent.insertBefore(newChild, refChild.nextSibling);
}

function createHeaderNew()
{
	var div = document.createElement('div');
	div.setAttribute('id', 'header_new');
	
	var logoJp = document.createElement('p');
	logoJp.setAttribute('id', 'logo_jp');
	
	var nOnLogoJp = document.createTextNode('kanko学生服の尾崎商事株式会社');
	
	logoJp.appendChild(nOnLogoJp);
	div.appendChild(logoJp);
	
	var logo = document.createElement('p');
	logo.setAttribute('id', 'logo');
	
	var nOnLogo = document.createTextNode('ｏｚａｋｉ');
	
	logo.appendChild(nOnLogo);
	div.appendChild(logo);
	
	return div;
}

function createLogo()
{
	var logo = document.createElement('p');
	logo.setAttribute('id', 'logo');
	
	var n = document.createTextNode('');
	logo.appendChild(n);
	
	return logo;
}

function createBtnOnPrintWindow(css, href, text)
{
	var p = document.createElement('p');
	p.setAttribute('class',		css);
	p.setAttribute('className',	css);
	
	var a = document.createElement('a');
	a.setAttribute('href', href);
	
	var n = document.createTextNode(text);
	
	a.appendChild(n);
	p.appendChild(a);
	
	return p;
}

function handleFontSize()
{
	var fn01 = document.getElementById('fn01');
	var cf = activateBtnOnHeader(fn01);
	
	if( cf != null )
	{
		cf.onclick = function()
		{
			changeFontSize();
			applyFontSize(cf);
			return false;
		}
		
		applyFontSize(cf);
	}
}

function activateBtnOnHeader(parent)
{
	if( parent != null )
	{
		var a = parent.firstChild;
		a.style.cssText = 'display:block';
		
		return a;
	}
	
	return null;
}

function loadFontSize()
{
	var fontSize = getCookie(cookieName);
	if (fontSize == null) fontSize = 'medium';
	
	return fontSize;
}

function applyFontSize(anchor)
{
	var fontSize = loadFontSize();
	var yPos, yPosHover;
	
	// see also /css/default.css
	if (fontSize == 'large')
	{
		fontSize	= '100%';
		yPos		= '-38px';
		yPosHover	= '-57px';
	}
	else
	{
		fontSize	= '82%';
		yPos		= '0';
		yPosHover	= '-19px';
	}
	
	// apply font-size
	document.body.style.fontSize = fontSize;
	
	// apply style
	anchor.style.backgroundPosition = '0 '+yPos;
	anchor.onmouseover = function()
	{
		anchor.style.backgroundPosition = '0 '+yPosHover;
	}
	anchor.onmouseout = function()
	{
		anchor.style.backgroundPosition = '0 '+yPos;
	}
}

function changeFontSize()
{
	var fontSize = loadFontSize();
	
	if (fontSize == 'medium') fontSize = 'large';
	else fontSize = 'medium';
	
	setCookie(cookieName, fontSize, 0, '/');
}

function getCookie(name)
{
	var arg		= name+'=';
	var alen	= arg.length;
	var clen	= document.cookie.length;
	
	var i = 0;
	
	while (i < clen)
	{
		var j = i+alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
		i = document.cookie.indexOf(' ', i)+1;
		if (i == 0) break;
	}
	
	return null;
}

function setCookie(name, value, term, pt)
{
	var expires = '', path = '';
	
	if (term)
	{
		var expires = new Date();
		term += expires.getMonth();
		expires.setMonth(term%12);
		expires.setFullYear(expires.getFullYear()+parseInt(term/12));
		expires = expires.toGMTString();
		expires = '; expires='+expires;
	}
	
	if (pt) path = '; path='+pt;
	
	document.cookie = name+'='+escape(value)+expires+path+';';
}

function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf(';', offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

// called from swf
function openWindowFromSwf(url)
{
	var pIndex = url.indexOf('://')+3;
	var drIndex = url.indexOf('/', pIndex);
	var domain = url.substr(pIndex, drIndex-pIndex);
	domain = domain.replace(/\.|-/g, '');
	
	var w = window.open(url, domain);
	w.focus();
	wobj[domain] = w;
}
