//JQuery Setup
$(document).ready(function(){
 
  //IE6 duct tape
  $.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);
  if ($.browser.msie6) {
    $('#nav li').hover(
      function() {
        $(this).addClass("sfhover");
      },
      function() {
        $(this).removeClass("sfhover");
      }
    );
    // Add other IE6 only code here
  }
  
  // Some effects rely on an element to be initially hidden,
  // but we only hide them if the user has javascript
  $('.jshide').addClass('hide');
  
  // Open external links in new windows
  $('a[@href^="http://"]').addClass('external').attr('target', '_blank');
  
  // Open pdf links in new windows
  $('a[@href$=".pdf"]').addClass('pdf').attr('target', '_blank');


  // Add sorting behavior to the membership listing table
  $(".membership-listing table").tablesorter({
    sortList: [[0,0]],
    cssAsc: 'headerSortUp',
    cssDesc: 'headerSortDown',
    cssHeader: 'header'
  });
  
  // insert a link before the table to show the entire membership list
  $(".membership-listing table").before('<p class="small"><a href="#" class="show-all">Show all</a></p>');
  $('a.show-all').click(function(){
    $.uiTableFilter($('.membership-listing table'), '', 'State');
  });


	// wrap img.caption in a div and use the title attr as a caption  
  $("#content img.caption").each(function (i) {
  	var $this = $(this);
  	var caption = $this.attr('title');
  	var width = $this.attr('width') + 10;
		$this.wrap('<div class="inset" style="width:'+width+'px;"></div>').after("<p>" + caption + "</p>");
  });


  //clear form fields
	$('.clearme').one("focus", function() {
  		$(this).val("");
	});

  // add 'back to top' link when the content is taller than the window
  // this has to be done after flash replacement because the content height changes
  if ($('#content').height() > $(window).height()) {
    $('#content').append('<p><a href="#hd">Back to top</a></p>');
  }

  // homepage flash
  $('#flash-homepage').flash({
    src: 'flash/homepage.swf',
    width: 485,
    height: 390,
    wmode: 'transparent',
    pluginOptions: { version: 8 }
  });
  

  // flash map of membership listing
  $('#flash-membershipMap').flash({
    src: 'flash/membershipMap.swf',
    width: 695,
    height: 450,
    wmode: 'transparent',
    pluginOptions: {version: 8}
  });

});//end document.ready

function filterTableByState(stateAbbr){
  $.uiTableFilter($('.membership-listing table'), stateAbbr, 'State');
}

function filterTableByCountry(country){
  $.uiTableFilter($('.membership-listing table'),'AB NB', 'State');
}
