function EstateWeb_Objects_DialogManager(oid){
	this.id = oid;
	this.parentID = "";
	this.parentMarginLeftOffset = 0;
	this.dialogTypes = new EstateWeb_Objects_DialogManager_DialogTypes(this);
	this.dialogOptions = new EstateWeb_Objects_DialogManager_DialogOptions(this);
	this.backgroundOptions = new EstateWeb_Objects_DialogManager_BackgroundOptions(this);
	this.events = new HttpManager.Browser.eventManager();
	//methods
	this.show = EstateWeb_Objects_DialogManager_Show2;
	this.hide = EstateWeb_Objects_DialogManager_Hide;
	this.prevalidate = EstateWeb_Objects_DialogManager_PreValidate;
	this.createUniqueID = EstateWeb_Objects_DialogManager_CreateUniqueID;
	this.sizeElements = EstateWeb_Objects_DialogManager_SizeElements;
	//events
	this.onBrowserWindowResize = EstateWeb_Objects_DialogManager_OnBrowserWindowResize;
	//events for dialog specific types
	//progress bar
	this.onProgressBarChange = EstateWeb_Objects_DialogManager_OnProgressBarChange;
}

function EstateWeb_Objects_DialogManager_DialogTypes(o){
	this.parent = o;
	this.information = 0;
	this.confirm = 1;
	this.progress = 2;
}

function EstateWeb_Objects_DialogManager_BackgroundOptions(o){
	this.parent = o;
	this.opacity = 1;
}

function EstateWeb_Objects_DialogManager_DialogOptions(o){
	this.parent = o;
	this.width = 300;
	this.height = 80;
	this.backgroundColour = "#FEFF99";
	this.type = this.parent.dialogTypes.information;
	this.title = new EstateWeb_Objects_DialogManager_DialogOptions_Title();
	this.controls = new EstateWeb_Objects_DialogManager_DialogOptions_Controls(this);
	this.progressOptions = new EstateWeb_Objects_DialogManager_DialogOptions_ProgressOptions(this);
	this.confirmOptions = new EstateWeb_Objects_DialogManager_DialogOptions_ConfirmOptions(this);
	this.isVisible = false;
}

function EstateWeb_Objects_DialogManager_DialogOptions_ProgressOptions(o){
	this.parent = o;
	this.bar;
	this.barWidth = 200;
	this.barHeight = 5;
	this.barColour = "#45A945";
	this.barContainerColour = "";
	this.barContainerBorderColour = "#004F42";
	this.barContainerBorderWidth = "1";
	this.barContainer;
}

function EstateWeb_Objects_DialogManager_DialogOptions_ConfirmOptions(o){
	this.parent = o;
	this.text = "";
}

function EstateWeb_Objects_DialogManager_DialogOptions_Controls(o){
	this.parent = o;
	this.ok = new EstateWeb_Objects_DialogManager_DialogOptions_Controls_Button(this,"ok");
	this.cancel = new EstateWeb_Objects_DialogManager_DialogOptions_Controls_Button(this,"cancel");
}

function EstateWeb_Objects_DialogManager_DialogOptions_Controls_Button(o,id){
	this.parent = o;
	this.id = id;
	this.imageSrc = "";
	this.imageOverSrc = "";
	this.enabled = false;
	this.render = EstateWeb_Objects_DialogManager_DialogOptions_Controls_Button_Render;
}

function EstateWeb_Objects_DialogManager_DialogOptions_Controls_Button_Render(){
	var obutton = document.createElement("img");
	with ( obutton ){
		src = this.imageSrc;
	}
	var tsrc = this.imageOverSrc;
	var tparent = this.parent;
	var tthis = this;
	obutton.onmouseover = function(){ HttpManager.Document.SwapImage( obutton, tsrc ) }
	obutton.onclick = function(){ tparent.parent.parent.events.raiseEvent("oncontrolbuttonclick", tthis, null);  }
	return obutton;
}

function EstateWeb_Objects_DialogManager_DialogOptions_Title(){
	this.text = "";
	this.fontFamily = "Tahoma";
	this.fontSize = "10pt";
}

function EstateWeb_Objects_DialogManager_SizeElements(){
	if ( this.dialogOptions.isVisible ){
		with ( HttpManager.Document.GetObject(this.createUniqueID("background")) ){
			style.backgroundColor = "#C3C3C3";
			style.position = "absolute";
			style.top = HttpManager.Document.GetObjectPosition(this.parentID).y + "px";
			style.left = this.parentMarginLeftOffset + "px";
			style.height = HttpManager.Document.GetObject(this.parentID).offsetHeight + "px";
			style.width = HttpManager.Document.GetObject(this.parentID).offsetWidth + "px";
			switch ( HttpManager.Browser.Type() ){
				case HttpManager.Browser.Types.Mozilla:
					style.opacity = this.backgroundOptions.opacity;
					break;
				case HttpManager.Browser.Types.InternetExplorer4Plus, HttpManager.Browser.Types.InternetExplorer5Plus:
					style.filter = "alpha(opacity="+(this.backgroundOptions.opacity*100)+")";
					break;
			}
			style.visibility = "visible";
		}
		
		with ( HttpManager.Document.GetObject(this.createUniqueID("messagecontainer")) ){
			style.width = this.dialogOptions.width + "px";
			style.height = this.dialogOptions.height +"px";
			style.position = "absolute";
			style.left = ( ( ( HttpManager.Document.GetObject(this.parentID).offsetWidth / 2 ) + HttpManager.Document.GetObject(this.createUniqueID("background")).offsetLeft )  - (offsetWidth/2))+ "px";
			style.top = ( ( ( HttpManager.Document.GetObject(this.parentID).offsetHeight / 2 ) + HttpManager.Document.GetObject(this.createUniqueID("background")).offsetTop )  - (offsetHeight/2))+ "px";
			style.visibility = "visible";
		}
	}
}

function EstateWeb_Objects_DialogManager_Hide(){
	HttpManager.Document.GetObject(this.createUniqueID("background")).style.visibility = "hidden";
	HttpManager.Document.GetObject(this.createUniqueID("messagecontainer")).style.visibility = "hidden";
	this.dialogOptions.isVisible = false;
}

function EstateWeb_Objects_DialogManager_Show2(parentID){
	if ( parentID ){
		this.parentID = parentID;
	}
	if ( this.prevalidate() ){
		var omessagecontainer;
		if (!HttpManager.Document.GetObject(this.createUniqueID("background")) ){
			var obackground = document.createElement("div");
			obackground.id = this.createUniqueID("background");		
			omessagecontainer = document.createElement("div");
			omessagecontainer.id = this.createUniqueID("messagecontainer")
			//attach to body
			document.getElementsByTagName("body")[0].appendChild( obackground );
			document.getElementsByTagName("body")[0].appendChild( omessagecontainer );
		}else{
			omessagecontainer = HttpManager.Document.GetObject(this.createUniqueID("messagecontainer"));
			//clear the contents
			HttpManager.Document.RemoveObject(this.createUniqueID("contenttable"));
		}

		var ocontents = document.createElement("table");
		var ocontents_tbody = document.createElement("tbody");
		var ocontents_row1 = document.createElement("tr");
		var ocontents_row1_cell1 = document.createElement("td");
		var ocontents_row1_cell2 = document.createElement("td");
		var ocontents_row1_cell3 = document.createElement("td");
		var ocontents_row2 = document.createElement("tr");
		var ocontents_row2_cell1 = document.createElement("td");
		var ocontents_row2_cell2 = document.createElement("td");
		var ocontents_row2_cell3 = document.createElement("td");
		var ocontents_row3 = document.createElement("tr");
		var ocontents_row3_cell1 = document.createElement("td");
		var ocontents_row3_cell2 = document.createElement("td");
		var ocontents_row3_cell3 = document.createElement("td");
		//appending
		ocontents.appendChild( ocontents_tbody );
		ocontents_row1.appendChild( ocontents_row1_cell1 );
		ocontents_row1.appendChild( ocontents_row1_cell2 );
		ocontents_row1.appendChild( ocontents_row1_cell3 );
		ocontents_row2.appendChild( ocontents_row2_cell1 );
		ocontents_row2.appendChild( ocontents_row2_cell2 );
		ocontents_row2.appendChild( ocontents_row2_cell3 );
		ocontents_row3.appendChild( ocontents_row3_cell1 );
		ocontents_row3.appendChild( ocontents_row3_cell2 );
		ocontents_row3.appendChild( ocontents_row3_cell3 );
		ocontents_tbody.appendChild( ocontents_row1 );
		ocontents_tbody.appendChild( ocontents_row2 );
		ocontents_tbody.appendChild( ocontents_row3 );
		//
		ocontents.id = this.createUniqueID("contenttable");
		with ( ocontents ){
			style.width = "100%";
			style.height = "100%";
			cellPadding = "0";
			cellSpacing = "0";
		}
		
		//
		with ( ocontents_row1_cell1 ){
			style.width = "8px";
			style.height = "11px";
			if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
				style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/dialogs/style1/topLeft.png')";
			}else{
				style.backgroundImage = "url('/images/controls/dialogs/style1/topLeft.png')";
			}
			appendChild( EstateWeb_Objects_DialogManager_Show2_CreateSpacer(8,8) );
		}
		with ( ocontents_row1_cell2 ){
			style.width = "8px";
			style.height = "8px";
			style.backgroundImage = "url('/images/controls/dialogs/style1/top.gif')";
			style.backgroundRepeat = "repeat";
			appendChild( EstateWeb_Objects_DialogManager_Show2_CreateSpacer(8,8) );
		}
		with ( ocontents_row1_cell3 ){
			style.width = "16px";
			style.height = "11px";
			if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
				style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/dialogs/style1/topRight.png')";
			}else{
				style.backgroundImage = "url('/images/controls/dialogs/style1/topRight.png')";
			}
			appendChild( EstateWeb_Objects_DialogManager_Show2_CreateSpacer(13,8) );
		}
		with ( ocontents_row2_cell1 ){
			style.width = "8px";
			style.backgroundImage = "url('/images/controls/dialogs/style1/left.gif')";
			style.backgroundRepeat = "repeat-y";
			appendChild( EstateWeb_Objects_DialogManager_Show2_CreateSpacer(8,8) );
		}
		with ( ocontents_row2_cell2 ){
			appendChild( EstateWeb_Objects_DialogManager_Show_CreateContent(this) );
			style.backgroundColor = this.dialogOptions.backgroundColour;
		}
		with ( ocontents_row2_cell3 ){
			style.width = "16px";
			if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
				style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/dialogs/style1/right.png', sizingMethod='scale')";
			}else{
				style.backgroundImage = "url('/images/controls/dialogs/style1/right.png')";
			}
			style.backgroundRepeat = "repeat-y";
			appendChild( EstateWeb_Objects_DialogManager_Show2_CreateSpacer(13,8) );
		}
		with ( ocontents_row3_cell1 ){
			style.width = "11px";
			style.height = "15px";
			if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
				style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/dialogs/style1/bottomLeft.png')";
			}else{
				style.backgroundImage = "url('/images/controls/dialogs/style1/bottomLeft.png')";
			}
			appendChild( EstateWeb_Objects_DialogManager_Show2_CreateSpacer(8,13) );
		}
		with ( ocontents_row3_cell2 ){
			style.width = "100%";
			style.height = "15px";
			if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
				style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/dialogs/style1/bottom.png', sizingMethod='scale')";
			}else{
				style.backgroundImage = "url('/images/controls/dialogs/style1/bottom.png')";
			}
			style.backgroundRepeat = "repeat-x";
			appendChild( EstateWeb_Objects_DialogManager_Show2_CreateSpacer(8,13) );
		}
		with ( ocontents_row3_cell3 ){
			style.width = "16px";
			style.height = "15px";
			if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
				style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/dialogs/style1/bottomRight.png')";
			}else{
				style.backgroundImage = "url('/images/controls/dialogs/style1/bottomRight.png')";
			}
			appendChild( EstateWeb_Objects_DialogManager_Show2_CreateSpacer(13,13) );
		}
		//add contents to dialog
		omessagecontainer.appendChild( ocontents );
		//size elements
		this.dialogOptions.isVisible = true;
		this.sizeElements();	
	}
}


function EstateWeb_Objects_DialogManager_Show2_CreateSpacer(w,h){
	var oimg = document.createElement("img");
	with ( oimg ){
		style.width = w + "px";
		style.height = h + "px";
		src = "/images/structure/trans.gif";
	}
	return oimg;
}

function EstateWeb_Objects_DialogManager_Show_CreateContent(o){
	var ocontent = document.createElement("table");
	var ocontent_tbody = document.createElement("tbody");
	var ocontent_row1 = document.createElement("tr");
	var ocontent_row1_cell1 = document.createElement("td");
	var ocontent_row2 = document.createElement("tr");
	var ocontent_row2_cell1 = document.createElement("td");
	var ocontent_row3 = document.createElement("tr");
	var ocontent_row3_cell1 = document.createElement("td");
	
	ocontent_row1.appendChild( ocontent_row1_cell1 );
	ocontent_row2.appendChild( ocontent_row2_cell1 );
	ocontent_row3.appendChild( ocontent_row3_cell1 );
	
	ocontent_tbody.appendChild( ocontent_row1 );
	ocontent_tbody.appendChild( ocontent_row2 );
	ocontent_tbody.appendChild( ocontent_row3 );
	
	ocontent.appendChild( ocontent_tbody );
	
	with ( ocontent_row1_cell1 ){
		innerHTML = o.dialogOptions.title.text;
		style.height = "25px";
		if ( o.dialogOptions.title.fontFamily.length > 0 ){
			style.fontFamily = o.dialogOptions.title.fontFamily;
		}
		if ( o.dialogOptions.title.fontSize.length > 0 ){
			style.fontSize = o.dialogOptions.title.fontSize;
		}
	}
	
	
	with ( ocontent ){
		cellSpacing = 0;
		cellPadding = 0;
		//border = 1;
		style.width = "100%";
		style.height = "100%";
		style.paddingBottom = "10px";
	}
	
	switch ( o.dialogOptions.type  ){
		case o.dialogTypes.progress:
			//progress bar
			var oprogressbar = document.createElement("table");
			var oprogressbar_tbody = document.createElement("tbody");
			var oprogressbar_trheading = document.createElement("tr");
			var oprogressbar_tdheading = document.createElement("td");
			var oprogressbar_trcontent = document.createElement("tr");
			var oprogressbar_tdcontent = document.createElement("td");
			var oprogressbar_trfooter = document.createElement("tr");
			var oprogressbar_tdfooter = document.createElement("td");
			
			//create progress bar
			var oprogressbar_container = document.createElement("div");
			var oprogressbar_bar = document.createElement("div");
			
			//set styles
			with ( oprogressbar_container ){
				style.backgroundColor = o.dialogOptions.progressOptions.barContainerColour;
				style.width = "100%";
				style.height = o.dialogOptions.progressOptions.barHeight + "px";
				style.border = "solid "+o.dialogOptions.progressOptions.barContainerBorderWidth + "px " +  o.dialogOptions.progressOptions.barContainerBorderColour;
			}
			o.dialogOptions.progressOptions.barContainer = oprogressbar_container;
			with ( oprogressbar_bar ){
				style.backgroundColor = o.dialogOptions.progressOptions.barColour;
				style.width = "0px";
				style.height = o.dialogOptions.progressOptions.barHeight + "px";
				//alert(style.height);
				//style.position = "absolute";
			}
			o.dialogOptions.progressOptions.bar = oprogressbar_bar;
			with ( oprogressbar ){
				style.width = "100%";
			}
			
			
			oprogressbar_container.appendChild( oprogressbar_bar);
			oprogressbar_tdcontent.appendChild( oprogressbar_container );
			oprogressbar_trheading.appendChild( oprogressbar_tdheading );
			oprogressbar_trcontent.appendChild( oprogressbar_tdcontent );
			oprogressbar_trfooter.appendChild( oprogressbar_tdfooter );
			
			oprogressbar_tbody.appendChild( oprogressbar_trheading );
			oprogressbar_tbody.appendChild( oprogressbar_trcontent );
			oprogressbar_tbody.appendChild( oprogressbar_trfooter );
			
			oprogressbar.appendChild( oprogressbar_tbody );
			ocontent_row2_cell1.appendChild( oprogressbar );
			break;
		case o.dialogTypes.confirm:
			ocontent_row2_cell1.innerHTML = o.dialogOptions.confirmOptions.text;
			break;
	}
	
	return ocontent;
}

function EstateWeb_Objects_DialogManager_CreateUniqueID(id){
	//creates a unique id based on the id of the object
	return HttpManager.Strings.Format("{0}_{1}", this.id, id)
}

function EstateWeb_Objects_DialogManager_PreValidate(){
	//before creating the object, ensure that all parameters are set correctly
	if ( this.id.length == 0 ){
		HttpManager.Document.Errors.add("Dialog Manager", "Required property 'id' not set");
	}else if ( this.parentID.length == 0 ){
		HttpManager.Document.Errors.add("Dialog Manager", "Required property 'parentID' not set");
	}else if ( ! HttpManager.Document.GetObject(this.parentID) ){
		HttpManager.Document.Errors.add("Dialog Manager", "Unable to find object provided in 'parentID', verify you have spelt its name correctly");
	}
	
	if ( HttpManager.Document.Errors.errors.length > 0 ){
		HttpManager.Document.Errors.show();
		HttpManager.Document.Errors.clear("Dialog Manager");
		return false;
	}
	//attach events
	HttpManager.Browser.events.addHandler("onresize", this, this.onBrowserWindowResize );
	return true;
}

//event handling

 function EstateWeb_Objects_DialogManager_OnBrowserWindowResize(creator,sender,eventArgs){
	creator.sizeElements();
 }
 
 function EstateWeb_Objects_DialogManager_OnProgressBarChange(creator,sender,eventArgs){
	creator.dialogOptions.progressOptions.bar.style.width = (( 100 / eventArgs.total ) * ( eventArgs.index + 1)) + "%";
	if ( eventArgs.total == ( eventArgs.index + 1) ){
		setTimeout( function(){ creator.events.raiseEvent("onprogressbarcomplete", creator, null); },500);
	}
 }
 