// Common Tips
// auto load
$(function(){
	$('a img, input[type=image]').changeHoveringImages();
	$('#header ul.navi li').fadeInMenu('ul', 'fast');
	$('input#reset_form').confirm_reset();
	$('form').disableOnSubmit();
});

/*--------------------------------------*
 * jQuery Change navigation images on/off by hovering
 *--------------------------------------*/
$.fn.changeHoveringImages = function () {
	var this_images = this.filter('[src*="_off."]');
	if (this_images.length < 1) return;

	// method
	hover_images = {
		mouseover : function (e) {
			if (this.src) this.src = this.src.replace('_off.', '_on.');
		},
		mouseout  : function (e) {
			if (this.src) this.src = this.src.replace('_on.', '_off.');
		},
		// cache images
		cache   : [],
		preload : function (e) {
			if (!this.src) return;
			var img_off = new Image();  img_off.src = this.src;
			var img_on  = new Image();  img_on .src = this.src.replace('_off.', '_on.');
			hover_images.cache.push(img_off);
			hover_images.cache.push(img_on);
		}
	};
	// set mouse event
	this_images.mouseover(hover_images.mouseover)
	           .mouseout (hover_images.mouseout)
	           .each     (hover_images.preload);
};


/*--------------------------------------*
 * jQuery fadeIn for navigation menu
 *--------------------------------------*/
$.fn.fadeInMenu = function (ul, speed) {
	if (this.length < 1) return;
	if (!ul) ul = 'ul';

	// set hover event
	this.hover(
		function () { $(ul, this).hide().fadeTo(speed, 0.9); }
	);
	// for IE6
	if (document.body && typeof document.body.style.maxHeight == 'undefined') {
		this.hoverClass('hover');
	}
};
$.fn.hoverClass = function (c) {
	return this.each(function(){
		$(this).hover( 
			function () { $(this).addClass(c);    },
			function () { $(this).removeClass(c); }
		);
	});
};


/*--------------------------------------*
 * jQuery Disable On Submit Plugin
 *--------------------------------------*/
$.fn.disableOnSubmit = function(disableList, timeout){
	if (this.length < 1) return;

	var $list = (disableList) ? disableList : 'input[type=submit],button[type=submit]';
	if (!timeout) timeout = 3000;

	// Makes sure button is enabled at start
	this.find($list).attr('disabled', false);

	this.submit(function(){
		var buttons = $(this).find($list);
		setTimeout(function(){
		    buttons.attr('disabled', true);
		}, 1);
		setTimeout(function(){
			buttons.attr('disabled', false);
		}, timeout);
	});
	return this;
};


/*--------------------------------------*
 * jQuery Confirm to reset form Plugin
 *--------------------------------------*/
$.fn.confirm_reset = function (config) {
	if (this.length < 1) return;

	// set click event
	this.click(function (e) {
		return confirm('フォームの内容をリセットしてよろしいですか？');
	});
};


