Window.Bootstrap = function()
{
    // Fire off the main app
    if(Window.Main === undefined) {
        // alert('core.js - Window.Main not defined');
    }
    else {
        Window.Main();
    }
}; // end Bootstrap


// Custom Javascript goes here. Be sparing and stick to existing
// patterns.
// This function is called by HEADER when google has loaded all the
// javascript libraries. Don't monkey with it unless you know what you
// are doing. Right now it loads the YUI tabView and dataTable widgets
// and then turns control over to CoreMain
Window.PreBootstrap = function()
{
    var loader = new YAHOO.util.YUILoader({
    require: ["datatable", "tabview", "editor", "calendar", "container"],
    loadOptional: true,
    timeout: 10000,
    combine: true,
    onSuccess: Window.PostBoostrap
    });
    loader.insert();
}; // end PreBootstrap


// This function fires when the application javascript libraries are
// all fully loaded. The last thing it should do is call
// Window.CoreMain which can be defined on a page by page basis in the
// template files
Window.PostBoostrap = function()
{
    // Setup a YUI namespace
    YAHOO.namespace('Core');

    // Initialize any JQuery things here
    YAHOO.Core.JQueryInit = function() {
    // This is used to open external links properly
    $('A[rel="external"]').click( function() {
    window.open( $(this).attr('href') );
    return false;
    });
    }(); // end JQueryInit
    //YAHOO.Core.JQueryInit();

    // Initialize any YUI things here
    YAHOO.Core.YUIInit = function() {
    }();
    //YAHOO.Core.YUIInit();

    // Fire off the main app
    Window.CoreMain();
}; // end PostBoostrap


// Generic Ajax Update Method
Window.ajaxUpdate = function(callBackFn, newValue, url, paramString )
{
    var callback = {
    success: function(o) { callBackFn(true, newValue); },
    failure: function(o) { alert('fail'); },
    argument: []
    };
    YAHOO.util.Connect.asyncRequest('POST', url, callback, paramString );
};


// Generic YUI DataTable function
/**
 * type - String: the type of grid (instructor, course, class, partner, partnerinstructor, partnercourse. partnerclass )
 *
 * columnDefs - Array of Hashes: list of hashes defining dataTable columns: e.g
 * [
 *  {key:"courseCode", label:"Course Name", sortable:true, resizeable:true},
 *  {key:"active", label:"Is Active"},
 *  {key:"", label:"Edit", formatter:formatEdit, sortable:true, resizeable:true}
 * ];
 *
 * fieldList - Array of Strings: list of column in DataSource. E.G.
 * ['id', "courseName","courseCode", "active"]
 *
 * sortColumn: name of the datatable column to sort on paging. E.G. "code"
 *
 */
Window.GenericGrid = function(gridId, url, columnDefs, fieldList, sortColumn )
{
    var myDataSource = new YAHOO.util.DataSource(url);
    myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
    myDataSource.connXhrMode = "queueRequests";
    myDataSource.responseSchema = {
        resultsList: "records",
        fields: fieldList
    };

    var myConfigs = {
        sortedBy:{key:sortColumn,dir:"asc"},
        paginator: new YAHOO.widget.Paginator(
            {
            rowsPerPage: 10,
            template: YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
            rowsPerPageOptions: [10,25,50,100],
            pageLinks: 5
            }
        ),
        draggableColumns:true
    };

    var myDataTable = new YAHOO.widget.DataTable(gridId, columnDefs, myDataSource, myConfigs);
    var highlightEditableCell = function(oArgs) {
        var elCell = oArgs.target;
        if(YAHOO.util.Dom.hasClass(elCell, "yui-dt-editable")) {
        this.highlightCell(elCell);
        }
    };

    myDataTable.subscribe("cellClickEvent", myDataTable.onEventShowCellEditor);
    return {
        oDS: myDataSource,
        oDT: myDataTable
    };
};


Window.ajaxUpdate = function(fnCallback,oNewValue, url, params)
{
    var callback = {
    success: function(o) { fnCallback(true, oNewValue); },
    failure: function(o) { alert('fail'); },
    argument: []
    };

    YAHOO.util.Connect.asyncRequest('POST', url, callback, params);
};


