/* ------------------------------------------------------- */

/* common.js

/* ------------------------------------------------------- */


/* スワップイメージ
-----------------------------------------------------------*/
(function(){
	function swapImage() {
		var className = "swapImage";
		var defaultName = "_df";
		var hoverName = "_ov";
		
		var overReg = new RegExp("^(.+)" + defaultName + "(\\.[a-z]+)$");
		var outReg = new RegExp("^(.+)" + hoverName + "(\\.[a-z]+)$");
		
		var preload = new Array();
		var images = document.getElementsByTagName("img");
		var inputs = document.getElementsByTagName("input");
		
		for(var i = 0, il = images.length; i < il; i++) {
			var classStr = images[i].getAttribute("class") || images[i].className;
			var classNames = classStr.split(/\s+/);
			for(var j = 0, cl = classNames.length; j < cl; j++){
				if(classNames[j] == className){

					//preload
					preload[i] = new Image();
					preload[i].src = images[i].getAttribute("src").replace(overReg, "$1" + hoverName + "$2");

					//mouseover
					images[i].onmouseover = function() {
						this.src = this.getAttribute("src").replace(overReg, "$1" + hoverName + "$2");
					}

					//mouseout
					images[i].onmouseout = function() {
						this.src = this.getAttribute("src").replace(outReg, "$1" + defaultName + "$2");
					}
				}
			}
		}
		
		for(var i = 0, il = inputs.length; i < il; i++) {
			var classStr = inputs[i].getAttribute("class") || inputs[i].className;
			var classNames = classStr.split(/\s+/);
			for(var j = 0, cl = classNames.length; j < cl; j++){
				if(classNames[j] == className){

					//preload
					preload[i] = new Image();
					preload[i].src = inputs[i].getAttribute("src").replace(overReg, "$1" + hoverName + "$2");

					//mouseover
					inputs[i].onmouseover = function() {
						this.src = this.getAttribute("src").replace(overReg, "$1" + hoverName + "$2");
					}

					//mouseout
					inputs[i].onmouseout = function() {
						this.src = this.getAttribute("src").replace(outReg, "$1" + defaultName + "$2");
					}
				}
			}
		}
	}
	
	function addEvent(elem, event, func){
		if(elem.addEventListener) {
			elem.addEventListener(event, func, false);
		}else if(elem.attachEvent) {
			elem.attachEvent("on" + event, func);
		}
	}
	addEvent(window, "load", swapImage);
})();
