﻿ // Splitter Pane Event Handler
 var globalBottomSplitterHeight = "50";
 var globalSplitterState = "1";
 var globalLeftPaneOpen = false;
 var globalMapViewControlLoaded = false; // used in SplitterPaneHandler.js, SetMapViewPaneStatus 
                                        // to decide whether to do a callback when the splitter's opened 
                                        // should be true when the map view is drawn, 
                                        // then set to false for any MapViewControl callbacks that change the map view data
                                        
//function Splitter1_onResize(sender, eventArgs)
//{
//  try
//  {
//		if(document.getElementById('SplitterArrowDown') != null)
//			if(document.getElementById('SplitterArrowDown').className == 'HiddenArrowDown')
//				eventArgs.set_cancel(true);
//		if(globalSplitterState == 3)
//			eventArgs.set_cancel(true);
//		if(Splitter1.Panes[1].element.style.display == "none")
//			eventArgs.set_cancel(true);
//		setPaneSize();
//	}
//  catch(e)
//  {
//		alert('Splitter1_onResize() error');
//  }
//}

function Splitter1_onLoad(sender, eventArgs) 
{
	try
	{
		// Call into the javascript method to process the query string 
		// in the case we have been redirected after a forced sign-in/sign-on		
		processRedirection();
		if(window.location.search.toLowerCase().match("kgid") == "kgid" ||
		    window.location.search.toLowerCase().match("createnewkg") == "createnewkg" ||
		    window.location.search == "")
		{
			SetSessionProperty("Browser",navigator.appName);
			//			DoExpandMapViewPane('fully');
			if (SetMapPaneStatus != null)
			SetMapPaneStatus('map');
		}
		else {
			if (SetMapPaneStatus != null)
				SetMapPaneStatus('applications');
		}
  }
  catch(e)
  {
		alert('Splitter1_onLoad() error');
  }
}









function setPaneWidths() {
// Not used anymore
	return;
//		if(globalLeftPaneOpen)
//		{
//			//OpenLeftPanel(true);
//			//ToggleLeftPane(true);
//		}
//	try
//	{
//		if(globalLeftPaneOpen)
//		{
//			OpenLeftPanel(true);
//		}
//		else
//		{
//			if(document.getElementById("Splitter1_pane_1") != null
//			  && document.getElementById("Splitter1_pane_0") != null
//			  && document.getElementById("Splitter1_pane_0").lastChild != null
//			  && document.getElementById("Splitter1_pane_1").firstChild != null )
//			{
//				Splitter1_pane_0.lastChild.style.width = Splitter1.Width -5; // firstChild can be a message box
//				Splitter1_pane_0.width = Splitter1.Width -5;
//				Splitter1_pane_0.style.width = Splitter1.Width -5;
//				Splitter1_pane_1.firstChild.style.width = Splitter1.Width -5;
//				Splitter1_pane_1.width = Splitter1.Width -5;
//				Splitter1_pane_1.style.width = Splitter1.Width -5;
//			}
//		}
//	}
//  catch(e)
//  {
//		alert('setPaneWidths() error' + e.description);
//  }
}

// // PaneB Collapse Status
// function PaneBStatus()
// { 
//    var BCollapsed = null;
//    var PaneB = null;
//    if(Splitter1!=null && Splitter1.Panes!=null && Splitter1.Panes[0]!=null)
//    {
//       PaneB = Splitter1.Panes[0];
//    }
//    if(PaneB != null && PaneB.Collapsed != null){
//       BCollapsed = PaneB.Collapsed;
//    }
//    return BCollapsed;
// }
 
 // Get Show Status of KGFloatingControl by PaneA and PaneB Collaspe Status
 function ShowStatus()
 {
    var IsShow = false;
    var BCollapsed = !globalMapPaneOpen;
    if(BCollapsed == null || BCollapsed == false)
    {
      IsShow = BCollapsed;
    }
    else 
    {
      IsShow = false;
    }
    return IsShow;
 }
  
  function SetMapViewPaneStatus()
  {
			if (globalMapPaneOpen == true)
			{
				if (globalMapViewControlLoaded == false)
				{
					var srmIndex;
					try
					{
						srmIndex = SrmControl_SetInitial('OpenMapSplitter');
					}
					catch(e){}
					clearTimeout(mvcCallbackTimeout);
					MapViewControl1_CallbackTrigger(srmIndex);
				}
				else
				{
					if (MapViewDefaultFocus)
						MapViewDefaultFocus();
				}
			}
  }
  
  function HideOptionControls()
  {
    // hide KGPublishOption control
//    var PaneC = null;
//    if(Splitter1!=null && Splitter1.Panes!=null && Splitter1.Panes[0]!=null)
//    {
//       PaneC = Splitter1.Panes[1];
//    }
//    if(PaneC != null && typeof CloseKGPublishOptionControl == 'function'){
//      CloseKGPublishOptionControl();
  	//    }
	if (globalApplicationsPaneOpen == true && typeof CloseKGPublishOptionControl == 'function'){
      CloseKGPublishOptionControl();
  }

  }  
   
//  function DoCollapseMapViewPane()
//  {
//    var PaneB = null;
//    var PaneC = null;
//    if(Splitter1!=null && Splitter1.Panes!=null && Splitter1.Panes[0]!=null)
//    {
//       PaneB = Splitter1.Panes[0];
//       PaneC = Splitter1.Panes[1];
//    }
//    if(PaneB != null)
//    {
//			if(PaneB.Collapsed != true)
//			{
//				Splitter1.CollapsePane(PaneB);
//				PaneB.SetHeight(0);
//			}
//			PaneC.element.style.display = "";
//			PaneC.SetHeight(GetSplitterFullHeight());
//	
//	    // collapsing the map means the splitter is moving to the top		
//			SplitterToTop();
//    }
//    setPaneSize();
//    document.title = "Knowledge Genes\u00AE - Home";
//  }
  
//  function DoExpandMapViewPane(_expandType, _keepExistingHeight)
//  {
//    // before moving the splitter, check where it was
//		var wasMapCollapsed = !globalMapPaneOpen; //if false or null then map is visible
//		
//    var PaneB = null;
//    var PaneC = null;
//    if(Splitter1!=null && Splitter1.Panes!=null && Splitter1.Panes[0]!=null)
//    {
//       PaneB = Splitter1.Panes[0];
//       PaneC = Splitter1.Panes[1];
//    }
//    PaneC.Collapsed = false;
//    
//    if(PaneB != null)
//    {
//	   var ptopHeight = PaneB.Height;
//	   var pbottomHeight = PaneC.Height;
//	   
//		 PaneC.SetHeight(0);
//		 PaneB.SetHeight(0);
//     Splitter1.ExpandPane(PaneB);
//     
//      if(_expandType == "fully")
//      { 
//				PaneB.SetHeight(GetSplitterFullHeight());
//				PaneC.SetHeight(0);
//				PaneC.element.style.display = "none";
//				document.getElementById('SplitterArrowUp').className = "HiddenArrowUp";
//				ShowTabWidget();
//      }
//      else if(_expandType == "split")
//      {
//	      if(ptopHeight > 0 && pbottomHeight > 0 && _keepExistingHeight)
//	      {
//	        PaneB.SetHeight(ptopHeight);
//          PaneC.SetHeight(pbottomHeight);
//				  PaneC.element.style.display = "";		     
//	      }
//	      else
//	      {
//          var half = Math.floor(Splitter1.Height/2); // can't set non-integer height
//				  PaneB.SetHeight(half - 2);
//				  PaneC.SetHeight(half - 2);
//				  PaneC.element.style.display = "";
//				}
//				ShowSplitterArrowUp();
//				HideTabWidget();
//			}
//			setPaneSize();
//			
//			// if the map was collapsed, the splitter is moving from the top
//			if (wasMapCollapsed)
//			  SplitterFromTop();
//    }
//    
//    try
//    {
//      if (Active_KGName && Active_KGOwner)
//        document.title = "Knowledge Genes\u00AE - " + Active_KGName + " - " + Active_KGOwner;
//    }
//    catch(e)
//    {}
//     
//  }
	
//  function SplitterControl(_direction)
//  {
//  // globalSplitterSetting chooses between split view & single view
//  // direction decides what way to move the splitter
//		try
//		{
//		//debugger;
//			setPaneSize();
//			var PaneB = Splitter1.Panes[0];;
//			var PaneC = Splitter1.Panes[1];
//			if(globalSplitterSetting == "split") // means *going to be* split NOT currently is split
//			{
//				if (_direction == 'up')
//				{
//					if(PaneC.element.style.display == "none")
//					{
//						// if at bottom
//						DoExpandMapViewPane("split");
//					}
//					else
//					{
//					  // if in middle
//		        DoCollapseMapViewPane();
//					}
//				}
//				else if (_direction == 'down')
//				{
//					if(PaneB.Height == 0)
//					{
//						// if at top
//						DoExpandMapViewPane("split");
//						
//					}
//					else
//					{
//						// if in middle
//						DoExpandMapViewPane("fully");	
//					}	
//				}
//			}
//			else // not split
//			{
//				if (_direction == 'up')
//				{
//					// single view full snap
//					DoCollapseMapViewPane();
//				}
//				else if (_direction == 'down')
//				{
//					// single view full snap
//					DoExpandMapViewPane("fully");
//				}
//			}
//		}
//		catch(e)
//		{
//			alert('SplitterControl() error');
//		}
//  }
//  
  function SplitterToTop()
  {
    // CW: extra tidying required when the splitter moves to the top
    
    // add a tooltip to the down arrow - here because we don't have tooltips when this arrow is in the middle
    // note if we do want the same tooltip in the middle, it would make sense to add it permanently to the arrow (i.e. not in js, in home.aspx)
//    SetSplitterArrowDownTooltip("Minimize");
		
		// show restore arrow since we're arriving at the top (normal splitter arrow down will collapse all the way down now)
//    ShowSplitterArrowDownRestore();
		
		//hide the "My Maps" and "What/WhatIs" widgets if the left pane is open - because there isn't enough space for both
	  if (!globalLeftPaneOpen)
	  {
	    DisplayLeftPaneMyMapsWidget("Hidden");
//	    DisplayLeftPaneWhyMostObjectiveWidget("Hidden");
//			DisplayLeftPaneConceptWidget("Hidden");
		}
  }
  
  function SplitterFromTop()
  {
    // CW: for when the splitter moves away from the top
    // basically undoes SplitterToTop
		
		// remove the tooltip from the down arrow
		SetSplitterArrowDownTooltip("");
		
		// hide the 'restore' arrow - it's only required when you're right at the top
//    HideSplitterArrowDownRestore();
  
    //show the "My Maps" and "What/WhatIs" widgets
    // hmmm - is it ever possible they should stay hidden? I don't think so because the splitter implies a map is present, so would always want both
    if (!globalLeftPaneOpen) // if the left pane is open, the widgets should already be visible, because we didn't hide them
    {
      DisplayLeftPaneMyMapsWidget("Show");
//      DisplayLeftPaneWhyMostObjectiveWidget("Show");
//			DisplayLeftPaneConceptWidget("Show");
	  }
		
  }
  
//  function SetSplitterArrowDownTooltip(_title)
//  {
//    var arrowDown = document.getElementById("SplitterArrowDown");
//		if(arrowDown != null)
//			arrowDown.title = _title;
//  }
  
  function helpOverlaySplitterResize()
  {
  	return;

//		var SplitterPos = findPosNoAdjust(document.getElementById('Splitter1'));
//		
//		var footer = document.getElementById("SiteFooterContainer");
//		var footerHeight = 0;
//		if (footer != null)
//		  footerHeight = footer.clientHeight + 2;
//		
//		var margin = 0;
//		if (navigator.userAgent.indexOf('Firefox') == -1)
//		{
//			 margin = 50;//83;
//		}
//		//debugger;
//		Splitter1.Height = document.body.clientHeight - SplitterPos[1] - footerHeight - margin;

//		Splitter1.element.style.height = Splitter1.Height;
//		return GetSplitterFullHeight();
  }
  
  function setPaneSize()
  {
		try
		{
			if(document.getElementById("Splitter1") != null)
			{
				setPaneWidths();

//				var paneRight = document.getElementById("Splitter1");
//				paneRight.style.height = helpOverlaySplitterResize();
				//paneRight.firstChild.style.height = "100%";
				
				// remove the gap below the map in FF2
				/*var parent = paneRight.parentNode;
        if (parent.style.height == '1px')
          parent.style.height = '2px';
        else
          parent.style.height = '1px';*/
      
				var paneTop = document.getElementById("Splitter1_pane_0");
				if(paneTop)
				{
				  //paneTop.parentNode.style.height = '1px';// remove the gap below the map in FF2
				  //opera height not updated
				  var paneBottom = document.getElementById("Splitter1_pane_1");
				  if (paneBottom) 
				  {
				    if (paneTop.style.display == 'none')
				    {
							var PaneB = null;
							var PaneC = null;
							if(Splitter1!=null && Splitter1.Panes!=null && Splitter1.Panes[0]!=null)
							{
								 PaneB = Splitter1.Panes[0];
								 PaneC = Splitter1.Panes[1];
							}
							if(PaneB != null)
							{
								if(PaneB.Collapsed != true)
								{
									Splitter1.CollapsePane(PaneB);
									PaneB.SetHeight(0);
								}
								PaneC.element.style.display = "";
								PaneC.SetHeight(GetSplitterFullHeight());
							}
				    
				      paneBottom.style.height = GetSplitterFullHeight();
				      paneBottom.firstChild.style.height = "100%";
				    }
				    else if (paneBottom.style.display == 'none')
				    {
				      paneTop.style.height = GetSplitterFullHeight();
				      paneTop.firstChild.style.height = "100%";
				    }
				    else
				    {
				      var ptopHeight = Splitter1.Panes[0].Height;
				      var pbottomHeight = Splitter1.Panes[1].Height;
				      
				      if(ptopHeight > 0 && pbottomHeight > 0)
				      {
                paneTop.style.height = ptopHeight;
				        paneTop.firstChild.style.height = "100%";
				        paneBottom.style.height = pbottomHeight;
				        paneBottom.firstChild.style.height = "100%";				     
				      }else
				      {
				        paneTop.style.height = (Splitter1.Height/2) - 2;
				        paneTop.firstChild.style.height = "100%";
				        paneBottom.style.height = (Splitter1.Height/2) - 2;
				        paneBottom.firstChild.style.height = "100%";
				      }
				    }
				  }
				}
			}
//			if (scrollingDivID)
//			{
//			  var scrollingDiv = document.getElementById(scrollingDivID);
//			  if (scrollingDiv)
//			    scrollingDiv.style.height = helpOverlaySplitterResize();
//			}
		}
		catch(e)
		{
			alert('setPaneSize() error');
		}
	}
	
	function GetSplitterFullHeight()
	{
//	  var calcHeight = Splitter1.Height - 4;
//	  // don't let the height be less than zero
		//	  return Math.max(0, calcHeight);
		return screen.height;
	}
	
	// Collaspe PaneA or PaneB
  function CollapseLeftPane()
  {
    SetMapViewPaneStatus();
    HideOptionControls();
    if(window.TextViewDivResize)
      TextViewDivResize();
      
		setPaneWidths()
    return true;
  }

	function PanesAreSplit()
	{
//		var paneTop = document.getElementById("Splitter1_pane_0");
//		var paneBottom = document.getElementById("Splitter1_pane_1");
//		if(paneTop.style.height == "0%" || paneBottom.style.height == "0%" || paneTop.style.display == "none" || paneBottom.style.display == "none" )
//			return false;
		//		return true;

		if (globalMapPaneOpen == false || globalApplicationsPaneOpen == false)
			return false;
		else
			return true;
	}
  
  
function isOverlayVisible()
{
	if(isHelpVisible()||isSuggestionVisible()||isSuggestionLoggedInVisible()||isLogInVisible())
		return true;
	else
		return false;
}  

function isHelpVisible()
{
	var helpControl = document.getElementById("KGHelpControlID1");
	if(helpControl.className == "OverlayPosition")
		return true;
	else
		return false;
}
function isSuggestionVisible()
{
	if(document.getElementById("KGSuggestionControl") != null)
	{
		var SuggestionControl = document.getElementById("KGSuggestionControl");
		if(SuggestionControl.className == "OverlayPosition")
			return true;
		else
			return false;
	}
}
function isSuggestionLoggedInVisible()
{
	if(document.getElementById("KGSuggestionControl1") != null)
	{
		var Suggestion1Control = document.getElementById("KGSuggestionControl1");
		if(Suggestion1Control.className == "OverlayPosition")
			return true;
		else
			return false;
	}
}
function isLogInVisible()
{
	var LoginControl = document.getElementById("KGSignInControl1");
	if(LoginControl.className == "OverlayPosition")
		return true;
	else
		return false;
}

var m_leftPaneWidth = '230px';
function IsLeftPanelOpen()
{ 
	var treeWalker = document.getElementById("LeftPaneTreeWalker");
	if(treeWalker != null)
	{
			// tree walk to grab panes - couldnt assign id's due to hardcoded ids of inner controls - will update later
			var LeftPane = treeWalker.parentNode.parentNode;
			if (LeftPane.style.width == m_leftPaneWidth)
				return true;
			else
				return false;
	}
	return false;
}


function OpenLeftPanel(_force)
{ // nga see what we have to do to get rid of this function 
	try
	{
	  var wasAlreadyOpen = globalLeftPaneOpen;
	  
		var treeWalker = document.getElementById("LeftPaneTreeWalker");
		if(treeWalker != null)
		{
			// tree walk to grab panes - couldnt assign id's due to hardcoded ids of inner controls - will update later
			var LeftPane = treeWalker.parentNode.parentNode;
			
			// return if it is already open
			if(_force == false)
			  if (LeftPane.style.width == m_leftPaneWidth)
					return;
			
			// the left panel is now included on the welcome page so we need some additional checks before calling shiftAllKnowdes
			// easiest way is to check the href
			var currentLocation = window.location.href;
      var currentPage = currentLocation.substring(currentLocation.lastIndexOf('/')+1, currentLocation.length).toLowerCase();

      if (LeftPane.style.width != m_leftPaneWidth && (currentPage.indexOf('home') > -1 || currentPage.length == 0))
				shiftAllKnowdesLeft(-230);
				
			LeftPane.style.display = "block";
			LeftPane.style.width = m_leftPaneWidth;
			
			if (currentPage.indexOf('home') > -1 || currentPage.length == 0)
			{
				var iIndex;
				for(iIndex=0; iIndex<LeftPane.parentNode.childNodes.length; iIndex++)
				{
					if(LeftPane.parentNode.childNodes[iIndex].style != null && LeftPane.parentNode.childNodes[iIndex] != "undefined")
					{
						if(LeftPane.parentNode.childNodes[iIndex].style.width == "100%" || LeftPane.parentNode.childNodes[iIndex].style.width == "" || LeftPane.parentNode.childNodes[iIndex].style.width != m_leftPaneWidth)
						{
						  LeftPane.parentNode.childNodes[iIndex].style.width = Splitter1.WindowWidth - parseInt(m_leftPaneWidth.replace('px', ''));
							Splitter1.adjustSize(true);
							globalLeftPaneOpen = true;
						}
					}
				}
			}
			else
			{
			  globalLeftPaneOpen = true;
//			  document.getElementById("HomePageCentralContainer").className = "HomePageCentralContainerWithLeftPane";
//			  document.getElementById("RightPaneStyle").style.width = getBrowserWidth() - parseInt(m_leftPaneWidth.replace('px', '')) - 5;
//			  document.getElementById("RightPaneStyle").className = "Welcome_RightPaneStyle_LeftOpen";
//			  document.getElementById("HomePageCentralContainer").parentNode.style.overflow = "auto";
//			  document.getElementById("HomePageCentralContainer").parentNode.style.left = 280;
//			  document.getElementById("HomePageCentralContainer").parentNode.style.position = "absolute";

			}		
		}
	}
	catch(e)
	{
	  if ( _force == false || _force == "false" )
  	  window.setTimeout("OpenLeftPanel(false)", 100);
  	else if ( _force == true || _force == "true" )
  	  window.setTimeout("OpenLeftPanel(true)", 100);
  	
		//alert("Sorry, the Left Panel failed to open: OpenLeftPanel script");
	}
}

// We can get rid of this function i think
function CloseLeftPanel(e)
{
	try
	{
		var targ = document.getElementById("LeftPaneTreeWalker");
		var LeftPane = targ.parentNode.parentNode;
		
		// return if it is already closed
		if(LeftPane.style.width == "0px")
			return;
			
		LeftPane.style.display = "none";
		LeftPane.style.width = "0px";

		LeftPaneExpanderOptionBox_ToggleVisibility("Show", "Show", "Show");
		//left pane is now on welcome page so we need additional checks before calling into splitter resize and mapview functions
		// we'll do this using the href
		var currentLocation = window.location.href;
    var currentPage = currentLocation.substring(currentLocation.lastIndexOf('/')+1, currentLocation.length).toLowerCase();
		
		if (currentPage.indexOf('home') > -1 || currentPage.length == 0)
		{
			shiftAllKnowdesRight(230,null,null,null);
		  
			var iIndex;
			for(iIndex=0; iIndex<LeftPane.parentNode.childNodes.length; iIndex++)
			{
				if(LeftPane.parentNode.childNodes[iIndex].style != null && LeftPane.parentNode.childNodes[iIndex].style != "undefined")
				{
					if(LeftPane.parentNode.childNodes[iIndex].style.width != "0%")
					{
						LeftPane.parentNode.childNodes[iIndex].style.width = "100%";
						globalLeftPaneOpen = false;
					}
				}
			}
			
			Splitter1.adjustSize(true);
			
			
			
			// if mapview is collapsed, hide the widgets 
			if(Splitter1 != null && Splitter1.Panes[0] != null)
			{
				PaneB = Splitter1.Panes[0]; 
				if(PaneB.Collapsed == true)
				{
					// only hide the widgets if the down splitter arrows are showing (and we've already checked they're at the top)
					// because there isn't enough space for both
					// but if e.g. you're viewing a user profile so there aren't any splitter arrows, we don't need to hide the widgets
					var arrowDown = document.getElementById("SplitterArrowDown");
					if (arrowDown && arrowDown.className.indexOf('idden') == -1)
					{
						DisplayLeftPaneMyMapsWidget("Hidden");
//						DisplayLeftPaneWhyMostObjectiveWidget("Hidden");
//						DisplayLeftPaneConceptWidget("Hidden");
					}
				}
			}
		}
		else
		{//debugger;
		  
		  var homePageContainer = document.getElementById("HomePageCentralContainer");
			homePageContainer.className = "HomePageCentralContainer";
			
//			document.getElementById("RightPaneStyle").style.width = getBrowserWidth();
//			document.getElementById("RightPaneStyle").className = "Welcome_RightPaneStyle";
			globalLeftPaneOpen = false;
//			homePageContainer.parentNode.style.overflow = "hidden";
//			homePageContainer.parentNode.style.removeAttribute("left");
//			homePageContainer.parentNode.style.removeAttribute("position");
		}
  }
	catch(e)
	{
		alert("Sorry, the Left Panel failed to close: CloseLeftPanel script");
	}
}


function MoveElement(_element, _toElement)
{
  _toElement.appendChild(_element);
}
    


