
// this is the main object
var T = {}

T.defaults = {
    dialog: {
        position:   'center',
        autoOpen:   false,
        draggable:  true,
        resizable:  true,
        width:      450
    },
};


//
// object to hold utility functions that aren't specific
// to an area of the website
//
T.utils = function() {
    return {
        parseJSON: function (rsp) {
            return  (typeof rsp) == 'string' ? eval('(' + rsp + ')') : rsp;
        },
        // gets a dialog, creates if it doesn't exist
        getDialog: function(name) {
            var box = $('#' + name);
            if (! box.length) {
                $('body').append('<div id="' + name + '" class="dialog" style="display: none"></div>');
                box = $('#' + name);
                box.dialog(T.defaults.dialog);
            }
            return box;
        },
        showOkCancel: function(args) {
            var box = T.utils.getDialog('okCancelDialog');

            var closeAction = function() { };

            var okButton = (args['okbutton'] ? args['okbutton'] : 'okay');
            var okAction = (args['okaction'] ? args['okaction'] : closeAction);

            var negButton = (args['negbutton'] ? args['negbutton'] : 'cancel');
            var negAction = (args['negaction'] ? args['negaction'] : closeAction);

            box.dialog('option', {
                modal: true,
                buttons: {
                    'okay':     function() {
                        okAction();
                        $(this).dialog('close');
                    },
                    'cancel':   function() {
                        negAction();
                        $(this).dialog('close');
                    }
                }
            });

            if (args['title']) {
                box.dialog('option', 'title', args['title']);
            }

            box.html(args['text']);
            box.dialog('open');
        },

        // shows the error dialog
        showError: function(text, title) {
            var box = T.utils.getDialog('errorDialog');

            box.dialog('option',{
                modal:  true,
                title:  (title ? title : 'Error'),
                buttons: {
                    'okay':     function() {
                        $(this).dialog('close');
                    }
                }
            });
            box.html(text);
            box.dialog('open');
        },
        // shows the error dialog
        showNotice: function(text) {
            return T.utils.showError(text, 'Notice');
        },

        // shows a cool input box
        inputBox: function(text, callback) {

            var box = T.utils.getDialog('inputBoxDialog');

            box.dialog('option', {
                modal: true,
                buttons: {
                    'okay':     function() {
                        $(this).dialog('close');
                        // call the callbackwith the text from the textarea
                        callback($('textarea', box).val());
                    },
                    'cancel':   function() {
                        $(this).dialog('close');
                    }
                }
            });
            $('.message', box).html(text);
            box.dialog('option','title','Input Required');
            box.dialog('open');
        },

        // This thing sets up the onmouseover hilights for the table rows
        // and allows clicking anywhere in the row to cause the first link to be
        // clicked.  Used all over the place.
        initGridTable: function(table) {
            var addHovers = ! $(table).hasClass('nohover');
            var clickable = ! $(table).hasClass('noclick');

            var tbody = $('tbody:first', table);

            if (addHovers || clickable) {
                var i = 0;
                $(tbody).children('tr').each(function() {
                    var row = this;

                    // add an onclick for the whole row = the first anchor
                    if (clickable) {
                        if ($('a:first', row) != null) {
                            $(row).bind('click', function() {
                                document.location.href = $('a:first', row).attr('href');
                            });
                        }
                    }

                    // add the on-hover color changing
                    if (addHovers) {
                        $(row).bind('mouseover', function() {
                            $(row).addClass('hover');
                        });
                        $(row).bind('mouseout', function() {
                            $(row).removeClass('hover');
                        });
                    }

                    // tag the odd-number rows grey
                    if (i % 2 != 0) {
                        $(row).addClass('grey');
                    }

                    i++;
                });
            }
        }
    };
}();

