/**
 * 
 * common.js
 * 
 * @require prototype.js
 * @require scriptaculous.js
 * 
 */

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

/**
 * 親ウィンドウフォーカス
 */
function winFocus() {
	if(window.opener) { //親ウィンドウが開いている場合
		window.opener.focus();
	}else { //親ウィンドウが閉じている場合
		this.location.href="http://www.narasaho-c.ac.jp/index2.html";
	}
}
 
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

// smartScroll 1.4
function smartScroll ()
{	
	var allLinks = document.getElementsByTagName('a');
	
	for (var i=0;i<allLinks.length;i++)
	{
		var lnk = allLinks[i];
		
		if ((lnk.href && lnk.href.indexOf('#') != -1) && ((lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname)))
		{			
			var myHash = lnk.hash.replace(/#/g,"");
			if (!myHash.length == 0) {//hrefが"#"だった場合は処理を中止

				lnk.onclick = function ()
				{					
					//移動先のid名を取得
					var hash = this.hash;
					var sId = hash.replace(/#/g,"");
				
					if (!document.getElementById(sId)) return false;
					
					var nTargetY = document.getElementById(sId).offsetTop;	//移動先の位置（y座標）
					var nScrollTop = document.body.parentNode.scrollTop || window.pageYOffset || document.body.scrollTop; //現在の表示位置（y座標）
					
					//スクール量の調整			
					var winHeight = window.innerHeight || document.documentElement.clientHeight; //ウィンドウの高さ	
					var bodyHeight = document.body.offsetHeight; //bodyの高さ
					var footHeight = bodyHeight - nTargetY;
					var adjust = winHeight - (bodyHeight - nTargetY);
						
					//移動距離を計算
					if (winHeight > footHeight)
					{				
						yInterval = nTargetY - nScrollTop - adjust;
					} else
					{
						yInterval = nTargetY - nScrollTop;
					}
					
					function winScroll ()
					{
						var toY = Math.floor(yInterval * .2);					
						window.scrollBy(0,toY);
						yInterval -= toY; 
						myTimer = setTimeout(winScroll,30);
						if (toY == 0) clearTimeout(myTimer);
					}
									
					winScroll();	
					return false;
				};
			}
		}
	}	
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

/**
 * メニューの文字数制限
 */
function setMaxLength() {
	if (!document.getElementById("contentNavi")) return false;

	var text1 = document.getElementById("contentNavi").getElementsByTagName("h4");
	var l1 = text1.length;
	
	for (var i = 0; i < l1; i++) {	
		var maxLength = 21;
		var h4 = text1[i];
		var anchor = h4.firstChild;
		var myStr = anchor.innerHTML.length;	
		
		if (myStr > maxLength) {
			var myText = anchor.innerHTML.slice(0, maxLength) + "…";
			anchor.innerHTML = myText;
		}
	}
	
	var text2 = document.getElementById("contentNavi").getElementsByTagName("p");
	var l2 = text2.length;
	
	for (var i = 0; i < l2; i++) {	
		var maxLength = 11;
		var p = text2[i];
		
		if (p.className == "name") {
			var text = p.innerHTML.replace(/[\.\/]/g,"");
			var myStr = text.length;
			if (myStr > maxLength) {
				var myText = p.innerHTML.slice(0, maxLength) + "…";
				p.innerHTML = myText;
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

/**
 * 画像のhover設定
 * @param なし
 */
function setImageAction(){
	var images = new Array();
	var hoverImages = new Array();
	var tmp1 = document.getElementsByTagName("img");
	var tmp2 = document.getElementsByTagName("input"); 
	var l1 = tmp1.length;
	var l2 = tmp2.length;
	var e = new RegExp("hover", "i");
	var r = new RegExp("(.+)(\\.)(.+)", "i");
	for(var i=0; i<l1; i++){
		var t = tmp1[i];
		if(t.className && t.className.match(e)){
			images.push(t);
		}
	}
	for(var i=0; i<l2; i++){
		var t = tmp2[i];
		if(t.src && t.className && t.className.match(e)){
			images.push(t);
		}
	}
	var l = images.length;
	for(var i=0; i<l; i++){
		var t = images[i];
		t.url1 = t.src;
		t.url2 = t.src.replace(r, "$1_on.$3");
		t.onmouseover = function(){
			this.src = this.url2;	
		}
		t.onmouseout = function(){
			this.src = this.url1;	
		}
		hoverImages.push(t.url2);
	}
	preloadImages(hoverImages);
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

/**
 * 画像の先読み
 * @param images 先読みさせたい画像パスを格納する配列
 */
function preloadImages(images){
	var d = document;
	d.imageHolder = new Array();
	var l = images.length;
	for(var i=0; i<l; i++){
		d.imageHolder[i] = new Image();
		d.imageHolder[i].src = images[i];
	}
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

/**
 * リンクのスタイル設定
 * @param なし
 */
function setReportAction(){
	var tmp = document.getElementsByTagName("li");
	var l = tmp.length;
	var r = new RegExp("report", "i");
	for(var i=0; i<l; i++){
		var e = tmp[i];
		if(e.className && e.className.match(r)){
			e.url = e.getElementsByTagName("a")[0].href;
			e.onclick = function(){ 
				location.href= this.url;
			}
			e.onmouseover = function(){
				this.style.backgroundColor = "#EEEEEE";
				Element.addClassName(this, "on");
			}
			e.onmouseout = function(){
				this.style.backgroundColor = "#F6F2E6";
				Element.classNames(this).remove("on");
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

/**
 * 初期化
 * @param なし
 */
function init(){

	setImageAction();
	setReportAction();
	setMaxLength();
	smartScroll();

}

function addLoadEvent(func) {
	
	if (typeof window.addEventListener == 'function') {
		window.addEventListener('load', func, false);
		return true;
	} else if (typeof window.attachEvent == 'object') {
		window.attachEvent('onload', func);
		return true;
	}
	
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
	
}

addLoadEvent(init);



