/*
common.js
REQUIRES: prototype.js, scriptaculous.js
Site URI: http://www.jcis.co.jp/
Description: 株式会社ジェイシス
Since: 2009-02-23
Author: Takuro Hishikawa
Updated: 2009-02-23
*/


// *************************************************
// ページの読み込み時に起動
// *************************************************

Event.observe( window, 'load', function(){
	setRollOver('roll');
	hensai();
});



// *************************************************
// 汎用関数
// *************************************************


// 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("wrapper",{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(className){
	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, className)){
				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, className)){
				new ImgRollOver(input,"input");
			}
		}
	}
}


// *************************************************
// ギャラリー
// *************************************************

jQuery.noConflict();

jQuery(document).ready(function() {
	jQuery("a.gallery").fancybox();
});




// *************************************************
// 返済シミュレーション
// *************************************************

function hensai(){
	Event.observe(
		'hensai_submit',
		'click',
		getHensai
	)
}

function getHensai(){
	var objAjax = new Ajax.Request(
		'/includes/hensai.php',
		{
			method: 'post',
			parameters: Form.serialize('hensai_form'),
			onComplete: function(httpObj){
				$('result').innerHTML = httpObj.responseText;
				new Effect.Highlight('result');
			},
			onFailure: function(){
				alert('通信に失敗しました');
			}
		}
	);
}