// common.js
// REQUIRES: prototype.js, scriptaculous.js

var sitePath = '/';

Event.observe( window, 'load', function(){
	setRollOver();
});



// *************************************************
// 汎用関数
// *************************************************


// eventSetter
var isIE = isIE = (document.documentElement.getAttribute("style") == document.documentElement.style);

function eventSetter(obj,eventType,func){
	if(isIE) {
		obj.setAttribute(eventType,new Function(func));
	} else {
		obj.setAttribute(eventType,func);
	}
}


// open new window
function newWindow(uri,width,height){
	var myWindow = window.open(uri, 'newWindow', 'resizable=yes,scrollbars=yes,status=0,width='+width+',height='+height);
	if (myWindow.focus!=null) {
		myWindow.focus();
	}
}


// print window
function printWindow(){
	if(document.getElementById || document.layers){
		window.print();
	}
}


// Ajax recover UTF-8 (for Safari)
function recover_utf8(text){
	if(navigator.appVersion.indexOf('KHTML') > -1){
		var esc = escape(text);
		if(esc.indexOf('%u') < 0 && esc.indexOf('%') > -1){
			text = decodeURIComponent(esc);
		}
	}
	return text;
}


// input clear
function inputClear(id,txt){
	if ($(id).value == txt) $(id).value = '';
}

function isArray(obj){
	return (typeof obj == "object") && (obj instanceof Array);
}

// *************************************************
// 画像ロールオーバー
// *************************************************

var ImgRollOver = Class.create();
ImgRollOver.prototype = {
	initialize: function(img,type){
		this.img = img;
		this.outImgSrc = img.src;
		var imgSrcArray = img.src.split('.');
		var fileType = imgSrcArray[imgSrcArray.length - 1];
		this.overImgSrc = img.src.split('.' + fileType)[0] + '_o.' + fileType;
		if(type == "img"){
			Event.observe(
				img.parentNode,
				'mouseover',
				this.mouseOver.bindAsEventListener(this)
			);
			Event.observe(
				img.parentNode,
				'mouseout',
				this.mouseOut.bindAsEventListener(this)
			);
		}else if(type == "input"){
			Event.observe(
				img,
				'mouseover',
				this.mouseOver.bindAsEventListener(this)
			);
			Event.observe(
				img,
				'mouseout',
				this.mouseOut.bindAsEventListener(this)
			);
		}
		var preImage = new Image();
		preImage.src = this.overImgSrc;
	},
	mouseOver: function(){
		this.img.src = this.overImgSrc;
	},
	mouseOut: function(){
		this.img.src = this.outImgSrc;
	}
}

function setRollOver(){
	var imgs = document.getElementsByTagName('img');
	var inputs = document.getElementsByTagName('input');
	var preImages = new Array();
	var preInputs = new Array();
	if(imgs){
		for(var i = 0,num = imgs.length;i < num;i++){
			img = imgs[i];
			if(Element.hasClassName(img, "roll")){
				new ImgRollOver(img,"img");
			}
		}
	}
	if(inputs){
		for(var i = 0,num = inputs.length;i < num;i++){
			input = inputs[i];
			if(input.type == 'image' && Element.hasClassName(input, "roll")){
				new ImgRollOver(input,"input");
			}
		}
	}
}