// This field indicates if there is any pop-up open.
var popUpInUse;
// This field is the overlay for modal windows
var overlay;
// This field has a regularly changing reference to the body
var body=xGetElementsByTagName('body')[0];


// Create a pop up for the overlay from xml file at the given url
//
// URL of pop source file
// returns new popup element
function createOverlayPopUpFromXml(url){

	initOverlay();
	var d=xCreateElement('div');
	//xInnerHtml(d,getRequest(url));
	//new Ajax.Request(url,{method:"get",onComplete:function(request){xInnerHtml(d,request.responseText);}});
	new Ajax.Updater("overlay",url,{method:"get",insertion: Insertion.Bottom
});
	//xAppendChild(overlay,d);
	//return d;
}

// overlay just mattes out the background
function initOverlay(){
	if(!overlay){
		overlay=xCreateElement('div');
		overlay.setAttribute('id','overlay');
		overlayVis=xCreateElement('div');
		overlayVis.setAttribute('id','overlayVis');
		xAddEventListener(overlayVis,'click',hidePopUp);
		xInnerHtml(overlayVis,'<div class="clear"/>');
		xAppendChild(overlay,overlayVis);
		xDisplay(overlay,'none');
		body.insertBefore(overlay,xFirstChild(body));
		
		//opera doesnt support opacity, just set overlay background transparent
		if (xOp7Up||xOp6Dn) {
			overlayVis.style.backgroundColor='transparent';
		}
	}
}

function showPopUp(popUpName){
	popUpInUse=popUpName;
	initOverlay();
	popUpResizeHandler();
	xDisplay(overlay,'block');
	xAddEventListener(window,'scroll',popUpScrollHandler);
	xAddEventListener(window,'resize',popUpResizeHandler);
}

// This will hide the overlay and the pop up in use.  
// NOTE: it assumes pop up has a "hide" method to close.
function hidePopUp(){
	//careful w id/names w hyphens inside an eval
	eval(popUpInUse).hide();
	xDisplay(overlay,'none');
	xRemoveEventListener(window,'scroll',popUpScrollHandler);
	xRemoveEventListener(window,'resize',popUpResizeHandler);
	popUpInUse=false;
	element = xGetElementById('overlay');
	element.parentNode.removeChild(element);
}

function popUpScrollHandler(){
	xTop(overlay,xScrollTop());
	xLeft(overlay,xScrollLeft());
}
function popUpResizeHandler(){
	xHeight(overlay,xHeight(body));
	xWidth(overlay,xWidth(body));
}




//code to absolutely position drop shadows
function applyDropShadow(e) {
	//todo:
	//dont set negative heights (ie when something set to display:none)
	//fix when applied to borders
	//get to work with fenster?
	e=xGetElementById(e);
	var x = xPageX(e);
	var y = xPageY(e);
	width = xWidth(e);
	height = xHeight(e);
	
	//where are we appending
	if (e.hasChildNodes()) {
		e2 = e;
		positioning = xGetComputedStyle(e2,'position');
		if (positioning=='relative'||positioning=='absolute') {	
			x=0;
			y=0;
		}
	} else {
		e2 = document.body;
	}
	
	zind = xGetComputedStyle(e2,'z-index');
	
	if (zind > 0) {
		zind = zind+1;
	} else {
		zind = 1;
	}
	
	//add corners
	topRightCorner = xCreateElement('IMG');
	topRightCorner.style.position = 'absolute';
	topRightCorner.style.zindex = zind;
	topRightCorner.src = '/static/img/shade-tr.png';
	topRightCorner.className = 'alpha';
	
	bottomRightCorner = xCreateElement('IMG');
	bottomRightCorner.style.position = 'absolute';
	bottomRightCorner.style.zindex = zind;
	bottomRightCorner.src = '/static/img/shade-br.png';
	bottomRightCorner.className = 'alpha';
	
	bottomLeftCorner = xCreateElement('IMG');
	bottomLeftCorner.style.position = 'absolute';
	bottomLeftCorner.style.zindex = zind;
	bottomLeftCorner.src = '/static/img/shade-bl.png';
	bottomLeftCorner.className = 'alpha';
	
	xAppendChild(e2, topRightCorner);
	xAppendChild(e2, bottomRightCorner);
	xAppendChild(e2, bottomLeftCorner);

	rightBgOffset = 10;
	rightBgWidth = 5;
	bottomBgOffset = 10;
	bottomBgHeight = 5;

	//create bottom and right shadows
	rightShadow = xCreateElement('DIV');
	rightShadow.className = 'shadow-right';
	rightShadow.style.width=rightBgWidth+'px';
	rightShadow.style.height=(height-rightBgOffset)+'px';
	rightShadow.style.zindex = zind;
	rightShadow.innerHTML = '&nbsp;';
	
	bottomShadow = xCreateElement('DIV');
	bottomShadow.className = 'shadow-bottom';
	bottomShadow.style.width=(width-bottomBgOffset)+'px';
	bottomShadow.style.height=bottomBgHeight+'px';
	bottomShadow.style.zindex = zind;
	bottomShadow.innerHTML = '&nbsp;';
	
	xAppendChild(e2, rightShadow);
	xAppendChild(e2, bottomShadow);
	
	//position everything
	xMoveTo(rightShadow,x+width,y+rightBgOffset);
	xMoveTo(bottomShadow,x+bottomBgOffset,y+height);
	xMoveTo(topRightCorner,x+width,y);
	xMoveTo(bottomRightCorner,x+width,y+height);
	xMoveTo(bottomLeftCorner,x,y+height);
	
}

// XFenLight based on XFenster:
// xFenster, Copyright 2004-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xFenLight(eleId, iniX, iniY, barId) // object prototype
{
  // Private Properties
  var me = this;
  var ele = xGetElementById(eleId);
  ele.hasShadow = false;
  var x, y, w, h, maximized = false;
  // Public Methods
  this.onunload = function()
  {
    if (xIE4Up) { // clear cir refs
      xDisableDrag(barId);
      me = ele = null;
    }
  }
  this.show = function()
  {
  	showSelects(false);
  	xDisplay(ele,'block');
	if (!ele.hasShadow) {
		applyDropShadow(ele);
		ele.hasShadow = true;
	}
  }

  this.hide = function()
  {
  	showSelects(true);
  	xDisplay(ele,'none');
 
  }
  // Private Event Listeners
  function barOnDrag(e, mdx, mdy)
  {
    xMoveTo(ele, xLeft(ele) + mdx, xTop(ele) + mdy);
  }
  function fenOnMousedown()
  {
    xZIndex(ele, xFenLight.z++);
  }
  // Constructor Code
  xFenLight.z++;
  xMoveTo(ele, iniX, iniY);
  xEnableDrag(barId, null, barOnDrag, null);
  ele.onmousedown = fenOnMousedown;
} // end xFenster object prototype

xFenLight.z = 100; // xFenster static property

function showSelects(isVisible,myClass) {
	if (xIE4Up) {
		if (myClass) {
			mySelects = xGetElementsByClassName(tabClass,null,'DIV');
		} else {
			mySelects = xGetElementsByTagName('SELECT');
		}
		for(i=0; i<mySelects.length; i++) {
			xVisibility(mySelects[i],isVisible);
		}
	}
}
function showChart() {
	var mychart = xGetElementById('chartPanel');
	xShow(mychart);
}
function hideChart() {
	var mychart = xGetElementById('chartPanel');
	xHide(mychart);
}

