﻿// JScript File
  /* when resizing, document.detachEvent doesn't seem to work - it only picks up the mouseup 
  if you're over the control that received the mousedown, even if the function's 
  been attached to document.
  To make the resize code work with Firefox, it may be worth looking at:
  http://www.webreference.com/programming/javascript/mk/column2/*/
var resizeData = null; //This gets a value as soon as a resize starts
var resizeInnerEl = null; // for Firefox when inner table's content can be large elements

function resizeObject() 
{
	this.el = null; //pointer to the object
	this.grabx = null;     //Some useful values
	this.graby = null;
    this.width = null;
	this.height = null;
	this.left = null;
	this.top = null;
}

function resizeMouseDown(e,_id) 
{
    e = e || window.event;
      
    var el = document.getElementById(_id);
    //grab the current position and save it
	if (el == null) 
	{
		resizeData = null;
		return;
	}		

	resizeData = new resizeObject();
	resizeData.el = el;
	resizeData.x = e.clientX;
	resizeData.y = e.clientY;
	resizeData.width = el.offsetWidth;
	resizeData.height = el.offsetHeight;
	resizeData.left = el.offsetLeft;
	resizeData.top = el.offsetTop;
	
	if(_id == 'KGHelpControlID1')
	{
	  resizeInnerEl = document.getElementById(_id + '_Table');
	  if(resizeInnerEl != null)
	  {
	    resizeInnerEl.width = resizeData.width;
	    resizeInnerEl.height = resizeData.height;
	  }
	}
	
	// Attach the events to the document so it doesn't pick up any other such calls until it's finished
    document.onmousemove = resizeMouseMove;
    document.onmouseup = resizeMouseUp;
    
    CancelBubble(e);

    document.onselectstart = new Function("return false");
}

function resizeMouseMove(e)
{
  e = e || window.event;
  //only resize widthways if we're above the minimum
  if ( resizeData.width + (e.clientX - resizeData.x) > 410 )
  {
    resizeData.el.style.width = resizeData.width + (e.clientX - resizeData.x) + "px";
  }
  resizeData.el.style.height = resizeData.height + (e.clientY - resizeData.y) + "px";
  
  
  if(resizeInnerEl != null)
  {
    if ( resizeData.width + (e.clientX - resizeData.x) > 410 )
    {
      resizeInnerEl.width = resizeData.width;
    }
    resizeInnerEl.height = resizeData.height;
  }
	
  CancelBubble(e); 
}

function resizeMouseUp(e)
{
  e = e || window.event;
  if (resizeData != null) 
  {
 	// Update the session variable
    SetSessionProperty(resizeData.el.id + 'Position',resizeData.el.style.cssText);
 	resizeData = null;
  }

  // Stop capturing events
  document.onmousemove = null;  
  document.onmouseup = null;
  
  CancelBubble(e);  
}


/* Control dragging code from http://www.brainjar.com/dhtml/drag/, 24/04/08 */
/* JPC 05/08/08: had to add some extra code for when pointer moved over iframe during drag */
// Determine browser and version.
function Browser() 
{
  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) 
  {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) 
  {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) 
  {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.
var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(e, id) 
{
  e = e || window.event;
  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 
  {
    dragObj.elNode = e.target || e.srcElement;

    // 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.scrollX)
  {
    x = e.clientX + window.scrollX;
    y = e.clientY + window.scrollY;
  }
  else
  {
    x = e.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = e.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }

  // Save starting positions of cursor and element.
  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = GetPixelLeft(dragObj.elNode);
  dragObj.elStartTop   = GetPixelTop(dragObj.elNode);
  startDifferenceX = dragObj.cursorStartX - dragObj.elStartLeft;
  startDifferenceY = dragObj.cursorStartY - dragObj.elStartTop;

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.
  dragObj.elNode.style.zIndex = 500;//++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.
  if (document.addEventListener)
  {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    document.body.addEventListener("mouseout",   testMouseOut, true);
    e.preventDefault();
    if(window.globalShowingWebDoc && !globalShowingWebDoc) //can't attach events to web doc iframe because the src has a different domain
    {
      document.getElementById("HelpFrame").contentWindow.document.documentElement.addEventListener("mousemove", frameGo, true);
      document.getElementById("HelpFrame").contentWindow.document.documentElement.addEventListener("mouseup",   dragStop, true);
    }
  }
  else
  {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    document.body.attachEvent("onmouseout",   testMouseOut);
    e.returnValue = false;
    if(window.globalShowingWebDoc && !globalShowingWebDoc)
    {
      document.getElementById("HelpFrame").contentWindow.document.documentElement.attachEvent("onmousemove", frameGo);
      document.getElementById("HelpFrame").contentWindow.document.documentElement.attachEvent("onmouseup",   dragStop);
    }
  }
  CancelBubble(e);
}

function testMouseOut(event)
{
  var targ = event.target || event.srcElement;
  if (targ == document.body)
  {
    dragStop();
  }
  else if (window.globalShowingWebDoc && globalShowingWebDoc)
  { // if the mouse goes over a web doc, hide the iframe
    // because iframe for web doc won't register mouse up so drag won't stop
    var toEl = event.relatedTarget || event.toElement;
    if (toEl && toEl.id == "HelpFrame")
    {
      m_HelpFrame.style.visibility = 'hidden';
    }
  }
}

function frameGo(event)
{
  var x, y;
  // Get cursor position with respect to the page.
  if (window.scrollX)
  {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  else
  {
    x = event.clientX + document.body.scrollLeft
      + document.documentElement.scrollLeft;
    y = event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop + m_MapHelpBox.scrollTop;
  }

  // Move drag element by the same amount the cursor has moved.
  dragObj.elNode.style.left = (dragObj.elNode.offsetLeft+ x - startDifferenceX) + "px";
  dragObj.elNode.style.top  = (dragObj.elNode.offsetTop + y - startDifferenceY) + "px";
}

function dragGo(event) 
{
  var x, y;

  // Get cursor position with respect to the page.
  if (window.scrollX)
  {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  else
  {
    x = event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
    
  // 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";
  }

function dragStop(event) 
{
	//debugger;
  // Stop capturing mousemove and mouseup events.
  if (document.removeEventListener)
  {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
    document.body.removeEventListener("mouseout",   testMouseOut, true);
    if(window.globalShowingWebDoc && !globalShowingWebDoc)
    {
      document.getElementById("HelpFrame").contentWindow.document.documentElement.removeEventListener("mousemove", frameGo, true);
      document.getElementById("HelpFrame").contentWindow.document.documentElement.removeEventListener("mouseup",   dragStop, true);
    }
    else if (window.m_HelpFrame)
    {
      m_HelpFrame.style.visibility = '';
    }
  }
  else
  {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
    document.body.detachEvent("onmouseout",   testMouseOut);
    if(window.globalShowingWebDoc && !globalShowingWebDoc)
    {
      document.getElementById("HelpFrame").contentWindow.document.documentElement.detachEvent("onmousemove", frameGo);
      document.getElementById("HelpFrame").contentWindow.document.documentElement.detachEvent("onmouseup",   dragStop);
    }
    else if (window.m_HelpFrame)
    {
      m_HelpFrame.style.visibility = '';
    }
  }
    // Update the session variable
  SetSessionProperty(dragObj.elNode.id + 'Position',dragObj.elNode.style.cssText);
  
  if (window.m_dragBox && m_dragBox.CurrentlyDragging)
  {
    KGDragStop(event);
  }
  
  // find x & y coords of all drop lists - hide list if there is an overlay.
	if(navigator.appVersion.match("MSIE 7.0") != "MSIE 7.0" && navigator.appName == "Microsoft Internet Explorer")
	{
		m_MapHelpBox = document.getElementById("KGHelpControlID1");
		 //If the control exists on the page
    if (m_MapHelpBox != null)
    {
			if(m_MapHelpBox.className != "OverlayPositionHidden")
			{
				// find elements x & y coords
				var elemCoords = findElementCoords(m_MapHelpBox);
				var elemCoordsAndWidth = m_MapHelpBox.clientWidth + elemCoords[0];
				var elemCoordsAndHeight = m_MapHelpBox.clientHeight + elemCoords[1];
			}
			// retrieve and hide all droplists.
			var droplist = document.getElementsByTagName("SELECT");
			for(var num=0;num<droplist.length;num++)
			{
				//only hide the visible boxes
				if(droplist[num].className == "list_dropList")
				{
					var dropCoords = findElementCoords(droplist[num])
					var dropCoordsAndWidth = droplist[num].clientWidth + dropCoords[0];
					var dropCoordsAndHeight = droplist[num].clientHeight + dropCoords[1];
					
					if(dropCoords != null && elemCoords != null && (dropCoords[0] >= elemCoords[0] || dropCoordsAndWidth >= elemCoords[0]) && (dropCoords[0] <= elemCoordsAndWidth || dropCoordsAndWidth <= elemCoordsAndWidth))
					{
						//overlays in the x-range
						if((dropCoords[1] >= elemCoords[1] || dropCoordsAndHeight >= elemCoords[1]) && (dropCoords[1] <= elemCoordsAndHeight || dropCoordsAndHeight <= elemCoordsAndHeight))
						{
							//overlays in the x & y ranges
							droplist[num].style.display = "none";
						}
						else
						{
							droplist[num].style.display = "inline";
						}
					}
					else
					{
						droplist[num].style.display = "inline";
					}
				}
			}
		}
	}

}

