var Message={
    options:{
        title_max:'Let\'s Chat Now ?',
        title_min:'Ladies',
        showtime:30,
        flashObjectId:'flashmanonlinecheck'
    },

	set: function() {//最小化与恢复状态切换Minimize/Maximize
		var set=this.minbtn.status == 1?[0,1,'block',this.char[0],'Minimize']:[1,0,'none',this.char[1],'Maximize'];
		this.minbtn.status=set[0];
		this.win.style.borderBottomWidth=set[1];
		this.content.style.display =set[2];
		this.minbtn.innerHTML =set[3]
		this.minbtn.title = set[4];
		this.win.style.top = this.getY().top;
        if (this.minbtn.status == 1){
            this.title.innerHTML = this.options.title_min+ '('+this.content.children.length+')';
        }else{
            this.title.innerHTML = this.options.title_max;
        }
	},

	close: function() {//关闭
		this.win.style.display = 'none';
        clearInterval(this.timer3);
		window.onscroll = null;
	},
	setOpacity: function(x) {//设置透明度
		var v = x >= 100 ? '': 'Alpha(opacity=' + x + ')';
		this.win.style.visibility = x<=0?'hidden':'visible';//IE有绝对或相对定位内容不随父透明度变化的bug
		this.win.style.filter = v;
		this.win.style.opacity = x / 100;
	},
	show: function() {//渐显
		clearInterval(this.timer2);
		var me = this,fx = this.fx(0, 100, 0.1),t = 0;
		this.timer2 = setInterval(function() {
			t = fx();
			me.setOpacity(t[0]);
			if (t[1] == 0) {clearInterval(me.timer2)	}
		},10);
	},
	fx: function(a, b, c) {//缓冲计算
		var cMath = Math[(a - b) > 0 ? "floor": "ceil"],c = c || 0.1;
		return function() {return [a += cMath((b - a) * c), a - b]}
	},
	getY: function() {//计算移动坐标
		var d = document,b = document.body,	e = document.documentElement;
		var s = Math.max(b.scrollTop, e.scrollTop);
		var h = /BackCompat/i.test(document.compatMode)?b.clientHeight:e.clientHeight;
		var h2 = this.win.offsetHeight;
		return {foot: s + h + h2 + 2+'px',top: s + h - h2 - 2+'px'}
	},
	moveTo: function(y) {//移动动画
		clearInterval(this.timer);
		var me = this,a = parseInt(this.win.style.top)||0;
		var fx = this.fx(a, parseInt(y));
		var t = 0 ;
		this.timer = setInterval(function() {
			t = fx();
			me.win.style.top = t[0]+'px';
			if (t[1] == 0) {
				clearInterval(me.timer);
				me.bind();
			}
		},10);
	},
	bind:function (){//绑定窗口滚动条与大小变化事件
		var me=this,st,rt;
		window.onscroll = function() {
			clearTimeout(st);
			clearTimeout(me.timer2);
			me.setOpacity(0);
			st = setTimeout(function() {
				me.win.style.top = me.getY().top;
				me.show();
			},100);
			//clearTimeout(rt);
			//rt = setTimeout(function() {me.win.style.top = me.getY().top},100);	
		};
		window.onresize = function (){
			clearTimeout(rt);
			rt = setTimeout(function() {me.win.style.top = me.getY().top},100);					
		}

    	window.onload = function (){
			clearTimeout(rt);
			rt = setTimeout(function() {me.win.style.top = me.getY().top},100);					
		}       
	},
   
	init: function(showit) {//创建HTML
		function $(id) {return document.getElementById(id)};
		this.win=$('msg_win');
		var set={minbtn: 'msg_min',closebtn: 'msg_close',title: 'msg_title',content: 'msg_content'};
		for (var Id in set) {this[Id] = $(set[Id])};
		var me = this;
		this.minbtn.onclick = function() {me.set();this.blur()};
		this.closebtn.onclick = function() {me.close()};
		this.char=navigator.userAgent.toLowerCase().indexOf('firefox')+1?['_','::','×']:['0','2','r'];//FF不支持webdings字体
		this.minbtn.innerHTML=this.char[0];
		this.closebtn.innerHTML=this.char[2];
		setTimeout(function() {//初始化最先位置
			if (showit){
                me.win.style.display = 'block';                
            }else{
                me.win.style.display = 'none'; 
                me.title.innerHTML = me.options.title_max;
            }
			me.win.style.top = me.getY().foot;
			me.moveTo(me.getY().top);
		},0);

        clearInterval(this.timer3);
        this.timer3=setInterval(function(){
           for(var i=0;i<me.content.children.length;i++){
                //document.write(me.content.children[i].id); 
                //me.AddInvite(me.content.children[i].id);
                var objId=me.content.children[i].id;                
                var process=document.getElementById(objId+'_span');
                var endtime=document.getElementById(objId).endtime;
                process.style.width=Math.floor(endtime/me.options.showtime*100)+'%';
                document.getElementById(objId).endtime=endtime-1;
                if(endtime<=1){
                    me.closeInvite(objId);
                }
            
           }
        },500);
        
		return this;
	},

    showInvite: function(InviteId,womanid,firstname,lastname,photourl,birthday,flag,msg,desc,localConnFlag){   //增加邀请窗口 
        var testDate = new Date();
        if(InviteId==''){
            var InviteId = 'CW'+testDate.getTime();//+'-'+testDate.getUTCMilliseconds();
        }
		
		//检查该邀请是否存在，如果存在的话，更新内容，并延长时间
		var nod=document.getElementById(InviteId);
		if(nod){
			nod.starttime=testDate.getTime();
			nod.endtime=this.options.showtime; 
			nod.style.width='100%';
			this.callFlashClose();
			var msgcontent=document.getElementById(InviteId+'_msg');
			msgcontent.innerHTML=msg;
			return false;
		}

        var nod=document.createElement("DIV");
        nod.style.display='inline';
        nod.id=InviteId;
        nod.flag=flag;
        nod.starttime=testDate.getTime();
        nod.endtime=this.options.showtime; 
        nod.style.width='100%';

        if (this.content.children.length<=0){
            this.content.appendChild(nod);         
        }else{
            this.content.insertBefore(nod,this.content.firstChild); 
        }                
        nod.innerHTML=this.InviteBox(InviteId,womanid,firstname,lastname,photourl,birthday,flag,msg,desc,localConnFlag);
        this.moveTo(this.getY().top);       
        if(this.content.children.length==1 || this.win.style.display == 'none'){
            this.init(true);  
        }   
		//window.focus();
		if (flag==1) 
		{
			window.focus();
		}    
		
        if (this.minbtn.status == 1){
            this.title.innerHTML = this.options.title_min+'('+this.content.children.length+')';
        }      
    },

    InviteBox: function(InviteId,womanid,firstname,lastname,photourl,birthday,flag,msg,desc,localConnFlag){
		var opts;
		if (flag==1)
		{
			opts=['','startChat','fdvdb'];	
		}else{
			opts=['none','joinnow','fdvde'];
		}

		if (localConnFlag=='1')
		{
			opts[1]='startChat';
		}else{
			opts[1]='joinnow';	
		}
        		
        str =   "      <ul class='fdtxt' id='"+InviteId+"_table'>";
        str=str+"          <li class='fdvc'>";
        str=str+"              <p><a href='/my/woman.php?womanid="+womanid+"' target='_blank'><img src='"+photourl+"' width='77' height='94' border='0'></a></p>";
        str=str+"              <div class='fdvcb'><img src='/flash/img/fdpic06.gif' width='100%' height='4' id='"+InviteId+"_span'></div>";
        str=str+"              <p><a href='javascript:void 0' onclick='javascript:Message.closeInvite(\""+InviteId+"\")'>Close</a></p>";
        str=str+"          </li>";
        str=str+"          <li class='fdvd'>";
        str=str+"              <p><font><a href='/my/woman.php?womanid="+womanid+"'>"+firstname+"</a>&nbsp;&nbsp;(ID:"+womanid+")</font><br />"+birthday+", <br />"+desc+"</p>";
        str=str+"              <div class='"+opts[2]+"'>";

        //str=str+"                    <font style='display:"+opts[0]+"'>"+firstname+" says:</font>";
        str=str+"                    <p id='"+InviteId+"_msg' style='display:"+opts[0]+";overflow:auto;margin-top:3px;height:35px;width:160px'>"+msg+"</p>";

        str=str+"              </div>";
        str=str+"              <p><a href='javascript:void 0' onclick='Message."+opts[1]+"(\""+InviteId+"\",\""+womanid+"\")'><img src='/flash/img/fdpic04.gif' border='0'></a></p>";
        str=str+"          </li>";
        str=str+"      </ul>";
        return str;
    },

    closeInvite: function(objId){  //删除邀请窗口
        this.content.removeChild(document.getElementById(objId+'_table').parentNode);
        if (this.content.children.length<=0){
            this.close();           
        } 
		
		//alert(this.content.style.height);
		//this.content.style.height=this.content.children.length*151;

        if (this.minbtn.status == 1){
            this.title.innerHTML = this.options.title_min+'('+this.content.children.length+')';
        }            
        this.moveTo(this.getY().top);
        this.callFlashClose();
    },

    startChat: function(InviteId,womanid){
		var widthVal=800;
		var heightVal=600;
		var chaturl='/my/chat.php?inviteid='+InviteId+'&womanid='+womanid;
		window.open(chaturl,"chatwin"+womanid,"left="+(window.screen.width-widthVal)/2+",top="+(window.screen.height-heightVal-6)/2+",width="+widthVal+",height="+heightVal+",scrollbars=yes,resizable=yes");
		if(InviteId.length>0){
			this.closeInvite(InviteId);
		}
	},

    joinnow: function(InviteId,womanid){
		window.location.href='/my/join.php?womanid='+womanid;
		this.closeInvite(InviteId);
        //alert('join now');
    },

    addblock: function(InviteId){
        this.closeInvite(InviteId);
        alert('addblock');  
    },

	getRequestObject: function ()
	{
		var req = null;
		if (typeof XMLHttpRequest != "undefined")
			req = new XMLHttpRequest();
		if (!req && typeof ActiveXObject != "undefined")
		{
			try
			{
				req=new ActiveXObject("Msxml2.XMLHTTP");
				XMLHttpRequest = function(){return new ActiveXObject("Msxml2.XMLHTTP");}
			}
			catch (e)
			{
				try
				{
					req=new ActiveXObject("Microsoft.XMLHTTP");
					XMLHttpRequest = function(){return new ActiveXObject("Microsoft.XMLHTTP");}
				}
				catch (e2)
				{
					try {
						req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
						XMLHttpRequest = function(){return new ActiveXObject("Msxml2.XMLHTTP.4.0");}
					}
					catch (e3)
					{
						req=null;
					}
				}
			}
		}
		if(!req && window.createRequest) req = window.createRequest();		
		if (!req) alert("Request Object Instantiation failed.");			
		return req;
	},
	
	ajaxRequest:function(url){
		var r;
		if (document.body )	document.body.style.cursor = 'wait';
		window.status = 'Sending Request...';
		getobj(Div).innerHTML='<br><br><br><br><center><b>Loading...</b></center>';
		
		r = getRequestObject();
		if (!r) return false;
		
		r.open("GET", uri, true);
		
		r.onreadystatechange = function()
		{
			if (r.readyState != 4)	return;
			getobj(Div).innerHTML=r.responseText;	
			if (document.body )	document.body.style.cursor = 'default';
		}
		r.send(null);
		return true;
				
	},

	showTipWind:function(msg){
		if(msg=='killuser'){
			alert('You have been kicked out by the administrator.');
		}
		if(msg=='otherlogin'){
			alert('Your account has logged in somewhere else and made your session invalid!');
		}
		if(msg=='inactivetime'){
			alert('Your session has expired, please login again.');
		}		
	},

    callFlashClose: function(){
        var flashobj=document.getElementById(this.options.flashObjectId);
		if(flashobj){
            flashobj.closeInviteWindows();
		}
    }    
};
Message.init(false);
