var _selectedLinkID = null;
var _openHeight = null;
var _closedHeight = null;

$jQuery(document).ready(function()
  {
    _closedHeight = $jQuery("#TopMenuBar").height();
    $jQuery("#TopMenuBar").height(_closedHeight);
    _openHeight = $jQuery("#TopMenuBar li:first").height() + $jQuery("#TopMenuBar").height() + 10;
  }
)

///Open the drop down menu
function toggleMenu(linkID, path) {
  if (_selectedLinkID != null)
    $jQuery("#" + _selectedLinkID).removeClass("TopMenuSelected");

  if ($jQuery("#TopMenu").hasClass("TopMenuClosed")) {
    $jQuery("#TopMenu").addClass("TopMenuOpen");
    $jQuery("#TopMenu").removeClass("TopMenuClosed");
    //$jQuery(".TopMenuSubContainer *").hide();
    $jQuery("#TopMenuBar").animate({height: _openHeight + "px"}, "fast", "linear", openCallBack);
    _selectedLinkID = linkID;

    //Register the menu click with Google Analytics
    trackClick("/topmenu/open" + path);
  }
  else {
    if (linkID == _selectedLinkID) {
      $jQuery("#TopMenuBar").animate({ height: _closedHeight + "px" }, "fast", "linear", animateCallBack);
      _selectedLinkID = null;
      trackClick("/topmenu/close" + path);
    }
    else {
      _selectedLinkID = linkID;
      //Register the menu click with Google Analytics
      trackClick("/topmenu/change" + path);
    }
  }

  if (_selectedLinkID != null)
    $jQuery("#" + _selectedLinkID).addClass("TopMenuSelected");
}

function animateCallBack() {
  $jQuery("#TopMenu").addClass("TopMenuClosed");
  $jQuery("#TopMenu").removeClass("TopMenuOpen");
}

function openCallBack() {
  //$jQuery(".TopMenuSubContainer *").fadeIn("fast");
}

//===============================
/* For preview start */
//===============================
var httpRequest = null;
 
function menuPreview(ID)
{
/*
  var previewBox = document.getElementById('MenuPreviewBox');
  if (previewBox == null)
    return;

  if (ID != null) {
    GetHttpRequest();
    GetPreviewText(ID)
  }
  else {
    previewBox.innerHTML = "";
  }
*/
}

function GetHttpRequest()
{
  if (window.XMLHttpRequest) 
  { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
  } else if (window.ActiveXObject) 
  { // IE
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  }
 }

function GetPreviewText(ID)
{
  if (httpRequest.readyState == 4 || httpRequest.readyState == 0) 
  {		
    //Setup the connection as a GET call to SayHello.html.		
    //True explicity sets the request to asyncronous (default).		
    httpRequest.open('POST', '/pt.website2008/navigation/Service/PreviewService.asmx/GetPreviewText', true);		
    //Set the function that will be called when the XmlHttpRequest objects state changes.		
    httpRequest.onreadystatechange = handlePreviewText; 		
    // Set request header
    httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    //Make the actual request.		
    httpRequest.send('ID='+escape(ID));	
  }			
}

function handlePreviewText() 
{	
  //Check to see if the XmlHttpRequests state is finished.	
  if (httpRequest.readyState == 4)
  {		
    if (httpRequest.status == 200) 
    {
      var previewXml = httpRequest.responseXML;
      
      var responseElement= previewXml.getElementsByTagName('string')[0];

      var previewBox = document.getElementById('MenuPreviewBox');
      if (previewBox == null)
        return;
      if (responseElement.firstChild == null)
      {
        previewBox.innerHTML = "";
        return;
      }  
      
      var previewText=responseElement.firstChild.nodeValue;
          
      //Set the contents of our span element to the result of the asyncronous call.
      previewBox.innerHTML = unescape(previewText);
    }
  }
}

function goToSupport() {
  window.open("http://support.pentia.dk");
}