var app = {
	loading : function(is, position) {
		$('#loading').css('display', is ? 'block' : 'none');
		is && position && ($('#loading').addClass(position));
	},
	info : function(message, title, position) {
		toastr.info(message, title, {
			positionClass : 'toast-' + (position || 'top-center'),
			timeOut : 1000
		});
	},
	error : function(message, title, position) {
		toastr.error(message, title, {
			positionClass : 'toast-' + (position || 'top-center')
		});
	},
	getCookie: function(name) {
		var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
		return arr ? unescape(arr[2]) : null;
	},
	setCookie: function(name, value) {
		var exp = new Date(new Date().getTime() + 86400000);
		document.cookie = name + "="+ escape(value) + ";expires=" + exp.toGMTString();
	},
	delCookie: function(name) {
		var exp = new Date(new Date().getTime() - 1);
	    var val = app.getCookie(name);
	    document.cookie = name + "=" + val + ";expires=" + exp.toGMTString();
	},
	login : function(e, loginData) {
		var form = $('#form-wrap form');
		loginData = loginData || form.serializeObject();
		if (!loginData.username) {
			form.find('[name=username]').focus();
			app.error('请填写账号');
		} else if (!loginData.password) {
			form.find('[name=password]').focus();
			app.error('请填写密码');
		} else {
			app.loading(true);
			$.post('sso/mobile/login', loginData, function(data) {
				app.loading(false);
				if (data) {
					if (data.errMsg)
						app.error(data.errMsg, '警告');
					else if (data.content) {
						if(data.content.spaces) {
							app.showSpaceSelect(data.content.spaces, function(spaceId){
								loginData.spaceId = spaceId;
								app.login(null, loginData);
							});
						} else {
							$('#body').val(data.content.returnUrl || 'http://www.ubtob.com') ;
							app.loading(true);
							app.loginMall(data.content.data, data.content.loginUrl);
						}
					}
				} else {
					app.error('未知错误');
				}
			}, 'json');
		}
	},
	addCount: function () {
		app.returnHref();
	},
	loginMall: function (formdata, loginUrl) {
		app.loginOther(formdata, loginUrl);
	},
	loginOther: function (data, loginUrl) {
		$.ajax({
			type:"post",
			url:loginUrl,
			dataType:'jsonp',
			jsonp:'callback',
			jsonpCallback:"successCallback",
			data:data,
			success:function(json) {
				console.log("...");
				app.addCount();
			},
			error: function (json) {
				console.log(json);
				app.addCount();
			},
		});
	},
	returnHref: function () {
		var url = $('#body').val();
		window.location.href = url;
	},
	showSpaceSelect: function(spaces, callback) {
		console.log(spaces);
		var mask = $('#mask');
		var weuiActionsheet = $('#weui_actionsheet');
		weuiActionsheet.addClass('weui_actionsheet_toggle');
		mask.show()
			.focus()//加focus是为了触发一次页面的重排(reflow or layout thrashing),使mask的transition动画得以正常触发
			.addClass('weui_fade_toggle').one('click', function () {
				app.hideSpaceSelect(weuiActionsheet, mask);
        	});
		$('#actionsheet_cancel').one('click', function () {
			app.hideSpaceSelect(weuiActionsheet, mask);
		});
		mask.unbind('transitionend').unbind('webkitTransitionEnd');
		
		weuiActionsheet.find('.weui_actionsheet_menu').appendTpl('<div data-id="#{id}" class="weui_actionsheet_cell">#{name}</div>', spaces)
		weuiActionsheet.find('.weui_actionsheet_menu .weui_actionsheet_cell').click(function() {
			callback.call(null, $(this).data('id'));
		});
	},
	hideSpaceSelect: function(sheet, mask) {
		sheet.removeClass('weui_actionsheet_toggle');
		mask.removeClass('weui_fade_toggle');
		mask.one('transitionend', function () {
			mask.hide();
		}).one('webkitTransitionEnd', function () {
			mask.hide();
		});
	},
	init : function() {
		// 登录方式切换
		$('#form-wrap .btn').click(app.login);
		// 图片轮播
		app.loading(false);
	}
};
$(document).ready(function() {
	app.init();
	
	$('.x-toggle').click(app.showSpaceSelect)
});