var ticket;

function getSSOLogUri(){
	var uri=window.location.href;
	var bb=encodeURIComponent(uri);
	var bas="https://sso1.nlc.cn/sso/jsp/mylib-login.jsp?appId=90037&paramUrl="
	window.location.href=bas+bb;
}


$(document).ready(function(){
	   var data={
				'access_token' : ticket,
				'timestamp':'<%=System.currentTimeMillis()%>',
				'appId' : '90037',
				};
	   
		   $.ajax({
			   async:false,
			   url:"https://sso1.nlc.cn/sso/foreign/authenManager/sso-login",
			   type: "GET",
			   dataType: 'jsonp',
			   jsonp: 'callback',
			   data : $.param(data,true),
			   success: function (obj) {
					  if(obj.state==0){
			//			alert("验证成功");
						var unm=obj.user.uid;
						if(unm!=uName){
							validateSuccess(obj.user);
						}
					  }else{
			//			 	alert("单点失败，错误代码：" + obj.state + "，失败原因：" + obj.errmsg);
							if(obj.state==27){
							}
							if(obj.state==28){
						//  validateFail();
							}
					  }
			   	}
			});
   	});
	
	
	function validateSuccess(user){
		var name=user.uid;
		var use=JSON.stringify(user);
		$.ajax({
			async: true,
			type : "post",
			url : basePath + '/ssso/validateSuccess',
			data : {use:use},
			dataType : "json",
			success : function (data) {
				if(data){
					uName=name;
					var loginHtml="<a class='a3' onclick='ssoLogout()' style='cursor:pointer;' target='_blank'>退出</a>"+
								  "<a class='a2' onClick='logValidate()' style='color:#FF4500;cursor:pointer;'>"+uName+"</a>";
								  $("#logDiv").html(loginHtml);
				}else{
				
				}
			}
		});
	}
		
	function validateFail(){
		$.ajax({
			async: true,
			type : "post",
			url : basePath + '/ssso/validateFail',
			dataType : "json",
			success : function (data) {
				var loginHTML ="<a class='a2' onclick='getSSOLogUri()'>登 录</a>"+
					           "<a class='a2' href='https://sso1.nlc.cn/sso/userRegist/toRegisteUser'>注 册</a>";
				$("#loginDiv").html(loginHTML);
			}
		});
	}	
	
	
   function logValidate(){
    	var nm=uName;
    	if(typeof nm == "undefined" || nm == null || nm == ""){
    		window.location.href = basePath + '/user/login';
    	}else{
    		window.location.href =basePath + '/MyLibrary/manager';
    	}
    }
	
    //单点--方式退出登录
	function ssoLogout(){
		var data={
				'timestamp':'<%=System.currentTimeMillis()%>',
				}
		$.ajax({
			   async:false,
			   url:"https://sso1.nlc.cn/sso/foreign/ssoManager/logout",
			   type: "GET",
			   dataType: 'jsonp',
			   jsonp: 'callback',
			   data : $.param(data,true),
			   success: function (obj) {
				  if(obj.state==0){
			//		 alert("退出成功");
					 $.post(basePath + '/ssso/logout', function(result) {
			               result = $.parseJSON(result);
			               if(result){
			            	   clearCookie();
			            	   window.location.reload();
			               }
			           }, 'text');
				  }else{
				//	 alert("退出失败，错误代码：" + obj.state + "，失败原因：" + obj.errmsg);
					//跳转到登录页
				  }
			   }
			});
	}
	
	
		//退出登陆
		function userLogout(){
           $.post(basePath + '/user/logout', function(result) {
               result = $.parseJSON(result);
           //    alert(result);
               if(result.success){
                   window.location.href = basePath + '/';
               }
           }, 'text');
		}
		
		
		//获取cookie
		function getCookie(cname) {
		    var name = cname + "=";
		    var data=document.cookie;
		    var decodedCookie = decodeURIComponent(document.cookie);
		    var ca = decodedCookie.split(';');
		    for(var i = 0; i <ca.length; i++) {
		        var c = ca[i];
		        while (c.charAt(0) == ' ') {
		            c = c.substring(1);
		         }
		         if (c.indexOf(name) == 0) {
		            return c.substring(name.length, c.length);
		         }
		     }
		    return "";
		} 
		
		
		//清除所有cookie函数
		function delSSOCookie(){
//			 alert("清除cookie");
			$.removeCookie('accessToken',{domain:'sso1.nlc.cn',path:'/'});
			$.removeCookie('ticket',{domain:'sso1.nlc.cn',path:'/'});
		}
		
		// 清除cookie 
		function clearCookie() {
		    setCookie("accessToken", "", -1); 
		}

		// 设置cookie   
		function setCookie(c_name, value, expiredays) {                   
		    var exdate = new Date();                   
		    exdate.setDate(exdate.getDate() + expiredays);                   
		    document.cookie = c_name + "=" + escape(value);     
		    expires=" + exdate.toGMTString() + ";     
		    path="/";         
		} 
