/*##########################################################
Global variables used to keep track of states of different
items in the page.

a (Array) holds reference to animation function

b (Array) holds corresponding item/object to be animated

c (int)   used to keep track of when the animations are done
to flag the killing of the interval

intervalReference = reference to the interval so that it may
may be stopped when it is no longer useful
##########################################################*/

var b = new Array();
var a = new Array();
var c = 0;
var intervalReference;
var origImageName = "";
var exposeIncluded;
var $currentLayover;
var currentOverlay; 


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;
}

var centerOverlay = function(d) {
  var $o = $(d);
  //var $oc = $("#overlay-close");

  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"));

  /* close option */
  //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"));

}

var closeCurrentOverlay = function() {
  if ($("#pageCover").is(":visible")) {
    if ($("#overlay-close").is(":visible")) {
      $("#overlay-close").fadeOut();
    }
    $("#pageCover").hide();
  }
  if ($("#operatorsBusy").is(":visible")) {
    location.reload(true);
  }
  if (currentOverlay != undefined) {
    currentOverlay.fadeOut();
  }
  currentOverlay = undefined;
}


var showHideLayoverFlash = function() {
  showOverlay($("#overlay-testimonials"));
}


/*##########################################################
Functions definitions for animations and other uses within
the page.

##########################################################*/
/* 
testFunction - used to see if event handlers are being
attached correctly and to the correct DOM objects
*/
var testFunction = function() {
  alert("called the test function successfully");
}

var centerLayover = function(d) {
  var $o = $(d);
  var left = ($(window).width() - $o.width()) / 2;
  var top = (($(window).height() - $o.height()) / 2) + $(window).scrollTop();

  left -= 100;
  $o.css("left", (left + "px"));
  $o.css("top", (top + "px"));
}


/* 
getFlashMovie - obtain the name of the Flash Movie
*/
var getFlashMovie = function(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

/* 
animateMarquee - animation specifically for the marquee
*/
var animateMarquee = function(obj) {
  var $o = $(obj);
  $o.fadeIn(100);
  //$o.animate({top: "0px"}, {queue:false, duration: 400});
}

/* 
animateHeading - animation specifically for the headings
*/
var animateHeading = function(obj) {
  var $h = $(obj);
  $h.fadeIn(100);
  //$h.animate({marginLeft: "0px"}, {queue:false});
}

/* 
animateStep - animation specifically for the individual steps on screen
*/
var animateStep = function(obj) {
  var $o = $(obj);
  $o.fadeIn(100);
  $o.animate({ marginLeft: "0px" }, { queue: false });
}

/* 
animateForm - animation specifically for the appearance of the form
*/
var animateForm = function(obj) {
  var $o = $(obj);
  $o.fadeIn(600);
  $o.animate({ marginTop: "-90px" }, { queue: false, duration: 400 });
}

/* 
showMap - animation specifically for the appearance of the map
*/
var showMap = function(e) {
  $(".mapPlaceholder").fadeIn(100);
  $(".mapPlaceholder").animate({ top: "0px" }, { queue: false, duration: 400 });
}

/* 
exposeForm - function to specify conditions / particulars for the form expose
*/
var exposeForm = function(e) {
  $("#formArea").expose({ zIndex: 50 });
  //alert("form z-index, exposeMask z-index: " + $("#formArea").css("z-index") + ", " + $("#exposeMask").css("z-index"));
}
/* 
showElements - function to coordinate appearance of the on screen elements
*/
var showElements = function() {
  if ((c < a.length)) {
    a[c](b[c]);
    if (c == 4) { $(".subContent").fadeIn(100); }
    c++;
    //alert(c + ", " + a.length); 
  }
  else clearInterval(intervalReference);
}

/* 
changeIcon - function to conditionally change the icons in the form field
*/
/* NO LONGER USED
var changeIcon = function(e) {
var $o = $(e.target);
if (e.target.value.length > 5) {
($o.prev().attr("class", "checkMark"));
}
else $o.prev().attr("class", "questionMark");
}*/

/* 
highlightField - function to highlight text field
*/
var highlightField = function(e) {
  changeIcon(e);
  $(e.target).addClass("fieldTextboxOn");
}

/* 
killHighlight - function to remove highlight text field
*/
var killHighlight = function(e) {
$(e.target).removeClass("fieldTextboxOn");
}

/* 
stepOver - function used to create hover effect over 123
*/
var stepOver = function(e) {
  var $o = ($(this)).next();
  if ($o.is(":hidden")) {
    $o.slideDown();
  }
  /*
  origImageName = $o.find("img:first").attr("src");
  $o.addClass("numberedStepOver");
  $o.find("img:first").attr("src", origImageName.substring(0, origImageName.length - 4) + overExt);
  /*
  $o.fadeOut(500, function(){$o.addClass("numberedStepOver"); $o.find("img:first").attr("src", origImageName.substring(0, origImageName.length-4) + overExt);}).fadeIn(500);
  */
}
/* 
stepOut- function to remove hover effect from 123
*/
var stepOut = function(e) {
  if ($(".text123").is(":visible")) {
    $(".text123").slideUp();
  }
  //var $o = $(this);
  //$o.removeClass("numberedStepOver");
  //$o.find("img:first").attr("src", origImageName);
  /*
  $o.fadeOut(500, function(){$o.removeClass("numberedStepOver"); $o.find("img:first").attr("src", origImageName);}).fadeIn(500);
  */
}

/* 
showMapInfo - function to display map information area
*/
var showMapInfo = function(e) {
  $("#mapInfoDisplay").show("slow");
}

/* 
closeMapInfo - function to close map information area
*/
var closeMapInfo = function(e) {
  $("#mapInfoDisplay").hide("slow");
}

/* 
buttonOver - function create rollover for form button
*/
var buttonOver = function(e) {
  $(e.target).addClass("buttonImage_over");
}

/* 
buttonOut - function to return rollover to out state for form button
*/
var buttonOut = function(e) {
  $(e.target).removeClass("buttonImage_over");
}

/* 
showPopup - function to display the exit popup
*/
var showPopUp = function(e) {
showOverlay({ data: { overlay: $("#overlay-exitpopup")} });
$("#exitPopup").fadeIn();
$("#popupTrigger").hide();
/*
if ($("#exitPopup").is(":hidden")) {
$(".popupContainer").show();
$("#customCover").show();
$("#exitPopup").fadeIn();
$("#popupTrigger").hide();
//$("#exitPopupMatte").show();
}
*/
}

/* 
closePopup - function to close exit popup and go to exit survey
*/
var closePopup = function(e) {
  //alert("trying to close popup");
  if ($("#exitPopup").is(":visible")) {
    $("#exitPopup").fadeOut();
    setTimeout("$('#exitSurvey').fadeIn()", 500)
    //$("#exitPopupMatte").hide();
  }
}

/* 
closePopupToPage - function to close exit popup and return to page
*/
var closePopupToPage = function(e) {
  if ($("#exitPopup").is(":visible")) {
    $("#exitPopup").fadeOut();
    $("#customCover").hide();
    $(".popupContainer").fadeOut();
  }
}

/* 
closeSurveyToPage - function to close survey and return to page
*/
var closeSurveyToPage = function(e) {
  if ($("#exitSurvey").is(":visible")) {
    $("#exitSurvey").fadeOut();
    $("#customCover").hide();
    $(".popupContainer").fadeOut();
  }
}

/* 
activateTrigger - function to show trigger for popup
*/
var activateTrigger = function() {
  if (!(getBrowserType() == "MSIE" && getBrowserVersion() <= 6)) {
    if (!killTrigger) {
      $("#popupTrigger").show();
    } else {
      $("#popupTrigger").hide();
    }
  }
  else $("#popupTrigger").hide();
}

/* 
showExitDesc - function to show the text box on the exit survey
*/
var showExitDesc = function(e) {
  if ($(".exitWrongLocation").is(":visible")) {
    $(".exitWrongLocation").slideUp("slow");
  }
  if ($(".exitDescription").is(":hidden")) {
    $(".exitDescription").slideDown("slow");
  }
}

/* 
hideTexts - function to hide the text boxes on the exit survey
*/
var hideTexts = function(e) {
  if ($(".exitWrongLocation").is(":visible")) {
    $(".exitWrongLocation").slideUp("slow");
  }
  if ($(".exitDescription").is(":visible")) {
    $(".exitDescription").slideUp("slow");
  }
}


/*
showLocDesc - function to show the text box on the exit survey
*/
var showLocDesc = function(e) {
  if ($(".exitDescription").is(":visible")) {
    $(".exitDescription").slideUp("slow");
  }
  if ($(".exitWrongLocation").is(":hidden")) {
    $(".exitWrongLocation").slideDown("slow");
  }
}
/* 
showFeedbackThanks - function to show thank you message in the survey space
included at the end of the ASPX page to allow for dimming on postback.

var showFeedbackThanks = function(e) {
activateTrigger = null;
if ($("#customCover").is(":hidden")) {
$("#customCover").show();
}
if ($(".popupContainer").is(":hidden")) {
$(".popupContainer").show();
}
if ($("#exitSurvey").is(":hidden")) {
$("#exitSurvey").show();
}
$("#exitSurvey").children().hide();
$(".exitSurveyThanks").fadeIn();
setTimeout("$('.exitSurveyThanks').fadeOut(); $('#customCover').fadeOut(); $('.popupContainer').fadeOut()", 3000);
}
*/
var showThanksFromNothing = function(e) {
  if ($("#customCover").is(":hidden")) {
    $("#customCover").show();
  }
  if ($(".popupContainer").is(":hidden")) {
    $(".popupContainer").show();
  }
  if ($("#exitSurvey").is(":hidden")) {
    $("#exitSurvey").show();
  }
  showFeedbackThanks(new Object());

}

var showHideSites = function(e) {
  var $o = $(".otherSites");

  if ($o.is(":hidden")) {
    $o.fadeIn();
    $(".menuTrigger").show();
  }
  else {
    $(".menuTrigger").hide();
    $o.fadeOut();
  }
}

var showHideTestimonials = function(e) {
  var $o = $(".testContainer");

  if ($("#testimonialCover").is(":hidden")) {
    $("#testimonialCover").fadeIn();
  }
  else $("#testimonialCover").fadeOut();

  if ($o.is(":visible")) { $o.fadeOut(); }
  else $o.fadeIn();
}

var showHideFAQ = function(e) {
  var $o = $(".faqContainer");

  if ($("#testimonialCover").is(":hidden")) {
    $("#testimonialCover").fadeIn();
  }
  else $("#testimonialCover").fadeOut();

  if ($o.is(":visible")) { $o.fadeOut(); }
  else $o.fadeIn();
}

/* Black cover for testimonials */
var showHideLayover = function(e) {
  //alert(e.target.className);
  //alert("left: " + window.document.scrollLeft + ",   top: " + window.document.scrollTop);
  if ($(e.target).attr("class") == $(".faqLink").attr("class")) {
    $currentLayover = $(".faqContainer");
  } else if ($(e.target).attr("class") == $(".testimonialsLink").attr("class")) {
    $currentLayover = $(".testContainer");
  } else if ($(e.target).attr("class") == $(".aboutUsLink").attr("class")) {
    $currentLayover = $(".aboutUsContainer");
  }


  if ($("#testimonialCover").is(":hidden") && $currentLayover.is(":hidden")) {
    $("#testimonialCover").show();
    $currentLayover.fadeIn();
  }
  else if ($("#testimonialCover").is(":visible") && $currentLayover.is(":visible")) {
    if (e.target.nodeName != "A" && e.target.className != "scrollable" && e.target.className != "items" && e.target.className != "active" && e.target.className != "testText" && e.target.className != "testImage" && e.target.nodeName != "P" && e.target.className != "testDisclaimer") {
      $("#testimonialCover").hide();
      $currentLayover.fadeOut();
      $currentLayover = undefined;
    }
  }
}

/* Returns browser type */
var getBrowserType = function() {
  var b_version = navigator.appVersion;
  var temp = new Array();
  var browser;

  temp = b_version.split(' ');
  browser = temp[2];

  return browser;
}

/* Returns browser version */
var getBrowserVersion = function() {
  var b_version = navigator.appVersion;
  var temp = new Array();
  var version;

  temp = b_version.split(' ');
  version = parseFloat(temp[3]);

  return version;
}




$(document).ready(function() {
  //correcting layovers to make more efficient
  $("#pageCover").css("height", ($(document).height() + "px"));
  $("#pageCover").bind("click", closeCurrentOverlay);
  $(".overlay-testimonials").bind("click", { overlay: $("#overlay-testimonials") }, showOverlay);
  $(".aboutUsLink").bind("click", { overlay: $("#overlay-aboutUs") }, showOverlay);
  $(".faqLink").bind("click", { overlay: $("#overlay-faq") }, showOverlay);
  $(".popupFormClose").bind("click", closeCurrentOverlay);
  $(".noThanksButton").bind("click", { overlay: $("#overlay-exitSurvey") }, showOverlay);
  $("#popupTrigger").mouseover(showPopUp);
  $(".placeHolder").click(showPopUp);

  //alert("making sure we are connected");
  var pageHeight = $(document).height() + "px";
  if (exposeIncluded != undefined)
    if (exposeIncluded) {
    $("#formArea").click(exposeForm);
  }
//  $(".fieldTextbox").keypress(changeIcon);
//  $(".fieldTextbox").focus(highlightField);
//  $(".fieldTextbox").blur(killHighlight);
  $(".numberedStep").hover(stepOver, stepOut);
  $("#closeMapInfoLnk").click(closeMapInfo);
  $(".buttonImage_out").mouseover(buttonOver);
  $(".buttonImage_out").mouseout(buttonOut);
  $(".popupFormClose").click(closePopupToPage);
  //$(".surveyCloseToPage").click(closeSurveyToPage);
  $(".radioDescription").click(showExitDesc);
  $(".radioLocation").click(showLocDesc);
  $(".radioSurvey").click(hideTexts);
  //$(".exitButtonInput").click(showFeedbackThanks);
  $("#customCover").css("height", pageHeight);
  $("#testimonialCover").css("height", pageHeight);
  $("#testimonialCover").click(showHideLayover);
  $("div.scrollable").scrollable({ size: 2 });
  $(".sitesLink").click(showHideSites);
  $(".menuTrigger").mouseover(showHideSites);
  //setting the behavior for the faq link and container
  // $(".faqLink").click(showHideLayover);
  //$(".faqMargins").click(showHideLayover);
  $(".faqCloseButton").click(closeCurrentOverlay);
  //setting the behavior for the testimonials link and container
  //$(".testimonialsLink").click(showHideLayover);
  $(".testMargins").click(showHideLayover);
  $(".testContainer").click(showHideLayover);
  //setting the behavior for the about Us link and container
  //$(".aboutUsLink").click(showHideLayover);
  //$(".aboutUsMargins").click(showHideLayover);
  $(".aboutUsCloseButton").click(closeCurrentOverlay);
  //adjustments to the share this link
  $(".stbutton").click(function() { $("#stframe").css("height", "400px"); });
  centerLayover($("#talkingSpokesperson"));
  $("#talkingSpokesperson").show();
  $(window).bind("resize", function() { centerLayover($("#talkingSpokesperson")); });


  //timeout for trigger to show
  //setTimeout('activateTrigger()', 3000);
  if (activateTrigger != null) {
    if (!(getBrowserType() == "MSIE" && getBrowserVersion() <= 6)) {
      setTimeout('activateTrigger()', 5000);
    }
  }

});
