/**
 * 系统管理员登录 
 */
var at;
$(function() {
	at = (new Date()).valueOf();


    function IsPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ["Android", "iPhone",
            "SymbianOS", "Windows Phone",
            "iPad", "iPod"];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
    var c = IsPC();
    if(c){
        $('#drag').drag();
    }else{
        getVerifyCodeF();
    }


	/**
	 * 点击登录按钮
	 */
	$('#btn_login').click(function(){
		doFormPost("admin_login", ctx+"/adm/login", function(data){
			if(data.error==0){
				window.location.href=ctx+data.url;
			}else{
				getVerifyCode();
				$('#errormsg').text(data.errormsg);
				if(errormsg=='验证码错误'){
					$(".alert").removeClass("alert-danger");
					$(".alert").addClass("alert-warning");
				}else{
					$(".alert").removeClass("alert-warning");
					$(".alert").addClass("alert-danger");
				}
				$(".alert").slideDown("slow");
				setTimeout('$(".alert").slideUp("slow")',10000);
			}
		});
	});
	canLoginOrNot();
});
function waitingStop(){
	if($("#waitingModal")!=null && $("#waitingModal").length>0){
		$("#waitingModal").slideUp("slow");
	}
}
function doFormPost(formName,url,successCallbackF){
	var inputs = $("form[name="+formName+"] :input");
	var data = {};
	$(inputs).each(function(){
		var input = $(this);
		if(this.tagName.toLowerCase()=='checkbox' ||
			(this.tagName.toLowerCase()=='input' && this.type.toLowerCase()=='checkbox')){
			if(data[input.attr('name')]==null){
				data[input.attr('name')] = [];
			}
			data[input.attr('name')].push(input.val());
		}else{
			data[input.attr('name')]=input.val();
		}
	});
	$.ajax({
		type:"POST",
		url:url,
		data:data,
		success:function(repJson){
			successCallbackF(repJson);
		},
		complete:function(){
			waitingStop();
		},
		beforeSend:function(){
			waiting();
		}
	});
}
function getVerifyCodeF(){
	drag = $('#drag').drag;
	var t = drag.prototype.et - drag.prototype.st;
	var s_at = drag.prototype.et - at;
	var p = $('#drag').parent();
	var html = '<label>验证码:</label>'+
    '<input name="verifyCode" class="login_input my-input-verify" value=""/>'+
    '<img class="refresh_code" id="verifyCode"/>'+
    '<span id="refresh_code" class="btn-lg glyphicon glyphicon-repeat refresh_code" style="padding:0px;"></span>';
	p.empty();
	p.append(html);
	//监听验证码输入
	$('[name=verifyCode]').on('input',function(){
		disLoginBtn();
	});
//	$('#verifyCode').attr('src',ctx+"/adm/login/verify_code/"+t+"-"+s_at);
    getVerifyCode();
	/**
	 * 点击刷新验证码按钮
	 */
	$('.refresh_code').click(function(){
		getVerifyCode();
	});
}
function getVerifyCode(){
	var timestamp = Date.parse(new Date());
	$('#verifyCode').attr('src',ctx+"/adm/login/verify_code/"+timestamp);
}
function waiting(){
	var temp = 
		'<div class="modal waiting-modal"  id="waitingModal">'+
	    '	<figure>'+
	    '		<section><div></div></section>'+
	    '		<section><div></div></section>'+
	    '		<section><div></div></section>'+
	    '		<section><div></div></section>'+
	    '		<section><div></div></section>'+
		'		<section><div></div></section>'+
		'		<section><div></div></section>'+
		'		<section><div></div></section>'+
		'	</figure>'+
		'</div>';
	if($("#waitingModal").length==0){
		$("div.container-fluid").append(temp);
	}
	var dialogWidth = $("#waitingModal").find('figure').width();
	$("#waitingModal").find('figure').css({
		'margin-top': '30%',
		'margin-left': function () {
			return ($(window).width() - dialogWidth)/2;
		}
	});
	$("#waitingModal").show();
}
/**
 * 登录按钮的禁用与否
 */
function canLoginOrNot(){
	//初始禁用登录按钮
	$('#btn_login').attr('disabled',"true");
	$('#btn_login').addClass("disabled_btn");
	$('#btn_login').removeClass("login_btn");
	//监听用户名输入
	$('#username').on('input',function(){
		disLoginBtn();
	});
	//监听密码输入
	$('#password').on('input',function(){
		disLoginBtn();
	});
}

function disLoginBtn(){
	var username=$('#username').val();
	var password=$('#password').val();
	var verifyCode=$('[name=verifyCode]').val();
	if(username!="" && password!="" && verifyCode!=undefined && verifyCode!=""){
		$('#btn_login').removeAttr("disabled");
		$('#btn_login').removeClass("disabled_btn");
		$('#btn_login').addClass("login_btn");
	}else{
		$('#btn_login').attr('disabled',"true");
		$('#btn_login').removeClass("login_btn");
		$('#btn_login').addClass("disabled_btn");
	}
}
