﻿$(document).ready(function()
{
    $(".focus:first").focus();
});

var tju = {};

tju.get = function (url) {
    tju.ajax({ url: url, target: this });
};

tju.modal = function (options) {
    //defaults
    var target = $("<div/>");
    options.width = options.width || 450;
    options.height = options.height || 300;
    options.close = function (event, ui) { target.remove() };
    options.modal = true;

    //if no url, fill html and show dialog
    if (!options.url)
        target.html(options.html).dialog(options);
    else { //ajax call
        tju.ajax({ target: target, url: options.url });
        target.html("<img src='/content/images/loading.gif'/>").dialog(options);
    }

    return false;
};

tju.ajax = function (options) {
    //notify user of action
    $(options.target).html("<img src='/_cms/images/loading.gif'/>");

    //callbacks
    options.success = function (data) {
        $(options.target).html(data);
        $(".focus:visible:first").focus();
    };
    options.error = function (xhr, text, err) {
        $(options.target).html(xhr.responseText);
    };

    //go
    $.ajax(options);
};

tju.ajaxPost = function (options) {
    options.data = $(options.form).serialize();
    options.type = "POST";
    options.url = options.url || $(options.form).action;
    tju.ajax(options);
};

tju.closeParent = function (e) {
    $(e).parents('.ui-dialog-content').dialog('close');
    return false;
};

tju.create = function () {
    $('<div/>')
        .load('/students/create')
        .dialog({
            close: function () { $(this).remove() },
            modal: true,
            width: 740,
            height: 500
        });
    return false;
};
