// Load a new page and place it in the dialog
function loadPage( page ) {
  $j('#dialog-content').load( page + ' #location_0 > *', function() { $j('#location_0');});
}

// do this stuff when the page is "ready"
$j( document ).ready( function() {
  setupLinks();
  setupDialog();
  ajaxDesc();
  faqClick();
//faqList() not needed for the way the faq list looks right now.
});

// function to show content in the custom map lightbox
function setupLinks() {
  $j('a.map_link').click( function(e) {
    // prevent default link action
    e.preventDefault();

    // Hide the scrollbars
    $j('body').css('overflow', 'hidden');

    // clear the dialog content
    $j('#dialog-content').empty();
    // start loading the new content
    loadPage( $j(this).attr('href') );
    // open the new dialog
    var id = '#content-dialog';

    //Get the screen height and width  
    var maskHeight = $j(document).height();
    var maskWidth = $j(window).width();

    //Set height and width to mask to fill up the whole screen
    $j('#mask').css({'width':maskWidth,'height':maskHeight,'top':0,'left':0});

    //transition effect
    $j('#mask').fadeIn(300);
    $j('#mask').fadeTo("fast",0.8);

    //Get the window height and width
    var winH = $j(window).height();
    var winW = $j(window).width();

    //Set the popup window to center
    $j(id).css('top',  parseInt((winH/2-$j(id).height()/2) + $j(document).scrollTop()));
    $j(id).css('left', winW/2-$j(id).width()/2);

    //transition effect
    $j(id).fadeIn(500);
  });
}

// some settings for the custom map lightbox
function setupDialog() {
  //if close button is clicked
  $j('.window .close').click( function(e) {
      //Cancel the link behavior
      e.preventDefault();
      $j('#mask, .window').hide();
      $j('body').css('overflow', 'auto');
  });
  //if mask is clicked
  $j('#mask').click(function () {
      $j(this).hide();
      $j('.window').hide();
      $j('body').css('overflow', 'auto');
  });
  //if escape key is pressed
  $j(document).keyup(function(e) {
    if (event.keyCode == 27) {
      $j('#mask, .window').hide();
      $j('body').css('overflow', 'auto');
    }
  });
}

// function to ajax in the first content block from the appropriately named page
function ajaxDesc() {
  // when an a.ajax-link is clicked
  $j('a.ajax-link').click( function(e) {
    // prevent default link action
    e.preventDefault();
    linkText = $j(this).attr('href');
    lastSlash = linkText.lastIndexOf("/");
    // strip everything but the last word of the link out of linkText and put the results into linkTextS.
    // if there are no slashes just make linkTextS = linkText
    if (lastSlash) {
      linkTextS = linkText.slice(lastSlash+1);
    } else {
      linkTextS = linkText;
    }
    // load in just the .component_content of the first item in #location_0
    // trigger slideToggle when the load is completed
    $j('div.' + linkTextS + ' > div.loadzone').load( linkText + ' #ftPg', function() {
      $j('.hidden '+linkTextS).css('height', $j('div.'+ linkTextS).height() + 'px');
      $j('div.' + linkTextS).slideToggle('slow');
      });
  });
  // handle viviti form submit button clicks
  $j(':submit').click( function(e) {
    $j(':submit').parents('.loadzone').load('');
  })

}
function faqClick() {
  $j('a.question').click( function(e) {
    //prevent default link action
    e.preventDefault();
    question = $j(this).parent();
    answer = $j(question).children().find('li');
    $j(answer).css('height', $j(answer).height() + 'px');
    $j(answer).stop(true,true).slideToggle('slow');
  });
}
function faqList() {
  var questionLi = $j('a.question').parent();
  $j('li > a.question:even').css('backgroundColor','#023');
  $j('li.answer:even').css('backgroundColor','#023');
  $j('li > a.question:odd').css('backgroundColor','#013');
  $j('li.answer:odd').css('backgroundColor','#013');
}
