( function($) {
	if (jQuery.browser.mozilla) {
		// do when DOM is ready
		$( function() {
			// search form, hide it, search labels to modify, filter classes nocmx and error
			$('form.ajaxLogin').hide().find('p>label:not(.nocmx):not(.error)').each( function() {
				var $this = $(this);
				var labelContent = $this.html();
				var labelWidth = "200px";
				// create block element with width of label
				var labelSpan = $("<span>").css("display", "block").width(labelWidth).html(labelContent);
				// change display to mozilla specific inline-box
				$this.css("display", "-moz-inline-box")
				// remove children
						.empty()
						// add span element
						.append(labelSpan);
				// show form again
			}).end().show();
		});
	};
	
	$( function() {
		//code
		$("#errorMsg").css("display", "none");
		$("#logOutBtn").click($(this).loginOut);
		$("#reLoginBtn").click($(this).reLogin);
		$("#rememberMe").change($(this).checkUp);

		$().ready( function() {
			// avoid submitting from form directly with incorrect parameters
			$("#ajaxLogin").submit(function() {
				return false;
			});
			// validate the comment form when it is submitted
			// validate signup form on keyup and submit
			// var container = $("#errorCall");
			var validator = $("#ajaxLogin").validate( {
				errorClass: "red",
				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) {
					$("#ajaxLogin").find("label[for=" + element.name + "]").css("color", "#F00");
					$("#ajaxLogin").find("label[for=" + element.name + "]").css("display", "block");
	
					/* set bg yellow */
					/*$("#ajaxLogin").find("label[for=" + element.name + "]").parent().css( {
						background: "#FEF7D0"
					});*/
					$("#ajaxLogin").find("label[for=" + element.name + "]").parent().attr("class", "error");
				},
				unhighlight: function(element, errorClass) {
					$("#ajaxLogin").find("label[for=" + element.name + "]").css("color", "#3D362D");
					$("#ajaxLogin").find("label[for=" + element.name + "]").css("display", "block");
	
					/* reset bg yellow */
					/*$("#ajaxLogin").find("label[for=" + element.name + "]").parent().css( {
						background: "none"
					});*/
					$("#ajaxLogin").find("label[for=" + element.name + "]").parent().attr("class", "");
				},
				check: function(element) {
					$("#ajaxLogin").find("label[for=" + 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."
					}
	
				}
			});

			// check if confirm password
			// $("#ajaxLogin #password").blur(function() {
			// $("#ajaxLogin #password").valid();
			// $("#ajaxLogin #errorMsg").css("display","none");
			// });
			//			
			// $("#ajaxLogin #username").blur(function() {
			// $("#ajaxLogin #username").valid();
			// $("#ajaxLogin #errorMsg").css("display","none");
			// });
			// ${"#ajaxLogin"}.keypress(function(e) {
			// if(e.keyCode == 13){
			// if(validator.form()){
			// $(this).loginJsonp();
			// }
			// }
			// });

			$("#ajaxLogin #loginBtn").click( function() {
				$("#ajaxLogin #message").html("");
				if (validator.form()) {
					$(this).loginJsonp();
				}
			});

			// for enter keyboard submit
			$("#ajaxLogin :input").each( function() {
				$(this).keypress( function(e) {
					var key = window.event ? e.keyCode : e.which;
					if (key.toString() == "13") {
						$("#ajaxLogin #loginBtn").click();
					}
				});
			});
		});
	});

	$.fn.checkUp = function() {
		$(this).attr("value", $(this).attr("checked"));
	}
	
	$.fn.loginJsonp = function() {
		var url = $("#authCasServerLoginUrl").val();
		var qsData = {
			"username": $("#ajaxLogin #username").val(),
			"password": $("#ajaxLogin #password").val(),
			"rememberMe": $("#ajaxLogin #rememberMe").val(),
			"appLogin": $("#ajaxLogin #appLogin").val(),
			"service": $("#ajaxLogin #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 =loadSucessUrl;
						}
					});
				} else if (msg == 'errorLoginValid') {
					$("#ajaxLogin #password").attr("value", "");
					$("#ajaxLogin #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.");
					$("#ajaxLogin #message").show();
					$("#ajaxLogin label[for='username']").css("color", "red");
					$("#ajaxLogin label[for='password']").css("color", "red");
					//alert("EE");
				} else if (msg == 'errorThreeTime') {
					$("#ajaxLogin #loginCount").attr("value", "false");
					$("#login").css("display", "none");
					$("#login-success").css("display", "block");
				}
			},
			complete: function(XMLHttpRequest, textStatus) {
				//alert("complete");
			},
			error: function(xhr, status) {
				if (xhr.status > 200 && xhr.status <= 500) {
					alert("error comminucation.");
				}
			}
		});
	}
	
	$.fn.loginOut = function() {
		$("#ajaxLogout").submit();
	}

	$.fn.reLogin = function() {
		var options = {
			success: function(result) {
				if (result.success == true) {
					$("#login-success").css("display", "none");
					$("#login").css("display", "block");
					$("#login #message").html("");
					$("#ajaxLogin #username").attr("value", "");
					$("#ajaxLogin #password").attr("value", "");
					$("#ajaxLogin #loginCount").attr("value", "0");
					return;
				}
			},
			error: function(xhr, status) {
				if (xhr.status > 200 && xhr.status <= 500) {
					alert("error comminucation.");
				}
			},
			complete: function(xhr, status) {
				//var height = 130+$("#formPanl3").height(); 
				// $(window.parent.document).find("#submitPage").height(height);
			},
			dataType: "json"
		};

		$("#reLoginForm").ajaxSubmit(options);
	}
})(jQuery);