/**
 * bjs dialog
 * @author Junbo Bao <baojunbo@gmail.com>
 * @version 1.0.0
 */
function BjsDialog(w,h,overflow) {
	var t=this,id=Math.random();
	getPageSize();
	t.pH= pH;
	t.w = (w) ? w : 400;
	t.h = (h) ? h : 200;
	t.overflow = (overflow) ? overflow + ':auto' : null;
	t.onconfirm='';t.oncancle ='';

	var getTopPlace=function(h, s){
		if (h) t.h=h;
		if (s) return (d.body.clientHeight - t.h - 90)/2 + d.body.scrollTop;
		return (t.pH - t.h - 100)/2 + d.documentElement.scrollTop;
	};

	var showDialog=function(c){
		var cobj=d.getElementById('pop_dialog_'+id);
		if(cobj!=null) d.body.removeChild(cobj);
		var div=d.createElement('div');
		div.id='pop_dialog_'+id;
		div.innerHTML=c;
		d.body.appendChild(div);
		if ($('confirm_'+id)) {
			$('confirm_'+id).focus();
			$('confirm_'+id).onclick=hiddenDialog;
		}
		$('closeButton_'+id).onclick=hiddenDialog;
	};

	var hiddenDialog=function(event){
		var rs=true;
		src=eSrc(event);
		if(t.onconfirm!=''&&src.id=='confirm_'+id) rs=t.onconfirm();
		if(t.oncancle!=''&&src.id=='cancle_'+id) rs=t.oncancle();
		if(rs){
			var cobj=d.getElementById('pop_dialog_'+id);
			if(cobj!=null) d.body.removeChild(cobj);
		}
		stopEvent(event);
	};

	var getDialogContent=function(title,content,confirmB,cancleB){
		t.t = getTopPlace();
		if (t.overflow) var overflow = t.overflow;
		var bottom = bottomLine = '';
		if (confirmB) {
			var cb='',sb='<input id="confirm_'+id+'" type="button" value="'+confirmB+'" class="confirm">';
			if(typeof cancleB!='undefined'){
				cb='&nbsp;<input id="cancle_'+id+'" type="button" value="'+cancleB+'" class="confirm cancle">';
			}
			bottom = '<tr><td class="pop_border"></td><td class="pop_bottom">'+sb+cb+'</td><td class="pop_border"></td></tr>';
		} else {
			var bottomLine = ' style="border-bottom:1px solid #555;"';
		}
		var c='<div class="pop_bg" style="height:'+d.body.clientHeight+'px;"></div><div id="pop_dialog_div_'+id+'" class="pop_dialog_div" style="top:'+t.t+'px;"><table class="pop_dialog_table" id="pop_dialog_table_'+id+'" style="width:'+t.w+'px;"><tr><td class="pop_topleft"></td><td class="pop_border"></td><td class="pop_topright"></td></tr><tr><td class="pop_border"></td><td class="pop_title"><div id="pop_title_'+id+'" class="left">'+title+'</div><div class="right"><a id="closeButton_'+id+'" href="#">X</a></div></td><td class="pop_border"></td></tr><tr><td class="pop_border"></td><td class="pop_content"'+bottomLine+'><div id="pop_content_'+id+'" style="height:'+t.h+'px;'+overflow+'">'+content+'</div></td><td class="pop_border"></td></tr>'+bottom+'<tr><td class="pop_bottomleft"></td><td class="pop_border"></td><td class="pop_bottomright"></td></tr></table></div>';
		return c;
	};

	t.setSize=function(w,h) {
		$('pop_dialog_table_'+id).setStyle('width',w+'px');
		$('pop_content_'+id).setStyle('height',h+'px');
		if (t.h != h){
			t.t = getTopPlace(h);
			if (t.t < 0) t.t = 90;
			$('pop_dialog_div_'+id).setStyle('top',t.t+'px');
		}
	};

	t.setConfirmButton=function(caption){
		$('confirm_'+id).setValue(caption);
	}

	t.setCancleButton=function(caption){
		$('cancle_'+id).setValue(caption);
	}

	t.setDialogTitle=function(title){
		$('pop_title_'+id).setInnerHTML(title);
	}

	t.setDialogContent=function(content){
		$('pop_content_'+id).setInnerHTML(content);
	}

	t.show = function(title, content) {
		var c=getDialogContent(title,content);
		showDialog(c);
	}

	t.showMsg = function(title,content,confirmB) {
		var c=getDialogContent(title,content,confirmB);
		showDialog(c);
	};

	t.showChoose=function(title,content,confirmB,cancleB) {
		var c=getDialogContent(title,content,confirmB,cancleB);
		showDialog(c);
		$('cancle_'+id).onclick=hiddenDialog;
	};

	t.hiddenDialog=function(){
		var cobj= d.getElementById('pop_dialog_'+id);
		if(cobj!=null) d.body.removeChild(cobj);
	};
}

