﻿/*
 * create by JMocha
 */

// 이미지 롤오버 클래스 (over는 기본으로 보여질 id값으로 없어도 됌)
function ImageOver(over) {
	this._over = over;
	this._id = new Array();
	this._img = {};
}

// 이미지 추가 (option에는 over : 오버시 이미지 경로, out : 마우스 아웃시 이미지 경로, sub : 오버시 보여질 div id(없을시 롤오버아웃만 처리)가 올수 있다.)
ImageOver.prototype.add = function (id, option) {
	var isValidId = false;
	
	for (var i = 0; i < this._id.length; i++) {
		if (this._id[i] == id) {
			isValidId = true;
			break;
		}
	}

	if (!isValidId) this._id[this._id.length] = id;
	
	this._img[id] = option;
};

// 이미지 삭제
ImageOver.prototype.remove = function (id) {
	this._img[id] = {};

	for (var i = 0; i < this._id.length; i++) {
		if (this._id[i] == id) {
			delete(this._id[i]);
			break;
		}
	}
};

// 이미지 오버 처리
ImageOver.prototype.over = function (id) {
	if (this._over == id) return;
	
	if (this._over != null) {
		document.getElementById(this._over).src = this._img[this._over]["out"];

		if (this._img[this._over]["sub"])
			document.getElementById(this._img[this._over]["sub"]).style.display = "none";
	}

	document.getElementById(id).src = this._img[id]["over"];
	if (this._img[id]["sub"])
		document.getElementById(this._img[id]["sub"]).style.display = "";
	this._over = id;
};

function SimpleImageOver(outPatten, overPatten, over, overSub) {
	this._outPatten = outPatten;
	this._overPatten = overPatten;
	this._over = over;
	this._overSub = overSub;
	this._isOver = false;
	this._firstOver = over;
	this._firstOverSub = overSub;
	this._outTime = 2000;
};

SimpleImageOver.prototype.over = function (id, sub) {
	this._isOver = true;
	
	if (id) {
		if (this._over == id) return;
		
		if (this._over != null) {
			document.getElementById(this._over).src = this._outPatten.replace("%{id}", this._over);
			document.getElementById(this._over).src = document.getElementById(this._over).src.replace("pages/", "");
			
			if (this._overSub) {
				document.getElementById(this._overSub).style.display = "none";
			}
		}

		document.getElementById(id).src = this._overPatten.replace("%{id}", id);
		document.getElementById(id).src = document.getElementById(id).src.replace("pages/", "");
		if(sub)
			document.getElementById(sub).style.display = "block";
		
		this._over = id;
		this._overSub = sub;
	}
};

SimpleImageOver.prototype.out = function () {
	this._isOver = false;
	var self = this;
	
	if (this._to) clearTimeout(this._to);
	
	this._to = setTimeout(function () {
		if (!self._isOver) {
			if (self._over != self._firstOver) {
				self.over(self._firstOver, self._firstOverSub);
			}
		}
	}, 2000);
};

SimpleImageOver.prototype.setOutTime = function (time) {
	this._outTime = time;
};
