var cookie_operate = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = $.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
          -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
          -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
          -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
          52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
          -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
          15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
          -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
          41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
      );

//编码函数
function base64encode(str) {
      var out, i, len;
      var c1, c2, c3;
      len = str.length;
      i = 0;
      out = "";
      while(i < len) {
          c1 = str.charCodeAt(i++) & 0xff;
          if(i == len) {
              out += base64EncodeChars.charAt(c1 >> 2);
              out += base64EncodeChars.charAt((c1 & 0x3) << 4);
              out += "==";
              break;
          }
          c2 = str.charCodeAt(i++);
          if(i == len) {
              out += base64EncodeChars.charAt(c1 >> 2);
              out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
              out += base64EncodeChars.charAt((c2 & 0xF) << 2);
              out += "=";
              break;
          }
          c3 = str.charCodeAt(i++);
          out += base64EncodeChars.charAt(c1 >> 2);
          out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
          out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
          out += base64EncodeChars.charAt(c3 & 0x3F);
      }
      return out;
}


function tipAlert(title,content, class_name, showtime) {
       var _div =  toElement('<div id="msg_main"  style="visibility: visible; display: block;">');           
       _div.innerHTML = '<h3><em></em><span id="winbox_tit">' + title + '</span><a title="点击关闭" class="winbox_close" href="javascript:closeTipAlert();">×</a></h3><div class="regarea">'
           +'<table class="winbox_table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td class="winbox_ico">&nbsp;</td><td>' + 
           '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td><b>'+ content +'</b></td></tr></table></td></tr></table></div>'
       _div.style.position = "absolute";
       _div.style.zIndex = "9999";
	   _div.className = class_name;
       document.body.appendChild(_div); 
       
       var client_scroll = getClientAndScroll();
       _div.style.left = (client_scroll.client_width -_div.clientWidth)/2 + client_scroll.scroll_left + "px";
       _div.style.top = (client_scroll.client_height -_div.clientHeight)/2 +client_scroll.scroll_top + "px";
       if(showtime > 0) {
           setTimeout("closeTipAlert()", showtime);
       }   
} 

function closeTipAlert(){
    var _div = document.getElementById("msg_main");
    document.body.removeChild(_div);
}
    
var toElement = (function(){
    var div = document.createElement('div');
    return function(html){
        div.innerHTML = html;
        var el = div.firstChild;
        return div.removeChild(el);
    };
})();

function getClientAndScroll(){
    
    _scrollleft = window.pageXOffset || document.documentElement.scrollLeft ||  document.body.scrollLeft;
    
    _scrolltop  = window.pageYOffset || document.documentElement.scrollTop ||  document.body.scrollTop;
    
    _clientwidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    
    _clientheight = window.innerHeight ||  document.documentElement.clientHeight || document.body.clientHeight;
   
    return {scroll_top :_scrolltop, scroll_left: _scrollleft, client_width:_clientwidth, client_height: _clientheight };
}


/**
* 登录状态验证
*/

function chkLoginInfo(){
	  $.ajax({
		type: "GET",
		async: false,
	   	url: '/index.php?controller=port&action=ssoLoginCheck',
	   	success: function(msg){
	   		
	   		if(msg == 'LOCK_FOREVER'){
	   		    mya.my_alert('Sorry,Your account has been baned due to some inappropriate posts.',function(){},function(){},false );
	 			return ;
	 		}else if(msg == 'IP_DISABLE'){
	 		    mya.my_alert('Sorry,Your IP has been baned due to some inappropriate posts.',function(){},function(){},false );
	 		    return ;
	 		} else if(msg == 'INIT_NICKNAME') {
	 		    location.href="/index.php?controller=usercenter&action=initNickname";
	 		    return ;
	 		}
	 		
	   		var login_info = eval('('+msg+')');
	 		if(login_info['userid'] != undefined && login_info['userid'] > 0){
	 			$('#nickname').html(login_info['nickname']);
	 		    
	 			$('#user_score').html(login_info['score']);
	 			$('#user_face').attr('src',login_info['user_face']);

	 			login_user = login_info['userid'];
	 			login_nickname = login_info['nickname'];
	 			if(is_admin != undefined){
	 				is_admin = login_info['is_admin'];
	 			}

	 			$('#msg').html(login_info['pm_num']);
	 			
	 			$("#getlogin").show();
	 			$("#login_panel").hide();	 			
			    $("#logininfo").html('');
			    $("#successlogin").show();	
	 		}else if(login_info['locked'] != undefined){
 				var lock_time = parseInt(login_info['locked']);
 				var days = parseInt(lock_time/(3600*24));
 			    mya.my_alert("Sorry,Your account has been baned for "+days+" days due to some inappropriate posts.",function(){},function(){},false );
	 		}
	   	}
	});
}


/*
 *	退出状态
 */
function getExitInfo(){
	$.ajax({
		type: "GET",
	   	url: '/index.php?controller=port&action=userLogOut&sso=sso',
	   	success: function(msg){
	   		    
	   		_script = eval("(" + msg + ")");
        	len = _script.length;
        	for(var i=0; i<len; i++) {
        	    createScript(_script[i]);
        	}

		    $("#logininfo").html('<br /><br />正在退出系统，请稍等');
		    $("#successlogin").hide();	
			$("#login_panel").show();
		    
		        
	   	}
	});
}

function createScript(url){
    var head = document.getElementsByTagName("head").item(0);
	oScript = document.createElement("script");
	oScript.setAttribute("src", url);
	oScript.setAttribute("type","text/javascript")
	head.appendChild(oScript);
	return oScript;
}

function showLogin() {
 	location.href = 'https://uslogin.91.com/LoginCheck.aspx?siteflag=7&action=login&backUrl=' + base64encode(location.href);	 
}

function loginwindow(){
	location.href = "https://uslogin.91.com/LoginCheck.aspx?siteflag=7&action=login&backUrl=" + base64encode(window.location.href);
}



var login_user = is_admin = login_nickname = false;
$(document).ready(function(){
    chkLoginInfo();
});




