/* Function:    ajax_form
 * Description: binds a FreeForm Form to ajax submit and does all 
 *              work to update the user of an error or to submit.
 * Parameters:  form_id
 * Returns:     null
 */
function ajax_form (form_id) {
  $(form_id).ajaxForm ({              //bind ajax submit to the form
    target: ".form_response",       //response from submission
    // beforeSubmit: quick_validate, 
    success: function(response){     //callback function for success  
      if(response == "success"){
        $(form_id).slideUp("fast");
        $('.form_response').html("Your information and message was sent.  We'll be in touch soon.").addClass('success').slideDown("slow");
        $(form_id).resetForm()      //clear the form
      }else{
        $('.form_response').html($('.form_response #content ul').html()).addClass('error').slideDown("slow");
      }
    }
  });
}

function ajax_comment_form (form_id) {
  $(form_id).ajaxForm ({              //bind ajax submit to the form
    target: ".form_response",       //response from submission
    success: function(response) {     //callback function for success  
      if ($(".form_response ul").length > 0 ) { //there were errors
        $('.form_response').html($('.form_response #content ul').html()).addClass('error').slideDown("slow");
      } else {
        $('.form_response').html($('.form_response #content p:first')).addClass('success')
        $('.form_response p:first').addClass('tight')
        $('.form_response').slideDown("slow");
        $(form_id).slideUp("fast");
        $(form_id).resetForm()  //clear the form
      }
    }
  });
}

function ajax_share_form (form_id) {
  $(form_id).ajaxForm ({              //bind ajax submit to the form
    target: ".form_response",         //response from submission
    // beforeSubmit: quick_validate, 
    success: function(response) {     //callback function for success  
      if ($(".form_response ul").length > 0 ) { //there were errors
        $('.form_response').html($('.form_response #content ul').html()).addClass('error').slideDown("slow");
      } else {
        $('.form_response').html("Thank You. Your email message has been sent.").addClass('success').slideDown("slow");
        $(form_id).slideUp("fast");
        $(form_id).resetForm()  //clear the form
      }
    }
  });
}

/* Function:    equalHeight
 * Description: vertically align a block level element inside another  
 * Parameters:  none
 * Returns:     null
 */
function equalHeight(elements,min_height) {
	var tallest = min_height;
	$(elements).each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	$(elements).each(function() {
	  $(this).height(tallest);
	});
}

//on page load functions
$(document).ready(function() {
  
  /* home page
  *****************************************/ 
  $(".feature_banners")
    .scrollable({size: 1})
    .circular()
    .autoscroll({
      interval:5000,
      autopause: true
    })
    .navigator({
      navi: ".feature_nav",
      naviItem: "a",
      activeClass: "current"
    })

	/* attorney carousel filter (remove main attorney)
	***************************************/
	$("#multiple_attorney").hide();

  /* pagination
  *****************************************/ 
	$(".pagination_results_per_page").change(function(){
		new_page_limit = $(this).val() + "/";
		window.location.href= new_page_limit;
	})
	
	$(".pagination_pagenum").change(function(){
		page_link = $(this).val() + "/";
		window.location.href= page_link;
	});
	
	
  /* attorney filter panel
  *****************************************/ 
  $("#attorney_search_panel a.expander").click(function() {
    if ($(this).hasClass('open')) {
      $("#attorney_search_panel #search_panel").slideUp("slow","easeOutExpo");
      $(this).removeClass('open').addClass('close');
    }else if ($(this).hasClass('close')) {
      $("#attorney_search_panel #search_panel").slideDown("slow","easeInExpo");
      $(this).removeClass('close').addClass('open');
    }
  });
  /* school autocomplete */
  $("#school").autocomplete(schools);

  /* force the search to search for the whole school name as a string*/
  $("#attorney_search").submit(function(){
    var school = $("#school").val();
    if (school != "") {
      var school = '%22'+school+'%22'
      $("#school").val(school);
    }
  })
  
  /* cta on attorneys page */
  $(".attn_search_btn a.btn_cta").click(function() {
    if ($("#attorney_search_panel a.expander").hasClass('open')) {
      $("#attorney_search_panel #search_panel").slideUp("slow","easeOutExpo");
      $("#attorney_search_panel a.expander").removeClass('open').addClass('close');
    }else if ($("#attorney_search_panel a.expander").hasClass('close')) {
      $("#attorney_search_panel #search_panel").slideDown("slow","easeInExpo");
      $("#attorney_search_panel a.expander").removeClass('close').addClass('open');
    }
  });
  /* form cancel btn */
  $("#btn_cancel").click(function() {
    $("#search_panel").slideUp("slow","easeOutExpo");
    $("#attorney_search_panel a.expander").removeClass('open').addClass('close');
  });
    
  /* resource center, newsroom, jobs
  *****************************************/ 
  /* listing css */
  $(".list_item:first").css("border-top","2px dotted #ccc");

  /* blogs css */
  $(".comment:first").css("border-top","2px dotted #ccc");

  /* category blocks */
  equalHeight(".resource_category",0);
  equalHeight(".blog_category",0);

  /* subnavs */
  $("#subnav li a").each(function() {
    var href = $(this).attr("href");
    if (window.location.pathname == href) {
      $(this).addClass("current")
    }
  });
  $("#subnav ul#related_resources li:last").addClass("last");
    
  /* practice accordions
  *****************************************/
  //hide subpractices
  $('.subpractices').slideUp("fast");
  
  //global buttons
  $('.global_expand').click(function(){
    $('.subpractices,.accordion_panel').slideDown("slow","easeInExpo");
    $('span.expander').removeClass('close').addClass('open');
  })
  $('.global_close').click(function(){
    $('.subpractices,.accordion_panel').slideUp("slow","easeOutExpo");
    $('span.expander').removeClass('open').addClass('close');
  })
  
  //accordion interactions
  $('h3.practice').click(function(){
    //see if the panel is open already
    if ($(this).children('span.expander').hasClass('open')) {
      $(this).next('.subpractices').slideUp("slow","easeOutExpo");
      $(this).children('span.expander').addClass('close').removeClass('open');
    }else if ($(this).children('span.expander').hasClass('close')){
      //Add Open class for the parent accordion holding it
      $(this).next('.subpractices').slideDown("slow","easeInExpo");
      $(this).children('span.expander').addClass('open').removeClass('close');
    }
  });
    
  /* modal windows
  *****************************************/
  $('a.modal_form').fancybox({
    'padding': 20,
    'overlayShow': true,
    'autoDimensions': false,
    'autoScale': false,
    'width': 660,
    'height': 'auto',
    'overlayOpacity': 0.75,
    'overlayColor': '#fff',
    'hideOnOverlayClick': true,
    'hideOnContentClick': false,
    'showNavArrows': false,
    'speedIn': 500,
    'titleShow': false
  });
  
  $('a.modal_text').fancybox({
    'padding': 20,
    'overlayShow': true,
    'autoDimensions': false,
    'autoScale': false,
    'width': 500,
    'height': "auto",
    'overlayOpacity': 0.75,
    'overlayColor': '#fff',
    'hideOnOverlayClick': true,
    'hideOnContentClick': false,
    'showNavArrows': false,
    'speedIn': 500,
    'titleShow': false
  });  
  
  $('a.modal_map').fancybox({
    'padding': 20,
    'overlayShow': true,
    'autoDimensions': false,
    'autoScale': false,
    'width': 600,
    'height': "auto",
    'overlayOpacity': 0.75,
    'overlayColor': '#fff',
    'hideOnOverlayClick': true,
    'hideOnContentClick': false,
    'showNavArrows': false,
    'speedIn': 500,
    'titleShow': false
  });
  
  /*ajax forms
  *****************************************/  
  ajax_form("#signup");
  
  if ($("#comment_form #logged_in").val() == "no") { 
    ajax_comment_form("#comment_form"); 
  }

  /*reject ie6
  *****************************************/
  $.reject({
    imagePath:    '/images/browser_titles/', // Absolute path to the images
    header:       'Did you know that your Internet Browser is out of date?', // Header of pop-up window 
    paragraph1:   'Your browser is out of date, and may not be compatible with our website. A list of the most popular web browsers can be found below.',
    paragraph2:   'Click on the icons to go to the download page for each browser',
    closeMessage: 'By closing this window you acknowledge that your experience on this website may be degraded', // Message displayed below closing link 
    closeLink:    'Close This Window' // Text for closing link  
  });
  
  /* pretty scrolls
  *****************************************/
  $("a.scrollto").localScroll({
    duration: 800,
  	hash: false,
  	easing: "easeOutExpo"
  });
    
  /* force main content to be the right height */
  equalHeight(".equal_height",560); //set to the height of the sidebar in case it is the tallest
});
