/*=========================================================================*
	*
	*function.js
	*よく使われるJSまとめ
	*
*=========================================================================*/

/*--------------------------------------------------------------------------*
	*
	☆jQueryプラグイン
	*画像のロールオーバー
	*用jQuery　リンクがないと動作しません
	*
*--------------------------------------------------------------------------*/
jQuery(function($) {
	var postfix = '_f2';
	$('a img.rollover').not('[src*="'+ postfix +'."]').each(function() {
		var img = $(this);
		var src = img.attr('src');
		var src_f2 = src.substr(0, src.lastIndexOf('.'))
		           + postfix
		           + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_f2);
		img.hover(function() {
			img.attr('src', src_f2);
		}, function() {
			img.attr('src', src);
		});
	});
});

/*--------------------------------------------------------------------------*
	*
	*スムーズにスクロールアップするJS
	*Scroll.js
	*
*--------------------------------------------------------------------------*/
function backToTop() {
  var x1 = x2 = x3 = 0;
  var y1 = y2 = y3 = 0;
  if (document.documentElement) {
      x1 = document.documentElement.scrollLeft || 0;
      y1 = document.documentElement.scrollTop || 0;
  }
  if (document.body) {
      x2 = document.body.scrollLeft || 0;
      y2 = document.body.scrollTop || 0;
  }
  x3 = window.scrollX || 0;
  y3 = window.scrollY || 0;
  var x = Math.max(x1, Math.max(x2, x3));
  var y = Math.max(y1, Math.max(y2, y3));
  window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
  if (x > 0 || y > 0) {
      window.setTimeout("backToTop()", 25);
  }
}

/*--------------------------------------------------------------------------*
	*
	*IE6 background cache / no more IE6 background flicker
	*http://evil.che.lu/2006/09/25/no-more-ie6-background-flicker
	*
*--------------------------------------------------------------------------*/
try {
	document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	
	
/*--------------------------------------------------------------------------*
 * 
 *  Firefox3以上とOperaでword-break:break-allを実現するJavaScriptライブラリ
 *	div class=wordBreakで実行
 *  wordBreak JavaScript Library for Opera & Firefox  
 *  MIT-style license. 
 *  
 *  2008 Kazuma Nishihata 
 *  http://www.to-r.net
 *  
 *--------------------------------------------------------------------------*/

new function(){
	if(window.opera || navigator.userAgent.indexOf("Firefox") != -1){
		var wordBreak = function() {
			var wordBreakClass = "wordBreak";
			var div = document.getElementsByTagName("div");
			for(var i=0,len=div.length ; i<len ; i++){
				var tbClass = div[i].className.split(/\s+/);
				for (var j = 0; j < tbClass.length; j++) {
					if (tbClass[j] == wordBreakClass) {
						recursiveParse(div[i])
					}
				}
			}
		}
		var recursiveParse = function(pNode) {
			var childs = pNode.childNodes;
			for (var i = 0; i < childs.length; i++) {
				var cNode = childs[i];
				if (childs[i].nodeType == 1) {
					recursiveParse(childs[i]);
				}else if(cNode.nodeType == 3) {
					if(cNode.nodeValue.match("[^\n ]")){
						var plTxt = cNode.nodeValue.replace(/\t/g,"")
						var spTxt = plTxt.split("");
						spTxt = spTxt.join(String.fromCharCode(8203));
						var chNode = document.createTextNode(spTxt);
						cNode.parentNode.replaceChild(chNode,cNode)
					}
				}
			}
		}

	}else{
		var wordBreak = function() {
			if( document.styleSheets[0].addRule ){
				document.styleSheets[0].addRule(".wordBreak","word-break:break-all");
			}else if( document.styleSheets[0].insertRule ){
				document.styleSheets[0].insertRule(".wordBreak{word-break:break-all}", document.styleSheets[0].cssRules.length );
			}else{
				return false;
			}
		}
	}
	var addEvent = function(elm,listener,fn){
		try{
			elm.addEventListener(listener,fn,false);
		}catch(e){
			elm.attachEvent("on"+listener,fn);
		}
	}
	addEvent(window,"load",wordBreak);
}

/*--------------------------------------------------------------------------*
 *  
 *  ブロック要素の高さを揃えるJS
 *  heightLine JavaScript Library beta4
 *  MIT-style license. 
 *  
 *  2007 Kazuma Nishihata 
 *  http://www.webcreativepark.net
 *  
 *--------------------------------------------------------------------------*/
new function(){
	
	function heightLine(){
	
		this.className="heightLine";
		this.parentClassName="heightLineParent"
		reg = new RegExp(this.className+"-([a-zA-Z0-9-_]+)", "i");
		objCN =new Array();
		var objAll = document.getElementsByTagName ? document.getElementsByTagName("*") : document.all;
		for(var i = 0; i < objAll.length; i++) {
			var eltClass = objAll[i].className.split(/\s+/);
			for(var j = 0; j < eltClass.length; j++) {
				if(eltClass[j] == this.className) {
					if(!objCN["main CN"]) objCN["main CN"] = new Array();
					objCN["main CN"].push(objAll[i]);
					break;
				}else if(eltClass[j] == this.parentClassName){
					if(!objCN["parent CN"]) objCN["parent CN"] = new Array();
					objCN["parent CN"].push(objAll[i]);
					break;
				}else if(eltClass[j].match(reg)){
					var OCN = eltClass[j].match(reg)
					if(!objCN[OCN]) objCN[OCN]=new Array();
					objCN[OCN].push(objAll[i]);
					break;
				}
			}
		}
		
		//check font size
		var e = document.createElement("div");
		var s = document.createTextNode("S");
		e.appendChild(s);
		e.style.visibility="hidden"
		e.style.position="absolute"
		e.style.top="0"
		document.body.appendChild(e);
		var defHeight = e.offsetHeight;
		
		changeBoxSize = function(){
			for(var key in objCN){
				if (objCN.hasOwnProperty(key)) {
					//parent type
					if(key == "parent CN"){
						for(var i=0 ; i<objCN[key].length ; i++){
							var max_height=0;
							var CCN = objCN[key][i].childNodes;
							for(var j=0 ; j<CCN.length ; j++){
								if(CCN[j] && CCN[j].nodeType == 1){
									CCN[j].style.height="auto";
									max_height = max_height>CCN[j].offsetHeight?max_height:CCN[j].offsetHeight;
								}
							}
							for(var j=0 ; j<CCN.length ; j++){
								if(CCN[j].style){
									var stylea = CCN[j].currentStyle || document.defaultView.getComputedStyle(CCN[j], '');
									var newheight = max_height;
									if(stylea.paddingTop)newheight -= stylea.paddingTop.replace("px","");
									if(stylea.paddingBottom)newheight -= stylea.paddingBottom.replace("px","");
									if(stylea.borderTopWidth && stylea.borderTopWidth != "medium")newheight-= stylea.borderTopWidth.replace("px","");
									if(stylea.borderBottomWidth && stylea.borderBottomWidth != "medium")newheight-= stylea.borderBottomWidth.replace("px","");
									CCN[j].style.height =newheight+"px";
								}
							}
						}
					}else{
						var max_height=0;
						for(var i=0 ; i<objCN[key].length ; i++){
							objCN[key][i].style.height="auto";
							max_height = max_height>objCN[key][i].offsetHeight?max_height:objCN[key][i].offsetHeight;
						}
						for(var i=0 ; i<objCN[key].length ; i++){
							if(objCN[key][i].style){
								var stylea = objCN[key][i].currentStyle || document.defaultView.getComputedStyle(objCN[key][i], '');
									var newheight = max_height;
									if(stylea.paddingTop)newheight-= stylea.paddingTop.replace("px","");
									if(stylea.paddingBottom)newheight-= stylea.paddingBottom.replace("px","");
									if(stylea.borderTopWidth && stylea.borderTopWidth != "medium")newheight-= stylea.borderTopWidth.replace("px","")
									if(stylea.borderBottomWidth && stylea.borderBottomWidth != "medium")newheight-= stylea.borderBottomWidth.replace("px","");
									objCN[key][i].style.height =newheight+"px";
							}
						}
					}
				}
			}
		}
		
		checkBoxSize = function(){
			if(defHeight != e.offsetHeight){
				changeBoxSize();
				defHeight= e.offsetHeight;
			}
		}
		changeBoxSize();
		setInterval(checkBoxSize,1000)
		window.onresize=changeBoxSize;
	}
	
	function addEvent(elm,listener,fn){
		try{
			elm.addEventListener(listener,fn,false);
		}catch(e){
			elm.attachEvent("on"+listener,fn);
		}
	}
	addEvent(window,"load",heightLine);
}


