/*
	This function is called when an Endeca query is started (through Atlas).
*/
function OnQueryStart()
{
	// Enable the hourglass
	ToggleHourGlass(true);
}

/*
	This function is called when an Endeca query is completed (through Atlas).
*/
function OnQueryEnd()
{
	// Disable the hourglass
	ToggleHourGlass(false);
}

/*
	This function toggles the hour glass.
*/
function ToggleHourGlass(show) {
	// varialbes
	var HourGlass;

	// hide/show banners opposite to the hourglass
	ShowBanners(!show);
    
	if(show)
	{
	  AttachWindowScollEvent();

	  if (typeof TabClientId != 'undefined' && TabClientId)
	  {
	    // Replace some elements
      ReplaceElementsRec(document.getElementById(TabClientId));
	  }
	}
	else
	{
	  DetachWindowScollEvent()
	}
	
	HourGlass = document.getElementById("hourGlass");
	if (HourGlass != null)
	{		
		if (show)
		{
			// Move the hourglass to the center of the page
			HourGlass.style.top = GetScrollTop() + 150 +'px' ;
		}
		HourGlass.style.display = show ? "block" : "none";
	}
}

// Replace the entities that will show through layers
function ReplaceElementsRec(node)
{
  if (node.tagName == "SELECT" && node.style.display != "none")
  {
    var parentNode = node.offsetParent;
    var replacementText = document.createElement("input");
    
    replacementText.type= "text";
    replacementText.name = "temp";
    replacementText.id = "temp";
    
    if (node.selectedIndex > -1)
    {
      replacementText.value = node.options[node.selectedIndex].text;
    }
    
    replacementText.className = node.className;
    //replacementText.style.width = 180;
        
    node.parentNode.insertBefore(replacementText, node);
    node.style.display = "none";
  }

  for(var i=0; i<node.childNodes.length ; i++)
  {    
    ReplaceElementsRec(node.childNodes[i]);
  }
}

function CenterHourglass(e)
{  
  ToggleHourGlass(true);
}

function AttachWindowScollEvent()
{
   window.onscroll = CenterHourglass;
}

function DetachWindowScollEvent()
{
  window.onscroll = null;
}

/*
	Gets the scroll top position of the document.
*/
function GetScrollTop()
{
	if (self.pageYOffset) // all except Explorer
	{
		 return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}

/*This function attaches the OnQueryStart function*/
AtlasEndeca.EndecaService.getInstance().add_endecaServiceStartEvent(OnQueryStart);

/*This function attaches the OnQueryEnd function*/
AtlasEndeca.EndecaService.getInstance().add_endecaServiceCompleteEvent(OnQueryEnd);

/*This function attaches the OnQueryEnd function */
AtlasEndeca.EndecaService.getInstance().add_endecaServiceExceptionEvent(OnQueryEnd);