// common.js
// REQUIRES: prototype.js, scriptaculous.js

var sitePath = '/';

Event.observe( window, 'load', function(){
	setRollOver();
	setPullDownMenu();
	setFukidashi();
});



// *************************************************
// 汎用関数
// *************************************************


// 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 = '';
}

// scroll to top
function scrollToTop() {
	new Effect.ScrollTo("top",{duration: 0.5});
}



// *************************************************
// 画像ロールオーバー
// *************************************************

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");
			}
		}
	}
}



// *************************************************
// プルダウンメニュー
// *************************************************

var PullDownMenu = Class.create();
PullDownMenu.prototype = {
	initialize: function(menu){
		this.menu = menu;
		this.img = this.menu.firstChild.firstChild;
		this.outImgSrc = this.img.src;
		var imgSrcArray = this.img.src.split('.');
		var fileType = imgSrcArray[imgSrcArray.length - 1];
		this.overImgSrc = this.img.src.split('.' + fileType)[0] + '_o.' + fileType;
		Event.observe(
			this.menu,
			'mouseover',
			this.mouseOver.bindAsEventListener(this)
		);
		Event.observe(
			this.menu,
			'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 setPullDownMenu(){
	if($("gloval-navi")){
		var gnavi = $("gloval-navi");
		var menus = gnavi.getElementsByClassName('menu');
		for(var i = 0,num = menus.length;i < num;i++){
			menu = menus[i];
			new PullDownMenu(menu);
		}
	}
}



// *************************************************
// トップページフキダシ
// *************************************************

var FukidashiBox = Class.create();
FukidashiBox.prototype = {
	initialize: function(elem){
		this.box = elem;
		this.fukidashi = this.box.firstChild.id;
		$(this.fukidashi).style.display = 'none';
		$(this.fukidashi).removeClassName('hide');
		Event.observe(
			this.box,
			'mouseover',
			this.mouseOver.bindAsEventListener(this)
		);
		Event.observe(
			this.box,
			'mouseout',
			this.mouseOut.bindAsEventListener(this)
		);
	},
	mouseOver: function(){
		if(isIE) {
			$(this.fukidashi).style.display = 'block';
		}else{
			$(this.fukidashi).visualEffect("Appear", { from:0.1, to:1.0, duration: 1 });
		}
	},
	mouseOut: function(){
		if(isIE) {
			$(this.fukidashi).style.display = 'none';
		}else{
			$(this.fukidashi).visualEffect("Fade", { from:1.0, to:0.0, duration: 0.1 });
		}
	}
}

function setFukidashi(){
	if($("main-feature")){
		var area = $("main-feature");
		var boxes = area.getElementsByClassName('service_box');
		for(var i = 0,num = boxes.length;i < num;i++){
			box = boxes[i];
			new FukidashiBox(box);
		}
	}
}
