$(document).ready(function(){

  // add print link and call print function
  $("#print-link").removeClass("hide");
  $("#print-link a").click(function () { 
     window.print();
     return false; 
  });

  // open members menu if currently on members page.
  if ((document.location.href).match("\/members\/")) {
      $("h2.selected").removeClass("selected");
      $("h2.members").addClass("selected");
  }


  // Replace the date in the Website field with yes or not available if date absent
  if ($('#websiteField').length == 1) {
    var website = $('#websiteField .fieldValue').text();
    if (website.length > 0){
      var newlink = "<a target='_blank' href='http://" + website + "'>" + website + "</a>";
      $('#websiteField .fieldValue').html(newlink);
    }
    else {
      $('#websiteField .fieldValue').text("Not Available");
    }
  }


  // Replace the date in the accredited field with yes or no if date absent
  if ($('#accreditedField').length == 1) {
    var accredited = $('#accreditedField .fieldValue').text();
    if (accredited.length > 0){
      $('#accreditedField .fieldValue').text("Yes");
    }
    else {
      $('#accreditedField .fieldValue').text("-");
    }
  }

          
  // login error
  // If login error then show error message. Also (temporarily due to bug) append 'login-error=true' into URL and refresh page. This gets a new Matrix login key.
  var error = "<div id='errbox'>The username/password combination you provided is incorrect. Please try again. If the problem continues, please <a href='/contact'>contact us</a>.</div>";

  if ($('#logerr').text() != '') {
    $('#column-two').prepend(error);
    $('#errbox').show();
    var url = refurl = unescape(window.location.href);
    if (url.indexOf("&") > -1) {
      url = url.replace(/&/,"");
    }
    if ($(document).getUrlParam("login-error") != "true") {
      this.location.href = url + '&login-error=true';
    }
  }

  if ($(document).getUrlParam("login-error") == "true") {
    $('#column-two').prepend(error);
    $('#errbox').show();
    
    // Remove 'login-error=true' from the form action.
    var formAction = $("#login_form_login_prompt").attr("action");
    formAction = formAction.replace(/&login-error=true/, "");
    $("#login_form_login_prompt").attr("action",formAction);
  }


        if ($("body.Home").length == 0) {
    $('#nav-pri').accordion({
      header: '.head',
      active: '.selected',
      selectedClass: 'active',
      alwaysOpen: true,
      autoHeight: false
    });
  };
        if ($("body.Home").length == 1 && $('li.logout').length == 1) {
    $('#nav-pri').accordion({
      header: '.head',
      active: '.members',
      selectedClass: 'active',
      alwaysOpen: true,
      autoHeight: false
    });
  };
  
  $("#queries_search_field_query").DefaultValue("Search the Site");
        
        $("input:submit").addClass("submit");
        $("input:text").addClass("text");


        if ($("#new_div_6138").length > 0) {
          var max = 0;
          $("#column-two #searchText label").each(function(){
            if ($(this).width() > max)
               max = $(this).width();   
          });
          $("#column-two #searchText label").width(max);


          $("select#district option").each(function() {
            if ($(this).text() == $("#getDistrict").text()) {
               $(this).attr("selected", "selected");
            }
          });

          $("input:checkbox").each(function() {
             if ($(this).attr("class") == "true") {
               $(this).attr("checked", "checked");
             }
          });

          var rst = true;
          $("#areaLaw label.true").each(function() {
             if (rst) {
               $("#resultsMsg").append("for ");
               rst = false;
             }
             $("#resultsMsg").append($(this).text()+", ");
          });

          $("fieldset#areaLaw legend").append(' &middot; <a id="sAll">Select All</a> &middot; <a id="cAll">Clear All</a>');
          $("#sAll").click(function() {
             $("fieldset#areaLaw input").attr("checked", "checked");
          });
          $("#cAll").click(function() {
             $("fieldset#areaLaw input").removeAttr("checked");
          });
          

        }




     
});