var contextPath = '';
var hubId = '';
var MYURL = '';
var loginModal;
img1 = new Image(16, 16);
img1.src="style/img/spinner.gif";

img2 = new Image(220, 19);
img2.src="style/img/ajax-loader.gif";

// When DOM is ready
$(document).ready(function() {

    $("#status > form").submit(function(){

        // Hide 'Submit' Button
        $('#submit').hide();

        // Show Gif Spinning Rotator
        $('#ajax_loading').show();
        // 'this' refers to the current submitted form
        var str = $(this).serialize();

        // -- Start AJAX Call --

        $.ajax({
            type: "POST",
            url:contextPath +'/login.do?method=login',  // Send the login info to this page
            data: str,
            success: function(msg){


                $("#status").ajaxComplete(function(event, request, settings){

                    // Show 'Submit' Button
                    $('#submit').show();

                    // Hide Gif Spinning Rotator
                    $('#ajax_loading').hide();
                    if(msg == 'OK' || msg == 'ADMIN') // LOGIN OK?
                    {

                        $('#simplemodal-container').css("width","350px");
                        $('#simplemodal-container').css("height","50px");
                        $('#simplemodal-container').css("top","50%");
                        $('#simplemodal-container').css("left","40%");
                        var login_response = '<div id="logged_in" style="margin-left: 0px;">' +
                                             '<div style="width: 330px; float: left; margin-left: 0px;">' +
                                             '<div style="width: 16px; float: left;">' +
                                             '<img style="margin-top: -12px" align="absmiddle" src="style/img/spinner.gif" with="24" height="24">' +
                                             '</div>' +
                                             '<div style="margin-top: -12px; float: right; width: 310px;font-weight:bold;color:#060">'+
                                             "You were successfully logged in! <br /> Please wait ...</div></div>";

                        $('a.modalCloseImg').hide();

                        $('#simplemodal-container').css("width","350px");
                        $('#simplemodal-container').css("height","50px");

                        $(this).html(login_response); // Refers to 'status'

                        // After 3 seconds redirect the

                        if(msg == 'ADMIN')
                            MYURL = contextPath +'/pages/admin/adminHome.jsp';

                        setTimeout('redirectMethod(MYURL)', 200);

                    } else if(msg == 'verification'){
                        MYURL = contextPath +'/login.do?method=goToLogin&verification';

                        setTimeout('redirectMethod(MYURL)', 200);
                    } else if(msg == 'invalid'){
                        MYURL = contextPath +'/login.do?method=goToLogin&invalid';

                        setTimeout('redirectMethod(MYURL)', 200);
                    } else {
                        $('#login_response').html(msg);
                        $('#login_response').css("margin-left","-40px");
                    }

                });

            }

        });

        // -- End AJAX Call --
        return false;

    }); // end submit event


//    $('#login_form').dialog({
//        autoOpen: false,
//        width:360,
//        position : ["center", "center"],
//        overlay: {
//            opacity: 0.5,
//            background: "black"
//        },
//        resizable:false,
//        close:false
//    });
//    $("#titleAndClose").remove();

});  // End of doucument.redy()  ---------------------------------------------------------------------------------------

function loginDialog(url) {

        loginModal = $('#login_form').modal();
//    $('#login_form').dialog('open');
    $('#username').focus();
    MYURL = url;
}

function closeDialog() {
//    $('#login_form').dialog('close');
        loginModal.close();
}

function redirectMethod(url)
{
    if(url.indexOf('/pages/admin/adminHome.jsp') != -1 || url.indexOf('/login.do?method=goToLogin') != -1)
        window.location = url;
    else
        window.location.reload();

}

