
/**
 * http://www.jobcn.com/
 * By zhangping
 *
 * For: IE6,IE7,Firefox	
 * 
 * Required :	masker.js;
 */

//default config
var DailogConfig = {
	width:"500px", 
	//height:"450px",	
	className: "pop_dailog",	
	title: "Dailog Box",
	top: "100px",
	//left: "0px",
	//topOffset: 320,
	
	model: false,
	drag: false,
	closeText: "[¹Ø±Õ´°¿Ú]",
	submit: function(){void(0);}
}

function Dailog(config){
	var dailogBox = null, panelBox = null;
		
	//public methods
	this.init = init; this.show = show;	this.hide = hide;	
	this.set = set;	this.setURL = setURL; this.insert = insert;	
	this.append = append; this.remove = remove; this.clear = clear;
	
	this.getConfig = getConfig; this.getId = getId; this.getPanel = getPanel;
	
	this.init();
	
	function init(){
		config = config || new Object();
		for(var prop in DailogConfig) { if(config[prop]==null) config[prop]=DailogConfig[prop];	}
		
		var body = document.getElementsByTagName('BODY')[0];
		dailogBox = document.createElement('div');
		body.appendChild(dailogBox);
		dailogBox.id = config.id?config.id: "id_" + new Date().getTime();
		dailogBox.className = config.className;		
		dailogBox.style.zIndex = 500;		
		if(config.width) dailogBox.style.width = config.width;
		if(config.height) dailogBox.style.height = config.height;

		var titleBox = document.createElement('DIV');
		var boxTitle = document.createElement('DIV');
		var boxClose = document.createElement('DIV');		
		dailogBox.appendChild(titleBox);
		titleBox.appendChild(boxTitle);
		titleBox.appendChild(boxClose);
		boxTitle.className = "win_title_L";
		boxClose.className = "win_title_R";		
		titleBox.className = "win_title";
		
		boxTitle.appendChild(document.createTextNode(config.title));		
		var link = document.createElement('A');
		link.href="javascript:void(0)";		
		link.onclick = this.hide;
		link.style.color = "#FFF";
		link.appendChild(document.createTextNode(config.closeText));
		boxClose.appendChild(link);		
		
		panelBox = document.createElement('DIV');
		dailogBox.appendChild(panelBox);
 		panelBox.className = "win_panel";
		
 		//for drag
 		if(config.drag && zDraggable)
 			new zDraggable(dailogBox, zDraggable.DRAG_X | zDraggable.DRAG_Y);
	}
	
	function set(element){
		while(panelBox.childNodes.length!=0)
			panelBox.removeChild(panelBox.firstChild);								
		panelBox.appendChild(element);
	}
	
	function setURL(url){
		while(panelBox.childNodes.length!=0)
			panelBox.removeChild(panelBox.firstChild);			
		var frame = document.createElement('IFRAME');		
		frame.src = url;
		frame.style.height = parseInt(dailogBox.style.height) - 38 + 'px';
		panelBox.appendChild(frame);
	}
	
	function append(element){
		panelBox.appendChild(element);
	}
	
	function insert(element){
		panelBox.insertBefore(element ,panelBox.firstChild);
	}
	
	function remove(element){
		try {panelBox.removeChild(element); }
		catch(e) { }
	}

	function clear(){
		while(panelBox.childNodes.length!=0)
			panelBox.removeChild(panelBox.firstChild);
	}
	
	function getId(){
		return dailogBox.id;
	}
	
	function getPanel(){
		return panelBox;
	}
	
	function getConfig(){
		return config;
	}
	
	function show(){
		if(config.model && Masker) { Masker.mask();	if(Masker.ie6) Masker.showSelect(dailogBox); }		
		
		var body = document.getElementsByTagName('BODY')[0];		
		var width  = dailogBox.style.width? dailogBox.style.width : dailogBox.offsetWidth;
		var height = dailogBox.style.height? dailogBox.style.height : dailogBox.offsetHeight;

		if(config.left) dailogBox.style.left = config.left;
		else dailogBox.style.left =  (body.clientWidth -parseInt(width))/2 + "px";
		
		if(config.top) dailogBox.style.top = config.top;
		else dailogBox.style.top = (body.clientHeight -parseInt(height))/2 - config.topOffset + "px";		

		dailogBox.style.display = 'block';
	}
	
	function hide(){
		if(config.model && Masker) Masker.unmask();		
		dailogBox.style.display = 'none';	
	}
}

Dailog.util = {};

Dailog.util.createTable = function(cells, columns, oddBgColor, eveBgColor) {
	var table = document.createElement('TABLE');
	table.style.width = "100%";	
	var tbody = document.createElement('TBODY');
	table.appendChild(tbody);
		
	var tr = null;
	var td = null;

	for(var i=0;i<cells.length;i++) {		
		if(i%columns==0){
			tr = document.createElement('TR');
			tbody.appendChild(tr);						
			if(i%2==0) tr.style.background = eveBgColor ;
			else tr.style.background = oddBgColor;
     }

		td = document.createElement('TD');
		td.style.width = (100/columns) + "%";

		td.appendChild(cells[i]);
		tr.appendChild(td);			
	}
	
   //fix the last row of the table
	var remain = !tr?0: tr.childNodes.length%columns;
	if(remain!=0){
		remain = columns - remain;
		while(remain>0){
			var td = document.createElement('TD');
			td.appendChild(document.createTextNode(''));
			td.style.background = "#FFF";
			tr.appendChild(td);
			remain--;
		}
	}
	return table;
}