﻿$(document).ready(function() {
    $('table.stores').each(function() {
        var $table = $(this);
        $table.alternateRowColors($table);
        $('th', $table).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($table);
                });
            }
        });
    });
    var $table = $('#findStoreTable th:first').click();
//    var rows = $table.find('tbody > tr').get();
//    var column = 0;
//    rows.sort(function(a, b) {
//    var keyA = $(a)
//        .children('td')
//        .eq(column)
//        .text()
//        .toUpperCase();
//        var keyB = $(b).children('td').eq(column).text()
//                                            .toUpperCase();
//        if (keyA < keyB) return -1;
//        if (keyA > keyB) return 1;
//        return 0;
//    });
//    $.each(rows, function(index, row) {
//        $table.children('tbody').append(row);
//    });
//    $table.alternateRowColors()
});