//
// Popup functions
//
var _W=window,_D=document,is,op,popupEl,nrSteps=15,dragObj,dragX,dragY,dragZ=1,close_n=new Image(13,13),close_h=new Image(13,13);

function getWinSize(what) {
	if (what == 'height')
		return (is.ie) ? (is.ieBox) ? _D.body.clientHeight : _D.documentElement.clientHeight : _W.innerHeight;
	if (what == 'width')
		return (is.ie) ? (is.ieBox) ? _D.body.clientWidth : _D.documentElement.clientWidth : _W.innerWidth;
}

function dragStart(e,el) {
	if (!e)e=_W.event;
	dragObj = el;
	//dragObj.style.zIndex = dragZ++;
	dragX = e.clientX - dragObj.offsetLeft;
	dragY = e.clientY - dragObj.offsetTop;
	_D.onmousemove=dragMove;
	_D.onselectstart=function(){return false};
}


function dragMove(e) {
	if (!e)e=_W.event;
	if(dragObj) {
		sX(dragObj,e.clientX - dragX);
		sY(dragObj,e.clientY - dragY);
	}
}

/* stop draggen en plakken */
function dragStop(e, el) {
	dragObj = null;
}

var mouseDown = false;
function makePopup(e, width, height, color, title, url) {
	killPopup(1);

	// begin achtergrond
	//var objOverlay = _D.getElementById('overlay');
	//objOverlay.setAttribute('ondblclick', 'killPopup()');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	//objOverlay.style.height = ((arrayPageSize[1]) + 'px');
	//objOverlay.style.display = '';

	// Disable select-boxes.
   var x, n, v, f, sels;
   if (is.ie && !is.ie7) {
      sels = _D.getElementsByTagName('select');
      for(x in sels) {
         n = sels[x].name, v = sels[x].value, f = sels[x].form;
         if (n && f) {
            f.insertAdjacentHTML('beforeEnd', '<input type="hidden" name="'+n+'" value="'+v+'" />');
         }
         sels[x].disabled = true;
      }
   }

	// end achtergrond

	height += 24;
	var wh = getWinSize('height'), ww = getWinSize('width'), st, sl;
	
	if (!e) {
		e = new Object();
		e.clientX = ww/2-width/2;
		e.clientY = wh/2-height/2;
	}
	
	var top = e.clientY, left = e.clientX;
	if (is.ie) {
		sl = (is.ieBox) ? _D.body.scrollLeft : _D.documentElement.scrollLeft;
		st = (is.ieBox) ? _D.body.scrollTop  : _D.documentElement.scrollTop;
	}
	else {
		sl = _W.pageXOffset;
		st = _W.pageYOffset;
	}
	if (e.clientX + width > ww)
		left = ww-width-5;
	if (e.clientY + height > wh)
		top = wh-height-12;

	// fix to make sure users can close the popup
	top = top < 0 ? 0 : top;

	popupEl = _D.createElement('div');
	popupEl.style.zIndex = 99;
	popupEl.id = 'popup';
	popupEl.onmousedown=function(event){mouseDown=true;dragStart(event,this)};

	/* dragstop */
	popupEl.onmouseup=function(event){mouseDown=false;dragStop(event, this)};
	popupEl.className = 'popup';
	_D.body.appendChild(popupEl);
	sizePopup(title,url,left+sl,top+st,width,height,color,nrSteps);
	
	return false;
}

function sizePopup(title,url,left,top,width,height,color,step) {
	// New and improved: with slomogrow effect!
	var html, mouse;
	if (step < 0) {
		popupEl.style.border='none';
		if (is.ie) {
			sW(popupEl, width+2);
			sH(popupEl, height+2);
		}

		html  = '<table cellpadding="0" cellspacing="0" border="0" id="popup" ondblclick="killPopup()">';
		html += '<tr>';
		html += '<td class="titlebar" bgcolor="'+color+'" style="padding-left:6px; cursor: pointer;"><div style="float:left; margin-top: 2px; color:#fff;">'+title+'</div><a style="float:right" href="#" onclick="killPopup(); return false"><img src="/pics/uvsv/cross.gif" alt="SLUIT" border="0" /></a></td>';
		html += '</tr>';
		html += '<tr style="background: #7F7F7F;">';
		html += '<td class="iframe">';
		html += '<iframe frameborder="0" src="'+url+'" style="width:'+width+'px;height:'+(height-24)+'px" scrolling="NO"></iframe>';
		html += '</td>';
		html += '</tr>';
		html += '</table>';
		wH(popupEl, html);
		return;
	}
	sX(popupEl, left + step*width/nrSteps/2);
	sY(popupEl, top + step*height/nrSteps/2);
	sW(popupEl, width - step*width/nrSteps);
	sH(popupEl, height - step*height/nrSteps + (is.safari?2:0));
	setTimeout("sizePopup('"+title+"','"+url+"',"+left+","+top+","+width+","+height+",'"+color+"',"+(step-1)+")",0);
}

function killPopup(now) {
	if (popupEl) {
		var code = '_D.body.removeChild(popupEl);popupEl=null;';
		if (now)
			eval(code);
		else
			setTimeout(code,0);
	}

	// Enable select-boxes.
   var x, n, v, f, sels;
   if (is.ie) {
      sels = _D.getElementsByTagName('select');
      for(x in sels) {
         n = sels[x].name, v = sels[x].value, f = sels[x].form;
         if (n && f) {
            f.insertAdjacentHTML('beforeEnd', '<input type="hidden" name="'+n+'" value="'+v+'" />');
         }
         sels[x].disabled = false;
      }
   }

	// gE('overlay').style.display = 'none';
}

// meuk voor achtergrond
/*
	Returns array with x,y page scroll values.
	Code from quirksmode.org
*/
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (_D.documentElement && _D.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = _D.documentElement.scrollTop;
	} else if (_D.body) {// all other Explorers
		yScroll = _D.body.scrollTop;
	}

	var arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

/*
	Returns array with page width, height and window width, height
	Code from - quirksmode.org
	Edit for Firefox by pHaez
*/
function getPageSize(){
	var xScroll, yScroll, pageWidth, pageHeight;
	
	if (_W.innerHeight && _W.scrollMaxY) {	
		xScroll = _D.body.scrollWidth;
		yScroll = _W.innerHeight + _W.scrollMaxY;
	} else if (_D.body.scrollHeight > _D.body.offsetHeight){ // all but Explorer Mac
		xScroll = _D.body.scrollWidth;
		yScroll = _D.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = _D.body.offsetWidth;
		yScroll = _D.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (_D.documentElement && _D.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = _D.documentElement.clientWidth;
		windowHeight = _D.documentElement.clientHeight;
	} else if (_D.body) { // other Explorers
		windowWidth = _D.body.clientWidth;
		windowHeight = _D.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
