/**
 * Halkoliiteri
 */
var submenuDiv = null;
var active = false;
var hideTimer = null;
var menuIsWorking           = false;
var menuRequest             = null;
var xPosition = false;

/**
 * Function initializes a request object which has already been constructed.
 * Parameters:
 *  requestType    - HTTP request type: GET or POST.
 *  url            - The URL of the handler script.
 *  asynchronous   - Boolean: use asynchronous connection or not.
 *  responseHandle - The name of the function that will handle the response.
 */
function menuInitRequest( requestType, url, asynchronous, responseHandle ) {
  try {
      // Set the callback function
      menuRequest.onreadystatechange = responseHandle;

      // Try to open connection
      menuRequest.open( requestType, url, asynchronous );

      if( requestType.toLowerCase() == "post" ) {
        menuRequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=utf-8" );
        menuRequest.send( arguments[4] );
      } else {
        menuRequest.send( null );
      }
  } catch (errv) {
    menuIsWorking = false;
    alert(  "The application cannot contact the server at the moment.\n"+
            "Please try again in a few seconds.\n" );
  }
}

/**
 * Function constructs a Request object. Handles both Mozilla-based
 * browsers and Microsoft-browsers.
 * Parameters:
 *  requestType    - HTTP request type: GET or POST.
 *  url            - The URL of the handler script.
 *  asynch         - Boolean: use asynchronous connection or not.
 *  responseHandle - The name of the function that will handle the response.
 *  Note:
 *  Any fifth parameters represented as arguments[4] are the data a
 *  POST request is designed to send.
 */
function menuHttpRequest( requestType, url, asynch, responseHandle ) {
  try
  {
    // Firefox, Opera 8.0+, Safari
    menuRequest=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      menuRequest=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        menuRequest=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  // Test for a null request if neither ActiveXObject was initialized
  if ( menuRequest ) {
    if( requestType.toLowerCase() != "post" ) {
      menuInitRequest( requestType, url, asynch, responseHandle );
    } else {
      //the POSTed data
      var args = arguments[4];
      if( args != null && args.length > 0 ){
        menuInitRequest( requestType, url, asynch, responseHandle, args );
      }
    }
  }
}

function showSubnavigation(id, linkElement) {
  if ( submenuDiv == null )
    submenuDiv = document.getElementById('subnavigation');

  if( !menuIsWorking )
  {
    if ( ! isNaN(id) )
    {
      xPosition = findPositionX(linkElement);
      var url = "/modules/subnavigation.jsp?id="+id;
      menuIsWorking = true;
      menuHttpRequest( "get", url, true, showSubnavigation_cb );
    }
    else
      alert( "Navigaatiota ei ole alustettu." );
  }
}

function showSubnavigation_cb() {
  if( menuRequest && menuRequest.readyState == 4 )
  {
    if ( menuRequest.responseText != "" )
    {
  	  submenuDiv.innerHTML = menuRequest.responseText;
      submenuDiv.style.visibility = 'visible';
      submenuDiv.style.display = 'inline-block';
      submenuDiv.style.left = xPosition +'px';
      //submenuDiv.style.paddingLeft = xPosition +'px';
    }
    else
    {
      hideSN();
    }
  }

  menuIsWorking = false;
}

function enableHide() {
  active = true;
  clearTimeout(hideTimer);
}

function hideSubnavigation() {
  if ( active == true ) {
    hideTimer = setTimeout('hideSN()', 500);
  }
}

function hideSN() {
  if ( submenuDiv == null )
    submenuDiv = document.getElementById('subnavigation');

  submenuDiv.style.visibility = 'hidden';
  submenuDiv.style.display = 'none';
  active = false;
}
