/// http://malsup.com/jquery/form/
/// http://kotowicz.net/jquery-hijack/demo/demo.html
///----
/// looks link jQuery BlockUI Plugin (v2) works like hijack. This true and should I use it instead of hijack?

$(function() {
    var close_on_submit = function() {
        /// Check if dialog window's html in current context contains any errorlist style classes.
        if ( !$('.errorlist', this).text() && !$('.form-message', this).text() ) {
            /// No error(s) found. Close dialog window.
            this.dialog('close');
            //-- Load the current url again opposed to reload() which will try to resubmit a form already in progress.
            //-- This is important when we discover a contest entrant that is a User and not logged in.
            //-- We want to force them to log in via a modal window.
            //-- We then abandon the contest entry form they already filled out and re-present a non editable contest entry form with their profile data.
            window.location.reload();

            /// TODO :: May need to handle a redirection to another URL after succesful submit. example...
            /// redirection can be true which will reload page, false which will NOT reload page or a string which contains a url path to redirect.
            /*
            if( redirection ) {
                if( redirection != true ) {
                    window.location.href = redirection;
                } else {
                    window.location.reload();
                }
            }
            */
        }
    };

/*
http://groups.google.com/group/jquery-ui/msg/7e90cf0ccba5771f
http://www.mail-archive.com/jquery-ui@googlegroups.com/msg07183.html
http://www.mail-archive.com/jquery-ui@googlegroups.com/msg07155.html
http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call
*/

    $(".xhr-modal").click(function() {
        var mytitle = $(this).attr('title');
        var mywidth = parseInt( $(this).attr('mywidth') );
        if( !mywidth ) { mywidth = 500 }
        var myheight = parseInt( $(this).attr('myheight') );
        if( !myheight ) { myheight = 300 }
        var my_username = $(this).attr('my_username');
        var message = $(this).attr('message');
        $("#dialog").load( $(this).attr('rel'), function( response, status, xhr ) { // load remote content from its 'rel' attribute into hidden div
            if ( status == "success" ) {
                //-- Populate username field with email attribute passed to this form.
                $("#id_username").val( my_username );
                if( message ) {
                    $( message ).prependTo('#dialog');
                }
            }
            $(this)
                .dialog("destroy") // destroy dialog widget (if exists)
                .dialog({ // and recreate it with following options
                    title: mytitle,
                    modal: true,
                    height: myheight,
                    width: mywidth,
                    bgiframe: true,
                    buttons: {
                        'Save': function() {
                            f = $(this).find("form");
                             //f.data('jc', 123)
                            f.submit();
                        },
                        Cancel: function() {
                            $(this).dialog('close');
                        }
                    },
                    //beforeclose: function(){ },
                    close: function() { },
                    open: function() { $(this).hijack( close_on_submit ) } /// This is where hijack() magic happens.
                });
        });
    });

    $(".xhr-message").click(function() {
        var mytitle = $(this).attr('title');
        var mywidth = parseInt( $(this).attr('mywidth') );
        if( !mywidth ) { mywidth = 500 }
        var myheight = parseInt( $(this).attr('myheight') );
        if( !myheight ) { myheight = 300 }
        $("#dialog").load( $(this).attr('rel'), function( data, status, xhr ) { // load remote content from its 'rel' attribute into hidden div
//                     alert( xhr.status );
            $(this)
                .dialog("destroy") // destroy dialog widget (if exists)
                .dialog({ // and recreate it with following options
                    title: mytitle,
                    modal: true,
                    height: myheight,
                    width: mywidth,
                    bgiframe: true,
                    close: function() { },
                    open: function() { $(this).hijack( close_on_submit ) } /// This is where hijack() magic happens.
                });
        });
    });

    $(".xhr-confirmation").click(function() {
        $("#dialog").dialog({
            bgiframe: true,
            resizable: false,
            height:140,
            modal: true,
            overlay: {
                backgroundColor: '#000',
                opacity: 0.5
            },
            buttons: {
                'Delete': function() {
                    $(this).dialog('close');
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            }
        });
    });

});

//$().ready(function() {
//     $.get("/xhr/assign_inventory_to_crowds/", { jc: "xxx" },
//         function(data){
//             $("#id_crowds").text( data );
//     });


