// modified from http://net.tutsplus.com/tutorials/javascript-ajax/using-jquery-to-manipulate-and-filter-data/

$(document).ready(function() {
  zebraRows('tr:odd td', 'odd');

  // take href from first td in row and apply it to the row
  $('table#physician_list tbody tr')
    .mouseover(function() {
      $(this).addClass('active');
      $(this).attr({
        title: $('a:first', this).attr('title')
      });
    })
    .mouseout(function() {
      $(this).removeClass('active');
    })
    .bind('click', function() {
      window.location = $('a:first', this).attr('href');
    });

  // add classes for hover
  $('tbody tr').hover(function(){
    $(this).find('td').addClass('hovered');
  }, function(){
    $(this).find('td').removeClass('hovered');
  });

  //default each row to visible
  $('tbody tr').addClass('visible');

  //grab all header rows
  $('thead th.sortable').each(function() {
    $(this).click(function(){
      var col = $(this).parent().children().index($(this));
      
      var findSortKey = function($cell) {
        return $cell.find('.sort_key').text().toUpperCase() + ' ' + $cell.text().toUpperCase();
      };
      var sortDirection = $(this).is('.sorted_asc') ? -1 : 1;
  
      //step back up the tree and get the rows with data
      //for sorting
      var $rows = $(this).parent().parent().parent().find('tbody tr').get();
  
      //loop through all the rows and find 
      $.each($rows, function(index, row) {
        row.sortKey = findSortKey($(row).children('td').eq(col));
      });
  
      //compare and sort the rows alphabetically
      $rows.sort(function(a, b) {
      	if (a.sortKey < b.sortKey) return -sortDirection;
      	if (a.sortKey > b.sortKey) return sortDirection;
      	return 0;
      });
  
      //add the rows in the correct order to the bottom of the table
      $.each($rows, function(index, row) {
      	$('tbody').append(row);
      	row.sortKey = null;
      });
  
      //identify the column sort order
      $('thead th').removeClass('sorted_asc sorted_desc');
      var $sortHead = $('thead th').filter(':nth-child(' + (col + 1) + ')');
      sortDirection == 1 ? $sortHead.addClass('sorted_asc') : $sortHead.addClass('sorted_desc');
  
      //identify the col to be sorted by
      $('td').removeClass('sorted')
      			.filter(':nth-child(' + (col + 1) + ')')
      			.addClass('sorted');
  
      $('.visible td').removeClass('odd');
      zebraRows('.visible:even td', 'odd');
    });
  });
    

  //overrides CSS display:none property
	//so only users w/ JS will see the
	//filter box
	$('#location_filter').show();
	
  // filter rows by selected location
	$("select#location_filter").change(function(){
    $("table#physician_list tbody tr").hide().removeClass();  

    var filterArray = new Array();
    filterArray[0] = $("#location_filter :selected").text()

    $.each(filterArray, function(i){
      if (filterArray[i].toString() == "All locations" || filterArray[i].toString() == "") {
        $("table#physician_list tbody tr").show().addClass('visible');
      }
    });

    $("table#physician_list tbody tr").find("td:contains('" + filterArray[0].toString() + "')").parents("tr").show().addClass('visible');

		//reapply zebra rows
		$('.visible td').removeClass('odd');
	  zebraRows('.visible:even td', 'odd');
  });
});



//used to apply alternating row styles
function zebraRows(selector, className)
{
	$(selector).removeClass(className)
							.addClass(className);
}
