function addWin(parameters)
{
win =
{winId:null,appId:null,title:"",icon:"",width:400,height:300,oldWidth:0,oldHeight:0,minWidth:100,minHeight:100,
x:0,y:0,z:0,oldx:0,oldy:0,maximized:false,allowResize:true,onTop:false,unLoad:true,focussed:false,
winResizeEvent:0,backColor:"#ffffff",opacity:1,ready:false,

draw:function()
{
testtt=document.createElement("DIV");
temp="<table width=100% border=0 cellpadding=0 cellspacing=0><tr height=#th><td width=#w style=\"background-image:url(#dir/tl.png)\" nowrap></td><td width=100% id=topbar## style=\"background-image:url(#dir/tm.png);\"></td><td width=#w style=\"background-image:url(#dir/tr.png)\" nowrap></td></tr></table><table width=100% border=0 cellpadding=0 cellspacing=0><tr><td width=#mw style=\"background-image:url(#dir/l.png)\" nowrap></td><td align=center height=100% width=100%><div id=$$stretch$$## style=\"background:#bc\"></div></td><td width=#mw style=\"background-image:url(#dir/r.png)\" nowrap></td></tr></table><table width=100% border=0 cellpadding=0 cellspacing=0><tr height=#bh><td width=#bw style=\"background-image:url(#dir/bl.png)\" nowrap></td><td width=100% style=\"background-image:url(#dir/bm.png)\"></td><td width=#bw style=\"background-image:url(#dir/br.png);\" nowrap></td></tr></table><div id=$$display$$## style=\"position:absolute;left:0px;top:0px;overflow:hidden;\"></div><img id=$$shield$$## class=shield onmousedown=\"setFocus(##);if(gecko){event.preventDefault()}\" src='img/spacer.gif' alt=''>";


//temp="<img style='position:absolute;left:0px;top:0px;width:#w;height:#th;' src='#dir/tl.png'><img style='position:absolute;right:0px;top:0px;width:#w;height:#th;' src='#dir/tr.png'><div id=topbar## style='background-image:url(#dir/tm.png);height:#th;margin-left:#w;margin-right:#w;'></div><div id=$$left$$## style='position:absolute;left:0px;top:#th;width:#mw;background-image:url(#dir/l.png);'></div><div id=$$right$$## style='position:absolute;right:0px;top:#th;width:#mw;background-image:url(#dir/r.png);'></div><div style='position:absolute;bottom:0px;width:100%;background-image:url(#dir/bm.png);height:#bh;margin-left:#bw;margin-right:#bw;background-repeat:none;'></div><img style='position:absolute;left:0px;bottom:0px;width:#bw;height:#bh;' src='#dir/bl.png'><img style='position:absolute;right:0px;bottom:0px;width:#bw;height:#bh;' src='#dir/br.png'><div id=$$display$$## style=\"background:#fff;position:absolute;left:0px;top:0px;overflow:hidden;\"></div><img id=$$shield$$## class=shield onmousedown=\"setFocus(##);if(gecko){event.preventDefault()}\" src='img/spacer.gif' alt=''>";


temp += "<div id=title## ondblclick=\"windows[##].maximize()\" onmousedown=\"dragInit(event,##)\" class=title style=\"z-index:999999;-moz-user-select:none;";
if (skin.titleStyle){temp += skin.titleStyle;}
temp += "\"></div>";

temp += "<img height=#is width=#is src=\"#dir/winicon.gif\" id=winIcon## style=\"position:absolute;z-index:999999;";
if (skin.iconStyle){temp += skin.iconStyle;}
temp += "\">";

if ((parameters) && (parameters.makeClose===false)){}else
{
temp += "<img onmouseup=\"winClose('##')\" src='#dir/close.png' onMouseOut=\"this.src='#dir/close.png'\" onMouseOver=\"this.src='#dir/closeover.png'\" style=\"position:absolute;z-index:999999;cursor:pointer;MozOpacity:1;";          
if (skin.closeStyle){temp += skin.closeStyle;}
temp += "\">";
}

if ((parameters) && (parameters.makeMin===false)){}else
{
temp += "<img src='#dir/min.png' onMouseOut=\"this.src='#dir/min.png'\" onMouseOver=\"this.src='#dir/minover.png'\" onclick=\"winMinimize("+this.winId+")\" style=\"position:absolute;z-index:999999;cursor:pointer;";
if (skin.minStyle){temp += skin.minStyle;}
temp += "\">";
}

if ((parameters) && (parameters.makeMax===false)){}else
{
temp += "<img src='#dir/max.png' onMouseOut=\"this.src='#dir/max.png'\" onMouseOver=\"this.src='#dir/maxover.png'\" onclick=\"windows[##].maximize()\" style=\"position:absolute;z-index:999999;cursor:pointer;";
if (skin.maxStyle){temp += skin.maxStyle;}
temp += "\">";
}

if ((parameters) && (parameters.allowResize===false)){}else
{
temp += "<img src='#dir/resize.png' onmousedown=\"resizeInit(event, '##')\" style=\"position:absolute;z-index:999999;cursor:nw-resize;";
if (skin.resizeStyle){temp += skin.resizeStyle;}
temp += "\">";
}

if (isNaN(skin.midWidth)){skin.midWidth=skin.width;}
if (isNaN(skin.bottomWidth)){skin.bottomWidth=skin.width;}

temp = temp.replace(/##/g,this.winId+"");
temp = temp.replace(/#w/g,skin.width);
temp = temp.replace(/#mw/g,skin.midWidth);
temp = temp.replace(/#bw/g,skin.bottomWidth);
temp = temp.replace(/#th/g,skin.topHeight);
temp = temp.replace(/#bh/g,skin.bottomHeight);
temp = temp.replace(/#is/g,skin.iconSize);
temp = temp.replace(/#maxX/g,skin.maxX);
temp = temp.replace(/#maxY/g,skin.maxY);
temp = temp.replace(/#dir/g,"skins/"+skin.name);
temp = temp.replace(/#bc/g,"#ffffff");


testtt.innerHTML=temp;
testtt.id="$$frame$$"+this.winId;
testtt.style.display="none";
testtt.style.border="none";
testtt.style.position="absolute";
testtt.style.overflow="hidden";
return(testtt);
},

redraw:function()
{
disp=document.getElementById("$$display$$"+this.winId).innerHTML;
tit=document.getElementById("title"+this.winId).innerHTML;
ico=document.getElementById("winIcon"+this.winId).src;

testtt=this.draw();

document.getElementById("$$frame$$"+this.winId).innerHTML=testtt.innerHTML;
document.getElementById("$$display$$"+this.winId).innerHTML=disp;
document.getElementById("title"+this.winId).innerHTML=tit;
document.getElementById("winIcon"+this.winId).src=ico;
this.resize(this.width,this.height);

if (document.getElementById("$$frame$$"+this.winId).style.zIndex<zTop)
{
document.getElementById("$$shield$$"+this.winId).style.zIndex=999998;
}
},


setTitle:function(winTitle)
{
this.title=winTitle;
document.getElementById("title"+this.winId).innerHTML=winTitle;
},
setContents : function(contents)
{
document.getElementById("$$display$$"+this.winId).innerHTML=contents;
},
show : function()
{
document.getElementById(this.winId+"dockIcon").parentNode.style.display="block";
if ((transitions)&&(!this.ready)){
if (this.width>this.height){wid=this.height;y=this.y;x=this.x+((this.width-wid)/2);}
else{wid=this.width;x=this.x;y=this.y+((this.height-wid)/2);}
anim.x1=parseInt(document.getElementById(this.winId+"dockIcon").style.left)+parseInt(dockbar.style.left)+parseInt(document.getElementById(this.winId+"dockIcon").style.width)/2-24;
anim.y1=25;
anim.w1=48;
anim.h1=48;
anim.x2=x;
anim.y2=y+50;
anim.w2=wid;
anim.h2=wid;
anim.winId=this.winId
if (!this.onTop){box.style.zIndex=this.z;}else{box.style.zIndex=999997;}
box.src=this.icon;
newAnim();
}
else{
this.ready=true;
document.getElementById("$$display$$"+this.winId).style.display="block";
document.getElementById("$$frame$$"+this.winId).style.display="block";
fireEvent("onWinResize"+this.winId,this.width,this.height);
}
},
move : function(x,y)
{
if (this.maximized){return;}
this.x=x;this.y=y;
document.getElementById("$$frame$$"+this.winId).style.left=x+"px";
document.getElementById("$$frame$$"+this.winId).style.top=y+50+"px";
},
resize : function(wd,ht)
{
if (this.allowResize){
//if (wd<this.minWidth){wd=this.minWidth}
//if (ht<this.minHeight){ht=this.minHeight}
this.width=wd;
this.height=ht;
document.getElementById("$$frame$$"+this.winId).style.width=wd+(skin.midWidth*2)+"px";
document.getElementById("$$frame$$"+this.winId).style.height=ht+skin.bottomHeight+skin.topHeight+"px";

document.getElementById("$$stretch$$"+this.winId).style.width=wd+"px";
document.getElementById("$$stretch$$"+this.winId).style.height=ht+"px";
//document.getElementById("$$left$$"+this.winId).style.height=ht+"px";
//document.getElementById("$$right$$"+this.winId).style.height=ht+"px";

document.getElementById("$$display$$"+this.winId).style.left=skin.midWidth+"px";
document.getElementById("$$display$$"+this.winId).style.top=skin.topHeight+"px";
document.getElementById("$$display$$"+this.winId).style.width=wd+"px";
document.getElementById("$$display$$"+this.winId).style.height=ht+"px";
document.getElementById("$$shield$$"+this.winId).style.left="0px";
document.getElementById("$$shield$$"+this.winId).style.top="0px";
document.getElementById("$$shield$$"+this.winId).style.width=wd+(skin.midWidth*2)+"px";
document.getElementById("$$shield$$"+this.winId).style.height=ht+skin.bottomHeight+skin.topHeight+"px";

document.getElementById("title"+this.winId).style.width=(wd-parseInt(document.getElementById("title"+this.winId).style.left))+"px";

fireEvent("onWinResize"+this.winId,this.width,this.height,this.winId);
}
},

maximize : function()
{
if (this.allowResize)
{
if (!this.maximized)
{
this.oldWidth=this.width;
this.oldHeight=this.height;
this.oldx=this.x;
this.oldy=this.y;
this.move(0-skin.midWidth,-2);
this.resize(document.body.clientWidth,document.body.clientHeight-48-skin.topHeight);
this.maximized=true;
fireEvent("onMaximize"+this.winId);
}
else
{
this.maximized=false;
this.resize(this.oldWidth,this.oldHeight);
this.move(this.oldx,this.oldy);
fireEvent("onUnMaximize"+this.winId);
}
if (!this.onTop)
{
zTop++;
document.getElementById("$$frame$$"+this.winId).style.zIndex=zTop;
}

}

},
centre : function()
{
this.move((document.body.clientWidth-parseInt(document.getElementById("$$frame$$"+this.winId).style.width))/2,(document.body.clientHeight-50-parseInt(document.getElementById("$$frame$$"+this.winId).style.height))/2);
},
stayOnTop : function(bool)
{
this.onTop=bool;
if (bool){document.getElementById("$$frame$$"+this.winId).style.zIndex=999998;this.z=9999998;}
},

loseFocus : function()
{
if (this.focussed){
this.focussed=false;
fireEvent("onLoseFocus"+this.winId);
}
},

getFocus : function()
{
resetSomeStuff();
if (!this.focussed){
if ((currentWindow!=this.winId) && (windows[currentWindow])){windows[currentWindow].loseFocus();}
fireEvent("onGetFocus"+this.winId);
this.focussed=true;
currentWindow=this.winId;
}
shieldsUp();
currentWindow=this.winId;
if (!this.onTop)
{
if (this.z<zTop){zTop++;}
document.getElementById("$$frame$$"+this.winId).style.zIndex=zTop;
this.z=zTop;
document.getElementById("$$shield$$"+this.winId).style.zIndex=-1;
}
else
{
this.z=999998;
document.getElementById("$$frame$$"+this.winId).style.zIndex=999998;
}
},
setIcon : function(winIcon)
{
this.icon=winIcon;
document.getElementById("winIcon"+this.winId).src=winIcon;
document.getElementById(this.winId+"dockIcon").src=winIcon;
},
getElement : function(elem)
{
return(document.getElementById(elem));
}
};
//*************************************************

shieldsUp();

//winNumCount++;
winNumCount=getNewWinId();

debug.print("winOpen "+winNumCount);

win.winId=winNumCount;

if (parameters)
{
if (parameters.allowResize){win.allowResize=parameters.allowResize;}
if (parameters.backColor){win.backColor=parameters.backColor;}
}

testtt=win.draw();
desktop.appendChild(testtt);

win.resize(400,300);




if (parameters)
{
if (parameters.title){win.setTitle(parameters.title);}
if ((parameters.width) && (parameters.height)){win.resize(parameters.width,parameters.height);}
win.centre();
if ((parameters.x) && (parameters.y)){win.move(parameters.x,parameters.y);}
if (parameters.stayOnTop){win.stayOnTop(parameters.stayOnTop);}
if (parameters.centred){win.centre(parameters.centred);}
if (parameters.appId){win.appId=parameters.appId;}
if (!parameters.centred){preventWinOverlap(win);}
if (parameters.unLoad){win.appId=parameters.unLoad;}
if (parameters.maximized){win.maximize();}
if (parameters.minHeight){win.minHeight=parameters.minHeight;}
if (parameters.minWidth){win.minWidth=parameters.minWidth;}
}

if (win.y<0){win.move(win.x,0);}

if (document.getElementById("app"+win.appId)){
document.getElementById("dockbar").removeChild(document.getElementById("app"+win.appId).parentNode);
//drawDock();
}
testtt=document.createElement("DIV");
testtt.innerHTML="<img id="+winNumCount+"dockIcon onclick=\"winToggle("+winNumCount+")\" style=\"position:absolute;-moz-user-select:none;left:0px;top:0px;width:48px;height:48px;\">";
testtt.style.display="none";
dockbar.appendChild(testtt);
drawDock();

win.setIcon("icons/default.png");

if (parameters)
{
if (parameters.icon){win.setIcon(parameters.icon);}
}

windows[winNumCount]=win;
resetSomeStuff();


win.getFocus();

/*
if (win.width>win.height){wid=win.height;y=win.y;x=win.x+((win.width-wid)/2);}
else{wid=win.width;x=win.x;y=win.y+((win.height-wid)/2);}
anim.x2=x+(wid/2);
anim.y2=y+(wid/2)+50;
anim.w2=0;
anim.h2=0;
wid=wid*8;
anim.x1=0;
anim.y1=0;
anim.w1=document.body.clientWidth;
anim.h1=document.body.clientWidth;
box.style.zIndex=999998;
box.src=win.icon;
newAnim();
*/

return(win);
}

// stops new windows occupying same x,y as current windows(hopefully)
function preventWinOverlap(win)
{
var repeat=false;
for (var i in windows)
{
if ((i!=win.winId) && (windows[i]) && (windows[i].x==win.x) && (windows[i].y==win.y)){win.move(win.x+20,win.y+20);repeat=true;break;}
}
if (repeat){preventWinOverlap(win);}
}


function getNewWinId()
{
var index = windows.length;
for (var i=1; i<windows.length; i++)
{
if (!windows[i])
{
index=i;
break;
}
}
return(i);
}
