var newstyle = null;
function initeditor(name,style){
	if (newstyle == null) {
		var styletext = document.createElement("style");
		styletext.type = "text/css";
		document.getElementsByTagName("head").item(0).appendChild(styletext);
		var sheetlength = document.styleSheets.length-1;
		newstyle=document.styleSheets[sheetlength];
	}
	if (document.all) {
		newstyle.addRule(name,style);
	} else {
		newstyle.insertRule(name+'{'+style+'}',newstyle.cssRules.length);
	}
}

function showDialog(type,message,autohide,callback) {
	if(!type) {
		type = 'error';
	}
	initeditor('.pw-prompt','background:#000;padding:5px;width:340px;filter:alpha(opacity=50);-moz-opacity:0.5;opacity:0.5;position: absolute;height:260px;z-index:999;');
	initeditor('.fr','float:right');
	initeditor('.pw-menu','border:1px solid #303293;background:#b2deeb;width:336px;height:256px; position: absolute;z-index:1000;');
	initeditor('.pw-menu table','background:#f6f7fa;height:200px;border:0;');
	initeditor('.pw-h','border-bottom:1px solid #387db3;color:#fff;padding:6px 8px;height:14px;background:#607ccb url(' + imgpath + '/pw-prompt.png) 0 top;font-size:14px;');
	initeditor('.pw-close','display:block;text-indent:-2000em;height:18px;width:42px;background:url(' + imgpath + '/pw-prompt.png) 0 -30px;');
	initeditor('.pw-close:hover','background-position:0 -50px;');
	initeditor('.pw-text','background:#f6f7fa url(' + imgpath + '/pw-prompt.png) 0 -140px;height:196px;*height:193px;');
	initeditor('.pw-text p','padding:0;margin:0;text-align:center;font-size:14px;');
	initeditor('.pw-bot','height:21px;background:url(' + imgpath + '/pw-prompt.png) 0 -70px;padding:4px 8px;');
	initeditor('.pw-input','border:1px solid #7374b5;background:url(' + imgpath + '/pw-prompt.png) 0 -110px;padding:2px 15px 0;height:21px;cursor:pointer;color:#0d2d87;');
	initeditor('.pw-input:hover','border:1px solid #454681;color:#000;');
	initeditor('.dialog-mask','position:absolute; top:0; left:0; min-height:100%; width:100%; background:#fff; opacity:.10; filter:alpha(opacity=10); z-index:100');
	//var dialogmask = elementBind('div','dialog_mask','dialog-mask');
	//dialogmask.style.height = document.body.offsetHeight + 'px';
	//document.body.appendChild(dialogmask);

	var dialog = elementBind('div','dialog');

	var pw_prompt = elementBind('div','pw_prompt','pw-prompt');
	var pw_menu = elementBind('div','pw_menu','pw-menu');
	dialog.appendChild(pw_prompt);
	dialog.appendChild(pw_menu);
	document.body.appendChild(dialog);
	var wholeWidth = ietruebody().clientWidth;
	pw_prompt.style.top = (getTop() + (ietruebody().clientHeight-pw_prompt.offsetHeight)/3) + 'px';
	pw_prompt.style.left = (wholeWidth - pw_prompt.offsetWidth)/2  + 'px';

	pw_menu.style.top = (getTop() + (ietruebody().clientHeight-pw_menu.offsetHeight)/3) + 'px';
	pw_menu.style.left = (wholeWidth - pw_menu.offsetWidth)/2 + 'px';

	var dialog_header = elementBind('div','dialog_header','pw-h');
	var dialog_close = elementBind('a','dialog_close','pw-close fr');
	dialog_close.onclick = hideDialog;
	dialog_header.appendChild(dialog_close);
	var dialog_title = elementBind('b');
	dialog_title.innerHTML = '提示';
	dialog_header.appendChild(dialog_title);
	
	pw_menu.appendChild(dialog_header);

	var table = elementBind('table');
	table.setAttribute('cellpadding',0);
	table.setAttribute('cellspacing',0);
	table.setAttribute('width','100%');
	table.setAttribute('border','0');
	var tbody = elementBind('tbody');
	table.appendChild(tbody);

	var tr2 = elementBind('tr');
	var td2 = elementBind('td',false,'pw-text');
	var p2 = elementBind('p');
	var img2 = elementBind('img');
	img2.setAttribute('src',imgpath+ '/'+ type +'_bg.gif');
	img2.setAttribute('align','absmiddle');
	p2.appendChild(img2);
	p2.innerHTML += message;
	td2.appendChild(p2);
	tr2.appendChild(td2);
	tbody.appendChild(tr2);
	pw_menu.appendChild(table);

	var dialog_foot = elementBind('div',false,'pw-bot');
	var foot_fr = elementBind('div',false,'fr');
	if (type == 'confirm' && typeof(callback) == 'function'){
		var y_botton = elementBind('input',false,'pw-input');
		y_botton.type = "submit";
		y_botton.value = "确定";
		y_botton.onclick = function(){
			hideDialog();
			callback();
		}
		var n_botton = elementBind('input',false,'pw-input');
		n_botton.type = "submit";
		n_botton.value = "取消";
		n_botton.onclick = hideDialog;
		foot_fr.appendChild(y_botton);
		foot_fr.appendChild(n_botton);
	}
	dialog_foot.appendChild(foot_fr);
	pw_menu.appendChild(table);
	pw_menu.appendChild(dialog_foot);

	if (autohide) {
		window.setTimeout("hideDialog()", (autohide * 1000));
	}
}

function hideDialog() {
	var dialog = document.getElementById('dialog');
	//var dialog_mask = document.getElementById('dialog_mask');
	//dialog_mask.parentNode.removeChild(dialog_mask);
	dialog.parentNode.removeChild(dialog);
}