/* global variables used for control of interface elements */
/* currentOverlay - keeps track of the current overlay */
/* calledConnectOverlay - flag to set if there the connect overlay need be shown */
var currentOverlay;  
var calledConnectOverlay = false; 

/* function that will center an overlay */
var centerOverlay = function(d) {
  var $o = $(d);
  //var $oc = $("#overlay-close"); /* experimental close button not used */

  var left = ($(window).width() - $o.width()) / 2;
  var top = (($(window).height() - $o.height()) / 2) + $(window).scrollTop();

  $o.css("left", (left + "px"));
  $o.css("top", (top + "px"));

  /* experimental close option - not used */
  //var oc_left = left + $o.width() - $oc.width();
  //var oc_top = top - $oc.height();
  //$oc.css("left", (oc_left + "px"));
  //$oc.css("top", (oc_top + "px"));
}

/* function to center the seal on the page */
var centerSeal = function(d) {
  var $o = $(d);
  var left;

  if ($(window).width() < 1024) {
    left = (1024 - $o.width()) / 2;
  }
  else {
    left = ($(window).width() - $o.width()) / 2;
  }
  $o.css("left", (left + "px"));
  $o.css("top", ("0px"));
}

/* function to position any initially visible interface elements contained in overlays */
var positionOverlays = function() {
  centerSeal($(".annivSeal"));
}

/* function used to position a form absolutely - not used but useful to reference at times */
/*
var positionForm = function(d) {
  var $o = $(d);
  var left = ($(window).width() - $(".page").width() + 30) / 2;
  var top = (($(".header").height() + $(".sect-01").height()));

  $o.css("left", (left + "px"));
  $o.css("top", (top + "px"));

  $o.show();
}
*/


/* function used to show an overlay.  All overlays should be shown through use of this method */
var showOverlay = function(e) {
  var $o;
  if (e.data.overlay != undefined) {
    $o = $(e.data.overlay);
  }
  else { $o = $(e); }
  if (currentOverlay != undefined) {
    $(currentOverlay).fadeOut();
  }
  if ($("#pageCover").is(":hidden")) {
    if ($("#overlay-close").is(":hidden")) {
      $("#overlay-close").fadeIn();
    }
    $("#pageCover").show();
  }
  if ($o.is(":hidden")) {
    centerOverlay($o);
    $o.fadeIn();
  }
  currentOverlay = $o;
}

/* sets flag indicating whether or not the #connectAttempt overlay should show */
var showConnectOverlay = function(e) {
  calledConnectOverlay = true;
}

/* used to show connect overlay in live site */
var showConnectOverlayTest = function(e) {
  showOverlay($("#connectAttempt"));
  setTimeout("showOverlay($('#operatorsBusy'));", 4000);
}

/* function intended to show connection attempt on postback */
var checkConnectOnPostBack = function() {
  if (calledConnectOverlay) {
    showOverlay($("#connectAttempt"));
    setTimeout("showOverlay($('#operatorsBusy'));", 4000);
  }
}

/* closes current overlay */
var closeCurrentOverlay = function() {
  if ($("#pageCover").is(":visible")) {
    if ($("#overlay-close").is(":visible")) {
      $("#overlay-close").fadeOut();
    }
    $("#pageCover").hide();
  }
  if ($("#operatorsBusy").is(":visible")) {
    location.reload(true);
  }
  currentOverlay.fadeOut();
  currentOverlay = undefined;
}

/* calls expose effect */
var exposeItem = function(e) {
  if (e.target.className != "confLink") {
    $(this).expose({ zIndex: 100 });
  }
}

/* determines browser type and returns character code */
var getBrowserType = function() {
  var b_version = navigator.appVersion;
  var temp = new Array();
  var browser;

  temp = b_version.split(' ');
  browser = temp[2];

  return browser;
}

/* determines browser version and returns version number as floating point value */
var getBrowserVersion = function() {
  var b_version = navigator.appVersion;
  var temp = new Array();
  var version;

  temp = b_version.split(' ');
  version = parseFloat(temp[3]);

  return version;
}

/* determines the browser type and version and displays the seal if the version is not IE6 */
var showSeal = function() {
  var browser = getBrowserType();
  var version = getBrowserVersion();

  if (!(browser == "MSIE" && version <= 6)) {
    $(".annivSeal").show();
  }
}

/* determines the version of internet explorer and activates expose effect if IE8 or above */
var attachExpose = function() {
  var browser = getBrowserType();
  var version = getBrowserVersion();

  if (!(browser == "MSIE")) {
    $(".exposeHandle").click(exposeItem);
  } else if (version >= 8) {
    $(".exposeHandle").click(exposeItem);
  }
}

/* asp helper function to dynamically insert text at a named section in the markup */
var insertText = function(s) {
  $("#textInsert").html(s);
}

/* function that displays the form for the exit popup when a user attempts to navigate away from the page */
var showExitPopup = function() {
  $("#popupTrigger").hide();
  showOverlay($("#overlay-exitpopup"));
  /* used to test the other overlays associated with the exit survey */
  //showOverlay($("#overlay-exitSurvey"));
  //showSurveyThanks();
}

/* used to show the text area in the exit survey */
var showHideSurveyDescription = function() {
  var $d = $(".surveyDescription");
  var $z = $(".surveyZip");

  if ($z.is(":visible")) {
    $z.slideUp("slow");
  }
  if ($d.is(":hidden")) {
    $d.slideDown("slow");
  }
}

/* used to show the text field for zip code in the exit survey */
var showHideSurveyZip = function() {
  var $d = $(".surveyDescription");
  var $z = $(".surveyZip");

  if ($d.is(":visible")) {
    $d.slideUp("slow");
  }
  if ($z.is(":hidden")) {
    $z.slideDown("slow");
  }
}

/* hides all dynaically presented text fields/areas in exit survey */
var hideSurveyFields = function() {
  var $d = $(".surveyDescription");
  var $z = $(".surveyZip");

  if ($d.is(":visible")) {
    $d.slideUp("slow");
  }
  if ($z.is(":visible")) {
    $z.slideUp("slow");
  }
}

/* shows the thank you after submitting the survey */
var showSurveyThanks = function() {
  showOverlay($("#overlay-surveyThanks"));
  setTimeout("closeCurrentOverlay();", 4000);
}


/* makes all initializations for behaviors of interface elements */
var initialize = function() {
  $("#pageCover").css("height", ($(document).height() + "px"));
  centerSeal($(".annivSeal"));
  if (typeof (GetMap) == "function") {
    GetMap();
  }
  $("#pageCover").bind("click", closeCurrentOverlay);
  $("#overlay-close").bind("click", closeCurrentOverlay);
  $("#popupTrigger").bind("mouseover", showExitPopup);
  $(".radioDescription").bind("click", showHideSurveyDescription);
  $(".noThanksBtn").bind("click", { overlay: $("#overlay-exitSurvey") }, showOverlay);
  $(".radioZip").bind("click", showHideSurveyZip);
  $(".radioOther").bind("click", hideSurveyFields);
  $(".overlay-howItWorks").bind("click", { overlay: $("#overlay-01") }, showOverlay);
  $(".overlay-privacyPolicy").bind("click", { overlay: $("#overlay-02") }, showOverlay);
  $(".overlay-faq").bind("click", { overlay: $("#overlay-faq") }, showOverlay);
  $(".overlay-aboutUs").bind("click", { overlay: $("#overlay-aboutUs") }, showOverlay);
  $(".connectToOperator").bind("click", showConnectOverlay);
  attachExpose();
  showSeal();
  checkConnectOnPostBack();
}

$(document).ready(function() {
  // do stuff when DOM is ready
  initialize();

});