var mainDialog=false;
var bgDIV=false;
var DialogInxes=1;
var myWidth = 0, myHeight = 0;
var scrOfX = 0, scrOfY = 0;
var Dialogs=new Array();

function setVisibility2Problematic(visibility) {
	if (document.all) { //IE
		setVisibility2TagName('select',visibility);	
		setVisibility2TagName('textarea',visibility);	
	}

	
	setVisibility2TagName('object',visibility);	
	setVisibility2TagName('embed',visibility);	
}

function setVisibility2TagName(TagName,visibility) {

	var elems=document.getElementsByTagName(TagName);
	var i;

	if (visibility!='old') {
		for (i=0;i<elems.length;i++) {
			elems[i].oldvisibility=elems[i].style.visibility;
			elems[i].style.visibility=visibility;
		}
	}
	else {
		for (i=0;i<elems.length;i++) {
			elems[i].style.visibility=(elems[i].oldvisibility)?elems[i].oldvisibility:'';
		}
	}
} 

function DialogJSInit() {
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE\n";
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode\n";
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
}

DialogJSInit();

function bgDIVShow() {
	var dialogStr;

	if (arguments.length<=0 || arguments[0])
		setVisibility2Problematic('hidden');
	if (!bgDIV) {
		bgDIV=document.createElement('div');
		bgDIV.setAttribute('class','dialog_bg');
		bgDIV.setAttribute('id','bgDIV');
		bgDIV.style.visibility='hidden';
		document.body.appendChild(bgDIV);
		bgDIV.className='dialog_bg';
	}

	DialogJSInit();
//	bgDIV.style.left=scrOfX+'px';
//	bgDIV.style.top =scrOfY+'px';
	var _docHeight = (typeof(document.height) != 'undefined') ? document.height : document.body.offsetHeight;
	var _docWidth = (typeof(document.width) != 'undefined') ? document.width : document.body.offsetWidth;

	bgDIV.style.width=String(_docWidth)+'px';
	bgDIV.style.height=String(_docHeight)+'px';

	bgDIV.style.visibility='visible';
}

function bgDIVHide() {
	setVisibility2Problematic('old');
	if (!bgDIV) {
		bgDIV=document.createElement('div');
		bgDIV.setAttribute('class','dialog_bg');
		bgDIV.setAttribute('id','bgDIV');
		bgDIV.style.visibility='hidden';
		document.body.appendChild(bgDIV);
	}

	bgDIV.style.visibility='hidden';
}

function findChildren(obj,className) {
	var i;
	var Child;
	var Result=new Array();
	for (i in obj.childNodes) {
		Child=obj.childNodes[i];
		if (Child.className==className) {
			Result.push(Child);
		}
	}

	return Result;
}

function dialogCreate() {
	var dialogStr;

	if (arguments.length>1)
		var DialogID=arguments[0];
	else {	
		var DialogID='dialog'+DialogInxes;
		DialogInxes++;
	}

	var dialogDIV=document.createElement('div');
	dialogDIV.setAttribute('id',DialogID);
	dialogDIV.style.visibility='hidden';
	dialogDIV.setAttribute('class','dialog');
	document.body.appendChild(dialogDIV);
	dialogDIV.className='dialog';

	dialogStr="";
	dialogStr+="<div class='dialog_cnt'>\n";
	dialogStr+="</div>\n";
	dialogStr+="<div class='dialog_btns'>\n";
	dialogStr+="<input type='submit' class='image_btn' value='OK'     style='float:left;' onclick='return this.dialogObj.OnBClick(this);'/>\n";
	dialogStr+="<input type='submit' class='image_btn' value='Cancel' style='float:right;' onclick='return this.dialogObj.OnBClick(this);'/>";
	dialogStr+="</div>\n";
	dialogDIV.innerHTML=dialogStr;

	var cntDIV=findChildren(dialogDIV,'dialog_cnt')[0];
	var dialogObj=new Dialog(dialogDIV,DialogID,cntDIV);
	dialogDIV.dialogObj=dialogObj;

	var btnsDIV=findChildren(dialogDIV,'dialog_btns')[0];
	var btns=findChildren(btnsDIV,'image_btn');
	var i,j=0;
	for (i=0;i<btns.length;i++) {
		btns[i].dialogObj=dialogObj;
		btns.bIndex=j;
		j++;
	}

	return dialogObj;
}

function Dialog(dialogDIV,dialogID,cntDIV) {
	this.mainDIV=dialogDIV;
	this.id=dialogID;
	this.cntDIV=cntDIV;
	this.form=null;
	this.onClose=null;
	this.owndata=[];
	Dialogs.push(this);
}

Dialog.prototype.fillContent=function (Content) {
	this.cntDIV.innerHTML=Content;
}

Dialog.prototype.hide=function() {
	this.mainDIV.style.visibility='hidden';
}

Dialog.prototype.show=function() {
	DialogJSInit();
	bgDIVShow();
	this.mainDIV.style.visibility='visible';
	this.mainDIV.style.left=Math.round(scrOfX+(myWidth -this.mainDIV.offsetWidth)/2)+'px';
	this.mainDIV.style.top =Math.round(scrOfY+(myHeight-this.mainDIV.offsetHeight)/2)+'px';
}

Dialog.prototype.OnBClick=function(button) {
	bgDIVHide();
	this.hide();
	this.value=button.value;
	this.bIndex=button.bIndex;
	if (this.onClose) {
		this.onClose(this);
	}
}

function dialogShow(Content) {
	if (!mainDialog)
		mainDialog=dialogCreate('mainDialog');

	var dialogObj=mainDialog;
	dialogObj.fillContent(Content);
	dialogObj.show();
	
	return dialogObj;
}

