/**
 * =============================================================
 *  rollover.js
 * =============================================================
 *  @copyright	COPYRIGHT (C) 2009 MdN Corporation., ALL RIGHTS RESERVED.
 *  @version	$Id
 */

var imageSuffix = {
	over: "_over",
	out: "_base",
	down: null,
	up: null
}

MyLib.event.observe(window, "load", rollover, false);	// HTMLが読み込まれた後にrollover関数がコールされる。

function rollover(){
	var image;
	var images = document.getElementsByTagName("img");

	for(var i=0; image=images[i]; i++){
		if(image.src.match(new RegExp(imageSuffix.out + "\\.(jpe?g|gif|png)$", "i")) != null){
			preLoadImage(image, imageSuffix.over);

			MyLib.event.observe(image, "mouseover", function(){
				swapImage(this, imageSuffix.out, imageSuffix.over);
			}, false);

			MyLib.event.observe(image, "mouseout", function(){
				swapImage(this, imageSuffix.over, imageSuffix.out);
			}, false);

			if(imageSuffix.down){
				preLoadImage(image, imageSuffix.down);

				MyLib.event.observe(image, "mousedown", function(){
					swapImage(this, imageSuffix.over, imageSuffix.down);
				}, false);
			}

			if(imageSuffix.up){
				MyLib.event.observe(image, "mouseup", function(){
					swapImage(this, imageSuffix.down, imageSuffix.over);
				}, false);
			}
		}
	}

	function swapImage(image, from , to){
		if(image){
			image.src = image.src.replace(from, to);
		}
	}

	function preLoadImage(image, suffix){
		if(image){
			var src = image.src.replace(imageSuffix.out, suffix);
			var loadImage = new Image().src = src;
		}
	}
}

