// Javascript file used for "PopUp CSS window
// Much of this code has been rewritten from the original
// Credit / inspiration from:
// Patrick Burt 
// http://www.pat-burt.com/web-development/how-to-do-a-css-popup-without-opening-a-new-window/
//
// findPosY() is from FireTree.net
// http://blog.firetree.net/2005/07/04/javascript-find-position/
//

// Width of the division
var popUpDiv_width = 300;
// position relative to top of screen (blanket)
var popUpDiv_height = 300;
// or pos relative to containing element
var popUpDiv_relheight = 100;


var framewidth = 30;
var frameheight = 70;


function enable_div_pos(div_id, height) {
	var el = document.getElementById(div_id);
	el.style.top = height;
	el.style.display = 'block';
}

function enable_div(div_id) {
	var el = document.getElementById(div_id);
	el.style.display = 'block';
}

function disable_div(div_id) {
	var el = document.getElementById(div_id);
	el.style.display = 'none';
}


// Get the size of screen to hide and value for positions
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
}


// Set position of popup window - mainly left as we overrite for right
function window_pos(popUpDivVar) {

	// First we set the size based on the image size
	// Image is same name as div - but with _img on the end.
	var thisdiv = document.getElementById(popUpDivVar);
	var thisimg = document.getElementById(popUpDivVar+"_img");
	var divwidth = thisimg.width + framewidth;
	var divheight = thisimg.height + frameheight;
	//alert ("Image size width " + divwidth + "px\n height " + divheight + "px") ;

	// Set width and height to 
	thisdiv.style.width = divwidth + "px";
	thisdiv.style.height = popUpDiv_height_size = divheight + "px" ;
	 
	
	
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-(popUpDiv_width/2);
	popUpDiv.style.left = window_width + 'px';
}




function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}



// Trigger pop-up
function popup_close(windowname) {
	// turn blanket on / off
	disable_div('blanket');
	// turn popup on / off
	disable_div(windowname);		
}


// Trigger pop-up
function popup(windowname) {
	// no pos specified so we place 150px from top of page
	popUpDiv_height = '100px';
	// set blanket to cover full screen
	blanket_size(windowname);
	// set position and size of popup
	window_pos(windowname);
	// turn blanket on / off
	enable_div('blanket');
	// turn popup on / off
	enable_div_pos(windowname, popUpDiv_height);		
}



// Trigger popup - same as popup, but positions relative to posDiv rather than top 
// we can use popup_pos to create and popup to close as we don't need to know pos when closing.
function popup_pos(windowname, posDiv) {
	// set blanket to cover full screen
	blanket_size(windowname);
	// set position and size of popup
	window_pos(windowname);
	// turn blanket on
	enable_div('blanket');
	var containingElement = document.getElementById(posDiv);
	var heightPos = findPosY (containingElement);
	// work out height based on posDiv
	popUpDiv_height = heightPos + popUpDiv_relheight + "px";
	// move to appropriate position on page - need to scroll up so that the picture fits within the screen (otherwise potentially we can have picture off bottom)
	window.scrollTo(0, heightPos);
	// turn popup on
	enable_div_pos(windowname, popUpDiv_height);		
}


