// 제작일 : 2007년 1월 27일
// 제작자 : 최영규 (http://hooriza.com/)

var HBASE = { // Hooriza Tatter Plugin-Base Script

	version : 1.1,
	
	blogURL : "",
	baseURL : "",
	
	getDocument : function() {
		return (
			(document.compatMode && document.compatMode != "BackCompat") ?
			document.documentElement : document.body
		);
	},
	
	setOffsetWidth : function(obj, width) {
		obj.style.width = width + "px";
		
		var real = obj.offsetWidth;
		if (real != width) obj.style.width = (width * 2 - real) + "px";
	},
	
	setOffsetHeight : function(obj, height) {
		obj.style.height = height + "px";
		
		var real = obj.offsetHeight;
		if (real != height) obj.style.height = (height * 2 - real) + "px";
	},
	
	fitWindow : function() {

		var doc = HBASE.getDocument();
		var now = [ doc.clientWidth, doc.clientHeight ];
		
		var gap = [ doc.scrollWidth - now[0], doc.scrollHeight - now[1] ];
	
		if (gap[0] < 0) gap[0] = 0;
		if (gap[1] < 0) gap[1] = 0;
		
		window.resizeBy(gap[0], gap[1]);
	},
	
	getInnerString : function(str, b, c) {
	
		if (!str) return str;
		
		var begin = str.indexOf(b);
		var close = str.indexOf(c, begin + b.length);
		
		if (begin == -1 || close == -1) return null;
		return str.substring(begin + b.length, close);
	},
	
	getLastInnerString : function(str, b, c) {
	
		if (!str) return str;
		
		var begin = str.indexOf(b);
		var close = str.lastIndexOf(c);
		
		if (begin == -1 || close == -1) return null;
		return str.substring(begin + b.length, close);
	},	
	
	tabs : null,
	panels : null,
	
	addPlugin : function(title, uniq) {
		Event.observe(window, "load", function() {
			var pair = HBASE.createTab(title, uniq);
			pair.tab.onclick = function() { HBASE.selectTab(pair);	}
		});
	},

	selectTab : function(pair) {
		
		$A(HBASE.tabs.childNodes).each(function(child) {
			if (child.tagName.toUpperCase() != "LI") return;
			child.className = (child == pair.tab ? "selected" : "");
		});

		$A(HBASE.panels.childNodes).each(function(child) {
			if (child.tagName.toUpperCase() != "LI") return;
			child.className = (child == pair.panel ? "selected" : "");
		});
	},
	
	createTab : function(title, uniq) {
		
		var pair = { tab : null, panel : null };

		HBASE.tabs = $("hooriza_plugins_tabs");
		HBASE.panels = $("hooriza_plugins_panels");
		
		HBASE.tabs.style.display = "block";
		HBASE.panels.style.display = "block";
		
		pair.tab = document.createElement("li");
		pair.panel = document.createElement("li");
		
		pair.tab.innerHTML = title;
		
		var panel = $(uniq);
		
		if (panel) {
			pair.panel.appendChild(panel);
			panel.style.display = "block";
		}
		
		HBASE.tabs.appendChild(pair.tab);
		HBASE.panels.appendChild(pair.panel);
		
		if (HBASE.tabs.childNodes.length == 1) {
			HBASE.selectTab(pair);
			HBASE.tabs.style.backgroundImage = "url(" + HBASE.baseURL + "/images/logo.gif)";
		}
		
		return pair;
	},
	
	blind : null,
	blind_visible : false,
	wnd : null,
	
	onWndResize : function() {
		if (!HBASE.blind_visible) return;
		
		var realdoc = HBASE.getDocument();
			
		HBASE.blind.style.width = realdoc.scrollWidth + "px";
		HBASE.blind.style.height = realdoc.scrollHeight + "px";
	},
	
	showBlind : function(flag) {
		
		if (!HBASE.blind) {
			
			HBASE.blind = document.createElement("div");
			
			with (HBASE.blind.style) {
				position = "absolute";
				backgroundColor = "#000";
				left = 0;
				top = 0;
				display = "block";
				filter = "alpha(opacity=40)";
				opacity = 0.4;
				zIndex = 9999;
			}

			document.body.appendChild(HBASE.blind);
		}
		
		if (HBASE.blind_visible = flag) {
			HBASE.onWndResize();
			HBASE.blind.style.display = "block";
		} else {
			HBASE.blind.style.display = "none";
		}
		
	},
	
	moveToCenter : function(obj) {
		
		try {
			if (obj.style.display == "none") return;
		} catch (e) {
			return;
		}
		
		var doc = HBASE.getDocument();
		
		var pos = [
					parseInt((doc.clientWidth - obj.offsetWidth) / 2) + doc.scrollLeft,
					parseInt((doc.clientHeight - obj.offsetHeight) / 2) + doc.scrollTop
		];
		
		with (obj.style) {
			position = "absolute";
			left = pos[0] + "px";
			top = pos[1] + "px";
		}
		
	},
	
	showWindow : function(title, width, height) {
		
		if (!HBASE.wnd) {
			
			HBASE.wnd = document.createElement("div");
			
			with (HBASE.wnd.style) {
				position = "absolute";
				display = "block";
				zIndex = 10000;
			}
			
			HBASE.wnd.addClassName("hooriza_window");
			HBASE.wnd.update('<h1 class="title">테스트</h1><div class="content"></div>');
			
			document.body.appendChild(HBASE.wnd);
		}

		HBASE.showBlind(title ? true : false);
		
		if (title) {

			var realdoc = HBASE.getDocument();
			var wndsize = [ realdoc.clientWidth, realdoc.clientHeight ];
			
			var wndpos = [
						  	realdoc.scrollLeft + (wndsize[0] - width) / 2,
							realdoc.scrollTop + (wndsize[1] - height) / 2
						 ];
			
			var content = HBASE.wnd.getElementsByTagName("div")[0];

			HBASE.wnd.style.left = wndpos[0] + "px";
			HBASE.wnd.style.top = wndpos[1] + "px";

			content.style.width = width + "px";
			content.style.height = height + "px";
			
			HBASE.wnd.style.display = "block";
		} else {
			HBASE.wnd.style.display = "none";
		}
		
	}

};

Event.observe(window, "resize", HBASE.onWndResize);

