﻿$(document).ready(function() {

    // Apply the right stylesheet
    var screenWidth = $(window).width();
    
    if (screenWidth > 1250) {
        $('link[href=/Content/screen1024.css]').attr({ href: '/Content/screen1280.css' });
    }

    // Apply striping to any details views
    $('ul.details li:odd').addClass('odd');

    // ########## INPUT MASKING  ##########
    // -> have to allow backspace for Firefox (8)
    // -> Have to use live so that events are still bound after fields have been moved

    // Integer
    $('input.integer').live('keypress', function(event) {
        if (event.which && (event.which < 48 || event.which > 57) && event.which != 8) {
            event.preventDefault();
        }
    });

    // Float - allow the decimal (46)
    $('input.float').live('keypress', function(event) {
        if (event.which && (event.which < 48 || event.which > 57) && event.which != 46 && event.which != 8) {
            event.preventDefault();
        }
    });

    // Date - allow slash (47)
    $('input.date').live('keypress', function(event) {
        if (event.which && (event.which < 47 || event.which > 57) && event.which != 8) {
            event.preventDefault();
        }
    });

    // ########## VALIDATION ###########

    // Apply error class to required fields that are empty or zero
    $('form :input').blur(function() {
        $(this).removeClass('error');
        if ($(this).hasClass('required')) {
            if (this.value == '' || parseFloat(this.value) == 0) {
                $(this).addClass('error');
            }
        }
    });

    // Regex validator for date fields
    $('input.date').blur(function() {
        if (this.value != '' && !/\d\d\/\d\d\/\d\d\d\d/.test(this.value)) {
            $(this).addClass('error');
        }
    });

    // Remove the error class on focus - by removing from the children
    // it works with multipart fields like duration start time
    $('form :input').focus(function() {
        $(this).parent('li').children('input').removeClass('error');
    });

    // ########### TABLE FEATURES ################


    // Following example from Learning jQuery pg. 159
    // Dropped in favour of tablesorter plugin

    //var alternateRowColors = function($table) {
    //    $('tbody tr:odd', $table).removeClass('even').addClass('odd');
    //    $('tbody tr:even', $table).removeClass('odd').addClass('even');
    //};

    //    $('table.sortable').each(function() {
    //        var $table = $(this);
    //        alternateRowColors($table);
    //        $('th', $table).each(function(column) {
    //            var $header = $(this);
    //            var findSortKey;
    //            if ($header.is('.sort-alpha')) {
    //                findSortKey = function($cell) {
    //                    return $cell.text().toUpperCase();
    //                };
    //            } else if ($header.is('.sort-numeric')) {
    //                findSortKey = function($cell) {
    //                    var value = parseFloat($cell.text());
    //                    return isNaN(value) ? 0 : value;
    //                };
    //            } else if ($header.is('.sort-date')) { // doesn't like dd/mm/yyyy
    //                findSortKey = function($cell) {
    //                    return Date.parse($cell.text());
    //                };
    //            }
    //            // Also need a sort-time version
    //            if (findSortKey) {
    //                $header.addClass('clickable').hover(function() {
    //                    $header.addClass('hover');
    //                }, function() {
    //                    $header.removeClass('hover');
    //                }).click(function() {
    //                    var rows = $table.find('tbody > tr').get();
    //                    $.each(rows, function(index, row) {
    //                        var $cell = $(row).children('td').eq(column);
    //                        row.sortKey = findSortKey($cell);
    //                    });
    //                    rows.sort(function(a, b) {
    //                        if (a.sortKey < b.sortKey) {
    //                            return -1;
    //                        } else if (a.sortKey > b.sortKey) {
    //                            return 1;
    //                        } else {
    //                            return 0;
    //                        }
    //                    });
    //                    $.each(rows, function(index, row) {
    //                        $table.children('tbody').append(row);
    //                    });
    //                    alternateRowColors($table);
    //                });
    //            }
    //        });
    //    });
});