(function() {

"use strict";

$.extend($.easing,  {
	easeOutBounce : function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeOutElastic : function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic : function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	}
});


var isFolded = true, isFolding;
$("#kitchen-helper > .kitchen-helper-switch, #kitchen-helper > .kitchen-helper-header").click(function() {
	if (isFolding) { return; }
	
	isFolding = true;
	
	if (isFolded) {
		$("#kitchen-helper-popup").animate({
			height: 286
		}, 1000, "easeOutBounce", function() {
			isFolding = false;
		});
		$("#kitchen-helper").addClass("kitchen-helper-unfold");
		$("#kitchen-helper > .kitchen-helper-header").css("paddingBottom", 14);
	} else {
		$("#kitchen-helper-popup").animate({
			height: 0
		}, 1000, "easeOutBounce", function() {
			$("#kitchen-helper").removeClass("kitchen-helper-unfold");
			$("#kitchen-helper > .kitchen-helper-header").css("paddingBottom", 0);
			
			isFolding = false;
		});
	}
	
	isFolded = !isFolded;
});


var popupMask = window.popupMask = $("#popup-mask");
popupMask.show = function() {
	var t = this;
	
	t.resize().css("display", "block");
	
	t._resize = function() {
		t.resize();
	};
	
	$(window).bind("resize", t._resize);
};
popupMask.resize = function() {
	var doc = document.documentElement;
	return this.css({
		width : Math.max(doc.scrollWidth, doc.clientWidth),
		height : Math.max(doc.scrollHeight, doc.clientHeight)
	});
};
popupMask.hide = function() {
	this.css("display", "none");
	
	if (this._resize) {
		$(window).unbind("resize", this._resize);
		delete this._resize;
	}
};

var loginDialog = window.loginDialog = $("#dialog-login");
loginDialog.show = function(tips, url) {
	var doc = document.documentElement, t = this;
	url && t.attr("jumpurl", url);

	popupMask.show();
	
	if (tips) {
		$("div.tips", t).html(tips).css("display", "block");
	} else {
		$("div.tips", t).css("display", "none");
	}

	t.css({
		left : ( doc.clientWidth - t.width() ) / 2,
		top : ( doc.clientHeight - t.height() ) / 3 + (doc.scrollTop || window.pageYOffset || 0),
		visibility : "visible"
	});

	$.ajax({
		url : t.get(0).getAttribute("data-formurl"),
		success : function(ret) {
			$("#login-form-block").html(ret);
			
			var input_email = $("input[name=name]", t);
			if ( !input_email.val() ) {
				input_email.focus();
			}
			
			$("#user-login input.cancel").bind("click", function() {
				t.hide();
			});
			
			$('#user-login .captcha img').click(function() {
				refreshCaptcha('user-login');
			});

			$("#user-login").bind("submit", function(e) {
				e.preventDefault();

				var data = { }, errs = [ ], firstField;
				$("input[type=text], input[type=password],input[type=hidden]", this).each(function(i, el) {
					data[el.name] = el.value;
				});
				
				if ( !data["name"] ) {
					errs.push("请填写邮箱");
					firstField = "edit-name-1";
				}
				
				if ( !data["pass"] ) {
					errs.push("请填写密码");
					!firstField && ( firstField = "edit-pass-1" );
				}
				
				if ( !data["captcha_response"] ) {
					errs.push("请填写验证码");
					!firstField && ( firstField = "captcha_response" );
				}
				
				if (firstField != null) {
					alert( errs.join("\r\n") );
					$("input[name=" + firstField + "]").focus();
				} else {
					$.ajax({
						type: 'POST',
						url : this.action,
						data : data,
						dataType : "json",
						success : function(data) {
							if (1 == data.result) {
								/*if (location.pathname.toLowerCase() == "/user/register") {
									location.href = "/";
								} else {
									location.reload();
								}*/
								var jumpUrl = loginDialog.attr("jumpurl");
								if (jumpUrl) {
									location.href = jumpUrl;
								} else if (data.redirect) {
									location.href = data.redirect;
								} else if (/club\/weekplan\/\d+$/.test(location.href)) {
									location.href = './';
								} else {
									location.reload();
								}
							} else {
								//$("#logform_container").html(data.form);
								refreshCaptcha('user-login');
								alert(data.message);
							}
						}
					});
				}
			});
		},
		error : function() {
			t.hide();
		}
	});
	
	
};
loginDialog.hide = function() {
	popupMask.hide();
	this.css("visibility", "hidden");
};


$("#show-login").bind("click", function(e) {
	e.preventDefault();
	loginDialog.show();
});


function refreshCaptcha(id) {
	var token  = $('input[name=captcha_sid]', $('#' + id)).val();//the captcha token id
	var formId = $('input[name=form_id]', $('#' + id)).val();//the form id
	$.get('?q=club/captcha_refresh/'+formId+'/'+ token, function(res)
		{
		  var img = $(res).attr('src');
		  $('#' + id + ' div.captcha img').attr('src', img);
		}
	);
}

$(document).ready(function() {
var operMsgs = $("#oper-msgs").html();
if ( operMsgs && operMsgs.replace(/^\s+|\s+$/, "") ) {
	if (window.alertBox) {
		window.alertBox.show({
			alertText : operMsgs,
			buttons : [
				{
					buttonCode : '<input type="button" class="button01" value="确 定" />',
					onclick : function() { window.alertBox.hide(); }
				}
			]
		});
	} else {
		window.alert(operMsgs);
	}
}
});

})();
