function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}
function copyToClipBoard(_content) {
	if($.browser.msie){
		var clipBoardContent = ""; 
		clipBoardContent += _content;
		window.clipboardData.setData("Text",clipBoardContent);
		alert("复制成功，请粘贴到你的QQ/MSN上推荐给你的好友");
	}else{
		alert('对不起，您的浏览器不支持该操作！请手动复制！');
	}
}
//收藏，支持Fox
function addfavorite(url,name)
{
   if (document.all)
   {
      window.external.addFavorite(url,name);
   }
   else if (window.sidebar)
   {
      window.sidebar.addPanel(name,url,"");
   }
}
function getOs() { 
   if(navigator.userAgent.indexOf("MSIE")>0)return 1; 
   if(navigator.userAgent.indexOf("Firefox")>0)return 2; 
   return 0; 
} 
function bookmarkit(){
    switch(getOs()){
       case 1:
        window.external.addFavorite('http://www.xtata.com','他健她康');
       break;
       case 2:
        window.sidebar.addPanel('他健她康', 'http://www.xtata.com', "");
       break;
       case 0:
        alert("加入收藏失败，请使用Ctrl+D进行添加");
       break;
    }
}
//设为首页
function setHome(obj){
	var vrl = 'http://www.xtata.com';
    try{
		obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
    }
    catch(e){
        if(window.netscape) {
                try {
                        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                }
                catch (e) {
                        alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
                }
                var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                prefs.setCharPref('browser.startup.homepage',vrl);
         }
    }
}
function messagebox(url)
{	
	if(isUndefined($('#xinxi')[0])){
		var html = '';
		html += '<div id="contai"><div id="xinxi" style="width: 674px;height:400px;">'
		/*html += '<div class="xinxinav">'
		html += '<h1>短消息</h1>'
		html += '<ul id="tags">'
		html += '<li id="selectTag"><a onclick="" href="javascript:void(0)">收件箱</a></li>'
		html += '<li><a onclick="" href="javascript:void(0)">发件箱</a></li>'
		html += '<li><a onclick="" href="javascript:void(0)">系统信息</a></li>'
		html += '<li><a onclick="" href="javascript:void(0)">发送短消息</a></li>'
		html += '</ul>'
		html += '<h2>5条未读短消息</h2>'
		html += '<h3><a href="javascript:void(0)" onclick="$(\'#xinxi\').hide()">关闭</a></h3>'
		html += '</div>'
		html += '<div id="tagContent">'
		html += '</div></div>'*/
		html += '</div></div>'
		$('body').append(html);
	}else{
		$('#xinxi').show();
	}
	$('#xinxi').asfmanDrag();
	$('#xinxi').load(url)
}
jQuery.extend({zIndex : 100});
jQuery.fn.extend({
asfmanDrag : function(callback,func)
{
   this.each(function(){
    this.posRange = {minX:0,minY:0,maxX:(document.compatMode == "CSS1Compat"?document.documentElement.clientWidth:document.body.clientWidth),maxY:(document.compatMode == "CSS1Compat"?document.documentElement.clientHeight:document.body.clientHeight)};
    this.onmousedown = function(e)
    {
       this.style.zIndex = $.zIndex++;
       this.style.position = "absolute";
       this.drag(e,callback,func);
    }
    this.drag = function(e,callback,func)
    {
        var element = this,ev = e || window.event;
        ev.rScreenX = ev.screenX;
        ev.rScreenY = ev.screenY;
        var pos = $(this).offset();
        element.dragConfig = {defaultX : parseInt(pos.left,10),defaultY : parseInt(pos.top,10),defaultW: parseInt($(this).width(),10),defaultH : parseInt($(this).height(),10)};
        document.onmouseup = function()
        {
            element.drop();
            callback && callback();
        };
        document.onmousemove = function(e)
        {
            var ev2 = e || window.event;
            if($.browser.msie&& ev2.button!=1)
            {
                return (element.drop(),callback && callback());
            }
            var mx = element.dragConfig.defaultX + (ev2.screenX - ev.rScreenX),
            my = element.dragConfig.defaultY + (ev2.screenY - ev.rScreenY),
            pr = element.posRange,mw = element.dragConfig.defaultW,mh = element.dragConfig.defaultH;
            with(element.style)
            {
                left = (mx<pr.minX?pr.minX:((mx+mw)>pr.maxX?(pr.maxX-mw):mx)) + "px";
                top = (my<pr.minY?pr.minY:((my+mh)>pr.maxY?(pr.maxY-mh):my)) + "px";
            }
            func && func();
                                  return false;
        };
        document.onselectstart = function(){return false;};
    }
    this.drop = function()
    {
        document.onmousemove = document.onselectstart = document.onmouseup = null;
    }
   })
}
})

function send_message(url)
{
	var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	var pmheight = clientHeight * 0.9;
	var pmheight = 475;
	SMessage('','<iframe id="sendMessageIframe" src="'+url+'" width="808" height="'+pmheight+'" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>',810,pmheight,'短消息');
	$('#sendMessageIframe').attr("src",url);
}

function SMessage(id,html,width,height,title,withoutHeader)
{
	var content = '';
		content += '<div class="tanchu">'
		if(!withoutHeader)content += '	<h1><div>'+title+'</div><span><a href="javascript:void(0)" onclick="unblock()">关闭</a></span></h1>'
		content += html;
		content += '</div>'
	$.blockUI.defaults.overlayCSS.opacity = '0';
	block(id,content,width,height,'0px','0px');
}

function miniLoginWindow(id,loginCheckUrl,registerUrl,toUrl)
{
	var html = '';
	html += '<div id="msg_log_win"><div class="b2"><strong>他她网用户登陆</strong>';
	html += '<form action="'+loginCheckUrl+'" id="miniLoginWindowForm" name="logincheck" method="post" onsubmit="return false" >';
	html += '<table border="0" cellpadding="0" cellspacing="0" width="240">';	
	html += '<tr><td id="errortd" colspan="2" style="color: red;" align="left"></td></tr>';
	html += '<tr><td class="f14" width="60" height="30">用户名:</td><td width="180"><input name="username" id="username" value="" class="input_text" type="text"></td></tr>';
	html += '<tr><td class="f14" width="60" height="30">密　码:</td><td width="180"><input name="password" value="" class="input_text" type="password"></td></tr>';
	html += '<tr><td colspan="2" height="30"><input name="mem_pass" type="checkbox" checked="checked">记住我的登录状态</td></tr>';
	html += '<tr><td colspan="2" width="240"><span class="item" id="rempassItem">请确保信息正确。</span></td></tr>';
	html += '<tr><td colspan="2" height="30"><input type="submit" class="text_button" value=" 登  陆 " onclick="miniLoginCheck(\''+toUrl+'\')" /> <a href="'+registerUrl+'" target="_blank">注册新会员</a> </td></tr>';
	html += '</table></form></div></div>';
	block(id,html,280,200,'0px','0px');
}

function showLoginDiv(_toUrl)
{
	var html = '';
	var _myUrl = my_root_url;
	if(!_toUrl)_toUrl = window.location.href;
	_myUrl += '/index.php?user/minilogin__'+encodeURIComponent(_toUrl) + '.html';
	html += '<div id="login">'
	html += '	<div class="login_inside">'
	html += '		<div class="login_nav">'
	html += '			<span style="float:left">快速登陆</span>'
	html += '			<span style="float:right;padding-right:10px"><a href="javascript:void(0)" onclick="unblock()">关闭</a></span>'
	html += '		</div>'
	html += '		<div><iframe src="'+_myUrl+'" frameborder="0" width="100%" scrolling="no" />'
	html += '		</div> '
	html += '	</div>'
	html += '</div>'
	block('',html,480,200,'0px','0px');
}

function showLoginDivTest(url)
{
	var html = '';
	_myUrl = my_root_url + '/index.php?user/login__refresh/'+url+'.html';
	window.location.href = _myUrl;
}
function miniLoginCheck(_toUrl)
{
	if(!_toUrl || (_toUrl==''))_toUrl=false;
	$('#miniLoginWindowForm').ajaxSubmit({	
		success:function(msg){
    		if(parseInt(msg)==1){
    			if(_toUrl){
    				window.location.href=_toUrl
    			}else{
    				window.location.reload();
    			} 
    		}else{
    			alert('登陆失败！'+msg)
    		}   				
    	}
	})
}
function miniMsg(_id,_msg,_width,_height)
{
	$.blockUI.defaults.overlayCSS.opacity = '0';
	if(!_width)_width = 300;
	if(!_height)_height = 30;
	block(_id,_msg,_width,_height);
}
function miniMsgWithTime(_id,_msg,_time,_width,_height)
{
	miniMsg(_id,_msg,_width,_height);
	setTimeout(function(){unblock(_id)},_time*1000);
}
/*定义观察者处理对象*/
function Subject()
{
	this.observers = [];
}
Subject.prototype.add = function(obj)
{
	this.observers.push(obj);
}
Subject.prototype.notify = function()
{
	var _num = this.observers.length;
	if(_num>0)
	{
		for(var _i=0;_i<_num;_i++)
		{
			this.observers[_i].update()
		}
	}
}

function observer(functionV)
{
	this.update = functionV;
}
/**事件处理机制*/
var keyDownSubject = new Subject();
if($.browser.msie) {
		document.attachEvent("onkeydown", listenKeyDown);
} else {
		addEventListener('keydown', function(e) {listenKeyDown(e);}, true);
}
function ctrlEnter(event, functionV, onlyEnter) {
	if(isUndefined(onlyEnter)) onlyEnter = 0;
	if((event.ctrlKey || onlyEnter) && event.keyCode == 13) {
		setTimeout(functionV,0)
	}
}
function listenKeyDown(event) {
	var _num = keyDownSubject.observers.length;
	if(_num>0)
	{
		for(var _i=0;_i<_num;_i++)
		{
			keyDownSubject.observers[_i].update(event)
		}
	}
}
function ctrlEnterObserver(functionV,onlyEnter)
{
	this.update = function(event)
	{
		ctrlEnter(event, functionV, onlyEnter)
	}
}

/**
 * 重写关于用户信息的观察者模式
 * @author feifengxlq
 * @since 2008-7-22 
 * @提供新功能：1、添加cookie 2、优先直接查询cookie 3、考虑了兼容性与更改的方便性
 * @注意事项 ： 需要jquery.cookie.js的支持
*/
function LoginSubject()
{
	this.observers = [];//观察者
	this.loginUserId = 0;//登陆用户ID
	this.loginUserName = '';//登陆用户名称
}
LoginSubject.prototype.add = function(obj,_runNow)
{
	if( (this.loginUserId != 0)||($.cookie('_cookieLoginUserId')!=null) )
	{		
		if(this.loginUserId==0){
			this.loginUserId = $.cookie('_cookieLoginUserId');
			this.loginUserName = $.cookie('_cookieLoginUserName');
			_login_userId = this.loginUserId;
			_login_userName = this.loginUserName;
		}
		//this.notify();
		if(!_runNow){
			this.observers.push(obj);
		}else{
			obj.update(this.loginUserId,this.loginUserName);
		}
	}else{
		this.observers.push(obj);
	}
}
LoginSubject.prototype.notify = function()
{
	var _tmp = this.observers.pop();
	while(_tmp!=null)
	{
		_tmp.update(this.loginUserId,this.loginUserName);
		_tmp = this.observers.pop();
	}
}
/**
 * 添加cookie的缓存功能，尽可能的减少所有的查询
*/
function xtataLoginInfoNotify(_subject,_getUserInfoUrl,_cookieDomain)
{
//	if( ($.cookie('_cookieLoginUserId')==null) && (_subject.loginUserId==0) )
//	{
		//获取登陆用户ID
		$.ajax({
			url:_getUserInfoUrl,
			type:'post',
			dataType:'text',
			data:'',
			success:function(_msg){
				//alert(_msg+'TEST');
				if(parseInt(_msg)!=0){
					var _userinfo = _msg.split(',');
					if(_userinfo.length>=2)
					{
						_subject.loginUserId = _userinfo[0];
						_subject.loginUserName = _userinfo[1];
						_login_userId = _subject.loginUserId;
						_login_userName = _subject.loginUserName;
						$.cookie('_cookieLoginUserId',_userinfo[0],{expires: 0, path: '/', domain:_cookieDomain})
						$.cookie('_cookieLoginUserName',_userinfo[1],{expires: 0, path: '/', domain:_cookieDomain})
						//调用观察者					
						_subject.notify();
					}					
				}
				else{
						$.cookie('_cookieLoginUserId',null,{expires: -1, path: '/', domain:_cookieDomain});
						$.cookie('_cookieLoginUserName',null,{expires: -1, path: '/', domain:_cookieDomain});
						_subject.loginUserId = 0;
						_subject.loginUserName = '';
						_login_userId = _subject.loginUserId;
						_login_userName = _subject.loginUserName;
						_subject.notify();
				}
			}
		})
//	}else{
//		if(_subject.loginUserId==0)
//		{
//			_subject.loginUserId = $.cookie('_cookieLoginUserId');
//			_subject.loginUserName = $.cookie('_cookieLoginUserName');
//			_login_userId = _subject.loginUserId;
//			_login_userName = _subject.loginUserName;
//		}		
//		_subject.notify();
//	}
}
