/**
 * @author Mauricio Bastos
 */
var myOpen=function(hash){ hash.w.fadeIn(); };
var myClose=function(hash) {
	if ($("#sucessoLogin").val()=='false' || $("#sucessoLogin").val()==undefined) {
		hash.w.fadeOut('1000',function(){ hash.o.remove(); }); 
	} else {
		if ($("#sucessoLogin").val() == 'erroEnvio') {
			hash.w.fadeOut('1000',function(){ hash.o.remove(); });
			$("#sucessoLogin").attr({ value: 'abrirEsq' });
		} else {
			if ($("#sucessoLogin").val() == 'abrirEsq') {
				hash.w.fadeOut('1000', function(){
					hash.o.remove();
				});
				$("#sucessoLogin").attr({
					value: 'false'
				});
				esqueci();
			} else {
				alert($('#redirect').val());
				document.location.href = $('#redirect').val();
			}
		}
	}
};
jQuery().ready(function(){
	$('#dialog').jqm({onShow:myOpen, onHide:myClose});
	$('#dialog1').jqm({onShow:myOpen, onHide:myClose});
	$("#enviandoLogin").hide();
	
	var optionsLogin = { 
        target:        '#dialog1',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequestLogin,  // pre-submit callback 
        success:       showResponseLogin  // post-submit callback 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#login').submit(function() {
        $(this).ajaxSubmit(optionsLogin);
        // !!! Important !!!
        // always return false to prevent standard browser submit and page navigation
        return false; 
    });
	
	var optionsEsq = { 
        target:        '#dialog1',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequestEsq,  // pre-submit callback 
        success:       showResponseEsq  // post-submit callback 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#esqueci').submit(function() {
        $(this).ajaxSubmit(optionsEsq);
        // !!! Important !!!
        // always return false to prevent standard browser submit and page navigation
        return false; 
    }); 
});

// pre-submit callback 
function showRequestLogin(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 	
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 	
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue
	$("#btnEnviarLogin").hide();
	$("#enviandoLogin").show();
	
    return true; 
} 
 
// post-submit callback 
function showResponseLogin(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 	
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 	
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 	
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.');
	$("#enviandoLogin").hide();
	$("#btnEnviarLogin").show();
	var sucVal = $("#sucessoLogin").val();
	if (sucVal.indexOf("/")==-1) {
		$("#dialog1").jqmShow();
	} else {
		document.location.href = $('#redirect').val();
	}
}

// pre-submit callback 
function showRequestEsq(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 	
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 	
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue
	$("#btnEnviarEsq").hide();
	$("#enviandoEsq").show();
	
    return true; 
} 
 
// post-submit callback 
function showResponseEsq(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 	
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 	
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 	
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.');
	$("#enviandoEsq").hide();
	$("#btnEnviarEsq").show();
	$("#dialog").jqmHide();
	$("#dialog1").jqmShow();
}


function esqueci() {
	$("#dialog").jqmShow();
}
