function _ChatWin(_skin,_w,_h,_serviceGrpId,_isGuest,_displayMode) {
	
	var win=null;

	var _url='http://chat.jp96.com/freechat'; 

    var w=parseInt(_w);
	var h=parseInt(_h);
	if(isNaN(w)) w=475;
	if(isNaN(h)) h=344;
	if(!_skin) _skin='light';
	switch(_displayMode) 
	{
		case 1:      //min wnd
		case 2:      //max wnd
			 break;
	    default:     //hidden wnd
			 _displayMode=0;
		     break;
	}

	var me=this;

	var scroll_x=0;
	var scroll_y=0;

	var move=false; 
    var start_x=0;
    var start_y=0;
	var cmd_exec=false; //debug

    var displayMode=_displayMode;  //display mode   0 hidden wnd 1 min wnd 2 max wnd
    var maxOnce=(displayMode==2);  //market Win live max

	var doc=document.body;
	try
	{
		if ('!'==document.firstChild.tagName && (new RegExp('-//W3C//DTD XHTML\\s','ig')).test(document.firstChild.nodeValue)) doc=document.documentElement; //xhtml
	}
	catch (ex)
	{
	}

	this.StartDrag=function(obj) {
		if(!win) return;

		if(win.contentWindow.event.button==1 && win.contentWindow.event.srcElement.tagName.toUpperCase()=='TD'){
			obj.className='win_title_bar win_title_bar_active_bg';
			var chatPanel=win.contentWindow.document.getElementById("chat_panel");chatPanel.parentNode.style.display='none';
			obj.setCapture();
			move=true;
		    start_x=win.contentWindow.event.clientX;
		    start_y=win.contentWindow.event.clientY;
		} 
	}
	this.Drag=function(obj) {
		if(!win) return;

		if(move){
	       win.style.pixelLeft+=win.contentWindow.event.clientX-start_x;
		   win.style.pixelTop+=win.contentWindow.event.clientY-start_y;
		}
	}
	this.StopDrag=function(obj){
		if(!win) return;

		obj.className='win_title_bar win_title_bar_bg';
		var chatPanel=win.contentWindow.document.getElementById("chat_panel");chatPanel.parentNode.style.display='';
		obj.releaseCapture();
		move=false;
	}
	this.Init=function() {
		me.CreateWnd(); 
	}
	this.CreateWnd=function() {
	    if(win) return; 

  		try { 
			win=document.createElement('iframe');
			win.style.position='absolute';
			win.style.zIndex=1000;
			win.frameBorder=0;
			win.src='about:blank';
			win.scrolling='no';
			win.style.pixelWidth=w;
			win.style.pixelHeight=h;
			document.body.appendChild(win);
			//iframe html
			win.contentWindow.document.open();
			win.contentWindow.document.write('<html><head>');
			win.contentWindow.document.write('<link href="'+_url+'/skin/'+_skin+'/global.css" type="text/css" rel="stylesheet" />');
			win.contentWindow.document.write('</head></body>');
			win.contentWindow.document.write(''+
						'<table width="'+w+'" height="'+h+'" border="0" cellpadding="0" cellspacing="0"  id="drag_win">\n'+
						'  <tr>\n'+
						'    <td align="left" valign="top"class="win_border">\n'+
						'	<table width="100%" border="0" cellpadding="0" cellspacing="0">\n'+
						'      <tr>\n'+
						'        <td align="right" valign="middle" class="win_title_bar win_title_bar_bg" id="title_bar">\n'+
						'		<div class="title_icon">\n'+
						'		</div>\n'+
						'		<div class="title_caption">'+unescape('%u5B9E%u65F6%u5728%u7EBF%u4EA4%u6D41%u7CFB%u7EDF')+'&nbsp;v1.00\n'+
						'		</div>\n'+
						'		<div class="close_btn" id="close_btn">\n'+
						'		</div>\n'+
						'		<div class="max_btn" id="max_btn">\n'+
						'		</div>\n'+
						'		<div class="min_btn" id="min_btn">\n'+
						'		</div>\n'+
						'		</td>\n'+
						'      </tr>\n'+
						'      <tr>\n'+
						'        <td align="left" valign="top" id="chat_panel">\n'+
						'		</td>\n'+
						'      </tr>\n'+
						'    </table></td>\n'+
						'  </tr>\n'+
						'</table>\n');
			win.contentWindow.document.write('</body></html>');
			win.contentWindow.document.close();

			win.style.left=parseInt(doc.scrollLeft)+doc.clientWidth-w;
			win.style.top=parseInt(doc.scrollTop)+doc.clientHeight-h;

			var titleBar=win.contentWindow.document.getElementById("title_bar");
			titleBar.onmousedown=function() {
			   me.StartDrag(this);
			}
			titleBar.onmouseup=function() {
			   me.StopDrag(this);
			}
			titleBar.onmousemove=function() {
			   me.Drag(this);
			}
			win.contentWindow.document.getElementById('max_btn').onclick=function() {
				me.DoMax();
			}
			win.contentWindow.document.getElementById('min_btn').onclick=function() {
				me.DoMin();
			}
			win.contentWindow.document.getElementById('close_btn').onclick=function() {
				me.DoClose();
			}
			win.contentWindow.cmdQueryTimer=window.setInterval(me.QueryCmd,1000); //debug

			var dragWin=win.contentWindow.document.getElementById("drag_win");
			var chatPanel=win.contentWindow.document.getElementById("chat_panel");
			
			dragWin.style.pixelWidth=win.style.pixelWidth-2;
			dragWin.style.pixelHeight=win.style.pixelHeight-2;
			chatPanel.style.pixelHeight=dragWin.style.pixelHeight-37;
			
			var winPanel=win.contentWindow.document.createElement('iframe');
			winPanel.className='win_panel';
			winPanel.frameBorder=0;
			winPanel.src=_url+'/Chat.aspx?w='+dragWin.style.pixelWidth+'&h='+chatPanel.style.pixelHeight+'&skin='+_skin+'&service_grp_id='+_serviceGrpId+'&is_guest='+(_isGuest?1:0)+'&purl='+escape(((new RegExp("^([^#]+?)#\\S*$","g")).test(location.href))?RegExp.$1:location.href); //debug
			winPanel.scrolling='no';
			winPanel.style.pixelWidth=dragWin.style.pixelWidth;
			winPanel.style.pixelHeight=chatPanel.style.pixelHeight;
			chatPanel.appendChild(winPanel);

			window.onscroll=function() {
				me.FixAnchor();
			}
			window.onresize=function() {
				me.FixAnchor();
			}

            switch(displayMode)
			{
				case 0:   //hidden wnd
				    win.style.display='none';
				    break;
				case 1:   //min wnd
					me.DirectMin(); //require do min
				    break;
			}
  		} 
  		catch (ex){
			alert(ex.description);
  		} 	
	}
	this.QueryCmd=function() { //debug
		if(!win) return;

		if((new RegExp("^#(\\S+)$","g")).test(location.hash)) {

			if(cmd_exec) return;
			cmd_exec=true;

			try
			{
				eval('me.'+RegExp.$1+'()'); //alert(RegExp.$1);
			}
			catch (e)
			{
			}
			
			location.hash='';
			
			cmd_exec=false;
		}
	}
	this.DoClose=function() {
		if(!win) return;
   
		win.style.display='none';
	}
	this.DirectMin=function() { //new
		if(!win) return;

		var dragWin=win.contentWindow.document.getElementById("drag_win");
		var chatPanel=win.contentWindow.document.getElementById("chat_panel");

		if('none'==chatPanel.style.display) return;

		//min to bar
		chatPanel.style.display='none';
		dragWin.style.pixelHeight-=chatPanel.style.pixelHeight;
		win.style.pixelHeight-=chatPanel.style.pixelHeight;

		//place bottom position
		win.style.pixelTop=doc.scrollTop+doc.clientHeight-win.style.pixelHeight;
	}
	this.DoMin=function() {
		if(!win) return;

		var dragWin=win.contentWindow.document.getElementById("drag_win");
		var chatPanel=win.contentWindow.document.getElementById("chat_panel");

		if(0==displayMode) {
			displayMode=1;
			win.style.display='';
		}

		if('none'==chatPanel.style.display) return;

		//debug
	    var timer=null;
		var minStep=30;
	    var StepMin=function() {
			if(doc.scrollTop+doc.clientHeight-(win.style.pixelTop+minStep)<win.style.pixelHeight-chatPanel.style.pixelHeight) {
				window.clearInterval(timer);
				timer=null;

				win.style.pixelTop=doc.scrollTop+doc.clientHeight-(win.style.pixelHeight-chatPanel.style.pixelHeight);

				//min to bar
				chatPanel.style.display='none';
				dragWin.style.pixelHeight-=chatPanel.style.pixelHeight;
				win.style.pixelHeight-=chatPanel.style.pixelHeight;

				return;
			}
		
			win.style.pixelTop+=minStep;		
	    }
	    timer=window.setInterval(StepMin,25); //play min movie
	}
	this.DoMax=function() {
		if(!win) return;

		var dragWin=win.contentWindow.document.getElementById("drag_win");
		var chatPanel=win.contentWindow.document.getElementById("chat_panel");

        var isWndHidden=(0==displayMode);
		if(isWndHidden) {
			displayMode=2;
			win.style.display='';
			chatPanel.style.display='none';
		}

		if(''==chatPanel.style.display) return;

		if(!maxOnce) {
			try
			{
				//refresh talk panel from browser cache
				win.contentWindow.document.getElementsByTagName('iframe')[0].contentWindow.document.location.reload(false);	

				maxOnce=true;
			}
			catch (e)
			{
			}
		}

		//max to full
		if(!isWndHidden) dragWin.style.pixelHeight+=chatPanel.style.pixelHeight;
		chatPanel.style.display='';
		if(!isWndHidden) win.style.pixelHeight+=chatPanel.style.pixelHeight;

		//debug
	    var timer=null;
		var maxStep=30;
	    var StepMax=function() {
			if(doc.scrollTop+doc.clientHeight-(win.style.pixelTop-maxStep)>win.style.pixelHeight) {
				window.clearInterval(timer);
				timer=null;

				win.style.pixelTop=doc.scrollTop+doc.clientHeight-win.style.pixelHeight;

				return;
			}
		
			win.style.pixelTop-=maxStep;		
	    }
	    timer=window.setInterval(StepMax,25); //play max movie
	}
	this.FixAnchor=function() {
		if(!win) return;

		win.style.pixelLeft+=doc.scrollLeft-scroll_x;
		win.style.pixelTop+=doc.scrollTop-scroll_y;

		scroll_x=doc.scrollLeft;
		scroll_y=doc.scrollTop;
	}

}
function _Eval() {
	if(arguments.length<1) return;
	
	var Fun=arguments[0];
	if(!Fun) return;
	
	try {
		var args=[];
		if('function'==typeof(Fun)) {	//only fun,params is validate
			for(var i=1;i<arguments.length;i++) args[i-1]=arguments[i];
			
			return Fun.apply(null,args);	
		}
		else return eval(Fun);
	}
	catch(e){
		
	}
}
function _AddJsToEvent() {
   if(arguments.length<2) return;
   
   var EvtId=arguments[0];
   if(typeof(EvtId)!='string') return;
   var EvtHandler=_Eval(EvtId);
   if(typeof(EvtHandler)!='function' && typeof(EvtHandler)!='object') return;
         
   var ExecJs=arguments[1];
   var BeforeInsert=true;
   var execParams=[];
   if(arguments.length>2)
   {
	  BeforeInsert=arguments[2];
	  if('function'==typeof(ExecJs)) {	//only ExecJs is fun, params is validate
	  	for(var i=3;i<arguments.length;i++) execParams[i-3]=arguments[i];
	  }
   }
	
   try {
      var NewEvt=function() {
		 if(BeforeInsert) {
			try {
			   if(typeof(ExecJs)=='function') ExecJs.apply(null,execParams);
			   else eval(ExecJs);
			}
			catch(ex1) 
			{
			}
		 }
		 if(EvtHandler!=null) {
		    try {
		      var onfun=eval(EvtHandler);
			  if(typeof(onfun)=='function') onfun.apply(null);
			}
			catch(ex2) 
			{
			}
		 }
		 if(!BeforeInsert) {
			try {
			   if(typeof(ExecJs)=='function') ExecJs.apply(null,execParams);
			   else eval(ExecJs);
			}
			catch(ex3) 
			{
			}
		 }
	  }
	  eval(EvtId+'='+NewEvt);
   }
   catch(ex) 
   {
   }
}
//display mode: hidden 0 min 1 max 2
function JoinFreeChat(skin,w,h,serviceGrpId,isGuest,displayMode) {
	_AddJsToEvent('document.body.onload',
		function InitChatWin() {
			(new _ChatWin(skin,w,h,serviceGrpId,isGuest,displayMode)).Init();
		},false);	
}
