//modified by JK to allow popups to appear within the content window);
//Not used but kept for reference http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}


function winWidth() {
  var myWidth = 0
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
     } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    
  } else if( document.body && ( document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
      }
  return myWidth;
  }


// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;				//JK  *********************CHANGE THIS SO IT ONLY RUNS WHEN CALLED NOT ON MOUSE MOVE************************

// Temporary variables to hold mouse x-y pos.s
var mousex = 0
var mousey = 0



// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      // Note: I am adding together both the "body" and "documentElement" scroll positions
      //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
      //         (example: IE6 in compatibility mode or strict)
      //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
      //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH 
      //         (from info at http://www.quirksmode.org/js/doctypes.html)
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }


}
//end modified by JK to allow popups to appear within the content window);
function insertAfter(newElement, targetElement) {
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
}

function posElement(event)
{

document.body.appendChild(popper);

//var thewidth = event.screenX;
//var theheight = event.screenY;
    var thewidth = e.clientX + document.body.scrollLeft
        + document.documentElement.scrollLeft;
    var theheight = e.clientY + document.body.scrollTop
        + document.documentElement.scrollTop;


document.getElementById("popper").style.left = (thewidth/2) - 220 + "px";
document.getElementById("popper").style.top = theheight/2 + "px";

}

function myPop(event, mycontent, mylabel) {

// remove a panel if it already exists
if (document.getElementById("popper")) {
var popgo = document.getElementById("popper");
document.body.removeChild(popgo);
}

var popper = document.createElement("div");
popper.setAttribute("id", "popper");
//var test_text = document.createTextNode("this is a test");
//popper.appendChild(test_text);
popper.setAttribute("style", "display:block;position:absolute;");

var popper2 = document.createElement("div");
popper2.setAttribute("id", "popper2");

popper.appendChild(popper2);

var navbar = document.createElement("div");
navbar.setAttribute("id", "navbar");

navbar.innerHTML = "<a id=\'nav_action\' title=\'close panel\' href=\'javascript: closeIt();\'>close</a><span id=\'navbarinner\' onmousedown=\'eval(dragStart(event, \"popper\"))\'>" + mylabel + "</span>";

popper2.appendChild(navbar);

var thecontent = document.createElement("div");
thecontent.setAttribute("id", "thecontent");

insertAfter(thecontent, navbar);
//thelabel.appendChild(nav_action);
//and bung it onto the end of the bodytag

document.body.appendChild(popper);


//modified by JK to allow popups to appear within the content window);
//var thewidth = document.documentElement.offsetWidth;
//var theheight = document.documentElement.offsetHeight;
//alert("Width" + mousex + " Height" + mousey)
//alert(window.innerWidth);
//document.getElementById("popper").style.left = (thewidth/2) - 220 + "px";
//document.getElementById("popper").style.top = theheight/2 + "px";

//added by JK to allow popups to appear within the content window);
var thepagewidth = winWidth();
//alert(thepagewidth);
var thecontentwidth = 950;
var theboxsize = 460;
var theoffset = (thepagewidth - thecontentwidth)/2;
if ((mousex + theboxsize) > (thecontentwidth + theoffset)) 
	{	
		if (thepagewidth > thecontentwidth)
		{
 			mousex = (thepagewidth - (theoffset + theboxsize))
		}
		 else 
	 	{mousex = (thecontentwidth - theboxsize)}
	}


document.getElementById("popper").style.left = mousex + "px";
document.getElementById("popper").style.top = mousey + "px";
//end modified by JK to allow popups to appear within the content window);
//strip out the nbsp's
var stripped_content = mycontent.replace(/&nbsp;/g, "");

document.getElementById("thecontent").innerHTML = stripped_content;

}

function closeIt() {
var popgo = document.getElementById("popper");
document.body.removeChild(popgo);
}


var dragObj = new Object();
dragObj.zIndex = 0;
function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id) {
    dragObj.elNode = document.getElementById(id);
  } else
    if (window.event) {
      dragObj.elNode = window.event.srcElement;
      } else {
      dragObj.elNode = event.target;
}
    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3) {
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

if (window.event) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  } else {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update elements z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex + 1000;

  // Capture mousemove and mouseup events on the page.

if (window.event) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  } else {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }


}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

if (window.event) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  } else {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

if (window.event) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  } else {
    event.preventDefault();
}
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

if (window.event) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  } else {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

