// JavaScript Document




$.fn.alternateRowColors = function() {
  $('tbody tr:odd', this).removeClass('even').addClass('odd');
  $('tbody tr:even', this).removeClass('odd').addClass('even');
  return this;
};

$(document).ready(function() {

  $('table.sortable').each(function() {
    var $table = $(this);
    $table.alternateRowColors();
    $table.find('th').each(function(column) {
      var findSortKey;

      if ($(this).is('.sort-alpha')) {
        findSortKey = function($cell) {
          return $cell.find('.sort-key').text().toUpperCase() + ' ' + $cell.text().toUpperCase();
        };
      }
      else if ($(this).is('.sort-numeric')) {
        findSortKey = function($cell) {
          var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''));
          return isNaN(key) ? 0 : key;
        };
      }
      else if ($(this).is('.sort-date')) {
        findSortKey = function($cell) {
          return Date.parse('1 ' + $cell.text());
        };
      }

      if (findSortKey) {
        $(this).addClass('clickable').hover(function() {
          $(this).addClass('hover');
        }, function() {
          $(this).removeClass('hover');
        }).click(function() {
          var newDirection = 1;
          if ($(this).is('.sorted-asc')) {
            newDirection = -1;
          }

          var rows = $table.find('tbody > tr').get();

          $.each(rows, function(index, row) {
            row.sortKey = findSortKey($(row).children('td').eq(column));
          });
          rows.sort(function(a, b) {
            if (a.sortKey < b.sortKey) return -newDirection;
            if (a.sortKey > b.sortKey) return newDirection;
            return 0;
          });
          $.each(rows, function(index, row) {
            $table.children('tbody').append(row);
            row.sortKey = null;
          });

          $table.find('th').removeClass('sorted-asc').removeClass('sorted-desc');
          var $sortHead = $table.find('th').filter(':nth-child(' + (column + 1) + ')');
          if (newDirection == 1) {
            $sortHead.addClass('sorted-asc');
          } else {
            $sortHead.addClass('sorted-desc');
          }
          $table.find('td').removeClass('sorted')
            .filter(':nth-child(' + (column + 1) + ')').addClass('sorted');

          $table.alternateRowColors().trigger('repaginate');
        });
		
		
      }
    });
  });
});




/***************************************
   =FILTERING
-------------------------------------- */

$(document).ready(function() {
  
  	
$('table.filterable').each(function() {
    var $table = $(this);

    $table.find('th').each(function (column) {
      if ($(this).is('.filter-column')) {
		  
		  
        var $filters = $('<ul class="filters atozlist"></ul>');
        //var keywords = {};

        //$table.find('tbody tr td').filter(':nth-child(' + (column + 1) + ')').each(function() {
        //  keywords[$(this).text()] = $(this).text();
        // });
       
        $('<li class="filter"><a href="#">Show all pages</a></li>').click(function() {
          $table.find('tbody tr').removeClass('filtered').not('.collapsed').show();
          $(this).addClass('active').siblings().removeClass('active');
          $table.trigger('stripe');
        }).addClass('clickable active').appendTo($filters);
         
		
		
		var keywords = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'Y'];
		
			
		$.each(keywords, function(index, keyword) {	
								  
		  
		  $('<li class="filter"></li>').html('<a href="#">' + keyword + '</a>').bind('click', {'keyword': keyword}, function(event) {
            $table.find('tbody tr').each(function() {
												  
												  
			       if ($('td', this).filter(':nth-child(' + (column + 1) + ')').text().charAt(0) == event.data['keyword']) {
                      $(this).removeClass('filtered').not('.collapsed').show();
				
				  
				         
				     		
                   }
              
			       else if ($('th',this).length == 0) {
                      $(this).addClass('filtered').hide();
					  
                   }				
				
             });
			

            $(this).addClass('active').siblings().removeClass('active');
            $table.trigger('stripe');
          }).addClass('clickable').appendTo($filters);

        });	
		
				
		$filters.insertBefore($table);
		
	  };
	  
    });    
	
  }); 
  
});




