//
// VERIDIAN CORE JAVASCRIPT
//
// These functions apply to all Veridian installations, regardless of the interface being used.
// Editing these functions is not recommended as all Veridian installations will be affected!
//
// Copyright (C) 2006-2011  DL Consulting Ltd.
//


// ----------------------------------------------------------------------------------------------
//  DEBUGGING
// ----------------------------------------------------------------------------------------------

var debugPopup = null;

function openDebugPopup()
{
  if (debugPopup == null)
  {
    debugPopup = window.open("", "veridiandebugpopup", "width=600, height=400, scrollbars=yes");
    writeDebugMessage("<h3>Veridian debug output</h3>");
  }
  if (window.focus)
  {
    debugPopup.focus();
  }
  return false;
}

function writeDebugMessage(debugMessage)
{
  if (debugPopup != null && debugPopup.document != null)
  {
    debugPopup.document.write(debugMessage);

    // Scroll to the bottom of the window so the debug message just written is visible
    debugPopup.scrollTo(0, (1 << 30));
  }
}


// ----------------------------------------------------------------------------------------------
//  LOGICAL SECTION HIGHLIGHTING
//  These functions are used by both the PanoJS interface and the "classic" page-level view
// ----------------------------------------------------------------------------------------------

var currentlyHighlightedLogicalSectionOID = -1;
var currentlySelectedLogicalSectionOID = -1;

function logicalSectionOnMouseOver(logicalSectionOID)
{
  // Highlight the logical section, unless it is already highlighted or it is selected
  if (logicalSectionOID != currentlyHighlightedLogicalSectionOID && logicalSectionOID != currentlySelectedLogicalSectionOID)
  {
    highlightLogicalSection(logicalSectionOID, true);
    currentlyHighlightedLogicalSectionOID = logicalSectionOID;
  }
}

function logicalSectionOnMouseOut(logicalSectionOID)
{
  // Unhighlight the logical section, unless it is selected
  if (logicalSectionOID != currentlySelectedLogicalSectionOID)
  {
    highlightLogicalSection(logicalSectionOID, false);
    currentlyHighlightedLogicalSectionOID = -1;
  }
}


function highlightLogicalSection(logicalSectionOID, highlight)
{
  for (var i = 0; i < logicalSectionAreas[logicalSectionOID].length; i++)
  {
    var logicalSectionArea = document.getElementById(logicalSectionAreas[logicalSectionOID][i].id);
    if (logicalSectionArea == undefined) continue;

    if (highlight)
    {
      $(logicalSectionArea).removeClass('veridiantransparent');
      $(logicalSectionArea).addClass('veridiansemitransparent');
    }
    else
    {
      $(logicalSectionArea).removeClass('veridiansemitransparent');
      $(logicalSectionArea).addClass('veridiantransparent');
    }
  }
}


// ----------------------------------------------------------------------------------------------
//  MISCELLANEOUS
// ----------------------------------------------------------------------------------------------

// Changes "stringXXXXXX" to "stringXXX..." 
function truncateString(elementID, maxLength)
{
  if ($('#' + elementID).html().length > maxLength)
  {
    $('#' + elementID).html($('#' + elementID).html().substr(0, maxLength) + "...");
  }
}


// ----------------------------------------------------------------------------------------------
//  SEARCH
// ----------------------------------------------------------------------------------------------

function initialiseSearchResultFacetGroup(element_basename)
{
  // Get the cookie
  var $status = $.readCookie(element_basename + 'status');

  // Hide the node if specified
  if ($status == "contracted")
  {
    $('#' + element_basename + 'plus').show();
    $('#' + element_basename + 'minus').hide();
    $('#' + element_basename + 'td').hide();
  }

  // Default to expand the node
  else
  {
    $('#' + element_basename + 'plus').hide();
    $('#' + element_basename + 'minus').show();
    $('#' + element_basename + 'td').show();
  }
}


// Flip the status - if it is expanded then turn it to contracted, vice versa
function toggleSearchResultFacetGroup(element_basename)
{
  // Contracted case
  if($('#' + element_basename + 'td').css('display') == 'none')
  {
    $('#' + element_basename + 'plus').hide();
    $('#' + element_basename + 'minus').show();
    $('#' + element_basename + 'td').show();

    // Store the new status into cookie (no duration, so just for this web browser session)
    $.setCookie(element_basename + 'status', "expanded", { });
  }

  // Expanded case
  else
  {
    $('#' + element_basename + 'plus').show();
    $('#' + element_basename + 'minus').hide();
    $('#' + element_basename + 'td').hide();

    // Store the new status into cookie (no duration, so just for this web browser session)
    $.setCookie(element_basename + 'status', "contracted", { });
  }
}


// Returns true if the search parameters are valid, false otherwise
// Currently we don't do any checking here -- this should normally be done in the C++ code instead
function validateSearch()
{
  return true;
}


// ----------------------------------------------------------------------------------------------
//  TABS
// ----------------------------------------------------------------------------------------------

function initialiseTabGroup (tabGroupID, defaultInitialSelectedTabID)
{
  // Select the right tab initially
  var initialSelectedTabID = $.readCookie(tabGroupID);
  if (initialSelectedTabID != null) changeTab(tabGroupID, initialSelectedTabID);
  else changeTab(tabGroupID, defaultInitialSelectedTabID);
}


function changeTab (tabGroupID, selectedTabID)
{
  // Don't do anything if the selected tab doesn't exist
  if (selectedTabID == null || selectedTabID == undefined || $('#' + selectedTabID).length == 0) return;

  // Unselect the previously selected tab
  $('#' + tabGroupID + ' a.veridiantabselected').each(function(i)
  {
    $(this).removeClass('veridiantabselected');
    $(this).addClass('veridiantabnotselected');

    // Hide the content from the previously selected tab
    var tabContentID = $(this).attr("id") + "content";
    $('#' + tabContentID).hide();
  });

  // Select the new tab
  $('#' + selectedTabID).removeClass('veridiantabnotselected');
  $('#' + selectedTabID).addClass('veridiantabselected');

  // Show the content from the selected tab
  $('#' + selectedTabID + 'content').show();

  // Save the selected value in a cookie for next time (for a year)
  $.setCookie(tabGroupID, selectedTabID, { duration : 365 });
}


// ----------------------------------------------------------------------------------------------
//  DYNAMIC POPUP HELP
// ----------------------------------------------------------------------------------------------

/**************************************/
/* Originally from www.yensdesign.com *
/**************************************/

// 0 means not visible; 1 means visible;
var popupStatus = 0;

function makePopupVisible ()
{
  if (popupStatus == 0)
  {
    $("#backgroundPopup").css({ "opacity": "0.5" });
    $("#backgroundPopup").show();
    $("#helpPopup").show();
    popupStatus = 1;
  }
}

function hidePopup ()
{
  if (popupStatus == 1)
  {
    $("#backgroundPopup").hide();
    $("#helpPopup").hide();
    popupStatus = 0;
  }
}

function placePopup ()
{
  // Request data for centering
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var bodyHeight = $("#bodydiv").height();
  var bodyWidth = $("#bodydiv").width();
  var popupHeight = $("#helpPopup").height();
  var popupWidth = $("#helpPopup").width();

  // These variables set the relative height and width of the screen to the div. 50/50 means centered.
  // X/Y means the center of the popup is X% of the way across the screen, and Y% of the way down the screen
  var popupLocationX = 50;
  var popupLocationY = 50;
  var popupLeft = (popupLocationX * windowWidth) / 100 - popupWidth/2;
  var popupTop = (popupLocationY * windowHeight) / 100 - popupHeight/2;

  // Make sure that the div is completely on the screen
  // Note that if the screen is in the center, these checks can be commented out.
/*
  if (popupLeft + popupWidth > windowWidth) 
  {
    popupLeft = windowWidth - popupWidth;
  }
  if (popupTop + popupHeight > windowHeight) 
  {
    popupTop = windowHeight - popupHeight;
  }
  if (popupLeft < 0) 
  {
    windowLeft = 0;
  }
  if (popupTop < 0) 
  {
    windowTop = 0;
  }
*/

  // Place the popup in the correct location
  $("#helpPopup").css({ "top": popupTop, "left": popupLeft });

  // Cover the entire page with a translucent grey filter
  $("#backgroundPopup").css({ "height": bodyHeight });
  $("#backgroundPopup").css({ "width": bodyWidth });
}

function loadHelpSection (sectionName)
{
  $("#helpPopupContents").load('?a=p&p=help #' + sectionName, function() {
    // Close the popup, using the close button
    $("div#helpPopupClose").click(function() {
      hidePopup();
    });

    // Close the popup, using the ESC keyboard button
    $(document).keypress(function(e) {
      if (e.keyCode == 27)
      {
        hidePopup();
      }
    });

    placePopup();
    makePopupVisible();
  });
}

