$(document).ready(function() {
//     var alternateRowColors = function($table) {
//     console.log('b called');
//         $('tbody tr:odd', $table).removeClass('even').addClass('odd');
//         $('tbody tr:even', $table).removeClass('odd').addClass('even');
//     };
    
		$.preloadImages("http://images.nationmaster.com/images/opacity.gif", "http://images.nationmaster.com/images/up.gif",
		"http://images.nationmaster.com/images/down.gif");
		
    $.fn.alternateRowColors = function() {
//         $('tbody tr:odd', this).removeClass('even').addClass('odd');
//         $('tbody tr:even', this).removeClass('odd').addClass('even');
         $('tbody tr', this).removeClass('graphHl');
         $('tbody tr:odd', this).addClass('graphHl');
        return this;
    };

    $('table.sortable').each(function() {
        var $table = $(this);
        $table.alternateRowColors($table);
//        $table.find('td').remove
        $('tbody tr td', $table).removeClass('graphHl');
        $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 = $cell.text().replace(new RegExp(/[^-?0-9.]/g),"");
                    var key = parseFloat($cell.text().replace(/[^-?0-9.]/g,""));
                    //key = parseFloat(key.replace(/^[^d.]*/, ''));
                    //console.log($cell.text() + ' :: ' + key);
                    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;
                    }

                    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($table);
                    //$table.trigger('repaginate');
                    
                    if ($('.sort-indicator', this)) {
                    	$('.sort-indicator', $table).attr('src', 'http://images.nationmaster.com/images/opacity.gif');
                    	if (newDirection == 1)
                    		$('.sort-indicator', this).attr('src', 'http://images.nationmaster.com/images/up.gif');
                    	else
                    		$('.sort-indicator', this).attr('src', 'http://images.nationmaster.com/images/down.gif');
                    }
                    return false;
                });
            }
        });
    });
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}