//requires generic.js to be included
//requires isIE6 to be set

var dialogs=new Array();
var topSpace=25;//a percentage of the screen size
var topOffset=300;//only used for IE 6
var centerOffset=200;//only used for type normal
var hideWait=150000;
var fadeTime=50;

var curDialog=-1;

//for iframe dialog
var iframeWidth=0;
var iframeHeight=0;

//type: normal, image
//hideOnClick: hide dialog when user clicks anywhere?
//authoHide: automatically hide dialog after hideWait ms?
//fade: fade dialog when hiding?
//greyBlank: grey out rest of page?
function Dialog(name,type,hideOnClick,autoHide,fade,greyBlank) {
	//this object requires a global array dialogs to be declared
	//this array is used to keep track of object instances for 
	//external referencing by event handlers and timeout functions
	
	this.index=dialogs.length;
	
	//default onFade behaviour
	callfunc=true;
	func=dialogFaded;
	opacity=10;

	if (fade) {
		this.fadediv=new FadeDiv(name,callfunc,func,opacity);
		fadedivs[fadedivs.length]=this.fadediv;
	}
	
	this.name=name;
	this.hideOnClick=hideOnClick;
	this.autoHide=autoHide;
	this.fade=fade;
	this.greyBlank=greyBlank;
	this.type=type;
	
	this.hideId=-1;
}

Dialog.prototype.setOnFade = function (opacity,callfunc,func) {
	if (this.fade) {
		this.fadediv.callfunc=callfunc;
		this.fadediv.func=func;
		this.fadediv.opacity=opacity;
	}
}

Dialog.prototype.setOpts = function (hideOnClick,autoHide,fade,greyBlank)//not sure if this functions will work
{
	if (fade) {
		this.fadediv=new FadeDiv(name,callfunc,func,opacity);
		fadedivs[fadedivs.length]=this.fadediv;
	}
	
	this.hideOnClick=hideOnClick;
	this.autoHide=autoHide;
	this.fade=fade;
	this.greyBlank=greyBlank;
}


function showGreyout(greyBlank)
{
	var gdiv=document.getElementById("greyout");
	gdiv.className="greyout_show";
	if (greyBlank) {
		setOpacity(gdiv,0);
	}
	if (isIE6) {
		gdiv.style.height=getScreenHeight()+"px";
		gdiv.style.width=getScreenWidth()+"px";
	}
}

function hideGreyout(greyBlank)
{
	var gdiv=document.getElementById("greyout");
	gdiv.className="greyout";
	if (greyBlank) {
		setOpacity(gdiv,8);
	}
	if (isIE6) {
		gdiv.style.height="0px";
		gdiv.style.width="0px";
	}
}

function fadeCurDialog()
{
	//if (dialogs[curDialog].hideId!=-1)
	//	clearTimeout(dialogs[curDialog].hideId);
	if (curDialog!=-1)
		fadeDiv(dialogs[curDialog].fadediv.index,fadeTime);
}

function dialogFaded()
{
	if (curDialog!=-1) {
		name=dialogs[curDialog].name;
		hideDialog(curDialog);
		//reset opacity once hidden
		opacity=10;
		setOpacity(document.getElementById(name),10);
	}
}

function showDialog(ind)
{
	curDialog=ind;
	showGreyout(dialogs[ind].greyBlank);
	//var gdiv=document.getElementById("greyout");
	
	var gdiv=document.getElementById(dialogs[ind].name);
	if (dialogs[ind].hideOnClick) {
		if (dialogs[ind].fade)
			gdiv.onclick=fadeCurDialog;
		else
			gdiv.onclick=hideCurDialog;
	}
	else {
		gdiv.onclick=null;
	}
	gdiv.className="dialog_show";
	
	var gdiv=document.getElementById(dialogs[ind].name+"_inner");
	if (dialogs[ind].type=="iframe") {
		gdiv.style.width=iframeWidth+"px";
		gdiv.style.height=iframeHeight+"px";
		gdiv.style.left=((getScreenWidth()/2)-(iframeWidth/2))+"px";
		gdiv.style.top="10%";
	}
	else if (dialogs[ind].type=="image") {
		//gdiv.style.left="auto";
		//gdiv.style.top=topSpace+"%";
	}
	else {
		gdiv.style.left=((getScreenWidth()/2)-centerOffset)+"px";
		gdiv.style.top=topSpace+"%";
	}
	
	if (isIE6) {
		gdiv.style.top=(getScrollTop()+topOffset)+"px";
	}
	if (dialogs[ind].autoHide && dialogs[ind].fade) {
		dialogs[ind].hideId=setTimeout("fadeDiv("+dialogs[ind].fadediv.index+","+fadeTime+")",hideWait);
	}
	else if (dialogs[ind].autoHide) {
		dialogs[ind].hideId=setTimeout("hideDialog("+ind+")",hideWait);
	}
}

function hideCurDialog()
{
	hideDialog(curDialog);
}

function hideDialog(ind)
{
	if (dialogs[ind].hideId!=-1)
		clearTimeout(dialogs[curDialog].hideId);
	hideGreyout(dialogs[ind].greyBlank);
	var gdiv=document.getElementById(dialogs[ind].name);
	gdiv.className="dialog";
	gdiv.style.left="0px";
	gdiv.style.top="0px";
	
	curDialog=-1;
}

function showOneb_Dialog(toptxt,middletxt,b1href,b1txt)
{
	var twob_top=document.getElementById("oneb_top");
	var twob_middle=document.getElementById("oneb_middle");
	var twob_b1=document.getElementById("oneb_b");
	twob_top.innerHTML=toptxt;
	twob_middle.innerHTML=middletxt;
	
	while (twob_b1.firstChild)
		twob_b1.removeChild(twob_b1.firstChild);
	if (b1href!="") {
		var lnk=document.createElement("a");
		lnk.href=b1href;
		lnk.className="nolink";
		var spn=document.createElement("span");
		spn.className="button_inner";
		spn.appendChild(document.createTextNode(b1txt));
		lnk.appendChild(spn);
		twob_b1.appendChild(lnk);
	}
	else {
		var spn=document.createElement("span");
		spn.className="button_inner";
		spn.appendChild(document.createTextNode(b1txt));
		spn.onclick=hideOneb_Dialog;
		twob_b1.appendChild(spn);
	}
	
	showDialog(oneb_Dialog);
}

function hideOneb_Dialog()
{
	hideDialog(oneb_Dialog);
}

function showTwob_Dialog(toptxt,middletxt,b1href,b1txt,b2href,b2txt)
{
	var twob_top=document.getElementById("twob_top");
	var twob_middle=document.getElementById("twob_middle");
	var twob_b1=document.getElementById("twob_b1");
	var twob_b2=document.getElementById("twob_b2");
	twob_top.innerHTML=toptxt;
	twob_middle.innerHTML=middletxt;
	
	while (twob_b1.firstChild)
		twob_b1.removeChild(twob_b1.firstChild);
	if (b1href!="") {
		var lnk=document.createElement("a");
		lnk.href=b1href;
		lnk.className="nolink";
		var spn=document.createElement("span");
		spn.className="button_inner";
		spn.appendChild(document.createTextNode(b1txt));
		lnk.appendChild(spn);
		twob_b1.appendChild(lnk);
	}
	else {
		var spn=document.createElement("span");
		spn.className="button_inner";
		spn.appendChild(document.createTextNode(b1txt));
		spn.onclick=hideTwob_Dialog;
		twob_b1.appendChild(spn);
	}
	
	while (twob_b2.firstChild)
		twob_b2.removeChild(twob_b2.firstChild);
	if (b2href!="") {
		var lnk=document.createElement("a");
		lnk.href=b2href;
		lnk.className="nolink";
		var spn=document.createElement("span");
		spn.className="button_inner";
		spn.appendChild(document.createTextNode(b2txt));
		lnk.appendChild(spn);
		twob_b2.appendChild(lnk);
	}
	else {
		var spn=document.createElement("span");
		spn.className="button_inner";
		spn.appendChild(document.createTextNode(b2txt));
		spn.onclick=hideTwob_Dialog;
		twob_b2.appendChild(spn);
	}
	
	showDialog(twob_Dialog);
}

function hideTwob_Dialog()
{
	hideDialog(twob_Dialog);
}

function showImageDialog(src)
{
	var img=document.getElementById("image_imageDialog");
	img.src=src;
	
	showDialog(imageDialog);
}

function showIFrameDialog(src,width,height)
{
	var ifrm=document.getElementById("dialogIframe");
	ifrm.src=src;
	ifrm.width=width;
	ifrm.height=height;
	iframeWidth=width;
	iframeHeight=height;
	
	showDialog(iframeDialog);
}
