// written by: modusinc

//stores a list of MDIWindows

//sends an MDI child window to a location
function openMDIWin(page,params,tileWins) {

//!!!! might want to make this such that you can open windows in child windows as well

	//make sure we are at top window
	if (window.name==window.top.name) {
		if (!tileWins) tileWins=0;

		useExisting=false;

		//see whether we have a named window we should use
		for(j=0;j<wins;j++){
			if(oWin[j]!=null && oWin[j].pageRef==oPages.getUrl(page)) {
				useExisting=true;
				winHandle=j;
			}
		}

		//if its there, give it url and show, otherwise create
		if (useExisting) {
			oWin[winHandle].regwin();
			if (confirm('Leave page "'+oPages.getName(page)+'"?')) {
				eval('MDIFrame'+winHandle+'.document.location="'+oPages.getUrl(page)+'?'+params+'"');
			}
		} else {
			createMDIWin(page,params,tileWins);
		}
	} else { //window is not top, so call same function in parent window. loops until window.top is found
		window.parent.openMDIWin(page,params,tileWins);
	}
}

function closeMDIWin(id) {

	//if id is not given and we're not at the top, set the closeThis flag to true and call again in the parent to create a close action on this frame.
	//otherwise close whatever id is passed.
	if (isNaN(id)) {
		
		//make sure we're at the top. find window to close and do it.
		if (window.top.name==window.name) {
			//loop through frames to find which one we should close.
			for(j=0;j<wins;j++){
				if (eval('document.frames.MDIFrame'+j+'.document.closeThis')) {
					closeMDIWin(j);
				}
			}
			
		} else {
			document.closeThis=true;
			parent.closeMDIWin();
		}
	} else {
		//set the window object to null
		oWin[id]=null;

		//for(j=id;j<wins;j++){
		//	oWin[j]=oWin[j+1];
		//}
		//oWin[wins]=null;
		
		//subtract from window count
		--wins;

		//delete the actual div
		document.all.myWindows.removeChild(document.getElementById('divWin'+id));

		//delete the windowsboxlink
		document.all.myWindowsLinkBox.removeChild(document.getElementById('divWinLink'+id));

		bFixFocus=true;
	}
}

function createMDIWin(page,params,tileWins) {
	var aHtml = new Array();
	
	//set id to next in line of windows
	//id=wins;

	//set id to next in line of windows
	id=wins;
	//now grab the first free one if there is one.
	for(j=0;j<wins;j++){
		if (oWin[j]==null) {
			id=j;		
		}
	}

	aHtml[aHtml.length]='<div id="divWinHead'+id+'" class="clWinHead">&nbsp;'+oPages.getName(page)+'</div>';
	aHtml[aHtml.length]='<div id="divWinButtons'+id+'" class="clWinButtons">';
	aHtml[aHtml.length]='<map name="map'+id+'">';
	aHtml[aHtml.length]='<area shape="rect" coords="26,2,35,11" href="#" onclick="closeMDIWin('+id+'); return false">';
	aHtml[aHtml.length]='<area shape="rect" coords="14,2,23,11" href="#" onClick="oWin['+id+'].maximize(); return false">';
	aHtml[aHtml.length]='<area shape="rect" coords="2,2,11,11" href="#" onClick="oWin['+id+'].minimize(); return false">';
	aHtml[aHtml.length]='</map><img usemap="#map'+id+'" src="images/win_buttons.gif" width="38" height="14" alt="" border="0">';
	aHtml[aHtml.length]='</div><div id="divWinResize'+id+'" class="clWinResize"></div><div id="divWindow'+id+'" class="clWindow">';
	aHtml[aHtml.length]='<div id="divWinText'+id+'" class="clText">';
	aHtml[aHtml.length]='<IFRAME id="MDIFrame'+id+'" name="MDIFrame'+id+'" SRC="'+oPages.getUrl(page)+'" WIDTH=550 HEIGHT=400 scrolling="auto" frameborder="0">Browser Incompatibility. Please use IE 5.0 or above.</IFRAME>';
	aHtml[aHtml.length]='</div></div><div id="divWinUp'+id+'" class="clUp"><a href="#" onmouseover="winScroll=1; oWin['+id+'].down();"  onmouseout="noScroll()"><img src="images/win_arrow_up.gif" width="11" height="12" alt="" border="0"></a></div>';
	aHtml[aHtml.length]='<div id="divWinDown'+id+'" class="clDown"><a href="#" onmouseover="winScroll=1; oWin['+id+'].up();" onmouseout="noScroll()"><img src="images/win_arrow_down.gif" width="11" height="12" alt="" border="0"></a></div>';

	var oDiv = document.createElement('DIV');
	oDiv.id='divWin'+id;
	oDiv.className='clWin';

	document.all.myWindows.appendChild(oDiv);

	document.getElementById('divWin'+id).innerHTML=aHtml.join('');

	var startx=128;
	var starty=51;
	var dx=10;
	var dy=15;
	//shift max 10 windows, and shift by 10 pixels
	startx = startx+(id%10)*dx;
	starty = starty+(id%10)*dy;

	popItUp(oPages.getUrl(page),id,tileWins,WINXSIZE_DEFAULT,WINYSIZE_DEFAULT,startx,starty);

	//create an entry in the window bar
	var oDiv = document.createElement('DIV');
	oDiv.id='divWinLink'+id;
	oDiv.className='';

	document.all.myWindowsLinkBox.appendChild(oDiv);
	var strName = oPages.getName(page).substr(0,18);
	if (strName!=oPages.getName(page)) strName=strName+'..';
	document.getElementById('divWinLink'+id).innerHTML='<div class="windowsLinkBox"><a href="#" onclick="javascript:oWin['+id+'].restore(); return false;">'+strName+'</a></div>';

	//register it so it pops up front
	oWin[id].regwin();
	
	//add MDI ID to the window
	if (params) {
		params+='&MDIWinID='+id;
	} else {
		params='MDIWinID='+id;
	}
	//send the window to the correct location
	eval('MDIFrame'+id+'.document.location="'+oPages.getUrl(page)+'?'+params+'"');

}
