$(document).ready(function()
{
  var options = {
    newsList: "#newsTicker",
    startDelay: 10,
    placeHolder1: " []"
  }

  $().newsTicker(options);

  $('.subMenuHeading').find('ul').addClass('up');

  $('.subMenuHeading').hover(
    function(){
      if($(this).find('ul').hasClass('up'))
      // up so slide down
      {
        $(this).find('ul').removeClass('up');

        slideMenuDown($(this).find('ul'));
      }
      else if($(this).find('ul').hasClass('down'))
      // down so stop queue
      {
        $(this).find('ul').stop(true);
      }
      else if($(this).find('ul').hasClass('slUp'))
      // sliding up so stop and slide down
      {
        $(this).find('ul').stop(true);

        $(this).find('ul').removeClass('slUp');

        slideMenuDown($(this).find('ul'));
      }
    },
    function(){
      if($(this).find('ul').hasClass('down'))
      {
        $(this).find('ul').removeClass('down');

        slideMenuUp($(this).find('ul'));
      }
      else if($(this).find('ul').hasClass('slDown'))
      {
        $(this).find('ul').addClass('straightUp');
      }
    }
  );

  /*
    show / hide member side menu
  */
  $("#memberMenuTrigger").hover(
    function(){
      $(this).find('ul').fadeIn();
    },
    function(){
      $(this).find('ul').fadeOut();
    }
  );

  /*
    Hide group selectors on email for if 'to'
    field has emails
  */
  if($('#send_to').html() != '')
  {
    $('#emailMemGroupWrap').hide();
    $('#emailStatusGroupWrap').hide();
  }

});

function slideMenuDown(target_menu)
{
  $(target_menu).addClass('slDown');

  $(target_menu).slideDown('fast', function(){
    $(this).removeClass('slDown');

    if($(this).hasClass('straightUp'))
    {
      $(this).removeClass('straightUp');

      slideMenuUp(this);
    }
    else
    {
      $(this).addClass('down');
    }
  });
}

function slideMenuUp(target_menu)
{
  // alert('up ' + target_menu);

  $(target_menu).addClass('slUp');

  $(target_menu).animate({'opacity': 1}, 750).slideUp('fast', function(){
    $(this).removeClass('slUp');

    $(this).addClass('up');

    $(this).css('height', 'auto');
  });
}

function showJob(element)
{
  // get content and toggle
  content = $(element).children('.listContent').slideToggle();
}

function expandQuestion(index)
{
  $('#questionBrief_' + index).slideUp();
  $('#questionDetail_' + index).slideDown();
}

function hideQuestion(index)
{
  $('#questionBrief_' + index).slideDown();
  $('#questionDetail_' + index).slideUp();
}

/*
  Clear radio and check boxes for acssociated question
*/
function clearRadioCheck(q_id)
{
  $('.' + q_id + '_radio').attr('checked', false);
}

/*
  Show or hide the new firm fields
*/
function toggleNewFirm()
{
  // toggle new firm fields
  $('#newFirmWrap').toggle();

  // clear selections in select box
  $('#profile_firm_id').attr('selectedIndex', 0);
}

/*
  Show / hide the public selector
*/
function togglePubSelect()
{
  if($('#profile_member_type_id').val() == 4 || $('#profile_member_type_id').val() == 5)
  // voting member show selector
  {
    $('#pubSelectWrap').css('display', 'block');
  }
  else
  // not voting member hide selector
  {
    $('#pubSelectWrap').css('display', 'none');

    // clear selector
    $('#profile_is_public_dir').attr('checked', false);
    $('#application_is_public_dir').attr('checked', false);

    // make sure pub info is hidden
    $('#pubishedDetails').css('display', 'none');
  }
}

/*
  Show hide published details
*/
function togglePubDetails ()
{
  $('#pubishedDetails').toggle();
}

// email form functions
/*
  Show / hide selection areas based on values
*/
function showHideSelectors()
{
  if($('#emailMemGroupWrap input:checked').length > 0)
  {
    $('#emailStatusGroupWrap').hide('fast');

    $('#emailToWrap').hide('fast');
  }
  else if($('#emailStatusGroupWrap input:checked').length > 0)
  {
    $('#emailMemGroupWrap').hide('fast');

    $('#emailToWrap').hide('fast');
  }
  else if($('#send_to').val() != '')
  {
    $('#emailMemGroupWrap').hide('fast');
    $('#emailStatusGroupWrap').hide('fast');
  }
  else
  // show all
  {
    $('#emailMemGroupWrap').show('fast');
    $('#emailStatusGroupWrap').show('fast');
    $('#emailToWrap').show('fast');
  }
}

/*
  clears the radio selectors
*/
function clearRadios()
{
  $('[name=emailStatus]').attr('checked', false);

  $('#emailMemGroupWrap').show('fast');
  $('#emailToWrap').show('fast');
}



/**
update fieldset based on combobox selected value
*/
function updateMembershipContainer()
{
  var otherFirm = document.getElementById('other_firm_select');
  if (otherFirm.checked)
    toggleNewFirm();

  var comboEl = $("#application_member_type_id");
  if (comboEl.length == 0)
  {
    comboEl = $("#profile_member_type_id");
  }

  var votingContainer = $("#votingContainer");
  var specialtyContainer = $("#specialtyContainer");
  var corporateContainer = $("#corporateContainer");
  var pubSelect =  $('#pubSelectWrap');

  var studentContainer = $("#paralegal-program-container");


  var container = $("#specialtySchoolContainer");
  var membershipId = parseInt(comboEl.val());

   $('#corporateContainer').css('display', 'none');
   $('#graduatedContainer').css('display', 'block');
   $('#profile_education_label').html('');
   $('#specialtySchoolContainer').css('display', 'block');

  container.html(specialtyContainer.html());

  if (comboEl != null)
  {
    // is student?
    if (membershipId == 1)
       $('#profile_education_label').html('<span class="important">&#42;</span>');
  // is corporate?
    if (membershipId == 2)
    {
    $('#corporateContainer').css('display', 'block');
    $('#graduatedContainer').css('display', 'none');
        container.html($("#paymentContainer").html());
    }
    else if (membershipId == 4 || membershipId == 5)
    {
      // is voting or director
   container.html(votingContainer.html() + specialtyContainer.html());
   if (pubSelect != null)
   {
    $('#pubSelectWrap').css('display', 'block');
   }
    }
  }
}

function displayValidEmail (domInput) {
$.get("/index.php/profile/ajaxValidateEmail", { email: domInput.value },
 function (data)
 {
   $('#email-validation-zone').html(data);
 }
);

}

// whenever the firm address is changing, update the home address is "use home address" is unchecked
function updateAddressFromFirm(selectId)
{
  var domSelect = document.getElementById(selectId);
  var firmId = domSelect.options[domSelect.selectedIndex].value;
  var useHomeAddressChk = document.getElementById("profile_firm_home_address");

  if (!useHomeAddressChk.checked)
  {

    $.ajax({
    url: '/index.php/profile/getFirmAddress?id=' + firmId,
    success: function(data) {
    odata = eval(data);

    for (var id in odata)
    {
      var element = document.getElementById(id);
      element.value = odata[id];
    }
    }
  });

  //<![CDATA[
//new Ajax.Updater('firmAddressFieldsContainer', '/frontend_debug.php/profile/getFirm', {asynchronous:true, evalScripts: true});
//]]>
  }


}