// 제작일 : 2007년 2월 17일
// 수정일 : 2007년 3월 31일
// 제작자 : 최영규 (http://hooriza.com/)

// Powered by jQuery

$.noConflict();

// jQuery 의 show 메쏘드가 시원찮어... -_-
jQuery.prototype._oldShow = jQuery.prototype.show;

jQuery.prototype.show = function(speed, callback) {
	
	if (arguments.length == 0) {
		this.css({ display : "block" });
		return this;
	}
	
	return this._oldShow.call(this, speed, callback);
}

var HBASE = { // Hooriza Tatter Plugin-Base Script

	version : 2.62,
	
	blogURL : "",
	baseURL : "",
	
	parseXMLString : function(xmlstr) {
		
		var doc;
		var parser;
		
		if (window.ActiveXObject) {

			var pos = xmlstr.indexOf("?>");
			if (pos > -1) xmlstr = xmlstr.substr(pos + 2);

		  doc = new ActiveXObject("Microsoft.XMLDOM");
		  doc.async = "false";
		  doc.loadXML(xmlstr);
		  
	  } else {
	  	
		  parser = new DOMParser();
		  doc = parser.parseFromString(xmlstr, "text/xml");
		  
	  }
	  
	  return doc.documentElement;
	},
	
	xmlToObject : function(node) {
		
		var obj = {};
		var child;
		
		for (child = node.firstChild; child; child = child.nextSibling) {
			
			switch (child.nodeType) {
			case 1:
				obj[child.tagName] = HBASE.xmlToObject(child);
				break;
				
			case 3:
				obj._text = child.nodeValue;
				break;
			}
		}
		
		return obj;
	},
	
	toQueryString : function(obj) {
		
		var parts = [];
		
		for (var key in obj) {
			if (obj[key] instanceof String) {
				parts.push(encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]));
			}
		}
		
		return parts.join("&");
	},
	
	getDocument : function() {
		return (
			(document.compatMode && document.compatMode != "BackCompat") ?
			document.documentElement : document.body
		);
	},
	
	setOffsetWidth : function(obj, width) { obj = jQuery(obj);
		obj.width(width);
		
		var real = obj.get(0).offsetWidth;
		if (real != width) obj.width(width * 2 - real);
	},
	
	setOffsetHeight : function(obj, height) { obj = jQuery(obj);
		obj.height(height);
		
		var real = obj.get(0).offsetHeight;
		if (real != height) obj.height(height * 2 - real);
	},
	
	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) {
		
		jQuery(window).bind("load", function() {
			var pair = HBASE.createTab(title, uniq);
			pair.tab.bind("click", pair, function(e) { HBASE.selectTab(e.data); });
		});
	},

	selectTab : function(pair) {
		
		HBASE.tabs.children().each(function() {
			if (this.tagName.toUpperCase() != "LI") return;
			this.className = (this == pair.tab.get(0) ? "selected" : "");
		});

		HBASE.panels.children().each(function() {
			if (this.tagName.toUpperCase() != "LI") return;
			this.className = (this == pair.panel.get(0) ? "selected" : "");
		});
	
	},

	createTab : function(title, uniq) {

		var pair = { tab : null, panel : null };

		HBASE.tabs = jQuery("#hooriza_plugins_tabs");
 		HBASE.panels = 
		jQuery("#hooriza_plugins_panels");

		HBASE.tabs.show();
 		HBASE.panels.show();

		pair.tab = jQuery("<li>");
 		pair.panel = jQuery("<li>");

		pair.tab.html(title);

		var panel = jQuery("#" + uniq);

		if (panel) {
	 		pair.panel.append(panel);
	 		panel.show();
	 	}

		HBASE.tabs.append(pair.tab);
 		HBASE.panels.append(pair.panel);

		if (HBASE.tabs.children().length == 1) {
	 		HBASE.selectTab(pair); 
			HBASE.tabs.css({
				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.width(realdoc.scrollWidth);
		HBASE.blind.height(realdoc.scrollHeight);
	},

	showBlind : function(flag) {

		if (!HBASE.blind) {

			HBASE.blind = jQuery("<div>");

			HBASE.blind.css({
				position : "absolute",
				backgroundColor : "#000", 
				left : 0,
				top : 0, 
				display : "block", 
				filter : "alpha(opacity=40)", 
				opacity : 0.4,
				zIndex : 9999
			});

			jQuery(document.body).append(HBASE.blind);
 		}

		if (HBASE.blind_visible = flag) {
	 		HBASE.onWndResize(); 
			HBASE.blind.show();
		} else {
			HBASE.blind.hide();
		}

	},

	moveToCenter : function(obj, speed) { obj = jQuery(obj);

		try {
	 		if (obj.css("display") == "none") return;
	 	} catch (e) {
	 		return;
		}

		var doc = HBASE.getDocument();

		var pos = [
			parseInt((doc.clientWidth - obj.width()) / 2) + doc.scrollLeft,
			parseInt((doc.clientHeight - obj.height()) / 2) + doc.scrollTop
		];
		
		obj.css({ position : "absolute" });
		
		if (speed) {

			obj.animate({
		 		left : pos[0], 
		 		top : pos[1]
		 	}, speed);
			
		} else {

			obj.css({
		 		left : pos[0] + "px", 
		 		top : pos[1] + "px"
		 	});
		 	
		}

	},

	showWindow : function(title, width, height) {

		if (!HBASE.wnd) {

			HBASE.wnd = jQuery("<div>");

			HBASE.wnd.css({
 				position : "absolute",
 				display : "block",
 				zIndex : 10000
		 	});

			HBASE.wnd.addClass("hooriza_window");
	 		HBASE.wnd.html('<h1 class="title">테스트</h1><div class="content"></div>');

			jQuery(document.body).append(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 = jQuery("div", HBASE.wnd)[0];

			HBASE.wnd.css({
				left : wndpos[0] + "px",
				top : wndpos[1] + "px"
			});
			
			content.width(width);
			content.height(height);
			
			HBASE.wnd.show();
		} else {
			HBASE.wnd.hide();
		}
		
	}

};

jQuery(window).bind("resize", HBASE.onWndResize);
