( function($) {
	if (jQuery.browser.mozilla) {
		// do when DOM is ready
		$( function() {
			// search form, hide it, search labels to modify, filter classes nocmx and error
		});
	};
	
	$( function() {
		//code
		$("#userAjaxLogin #errorMsg").css("display", "none");
		$("#userAjaxLogin #rememberMe").change($(this).userCheckUp);

		$().ready( function() {
			// avoid submitting from form directly with incorrect parameters
			$("#userAjaxLogin").submit(function() {
				return false;
			});
			// validate the comment form when it is submitted
			// validate signup form on keyup and submit
			// var container = $("#errorCall");
			var validator = $("#userAjaxLogin").validate( {
				errorClass: "errorRed",
				errorElement: "p",
				onfocusin: function(element) {
					this.lastActive = element;
					// this.settings.unhighlight &&
					// this.settings.unhighlight.call( this, element,
					// this.settings.errorClass );
				// this.errorsFor(element).hide();
			},
			onfocusout: function(element) {
			},
			onkeyup: function(element) {
			},
			onclick: function(element) {
				//this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass );
				// this.errorsFor(element).hide();
			},
			highlight: function(element, errorClass) {
				$("#userAjaxLogin").find("p[forer=" + element.name + "]").css("color", "#F00");
				$("#userAjaxLogin").find("p[forer=" + element.name + "]").css("display", "block");
				/* set bg yellow */
				$("#userAjaxLogin").find("p[forer=" + element.name + "]").parent().addClass("errorMask");

			},
			unhighlight: function(element, errorClass) {
				$("#userAjaxLogin").find("p[forer=" + element.name + "]").css("color", "#746C61");
				$("#userAjaxLogin").find("p[forer=" + element.name + "]").css("display", "block");

				/* reset bg yellow */
				$("#userAjaxLogin").find("p[forer=" + element.name + "]").parent().removeClass("errorMask");

			},
			check: function(element) {
				$("#userAjaxLogin").find("p[forer=" + element.name + "]").css("display", "block");
			},
			rules: {
				//"username": "required",
				//"password": "required",
				"username": {
					required: true,
					minlength: 3,
					maxlength: 75
				},
				"password": {
					required: true
				}
			},
			messages: {
				"username": {
					required: "Please enter a valid e-mail address.",
					minlength: "Please enter a valid e-mail address.",
					maxlength: "Please enter a valid e-mail address.",
					email: "Please enter a valid e-mail address."
				},
				"password": {
					required: "Please fill in password."
				}

			}
			});

			$("#userAjaxLogin #forgetpassword").click( function() {
				$("#userAjaxLogin #message").html("");
				if (validator.form()) {
					$(this).userLoginJsonp();
				}
			});

			// for enter keyboard submit
			$("#userAjaxLogin :input").each( function() {
				$(this).keypress( function(e) {
					var key = window.event ? e.keyCode : e.which;
					if (key.toString() == "13") {
						$("#userAjaxLogin #forgetpassword").click();
					}
				});
			});
		});
	});

	$.fn.userCheckUp = function() {
		$(this).attr("value", $(this).attr("checked"));
	}
	
	$.fn.userLoginJsonp = function() {
		var url = $("#authCasServerLoginUrl").val();
		var qsData = {
			"username": $("#userAjaxLogin #username").val(),
			"password": $("#userAjaxLogin #password").val(),
			"rememberMe": $("#userAjaxLogin #rememberMe").val(),
			"appLogin": $("#userAjaxLogin #appLogin").val(),
			"service": $("#userAjaxLogin #loginService").val(),
			"authVia": "ajaxLogin"
		};
		$.ajax( {
			type: "get",
			url: url,
			data: qsData,
			dataType: "jsonp",
			jsonp: "jsonpcallback",
			beforeSend: function() {
				//alert("beforeSend");
			},
			success: function(msg) {
				if (msg.status == "success") {
					$.ajax( {
						type: "get",
						url: msg.url,
						dataType: "script",
						success: function(msg) {
							document.location.href = currentUrl;
						}
					});
				} else if (msg == 'errorLoginValid') {
					$("#userAjaxLogin #password").attr("value", "");
					$("#userAjaxLogin #message").html("The combination of e-mail address and password are invalid. Check that your e-mail address is in the proper format (name@domain.com) and that your password is entered correctly.");
				} else if (msg == 'errorThreeTime') {
					$("#userAjaxLogin #loginCount").attr("value", "false");
					// set panel
					$("#ajaxLogin #loginCount").attr("value", "false");
					$(".container-mid").children().hide();
					$("#userForgetForm").show();
					$("#errorThreeTime").show();
					$("#regforget").hide();
				}
			},
			complete: function(XMLHttpRequest, textStatus) {
				//alert("complete");
			},
			error: function(xhr, status) {
				if (xhr.status > 200 && xhr.status <= 500) {
					alert("error comminucation.");
				}
			}
		});
	}
})(jQuery);