// templates.js

// 1 is text, 2 is autoscroll, 3 is image
var bottomLayer=false;
var firstcall=true;
var bbheight=30;  // height of the bottom bar
var oldscreenx=-1;
var oldscreeny=-1;
var myWin="";

function openIt(n){
	absurl=document.URL;
	
	// remove everything after the last slash
	absurl=absurl.substr(0,absurl.lastIndexOf("/"));

	n=absurl+"/"+n;
	
	if(myWin=="" || myWin.closed || myWin.name==undefined){
		myWin=window.open(n,"Editing","width=500,height=500,resizeable");
	}
	else
	{
		myWin.close();
		myWin=window.open(n,"Editing","width=200,height=200");
	}
}

function initDivs(blayer){
	if(blayer>0)bottomLayer=true;
			
	firstcall=true;
	refreshDivs();
}

function refreshDivs(){
	// positions the divs and then calls itself again
	// this way the resize problems are solved.

	// get screen dimensions
	screenx=800; screeny=500;

	if(window.innerWidth){
		screenx=window.innerWidth;
		screeny=window.innerHeight;
	}
	else if(document.body.clientWidth){
		screenx=document.body.clientWidth;
		screeny=document.body.clientHeight;
	}
	
	if(oldscreenx==screenx && oldscreeny==screeny){
		setTimeout("refreshDivs()",1500);
		return;
	}
	
	oldscreenx=screenx;oldscreeny=screeny;
	
	offsetx=offsety=0;
	
	// get the offset for the menubar
	f=document.getElementById("navBar");
	
	
	if(orientation==2) // horizontal menu
	{
		if(f.offsetHeight)
			offsety=f.offsetHeight;
		else if(f.clip.height)
			offsety=f.clip.height;
			
	
		if(f.style.left)
			offsetx+=f.style.left;
		if(f.style.pixelLeft)
			offsetx+=f.style.pixelLeft;
		if(f.offsetLeft)
			offsetx+=f.offsetLeft;
		
	}
	else // vertical menu
	{
		if(f.offsetWidth)
			offsetx=f.offsetWidth;
		else if(f.clip.width)
			offsetx=f.clip.width;
			
		if(f.style.top)
			offsety+=f.style.top;
		if(f.style.pixelTop)
			offsety+=f.style.pixelTop;
		if(f.offsetTop)
			offsety+=f.offsetTop;
		
	}
	
	screenx-=offsetx;
	screeny-=offsety;
	
	
	if(bottomlayerpx>0){
		bbheight=bottomlayerpx;
	}
	else if(bottomlayerpercent>0){
		bottomheight=bottompx;
		bbheight=(screeny/100)*bottomlayerpercent;
	}

	// is the bottomlayer displayed?
	if(bottomLayer){
		h=bbheight;
		l=offsetx;
		w=screenx;
		screeny-=bbheight;
		t=offsety+screeny;
		setPosition("bottombar",l,t,w,h,3);
	}
	
	
	// we need the leftwidth, rightwidth, topheight, bottomheight - in PIXELS!!!
	
	var leftwidth,rightwidth,topheight,bottomheight;
	
	if(toppx>0){
		topheight=toppx;
		bottomheight=(screeny-topheight)-4*margins;
	}
	else if(bottompx>0){
		bottomheight=bottompx;
		topheight=(screeny-bottomheight)-4*margins;
	}
	else
	{
		topheight=(screeny/100)*toppercent;
		bottomheight=(screeny-topheight)-4*margins;
	}
	
	if(leftpx>0){
		leftwidth=leftpx;
		rightwidth=(screenx-leftwidth)-4*margins;
	}
	else if(rightpx>0){
		rightwidth=rightpx;
		leftwidth=(screenx-rightwidth)-4*margins;
	}
	else
	{
		leftwidth=(screenx/100)*leftpercent;
		rightwidth=(screenx-leftwidth)-4*margins;
	}
	
	var tl_x,tl_y,tr_x,tr_y,br_x,br_y,bl_x,bl_y;
	
	tl_x=bl_x=offsetx+margins;
	tl_y=tr_y=offsety+margins;
	tr_x=br_x=offsetx+leftwidth+3*margins;
	br_y=bl_y=offsety+topheight+3*margins;
	
	if(topleftheight!='NULL') // we have a top left height
	{		
		// set the bl_y correctly.
		bl_y=offsety+topleftheight+3*margins;
		
		// now set the bottomleftheight properly
		bottomleftheight=bottomheight+(topheight-topleftheight);
	}
	else if(bottomleftheight!='NULL') // or a bottom left
	{
		// set the top correctly
		bl_y+=(bottomheight-bottomleftheight);
		// set the top height
		topleftheight=topheight+(bottomheight-bottomleftheight+margins);
	}
	
	if(topleftwidth!='NULL')
	{
		// set the tr_x correctly
		tr_x=offsetx+topleftwidth+3*margins;
		
		// now the toprightwidth property
		toprightwidth=rightwidth+(leftwidth-topleftwidth);
	}
	else if(toprightwidth!='NULL')
	{
		// set the x
		tr_x+=(rightwidth-toprightwidth);
		
		// set the tl width
		topleftwidth=leftwidth+(rightwidth-toprightwidth+margins);
	}
	
	if(bottomleftwidth!='NULL')
	{
		br_x=offsetx+bottomleftwidth+3*margins;
		bottomrightwidth=rightwidth+(leftwidth-bottomleftwidth);
	}
	else if(bottomrightwidth!='NULL')
	{
		br_x+=(rightwidth-bottomrightwidth);
		bottomleftwidth=leftwidth+(rightwidth-bottomrightwidth+margins);
	}
	
	if(toprightheight!='NULL')
	{
		br_y=offsety+toprightheight+3*margins;
		bottomrightheight=bottomheight+(topheight-toprightheight);
		
	}
	else if(bottomrightheight!='NULL')
	{
		br_y+=(bottomheight-bottomrightheight);
		toprightheight=topheight+(bottomheight-bottomrightheight+margins);
	}
	
	if(wholearea>0){
		t=offsety+margins;
		l=offsetx+margins;
		w=screenx-2*margins;
		h=screeny-2*margins;

		setPosition("wholearea",l,t,w,h,wholearea);
	}
	
	if(left>0){		
		l=offsetx+margins;
		w=leftwidth;
		t=offsety+margins;
		h=screeny-2*margins;
		setPosition("left",l,t,w,h,left);
	}
	if(right>0){
		t=offsety+margins;
		h=screeny-2*margins;
		l=offsetx+leftwidth+3*margins;
		w=rightwidth;
		
		setPosition("right",l,t,w,h,right);
	}
	if(top>0){
		h=topheight;
		w=screenx-2*margins;
		l=offsetx+margins;	
		t=offsety+margins;
		
		setPosition("top",l,t,w,h,top);
	}
	if(bottom>0){
		h=bottomheight;
		t=offsety+topheight+3*margins;
		w=screenx-2*margins;
		l=offsetx+margins;
		
		setPosition("bottom",l,t,w,h,bottom);
	}
	if(topleft>0){
		t=tl_y;
		l=tl_x;
		w = topleftwidth=='NULL' ? leftwidth : topleftwidth;
		h = topleftheight=='NULL' ? topheight : topleftheight;
		
		setPosition("topleft",l,t,w,h,topleft);		
	}
	if(topright>0){
		t=tr_y;
		l=tr_x;
		w = toprightwidth=='NULL' ? rightwidth : toprightwidth;
		h = toprightheight=='NULL' ? topheight : toprightheight;
		
		setPosition("topright",l,t,w,h,topright);
	}
	if(bottomright>0){
		t=br_y;
		l=br_x;
		w = bottomrightwidth=='NULL' ? rightwidth : bottomrightwidth;
		h = bottomrightheight=='NULL' ? bottomheight : bottomrightheight;
		
		setPosition("bottomright",l,t,w,h,bottomright);
	}
	if(bottomleft>0){
		t=bl_y;
		l=bl_x;
		w = bottomleftwidth=='NULL' ? leftwidth : bottomleftwidth;
		h = bottomleftheight=='NULL' ? bottomheight : bottomleftheight;
		setPosition("bottomleft",l,t,w,h,bottomleft);		
	}
		
	
	if(firstcall)
//		clickMenu();
		
	firstcall=false;
	setTimeout("refreshDivs()",1500);
}

function isLoaded(n){
	var isAutoScroller=false;
	// go through all the autoscrollers and set whichLayerIsTop to 0
	for(c=0;c<autoscrollerlayers.length;c++){
		whichLayerIsTop[c]=0;
		
		if(autoscrollerlayers[c]==n)
			isAutoScroller=true;
	}
	
	f=document.getElementById(n+"Div");
	f1=frames[n+'Frame'].document.body.innerHTML;
	f.innerHTML=f1;
	f.style.top=0; // reset to 0 in case it's a scroller!
	
	if(isAutoScroller){
		// set the second layer
		
		iT=findHeight(n+"Div");
		iT=parseInt(iT);
		
		// check to ensure that the height of the box is less than the text - ie it needs to scroll
		oH=findHeight(name+"Container");
		oH=parseInt(oH);
		
		if(iT<=oH) // we won't be scrolling this one, so move it off the bottom!
			iT=oH*2;
			
		f=document.getElementById(n+"Div2");
		f.innerHTML=f1;
		f.style.top=iT; // off the bottom!
		

	}
	
	
}

function isLoggedOut(){
	currentMenu=homeMenu;
	currentItem=homeItem;
	nme=homePageIsNamed;
	
	clickMenu(nme);
}

function setPosition(name,l,t,w,h,type){		
		f=document.getElementById(name+"Container").style;
		f.top=t;
		f.left=l;
		f.width=w;
		f.height=h;
		
		
		if (f.clip.right){
			f.clip.right = w;
			f.clip.bottom = h; 
			f.clip.left=0;
			f.clip.top=0;
		}
		else 
			f.clip = 'rect(0 ' + w + ' ' + h + ' 0)';
				
			
		
		if(firstcall)
			f.visibility="visible";
			
		
		// has it got scroll bars?
		if(type==1) // yes!
		{	
			t=t+h-arrowHeightJS;
			l=l+w-arrowWidthJS;
			f=document.getElementById(name+"Scroller").style;

			f.top=t;
			f.left=l;
			if(firstcall)
				f.visibility="visible";
		}

}

function internalLink(n){
	// n is the name - go through our nameLinks array and compare
	ci=-1;cm=-1;
	for(c=0;c<nameLinks.length;c++){
		// is the name the same?
		if(nameLinks[c] [0]==n){
			ci=nameLinks[c] [1];
			cm=nameLinks[c] [2];
			break;
		}
	}
	
	if(ci>=0 && cm>=0) // got one!
	{
		currentMenu=cm;
		currentItem=ci;
		clickMenu(n);
	}
}

function createPopup(urlname,width,height){
	window.open(urlname,"theWindow","width="+width+", height="+height+", location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbars=no");
}
