var sfInterval = 0;
var opacity;
function newWindow(uri, post, after) {
	var divShader = document.createElement('div');
	divShader.setAttribute('id', 'shader');
	divShader.className='shade';
	var divFull = document.createElement('div');
	divFull.setAttribute('id', 'full');
	divFull.className='full';
	var divPopup = document.createElement('div');
	divPopup.setAttribute('id', 'popup');
	divPopup.className='popup';
	var divPopupControls = document.createElement('div');
	divPopupControls.setAttribute('id', 'popupcontrols');
	divPopupControls.className = 'controls';
	var divPopupControlsText = document.createTextNode('');
	divPopupControls.appendChild(divPopupControlsText);
	divPopup.appendChild(divPopupControls);
	var divPopupContent = document.createElement('div');
	divPopupContent.setAttribute('id', 'popupcontent');
	divPopupContent.className = 'content';
	var divPopupContentText = document.createTextNode('');
	divPopupContent.appendChild(divPopupContentText);
	divPopup.appendChild(divPopupContent);
	divFull.appendChild(divPopup);
	window.document.body.appendChild(divShader);
	window.document.body.appendChild(divFull);
	divShader.style.visibility = 'visible';
	divShader.style.display = 'block';
	setOpacity('shader', 0);
	var s = 0;
	if (self.pageYOffset){
		s = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { 
		s = document.documentElement.scrollTop; 
	} else if (document.body) { 
		s = document.body.scrollTop; 
	}
	divFull.style.top = s+'px';
	divShader.style.top = s+'px';
	centerDiv('popup');
	sfInterval = setInterval("fadein('shader', '"+uri+"', '"+post+"', '"+after+"');", 100);
	window.onscroll = function() {
		var s = 0;
		if (self.pageYOffset){
			s = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) { 
			s = document.documentElement.scrollTop; 
		} else if (document.body) { 
			s = document.body.scrollTop; 
		}
		divFull.style.top = s+'px';
		divShader.style.top = s+'px';
		centerDiv('popup');
	}
}
function fadein(id, uri, post, after) {
	obj = document.getElementById(id).style;
	opacity = parseFloat(obj.opacity);
	if (opacity < 0.8) {
		opacity += 0.1; 
		setOpacity(id, opacity*100);
	} else {
		// let's display the popup window....
		document.getElementById('popupcontrols').innerHTML = '<a href="javascript: void(0);" onclick="closeWindow(\'\');">Close [X]</a>';
		win = document.getElementById('popup');
		win.style.display = 'block';
		centerDiv('popup');	
		win.style.visibility = 'visible';
		clearInterval(sfInterval);
		loadpage('popupcontent', uri, post, after);
	}
}
function fadeout(id, after) {
	obj = document.getElementById(id).style;
	opacity = parseFloat(obj.opacity);
	if (opacity > 0.0) {
		opacity -= 0.1; 
		setOpacity(id, opacity*100);
	} else {
		clearInterval(sfInterval);
		divShader = document.getElementById('shader');
		divShader.parentNode.removeChild(divShader);
		eval(after);
	}
}
function setOpacity(id, opacity) {
	obj = document.getElementById(id).style;
    obj.opacity = (opacity / 100);
    obj.MozOpacity = (opacity / 100);
    obj.KhtmlOpacity = (opacity / 100);
    obj.filter = "alpha(opacity=" + opacity + ")"; 
}
function closeWindow(after) {
	// Let's remove all the window elements...
	divPopupControls = document.getElementById('popupcontrols');
	divPopupControlsText = divPopupControls.firstChild;
	divPopupControls.removeChild(divPopupControlsText);
	divPopupControls.parentNode.removeChild(divPopupControls);
	divPopupContent = document.getElementById('popupcontent');
	divPopupContentText = divPopupContent.firstChild;
	divPopupContent.removeChild(divPopupContentText);
	divPopupContent.parentNode.removeChild(divPopupContent);
	divFull = document.getElementById('full');
	divPopup = document.getElementById('popup');
	divPopup.parentNode.removeChild(divPopup);
	divFull.parentNode.removeChild(divFull);
	sfInterval = setInterval("fadeout('shader', '"+after+"');", 100);
	window.onscroll = function() { }
}
/***************************************************/
/* JavaScript functions for centering a div        */
/***************************************************/
function f_clientWidth() {
        return f_filterResults (
                window.innerWidth ? window.innerWidth : 0,
                document.documentElement ? document.documentElement.clientWidth : 0,
                document.body ? document.body.clientWidth : 0
        );
}
function f_clientHeight() {
        return f_filterResults (
                window.innerHeight ? window.innerHeight : 0,
                document.documentElement ? document.documentElement.clientHeight : 0,
                document.body ? document.body.clientHeight : 0
        );
}
function f_filterResults(n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
                n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function centerDiv(id) {
        var divWidth, divHeight;
        var winWidth, winHeight;
        var x, y;
        obj = document.getElementById(id);
        obj.style.position = 'absolute';
        divWidth = obj.offsetWidth;
        divHeight = obj.offsetHeight;
        winWidth = f_clientWidth();
        winHeight = f_clientHeight();
        x = (winWidth / 2) - (divWidth / 2);
        y = (winHeight / 2) - (divHeight / 2);
        obj.style.left = x+'px';
        obj.style.top = y+'px';
}

