var currentWindow;
var SCROLLBAR_WIDTH=9;
var ADMINBAR_HEIGHT=0;
var FRAME_EDGE_WIDTH=30;
var BASE_ZINDEX=200;
var rCount=0;
var ignoreNextClose=false;

if(window.ie){
	SCROLLBAR_WIDTH=1;
}
else if(window.safari){
	SCROLLBAR_WIDTH=1;
}
else{
	SCROLLBAR_WIDTH=11;
}
var windowIndex=0;
var windowStack=new Array();

function FrameWindow(){
}


FrameWindow.prototype.reposition=function(){

  var c=document.getElementById(this.id+"Container");
  var b=document.getElementById(this.id+"Body");
  
  if (c != null) {
  
	var width,height,lowRes;
	lowRes = 0;
	if(this.maximized){
		width=getViewportWidth()-(SCROLLBAR_WIDTH*2);
		height=getViewportHeight();
	}
	else{
		width=this.width+(FRAME_EDGE_WIDTH*2);
		height=this.height+(FRAME_EDGE_WIDTH*2);
	}
	
	if(!this.maximized){
		var heightCandidate=height;
		if(height>(getViewportHeight()-ADMINBAR_HEIGHT)){
		    height = getViewportHeight()-ADMINBAR_HEIGHT;
		    lowRes = 1;
		}
	}
	c.style.height=(height)+"px";
	c.style.width=(width)+"px";
	if(!this.maximized){
		var topCandidate=(((getViewportHeight()-ADMINBAR_HEIGHT-height)/2)+getViewportScrollY()+ADMINBAR_HEIGHT);
		if(topCandidate<=(ADMINBAR_HEIGHT)){
			topCandidate=ADMINBAR_HEIGHT;
		}
		if (lowRes) {
		  topCandidate=getViewportScrollY();
		}
	}	else {
		topCandidate=getViewportScrollY();
	}
	c.style.top=topCandidate+"px";
	c.style.left=(((getViewportWidth()-width)/2)-SCROLLBAR_WIDTH)+"px";
     if (lowRes)  {
       this.bodyHeight=(height-(FRAME_EDGE_WIDTH));
     } else {
       //this.bodyHeight=Math.max((height-(FRAME_EDGE_WIDTH*2)),(587-2*topCandidate+2*getViewportScrollY()));
       this.bodyHeight=(height-(FRAME_EDGE_WIDTH*2));
     }
     
     
	this.bodyWidth=(width-(FRAME_EDGE_WIDTH*2));
	if(window.safari){
		document.getElementById(this.id+"h").style.height=(this.bodyHeight-(FRAME_EDGE_WIDTH*2))+"px";
		document.getElementById(this.id+"i").style.height=(this.bodyHeight-(FRAME_EDGE_WIDTH*2))+"px";
	}
	b.style.height=this.bodyHeight+"px";
	b.style.width=(this.bodyWidth+10)+"px";
	
  }
	
};


FrameWindow.prototype.show=function(){
	if (frames[this.id+"Body"] != null) frames[this.id+"Body"].location.href=this.url;
	if (document.getElementById(this.id+"Container") != null) {
		document.getElementById(this.id+"Container").style.display="";
		document.getElementById(this.id+"Container").style.visibility="visible";
		document.getElementById(this.id+"Container").style.zIndex=BASE_ZINDEX+this.windowIndex;
	}	
};



FrameWindow.prototype.hide=function(){
	document.getElementById(this.id+"Container").style.visibility="hidden";
	frames[this.id+"Body"].location.href="/js/framewindow-"+this.windowIndex+"-blank.html";
};

FrameWindow.prototype.toggleMaximized=function(){
	this.maximized=!this.maximized;
	this.reposition();
	if(window.ie){
		this.reposition();
    }
};

function openWindow(url,width,height,options){

	if (document.getElementById("MediaPlayer1")) {
    	document.getElementById("MediaPlayer1").style.visibility = "hidden";
  	}
	++windowIndex;

	nWindow=new FrameWindow();
	nWindow.id="window"+windowIndex;
	//nWindow.contentWindow=document.getElementById("window"+windowIndex+"Body").contentWindow;
	nWindow.parentWindow=currentWindow;
	nWindow.url=url;
	nWindow.width=width;
	nWindow.height=height;
	nWindow.windowIndex=windowIndex;
	nWindow.resizable=!(options&FRAMEWINDOW_FIXED);
	nWindow.maximized=false;
	nWindow.reposition();
	nWindow.show();
	document.getElementById("windowDimmer").style.height=(getViewportScrollY()+getViewportHeight())+"px";
	document.getElementById("windowDimmer").style.width="100%";
	document.getElementById("windowDimmer").style.display="block";
	currentWindow=nWindow;
	windowStack.push(nWindow);

}

function getUsingFrameWindows(){
	return true;
}

function getOpenerWindow(){
	if(currentWindow.parentWindow){
		return(currentWindow.parentWindow.contentWindow);
	}
	else{
		return(window);
    }
}

function closeSpecificWindow(id){
	if(id==windowIndex){
		closeWindow();
    }
}

function closeWindow(){
	if(windowIndex>0){
		currentWindow.hide();
		windowStack.pop();
		--windowIndex;

		if(windowStack.length>0){
			currentWindow=windowStack[windowStack.length-1];
		}
		else{
			document.getElementById("windowDimmer").style.display="none";
			currentWindow=null;
		}
	}
}

addEvent(window,"resize",
	 function()
	 {
		document.getElementById("windowDimmer").style.height=(getViewportScrollY()+getViewportHeight())+"px";
		for(var i=0;i<windowStack.length;++i){
			windowStack[i].reposition();
		}
	}
);

LAST_WINDOW_SCROLL=0;

function checkScroll(){
	if(LAST_WINDOW_SCROLL!=getWindowScroll()){
		framewindowScrollAdjust();
		LAST_WINDOW_SCROLL=getWindowScroll();
    }
}

setInterval("checkScroll();",10);

function framewindowScrollAdjust(){
	document.getElementById("windowDimmer").style.height=(getViewportScrollY()+getViewportHeight())+"px";
	for(var i=0;i<windowStack.length;++i){
		windowStack[i].reposition();
    }
}

addEvent(window,"scroll",framewindowScrollAdjust);

if(window.safari){
	addEvent(window,"load",
		function()
		{
			if(frames["window1Body"]){
				frames["window1Body"].location.href="/js/framewindow-1-blank.html";
			}
			if(frames["window2Body"]){
				frames["window2Body"].location.href="/js/framewindow-2-blank.html";
			}
			if(frames["window3Body"]){
				frames["window3Body"].location.href="/js/framewindow-3-blank.html";
			}
		});
}
