
/*
	File:       reiw.js
	Version:    0.0.1
	Author:     nakayoshi
	Capability: IE 6.0-7.0, Firefox 3
*/

if (! reiw) { // <IF_REIW_EXISTS>

var reiw = {

_DEFAULT_INTERVAL : 10,

version : "0.0.3",

init : function () {

	reiw.browser = {};
	reiw.browser.ie = (window.attachEvent && ! window.opera) ? true : false;
	reiw.browser.ie5 = (navigator.appVersion.indexOf("MSIE 5.0") > -1) ? true : false;
	reiw.browser.ie7 = (reiw.browser.ie && typeof document.body.style.maxHeight != "undefined") ? true : false;
	reiw.browser.opera = (window.opera ? true : false);
	reiw.browser.webkit = (navigator.userAgent.indexOf('AppleWebKit/') > -1);
	reiw.browser.konqueror =(navigator.userAgent.indexOf('Konqueror') > -1);
	reiw.browser.mozilla = (navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1);

	if (window.main) window.main();
},

benchmark : function () {
	var s = (new Date()).getTime();
	return function () {
		return ((new Date()).getTime() - s);
	};
},

event : {
	add : function (obj, evType, fn, useCap) {
		if (obj.addEventListener) {
			useCap = useCap || false;
			obj.addEventListener(evType, fn, useCap);
		} else if (obj.attachEvent) {
			obj.attachEvent("on" + evType, fn);
		}
	},
	remove : function (obj, evType, fn, useCap) {
		if (obj.removeEventListener) {
			useCap = useCap || false;
			obj.removeEventListener(evType, fn, useCap);
		} else if (obj.detachEvent) {
			obj.detachEvent("on" + evType, fn);
		}
	}
},

style : {

	add : function (selector, declarations) {
		var sheets = document.styleSheets;
		if (sheets) {
			var last_sheet = sheets[sheets.length - 1];
			if (document.all) {
				last_sheet.addRule(selector, declarations);
			} else {
				last_sheet.insertRule(selector + "{" + declarations + "}", last_sheet.cssRules.length);
			}
		}
	},

	getOpacity : function (ele) {
		if (reiw.browser.ie) {
			var m = ele.style['filter'].match(/alpha\(opacity=(.*)\)/)
			var op = m && m[1] ? m[1] : "auto";
			if (op == "auto" || !op) return 100;
			return (Number(op));
		} else {
			var op = ele.style.opacity;
			if (op == "auto" || !op) return 100;
			op = parseFloat(op) * 100;
			return (op);
		}
	},
	
	setOpacity : function (ele, value) {
		if (value > 100) value = 100;
		if (value < 0) value = 0;
		if (reiw.browser.ie) {
			ele.style.filter = "Alpha(Opacity=" + value + ")";
		} else {
			ele.style.opacity = "" + (value / 100);
		}
		return;
	}
},

screen : {

	getAbsPosition : function (ele) {
		var p = {x : 0, y : 0};
		while (ele) {
			p.x += ele.offsetLeft;
			p.y += ele.offsetTop;
			ele = ele.offsetParent;
		}
		return p;
	},

	getBrowseSize : function () {
		var p = {x : 0, y : 0};
		if (document.documentElement) {
			p.y = document.documentElement.clientHeight
			p.x = document.documentElement.clientWidth
		} else if (! window.opeara && window.attachEvent) {
			p.x = document.body.clientWidth;
			p.y = document.body.clientHeight;
		} else {
			p.y = window.innerHeight;
			p.x = window.innerWidth;
		}
		return p;
	},

	// マウスの座標を取得 onclickなどのEventオブジェクトを引数に渡す
	// ex) hoge.onclick = function (ev) { var p = reiw.screen.getMouse(ev); };
	getMouse : function (ev) {
		var p = {x : 0, y : 0};
		if (reiw.browser.opera) {
			p.x = ev.clientX;
			p.y = ev.clientY;
		} else if (reiw.browser.ie) {
			p.x = document.body.scrollLeft + window.event.clientX;
			p.y = document.body.scrollTop + window.event.clientY;
		} else {
			p.x = ev.pageX;
			p.y = ev.pageY;
		}
		return p;
	}
},

cookie : {},

ui : {}

};

reiw.event.add(window, 'load', reiw.init);

} // </IF_REIW_EXISTS>


/* ADDITIONAL LIBRARY (Option) ==================================== */


if (reiw && ! reiw.ajax) { // <IF_REIW_AJAX_EXISTS>

reiw.ajax = {
	/* USAGE:
			Ajax.get(obj);
			Ajax.post(obj);

		OBJECT (*required)
			* url: ""
			* onSuccess: function(req)
			* onFailure: function(req)
			  data: Object
			  delay: Number

	*/
	get : function (obj) {
		this._send("GET", obj, "");
	},
	post : function (obj) {
		var vars = obj.data;
		var data = "";
		for (var o in vars) {
			data += "&" + o + "=" + encodeURI(vars[o]);
		}
		this._send("POST", obj, data);
	},
	_send : function (action, obj, data) {

		var req;
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200 || req.status == 304) {
					obj.onSuccess(req);
				} else {
					obj.onFailure(req);
				}
			}
		}

		req.open(action, obj.url, true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
		if (obj.delay && obj.delay > 1) {
			window.setTimeout(function() {
				req.send(data);
			}, obj.delay);
		} else {
			req.send(data);
		}
		return;
	}
};

} // </IF_REIW_AJAX_EXISTS>


if (reiw && ! reiw.ui.dialog) { // <IF_REIW_UI_DIALOG_EXISTS>

reiw.ui.dialog = {
	
	CLASS_NAME : "reiw-dialog",
	ZINDEX : 10,
	INTERVAL : 20,
	INPUT_SIZE : 50,
	BTN_OK : "OK",
	BTN_CANCEL : "Cancel",
	BTN_CLOSE : "Close",
	TITLE_ALERT : "Alert",
	TITLE_CONFIRM : "Confirm",
	TITLE_PROMPT : "Input",

	_instance : undefined,
	
	_init : function () {

		var dialog = {};
		reiw.ui.dialog._instance = dialog;

		dialog.container = document.createElement('div');
		dialog.container.className = reiw.ui.dialog.CLASS_NAME;
		dialog.container.style.position = "absolute";
		dialog.container.style.left = "0px";
		dialog.container.style.top = "0px";
		dialog.container.style.zIndex = reiw.ui.dialog.ZINDEX;
		dialog.container.style.display = "none";
		reiw.style.setOpacity(dialog.container, 0);

		dialog.title = document.createElement('div');
		dialog.title.className = "title";
		dialog.title.innerHTML = "&nbsp;";
		dialog.close_btn = document.createElement('div');
		dialog.close_btn.className = "close";
		var btn = document.createElement('a');
		btn.innerHTML = reiw.ui.dialog.BTN_CLOSE;
		btn.href = "#dummy";
		btn.onclick = function () {
			reiw.ui.dialog.hide();
		};
		dialog.close_btn.appendChild(btn);
		dialog.body = document.createElement('div');
		dialog.body.className = "body";
		dialog.container.appendChild(dialog.title);
		dialog.container.appendChild(dialog.close_btn);
		dialog.container.appendChild(dialog.body);

		dialog.bg = document.createElement('div');
		dialog.bg.style.backgroundColor = "#ffffff";
		dialog.bg.style.position = "absolute";
		dialog.bg.style.top = "0px";
		dialog.bg.style.left = "0px";
		dialog.bg.style.display = "none";
		dialog.bg.style.zIndex = reiw.ui.dialog.ZINDEX - 1;
		reiw.style.setOpacity(dialog.bg, 0);

		dialog.intervalId = null;

		document.getElementsByTagName('body').item(0).appendChild(dialog.container);
		document.getElementsByTagName('body').item(0).appendChild(dialog.bg);

		dialog.update = function () {
			reiw.ui.dialog.centerizeElement(dialog.container);
			reiw.ui.dialog.fillWindow(dialog.bg);
		};

		return dialog;
	},

	getInstance : function () {
		return (reiw.ui.dialog._instance || reiw.ui.dialog._init());
	},

	alert : function (mesg, title) {
		var ins = reiw.ui.dialog.getInstance();
		reiw.ui.dialog.reset();

		var p_mesg = document.createElement('div');
		p_mesg.innerHTML = mesg;

		var p_btn = document.createElement('p');
		p_btn.className = "btn";
		var btn = document.createElement('input');
		btn.type = "button";
		btn.value = reiw.ui.dialog.BTN_OK;
		btn.onclick = function () {
			reiw.ui.dialog.hide();
		};
		p_btn.appendChild(btn);

		ins.title.innerHTML = (title || reiw.ui.dialog.TITLE_ALERT);
		ins.body.appendChild(p_mesg);
		ins.body.appendChild(p_btn);

		reiw.ui.dialog.show().onVisible = function () { btn.focus(); };
	},

	confirm : function (mesg, title, func) {
		var ins = reiw.ui.dialog.getInstance();
		reiw.ui.dialog.reset();

		var p_mesg = document.createElement('div');
		p_mesg.innerHTML = mesg;

		var p_btn = document.createElement('p');
		p_btn.className = "btn";
		var y_btn = document.createElement('input');
		y_btn.type = "button";
		y_btn.value = reiw.ui.dialog.BTN_OK;
		y_btn.onclick = function () {
			if (func) setTimeout(function () { func(true); }, 100);
			reiw.ui.dialog.hide();
		};
		var n_btn = document.createElement('input');
		n_btn.type = "button";
		n_btn.value = reiw.ui.dialog.BTN_CANCEL
		n_btn.onclick = function () {
			if (func) setTimeout(function () { func(false); }, 100);
			reiw.ui.dialog.hide();
		};
		p_btn.appendChild(y_btn);
		p_btn.appendChild(n_btn);

		ins.title.innerHTML = (title || reiw.ui.dialog.TITLE_CONFIRM);
		ins.body.appendChild(p_mesg);
		ins.body.appendChild(p_btn);

		reiw.ui.dialog.show().onVisible = function () { y_btn.focus(); };
	},

	prompt : function (mesg, init_value, title, func) {
		var ins = reiw.ui.dialog.getInstance();
		reiw.ui.dialog.reset();

		var p_mesg = document.createElement('div');
		p_mesg.innerHTML = mesg;
		var p_input = document.createElement('p');
		p_input.className = "input";
		var input = document.createElement('input');
		input.type = "text";
		input.size = reiw.ui.dialog.INPUT_SIZE;
		input.value = init_value || "";
		p_input.appendChild(input);

		var p_btn = document.createElement('p');
		p_btn.className = "btn";
		var btn = document.createElement('input');
		btn.type = "button";
		btn.value = reiw.ui.dialog.BTN_OK;
		btn.onclick = function () {
			if (func) setTimeout(function () { func(input.value); }, 100);
			reiw.ui.dialog.hide();
		};
		p_btn.appendChild(btn);

		ins.title.innerHTML = (title || reiw.ui.dialog.TITLE_PROMPT);
		ins.body.appendChild(p_mesg);
		ins.body.appendChild(p_input);
		ins.body.appendChild(p_btn);

		reiw.ui.dialog.show().onVisible = function () { input.focus(); };
	},

	start : function (options) {
		/*
		options => {
			title : <String>,
			content : <Element> or <String>,
			onVisible : <Function>
		}
		*/
		var ins = reiw.ui.dialog.getInstance();
		reiw.ui.dialog.reset();
		ins.title.innerHTML = (options.title || '');
		if (options.content.nodeType && options.content.nodeType == 1) {
			ins.body.appendChild(options.content);
		} else {
			ins.body.innerHTML = String(options.content);
		}
		reiw.ui.dialog.show().onVisible = (options.onVisible || undefined);
	},

	end : function () {
		reiw.ui.dialog.hide();
	},

	reset : function () {
		var ins = reiw.ui.dialog.getInstance();
		ins.body.innerHTML = "";
	},

	show : function () {
		var ins = reiw.ui.dialog.getInstance();
		if (ins.intervalId) clearInterval(ins.intervalId);
		var owner = ins;
		ins.container.style.display = "block";
		ins.bg.style.display = "block";
		ins.update();
		var count = 0;
		var evObj = {};
		ins.intervalId = setInterval(function() {
			count++;
			reiw.style.setOpacity(owner.bg, count * 11);
			reiw.style.setOpacity(owner.container, count * 18);
			if (count >= 5) {
				reiw.event.add(window, 'resize', owner.update);
				reiw.event.add(window, 'scroll', owner.update);
				clearInterval(owner.intervalId);
				owner.intervalId = null;
				if (evObj.onVisible) evObj.onVisible();
			}
		}, reiw.ui.dialog.INTERVAL);
		return evObj;
	},

	hide : function () {
		var ins = reiw.ui.dialog.getInstance();
		if (ins.intervalId) clearInterval(ins.intervalId);
		var count = 5;
		var evObj = {};
		ins.intervalId = setInterval(function() {
			count--;
			reiw.style.setOpacity(ins.bg, count * 11);
			reiw.style.setOpacity(ins.container, count * 18);
			if (count < 1) {
				clearInterval(ins.intervalId);
				ins.intervalId = null;
				ins.container.style.display = "none";
				ins.bg.style.display = "none";
				reiw.event.remove(window, 'resize', ins.update);
				reiw.event.remove(window, 'scroll', ins.update);
				if (evObj.onVisible) evObj.onVisible();
			}
		}, reiw.ui.dialog.INTERVAL);
		return evObj;
	},

	centerizeElement : function (target) {
		var p = {x:0, y:0};
		var doc = document;
		if (reiw.browser.ie7) {
			p.x = (doc.body.clientWidth / 2) - (target.clientWidth / 2);
			p.y = doc.documentElement.scrollTop + (doc.documentElement.clientHeight / 2) - (target.clientHeight / 2 );
			yoffset = doc.documentElement.scrollTop;
		} else if (reiw.browser.ie) {
			p.x = (doc.body.clientWidth / 2) - (target.clientWidth / 2);
			p.y = doc.body.scrollTop + (doc.body.clientHeight / 2) - (target.clientHeight / 2 );
			yoffset = doc.body.scrollTop;
		} else {
			p.y = window.pageYOffset + (window.innerHeight / 2) - (target.offsetHeight / 2 );
			p.x = window.pageXOffset + (window.innerWidth / 2) - (target.offsetWidth / 2 );
			yoffset = window.pageYOffset;
		}
		if (p.y < 20 + yoffset) p.y = 20 + yoffset;
		target.style.left = "" + p.x + "px";
		target.style.top = "" + p.y + "px";
	},

	fillWindow : function (target) {
		var body = document.getElementsByTagName('body').item(0);
		var x = body.scrollWidth;
		var y = body.scrollHeight;
		var p = reiw.screen.getBrowseSize();
		y = (y < p.y) ? p.y : y;
		target.style.width = x + "px"; 
		target.style.height = y + "px";
	}
};

} // </IF_REIW_UI_DIALOG_EXISTS>

