/*************************************************************************************************************
	
	developer : idw1@naver.com
	date: 2006-08-01

*************************************************************************************************************/

/*************************************************************************************************************
	
	DW_UTIL

*************************************************************************************************************/

DW_UTIL = new function() {
	this.preload = new Object();

	this.getParent = function(id) {
		return YAHOO.util.Dom.get(id) || document.body;
	}

	this.addDiv = function(id, parent_id) {
		var parent = this.getParent(parent_id);
		var el = document.createElement("div");
		
		el.id = id;
		parent.appendChild(el);

		return el;
	}

	this.addDivXYWH = function(id, parent_id, x, y, w, h) {
		var el = this.addDiv(id, parent_id);
		this.setXYWH(el, x, y, w, h);

		return el;
	}

	this.addImage = function(id, parent_id) {
		var parent = this.getParent(parent_id);
		var el = document.createElement("img");

		el.id = id;
		parent.appendChild(el);

		return el;
	}

	this.addInput = function(id, parent_id, type, caption) {
		var parent = this.getParent(parent_id);
		var el = null;

		if( type == "textarea" ) {
			el = document.createElement("textarea");
		}
		else {
			el = document.createElement("input");
			el.type = type;
		}

		el.value = caption;
		el.id = id;
		parent.appendChild(el);

		return el;
	}

	this.setImage = function(el, value) {
		if( this.getPreload(value) ) {
			el.src = this.getPreload(value);
		}
		else {
			el.src = value;
		}
	}

	this.setPreload = function(key, value) {
		if( !key || !value || this.getPreload(key) ) {
			return;
		}

		this.preload[key] = new Image();
		this.preload[key].src = value;
	}

	this.getPreload = function(key) {
		if( this.preload[key] ) {
			return this.preload[key].src;
		}
		else {
			return null;
		}
	}

	this.setXY = function(el, x, y) {
		if( YAHOO.util.Dom.get(el) ) {
			YAHOO.util.Dom.setStyle(el, "position", "absolute");
			YAHOO.util.Dom.setStyle(el, "left", x + "px");
			YAHOO.util.Dom.setStyle(el, "top", y + "px");
		}
	}

	this.setX = function(el, value) {
		if( YAHOO.util.Dom.get(el) ) {
			YAHOO.util.Dom.setStyle(el, "left", value + "px");
		}
	}

	this.setY = function(el, value) {
		if( YAHOO.util.Dom.get(el) ) {
			YAHOO.util.Dom.setStyle(el, "top", value + "px");
		}
	}

	this.setWH = function(el, w, h) {
		if( YAHOO.util.Dom.get(el) ) {
			YAHOO.util.Dom.setStyle(el, "float", "left");
			YAHOO.util.Dom.setStyle(el, "overflow", "hidden");
			YAHOO.util.Dom.setStyle(el, "width", w + "px");
			YAHOO.util.Dom.setStyle(el, "height", h + "px");
		}
	}

	this.setW = function(el, value) {
		if( YAHOO.util.Dom.get(el) ) {
			YAHOO.util.Dom.setStyle(el, "width", value + "px");
		}
	}

	this.setH = function(el, value) {
		if( YAHOO.util.Dom.get(el) ) {
			YAHOO.util.Dom.setStyle(el, "height", value + "px");
		}
	}

	this.setXYWH = function(el, x, y, w, h) {
		if( YAHOO.util.Dom.get(el) ) {
			YAHOO.util.Dom.setStyle(el, "position", "absolute");
			YAHOO.util.Dom.setStyle(el, "overflow", "hidden");
			YAHOO.util.Dom.setStyle(el, "left", x + "px");
			YAHOO.util.Dom.setStyle(el, "top", y + "px");
			YAHOO.util.Dom.setStyle(el, "width", w + "px");
			YAHOO.util.Dom.setStyle(el, "height", h + "px");
		}
	}

	this.setCaption = function(el, caption) {
		el = YAHOO.util.Dom.get(el);
		if( el ) {
			if( el.tagName.toUpperCase() == "IMG" ) {
				el.alt = caption;
			}
		}
	}

	this.setBackground = function(el, background) {
		if( YAHOO.util.Dom.get(el) ) {
			YAHOO.util.Dom.setStyle(el, "background", background);
		}
	}

	this.setBorder = function(el, size, color) {
		if( YAHOO.util.Dom.get(el) ) {
			color = color || "#000";
			YAHOO.util.Dom.setStyle(el, "border", "solid " + size + "px " + color);
		}
	}

	this.addPixel = function(parent_id, x, y, color, size) {
		color = color || "#000";
		size = size || 1;
		var el = this.addDiv("", parent_id);
		this.setXYWH(el, x, y, size, size);
		YAHOO.util.Dom.setStyle(el, "background", color);
	}

	this.safeDestroy = function(id, parent_id) {
		var parent = this.getParent(parent_id);
		var child = YAHOO.util.Dom.get(id);

		try {
			parent.removeChild(child);
			return true;
		}
		catch ( x ) {
			return false;
		}

//		if( YAHOO.util.Dom.isAncestor(parent, child) ) {
//			parent.removeChild(child);
//		}
	}

	this.alert = function() {
		var str = "";
		for( var i=0; i<arguments.length; i++ ) {
			str += arguments[i];
			if( i != arguments.length - 1 ) { 
				str += ", ";
			}
		}
		alert(str);
	}

	this.centerX = function(w) {
		return Math.floor((YAHOO.util.Dom.getClientWidth() - w) / 2);
	}

	this.centerY = function(h) {
		return Math.floor((YAHOO.util.Dom.getClientHeight() - h) / 2);
	}

	this.addTransparentDiv = function(id, parent_id, x, y, w, h, opacity, color) {
		opacity = opacity || 0.5;
		color = color || "#000";
		var el = this.addDiv(id, parent_id);

		this.setXYWH(el, x, y, w, h);
		this.setBackground(el, color);
		YAHOO.util.Dom.setStyle(el, "filter", "alpha(opacity:" + opacity * 100 + ", style:0)");
		YAHOO.util.Dom.setStyle(el, "mozOpacity", opacity);
		YAHOO.util.Dom.setStyle(el, "opacity", opacity);
		YAHOO.util.Dom.setStyle(el, "khtmlOpacity", opacity);

		return el;
	}

	this.setOpacity = function(el, opacity) {
		opacity = opacity || 0.5;

		YAHOO.util.Dom.setStyle(el, "filter", "alpha(opacity:" + opacity * 100 + ", style:0)");
		YAHOO.util.Dom.setStyle(el, "mozOpacity", opacity);
		YAHOO.util.Dom.setStyle(el, "opacity", opacity);
		YAHOO.util.Dom.setStyle(el, "khtmlOpacity", opacity);
	}

	this.addDottedLine = function(id, parent_id, x, y, length, dir, color) {
		var el = this.addDiv(id, parent_id);
		var line = null;
		dir = dir || 0;
		color = color || "#ccc";

		if( dir == 0 ) {
			this.setXYWH(el, x, y, length, 1);
			line = this.addDiv("", id);
			this.setXYWH(line, 0, 0, length, 1);
		}
		else {
			this.setXYWH(el, x, y, 1, length);
			line = this.addDiv("", id);
			this.setXYWH(line, 0, 0, 1, length);
		}

		YAHOO.util.Dom.setStyle(line, "border", "dotted 1px " + color);
	}

	this.getTextWidth = function(text, font, lh, max) {
		this.safeDestroy("get_string_width");
		if( !text || text == "" ) {
			return 0;
		}

		font = font || "9pt verdana";
		lh = lh || "130%";
		max = max || 1000;
		text = "ll" + text.replace(/\r\n/gi, "\r\nll");

		var el = this.addDiv("get_string_width");
		YAHOO.util.Dom.setStyle(el, "width", max + "px");
		YAHOO.util.Dom.setStyle(el, "font", font);
		YAHOO.util.Dom.setStyle(el, "lineHeight", lh);
		YAHOO.util.Dom.setStyle(el, "wordBreak", "break-all");
		el.innerText = text;

		var r = YAHOO.util.Dom.getRegion(el);
		var h = r.bottom - r.top;
		var original_height = h;
		var min = 0;
		var avg = 0;

		while( max - min > 10 ) {
			avg = Math.round((min + max) / 2);
			YAHOO.util.Dom.setStyle(el, "width", avg + "px");
			r = YAHOO.util.Dom.getRegion(el);
			h = r.bottom - r.top;
			
			if( h != original_height ) {
				min = avg;
			}
			else {
				max = avg;
			}
		}

		YAHOO.util.Dom.setStyle(el, "width", max + "px");
		h = original_height;

		while( h == original_height && max > 0 ) {
			max = max - 1;
			YAHOO.util.Dom.setStyle(el, "width", max + "px");
			r = YAHOO.util.Dom.getRegion(el);
			h = r.bottom - r.top;
		}
		this.safeDestroy("get_string_width");

		return max + 1;
	}

	this.getTextHeight = function(text, font, lh, max) {
		if( !text || text == "" ) {
			return 0;
		}
		this.safeDestroy("get_string_height");

		font = font || "9pt verdana";
		lh = lh || "130%";
		max = max || 1000;

		var el = this.addDiv("get_string_height");
		YAHOO.util.Dom.setStyle(el, "width", max + "px");
		YAHOO.util.Dom.setStyle(el, "font", font);
		YAHOO.util.Dom.setStyle(el, "lineHeight", lh);
		el.innerText = text;

		var r = YAHOO.util.Dom.getRegion(el);
		var h = r.bottom - r.top;

		this.safeDestroy("get_string_height");

		return h;
	}

	this.clearArray = function(array) {
		while( array.length ) {
			array.pop();
		}
	}

	this.hide = function(el) {
		YAHOO.util.Dom.setStyle(el, "display", "none");
	}

	this.show = function(el) {
		YAHOO.util.Dom.setStyle(el, "display", "block");
	}

	this.setScrollbarStyle = function(el) {
		YAHOO.util.Dom.setStyle(el, "scrollbarFaceColor", "#999");
		YAHOO.util.Dom.setStyle(el, "scrollbarArrowColor", "#fff");
		YAHOO.util.Dom.setStyle(el, "scrollbarBaseColor", "#999");
		YAHOO.util.Dom.setStyle(el, "scrollbarTrackColor", "#ccc");

		YAHOO.util.Dom.setStyle(el, "scrollbar3dLightColor", "#999");
		YAHOO.util.Dom.setStyle(el, "scrollbarDarkShadowColor", "#999");

		YAHOO.util.Dom.setStyle(el, "scrollbarHighlightColor", "#ccc");
		YAHOO.util.Dom.setStyle(el, "scrollbarShadowColor", "#666");
	}
};
