﻿// KGOverlayControl

var KGOverlayControl_marginLeft = 10; // the margin from right border
var KGOverlayControl_marginTop = 10; // the margin from bottom border

//function KGOverlayControl_UpdateOverlayDivPosStyle_Wrapped(m_KGOverlayControlDivId, style)
//{
//  if (style != null && style !='')
//  {
//   var holder = document.getElementById( m_KGOverlayControlDivId );
//   if(holder != null)
//   {
//     holder.style.property=style;
//   }
//  }
//}

//function KGOverlayControl_UpdateOverlayDivPos_Wrapped(_controlId, _posType, _posLeft, _posTop)
//{
//  var div=document.getElementById( _controlId );
//  if (div != null)
//  {
// 
//    var style='';
//    if(_posType != null && _posType !='')
//      div.style.position=_posType;
//    if(_posLeft != null && _posLeft !='')
//       div.style.left=_posLeft;
//    if(_posTop != null && _posTop !='')
//       div.style.top=_posTop;
//  }
//}

//function KGOverlayControl_UpdateOverlayTitle_Wrapped( m_Title_DivId, title)
//{
//   var div=document.getElementById( m_Title_DivId );
//   if (div != null && title != null)
//     SetInnerText(div,title);
//}

/// Wrapper of DisplayControlScript
function KGOverlayControl_DisplayControlScript_Wrapped(_controlId, _cssclass, _helpTopicKeyword, _helpTopicParams) 
{  
//debugger;
   var div = null; 
   
   if(_controlId != null && _controlId != '')
      div =document.getElementById(_controlId);
      
   if(div != null && _cssclass != null)
   {
			div.className = _cssclass;
			
			if(_controlId == "KGHelpControlID1")
			{
			  var currentKG = document.getElementById(currentlySelectedKG)
			  
			  if (currentKG != null)	
			    switch(currentKG.innerHTML)
			    {
			      case "Now showing the Knowledge Gene<sup>&#174;</sup> and document":
			        if(document.getElementById("DockedHelpWindow") != null)
			          currentKG.innerHTML = "Now showing the Knowledge Gene<sup>&#174;</sup> and document";	
			        else
			          currentKG.innerHTML = "Now showing the Knowledge Gene<sup>&#174;</sup>, click here to view the document";		  
			        break;
			      case "Now showing the Knowledge Gene<sup>&#174;</sup>, click here to view the document":  
	                currentKG.innerHTML = "Now showing the Knowledge Gene<sup>&#174;</sup> and document";
	   	            break;
			    }
			}
			
			// drop list z-index bugs for IE6
			if(_controlId.match("KGHelpControl") == "KGHelpControl")
			{				
				// 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")
				{
					// 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")
						{
							if(_cssclass == "OverlayPositionHidden")
							{
								droplist[num].style.display = "inline";
							}
						}
					}
				}
			}
			// Store the Help Topic Keywords and params if passed in 
			if(_helpTopicKeyword!=null)
			{ 
			}
	     
			// If the element is resizable, we need to remember the position
			if (div.className.indexOf("drsElement") > -1)
				SetSessionProperty(div.id + 'Position',div.style.cssText);  
			
			//debugger;	
			if(globalHideDockButton == true || m_bEditable == true) //globalSplitterSetting == "single" || 
			{
				// hide dock in window option
				if(document.getElementById("SiteOverlays_KGHelpControl1_DockInPageButton") != null)
				  document.getElementById("SiteOverlays_KGHelpControl1_DockInPageButton").style.display = "none";
				//globalHideDockButton = false;
			}
			else
			{
				// show dock in window option
				if(document.getElementById("SiteOverlays_KGHelpControl1_DockInPageButton") != null)
				  document.getElementById("SiteOverlays_KGHelpControl1_DockInPageButton").style.display = "";
			}  
   }
}


/// Wrapper of UpdateControlTitleScript
function KGOverlayControl_UpdateControlTitleScript_Wrapped(_controlId, _title)
{  
   var div = null; 
   
   if(_controlId != null && _controlId != '')
      div =document.getElementById(_controlId);
      
   if (div != null && _title != null)
     SetInnerText(div,_title);
}

/// Wrapper of CloseButtonOnClickScript
function KGOverlayCotrol_CloseButtonOnClickScript_Wrapped(_controlId, _hidenCssclass)
{
   var div = null; 
   
   if(_controlId != null && _controlId != '')
      div = document.getElementById(_controlId);
 
   if (div != null && _hidenCssclass != null)
     div.className= _hidenCssclass;
}


/// Wrapper of UpdateControlPositionScript to return script string for changing position of Control
function KGOverlayControl_UpdateOverlayDivPos_Wrapped( _controlId, _posType, _posLeft, _posTop)
{
	// hide the droplists for earlier IE versions
	if(navigator.appVersion.match("MSIE 7.0") != "MSIE 7.0" && navigator.appName == "Microsoft Internet Explorer")
	{
		if(_controlId.match("SignIn") == "SignIn" || _controlId.match("Suggestion") == "Suggestion")
		{
			HideDropLists();
		}
	}
	
	if(_controlId.match("Suggestion") == "Suggestion")
	{
		HideBubble(false);
	}

  var div = null;
  if(_controlId != null )
    div = document.getElementById( _controlId );
  
  if (div != null)
  {
    var posLeftNum = 0;
    var posTopNum = 0;
    var position = -1;
    
    try
    {
      // extract number part in postLeft, and set to posLeftNum
      if(_posLeft != null && _posLeft !='')
      {
        _posLeft +=""; // convert to string
        _posLeft = _posLeft.toLowerCase();
        position = _posLeft.indexOf('px');
        if(position >= 0)
          posLeftNum = parseInt(_posLeft.substring(0, position));
        else 
          posLeftNum = parseInt(_posLeft);
      }
      
      if(_posTop != null && _posTop !='')
      {
        _posTop +="";
        _posTop = _posTop.toLowerCase();
        position = _posTop.indexOf('px');
        if(position >= 0)
          posTopNum = parseInt(_posTop.substring(0, position));
        else 
          posTopNum = parseInt(_posTop);
      }
    }
    catch(e)
    {
      posLeftNum = 0;
      posTopNum = 0;
    }
    
    try 
    {              
      if(posLeftNum > 0 && posTopNum > 0)
      {
        // Get the proper Left and Top position by checking current window's border
        _posLeft = KGOverlayControl_GetLeft(div, _posType, posLeftNum)+"px";
        _posTop = KGOverlayControl_GetTop(div, _posType, posTopNum)+"px";
      }
      
			// debugger;
      if(_controlId.toLowerCase().indexOf("publishoptioncontrol") > -1 )
      {
				div.style.position="absolute";
				div.style.left= _posLeft;
				div.style.top= "150";


      }
      else
      {
				if(_posType != null && _posType !='')
					div.style.position=_posType;
				if(_posLeft != null && _posLeft !='')
					div.style.left=_posLeft;
				if(_posTop != null && _posTop !='')
					div.style.top=_posTop;
      }
    }
    catch(e)
    {
     // do nothing;
    }
  }
}

/// Get width of current window
function KGOverlayControl_GetWindowWidth() {
  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;
}

/// Get height of current window
function KGOverlayControl_GetWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

/// Get width of the div that embeds the whole control
function KGOverlayControl_GetControlDivWidth(_div)
{
  var divWidth = 0;
  if(_div != null && _div.offsetWidth != null)
    divWidth = _div.offsetWidth;
  
  return divWidth; 
}

/// Get height of the div that embeds the whole control
function KGOverlayControl_GetControlDivHeight(_div)
{
  var divHeight = 0;
  if(_div != null && _div.clientHeight != null)
    divHeight = _div.clientHeight;
  
  return divHeight;
}

/// Get Explorer's Type
function KGOverlayControl_GetExplorerType()
{
  var isIE = document.all ? 1 : 0
  var isNS = document.layers ? 1 : 0
  
  if(isIE == 1)  
    return "ie";
  else if(isNS == 1)
    return "ns";
  else 
    return "other";
}

/// Get x(left) postion of event
function KGOverlayControl_GetEventX()
{
  window.captureEvents(Event.CLICK);

  var explorerType = KGOverlayControl_GetExplorerType();
  var eventX = 0;
  if(explorerType == 'ie' && event != null && event.x != null)
    eventX = event.x;
  else if(explorerType == 'ns' && e != null && e.pageX != null)
    eventX = e.pageX;
 
  return eventX;
}

/// Get y(top) postion of event
function KGOverlayControl_GetEventY()
{
  window.captureEvents(Event.CLICK);

  var explorerType = KGOverlayControl_GetExplorerType();
  var eventY = 0;
  if(explorerType == 'ie' && event != null && event.y != null)
    eventY = event.y;
  else if(explorerType == 'ns' && e != null && e.pageY != null)
    eventY = e.pageY;
      
  return eventY;
}

// Get the left position by checking current window's border 
// If part of current control is out of border, get the proper left position 
function KGOverlayControl_GetLeft(_div, _posType, _posLeft)
{
  var _windowWidth = KGOverlayControl_GetWindowWidth();
  var _controlWidth = KGOverlayControl_GetControlDivWidth(_div);
  
  if(_div != null && _windowWidth > 0 && _controlWidth > 0 && _posLeft > 0)
  {
    var fromBorderRight = _windowWidth - _controlWidth - KGOverlayControl_marginLeft;
    if(fromBorderRight < 0)
      fromBorderRight = 0;
    
    if(_posType != null && _posType.toLowerCase() == 'absolute')
    {
      if(_posLeft > fromBorderRight)
        _posLeft = fromBorderRight;
      
    }
    else if (_posType != null && _posType.toLowerCase() == 'relative')
    {
      var eventX = KGOverlayControl_GetEventX();
      if(eventX != null && eventX > 0)
      {
        if(_posLeft > fromBorderRight - eventX )
          _posLeft = fromBorderRight - eventX;
      }
    }
    
    if(_posLeft < 0)
       _posLeft = 0;
       
  }
  return _posLeft;
}

// Get the top position by checking current window's border
// If part of current control is out of border, get the proper top position 
function KGOverlayControl_GetTop(_div, _posType, _posTop)
{
  var _windowHeight = KGOverlayControl_GetWindowHeight();
  var _controlHeight = KGOverlayControl_GetControlDivHeight(_div);

  if(_div != null && _windowHeight > 0 && _controlHeight > 0 && _posTop > 0)
  {
    var fromBorderBottom = _windowHeight - _controlHeight - KGOverlayControl_marginTop;
     
    if(fromBorderBottom < 0)
      fromBorderBottom = 0;

    if(_posType != null && _posType.toLowerCase() == 'absolute')
    {
      if(_posTop > fromBorderBottom)
        _posTop = fromBorderBottom;
      
    }
    else if (_posType != null && _posType.toLowerCase() == 'relative')
    {
      var eventY = KGOverlayControl_GetEventY();
      if(eventY != null && eventY > 0)
      {
        if(_posTop > fromBorderBottom - eventY )
          _posTop = fromBorderBottom - eventY;
      }
    }

    if(_posTop < 0)
       _posTop = 0;
       
  }
  return _posTop;
}

function KGOverlayControl_IsControlVisible_Wrapped(_controlId, _showClass, _hideClass)
{
  var control = document.getElementById(_controlId);
  if (control)
  {
    if (control.className == _showClass)
      return true;
    else if (control.className == _hideClass)
      return false;
  }
}

