var im_notshow_ad = false;
//var im_webchat_url = "http://wdknet.vicp.net/wdkchat/func/imwebchat.php";
//var im_buddylist_url = "http://wdknet.vicp.net/wdkchat/func/imbuddylist.php";
var im_webchat_url = "http://webim.myspace.cn/imwebchat.php";
var im_buddylist_url = "http://webim.myspace.cn/imbuddylist.php";

function base64encode(input) {
	var keyStr = "ABCDEFGHIJKLMNOP" +
                "QRSTUVWXYZabcdef" +
                "ghijklmnopqrstuv" +
                "wxyz0123456789+/" +
                "=";
  input = escape(input);
  var output = "";
  var chr1, chr2, chr3 = "";
  var enc1, enc2, enc3, enc4 = "";
  var i = 0;

  do {
     chr1 = input.charCodeAt(i++);
     chr2 = input.charCodeAt(i++);
     chr3 = input.charCodeAt(i++);

     enc1 = chr1 >> 2;
     enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
     enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
     enc4 = chr3 & 63;

     if (isNaN(chr2)) {
        enc3 = enc4 = 64;
     } else if (isNaN(chr3)) {
        enc4 = 64;
     }

     output = output + 
        keyStr.charAt(enc1) + 
        keyStr.charAt(enc2) + 
        keyStr.charAt(enc3) + 
        keyStr.charAt(enc4);
     chr1 = chr2 = chr3 = "";
     enc1 = enc2 = enc3 = enc4 = "";
  } while (i < input.length);

  return output;
}

function im_openWebchat(myuid, myname, mysid, siteid, destuid, destname, chatid, userparam, isnewwindow, imScriptPath){
   if(!myuid || myuid.length<=0){
     var notifystr = "%u60A8%u5C1A%u672A%u767B%u5F55%u6216%u8FDB%u884C%u4E86%u9690%u79C1%u8BBE%u7F6E%uFF0C%u8BF7%u767B%u5F55%u6216%u4FEE%u6539%u8BBE%u7F6E%u540E%u518D%u4F7F%u7528%u5728%u7EBF%u804A%u5929%u529F%u80FD%uFF01";
     alert(unescape(notifystr));
     return null;
   }
   if(typeof(im_openWebchatInPage)!="undefined" && !isnewwindow){
     return im_openWebchatInPage(destuid, destname, chatid);
   }
   if(siteid=="cnmyspace" || siteid=="xiaonengtest"){
     im_notshow_ad = true;
     flashheight = "100%";
   }
   else
   	  flashheight = "94%";
   /*var curdate = new Date();
   var curtime = curdate.getTime();
   var cookiename = "imlastopenwindow"+destuid;
   var lastopentime = im_wdkUtil.GetCookie(cookiename);
   if(lastopentime){
       if(curtime-lastopentime<5000){
         return true;
      }
   }*/

   if(typeof(chatid)=="undefined")
     var chatid = null;
   if(typeof(userparam)=="undefined")
     var userparam = null;
   var isIE = window.navigator.appName.indexOf("Microsoft") == 0;
   
   var windowWidth = 544;
   var windowHeight = 428;
   var bshowlocation = isIE?"0":"0";
   var bshowlocation = "1";
   var webchatUrl = "";
   if(typeof(im_webchat_url)!="undefined" && im_webchat_url){
     webchatUrl = im_webchat_url;
     if(myuid) webchatUrl += "?myuid="+(encodeURIComponent(myuid));
  	 if(myname) webchatUrl += "&myname="+(encodeURIComponent(myname));
  	 if(mysid) webchatUrl += "&mysid="+(encodeURIComponent(mysid));
  	 if(siteid) webchatUrl += "&siteid="+(encodeURIComponent(siteid));
  	 if(destuid) webchatUrl += "&destuid="+(encodeURIComponent(destuid));
  	 if(destname) webchatUrl += "&destname="+(encodeURIComponent(destname));
  	 if(chatid) webchatUrl += "&chatid="+(encodeURIComponent(chatid));
  	 if(userparam) webchatUrl += "&userparam="+(encodeURIComponent(userparam));
	 }
	 
	 try{
	   var webchatWindow = window.open( webchatUrl, "imwebchat_"+destuid, "width="+windowWidth+",height="+windowHeight+",toolbar=0,directories=0,menubar=0,status=0,location="+bshowlocation+",scrollbars=0,resizable=1" );
   }
   catch(e){
     webchatWindow = null;
     return null;
   }
   if(webchatWindow){
   	  try{
 		    //webchatWindow.resizeTo(windowWidth, windowHeight);
 		  }catch(e){};
 		  webchatWindow.focus();
 	 }

   if(typeof(im_webchat_url)!="undefined" && im_webchat_url)
     return webchatWindow;

   var titlestr = unescape("%u4E0E")+destname+unescape("%u804A%u5929");
   var popinstr = unescape("%u8DF3%u5165");
	 var docstr =
    '<html><head>\
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\
    <title>'+titlestr+'</title>\
    </head>\
    <body bgcolor="#ffffff" bottommargin="0" leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">\
      <div id="imwebchat" style="height:'+flashheight+'">\
      </div>\
        <script src="'+imScriptPath+'/promptwindow.js" type="text/javascript"></script>\
       	<script src="'+imScriptPath+'/flashobject_v2.js" type="text/javascript"></script>\
       	<script src="'+imScriptPath+'/openimwindow_v2.js" type="text/javascript"></script>\
      	<script type="text/javascript">\
      	var flashobj = null;\
      	var promptwindow = null;\
    		im_initChatWindow = function(){\
      		n = navigator.userAgent.toLowerCase(),\
      	  tt_ie = n.indexOf("msie") != -1 && document.all;\
      		flashobj = new FlashObject("'+imScriptPath+'/imbuddylist.swf", "webchatflash", "100%", "100%", "9", "#ffffff", false, "best");\
      		flashobj.imFlashType = "webchat";\
      		flashobj.imScriptPath = "'+imScriptPath+'";\
      		flashobj.addParam("scale", "noscale");\
      		flashobj.addParam("menu", "true");\
      		flashobj.addParam("salign", "LT");\
      		flashobj.addParam("allowScriptAccess", "always");\
      		if(tt_ie)\
      		  flashobj.addParam("wmode", "Window");\
      		else\
      		  flashobj.addParam("wmode", "Window");\
      		flashobj.addVariable("sessionid", "'+encodeURIComponent(mysid)+'");\
      		flashobj.addVariable("myuid", "'+encodeURIComponent(myuid)+'");\
      		flashobj.addVariable("myname", "'+encodeURIComponent(myname)+'" );\
      		flashobj.addVariable("siteid", "'+encodeURIComponent(siteid)+'" );\
      		flashobj.addVariable("destuid", "'+encodeURIComponent(destuid)+'" );\
      		flashobj.addVariable("destname", "'+encodeURIComponent(destname)+'" );\
      		flashobj.addVariable("chatid", "'+encodeURIComponent(chatid?chatid:'')+'" );\
      		flashobj.addVariable("userparam", "'+encodeURIComponent(userparam?userparam:'')+'" );\
      		flashobj.write("imwebchat");\
      		promptwindow = new PromptWindow();\
          promptwindow.init();\
        };\
        if(typeof(FlashObject)!="undefined")\
          im_initChatWindow();\
    	  </script>';
    	  
    if(!im_notshow_ad){
   	 docstr += 
   	 '<div style="width=100%;height=6%">\
   	  <span style="float:left;width:85%;height:100%">\
   	    <a href="http://www.xiaoneng.net" target="_blank"><img border="0" src="'+imScriptPath+'/images/650X22ad.jpg"></a>\
   	  </span>\
   	  <span style="float:right;margin:0px;padding:0px;height:100%">\
   	    <a href="#" onclick="javascript: im_popin_webchat();return false;">\
   	      <img style="width:50px;height:22px;" border="0" src="'+imScriptPath+'/images/btpopin.jpg"></img>\
   	    </a>\
   	  </span>\
   	  </div>';
    }
    docstr +='</body></html>';

    try{
      webchatWindow.document.open("text/html", "replace");
      webchatWindow.document.charset="utf-8";
      webchatWindow.document.write(docstr);
    }
    catch(e){
    	alert(e);
      return null;
    }
    webchatWindow.document.close();
    webchatWindow.focus();
    return webchatWindow;
}

function im_openBuddyList(myuid, myname, mysid, siteid, userparam, imScriptPath){
	 if (typeof(im_openwindow_inbuddylist)=='undefined'){
	    if(!im_user_id || im_user_id.length<=0){
         var notifystr = "%u60A8%u5C1A%u672A%u767B%u5F55%uFF0C%u8BF7%u767B%u5F55%u540E%u518D%u4F7F%u7528%u5728%u7EBF%u804A%u5929%u529F%u80FD%uFF01";
  	     alert(unescape(notifystr));
  	     return null;
  	   }
	 }
   if(typeof(userparam)=="undefined")
     var userparam = null;
   var isIE = window.navigator.appName.indexOf("Microsoft") == 0;

   var windowWidth = 292;
   var windowHeight = 520;
	 var bshowlocation = isIE?"0":"1";
	 var bshowlocation = "1";
	 var buddylistUrl = "";
   if(typeof(im_buddylist_url)!="undefined" && im_buddylist_url){
     buddylistUrl = im_buddylist_url;
  	 if(myuid) buddylistUrl += "?myuid="+encodeURIComponent(myuid);
  	 if(myname) buddylistUrl += "&myname="+encodeURIComponent(myname);
  	 if(mysid) buddylistUrl += "&mysid="+encodeURIComponent(mysid);
  	 if(siteid) buddylistUrl += "&siteid="+encodeURIComponent(siteid);
  	 if(userparam) buddylistUrl += "&userparam="+encodeURIComponent(userparam);
   }
	 
	 try{
  	  var buddylistWindow = window.open( buddylistUrl, "imbuddylist_"+myuid, "width="+windowWidth+",height="+windowHeight+",toolbar=0,directories=0,menubar=0,status=0,location="+bshowlocation+",scrollbars=0,resizable=1" );
	 }catch(e){
	 	 buddylistWindow = null;
	 	 return null;
	 }
	 if(buddylistWindow){
	 	  try{
 		    //buddylistWindow.resizeTo(windowWidth, windowHeight);
 		  }catch(e){};
		  buddylistWindow.focus();
   }
	 if(typeof(im_buddylist_url)!="undefined" && im_buddylist_url)
     return buddylistWindow;
	
   if(siteid=="cnmyspace" || siteid=="xiaonengtest"){
     im_notshow_ad = true;
     flashheight = "100%";
     titlestr = unescape("%u805A%u53CB%u5728%u7EBF%u804A%u5929");
   }
   else{
   	  flashheight = "94%";
   	  titlestr = unescape("%u5C0F%u80FD");
   }
   var popinstr = unescape("%u8DF3%u5165");
	 var docstr =
    '<html>\
     <head>\
     	<meta http-equiv=Content-Type content="text/html;  charset=utf-8">\
     	<title>'+titlestr+'</title>\
     </head>\
     <body bgcolor="#ffffff" bottommargin="0" leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">\
       <div id="imbuddylist" style="height:'+flashheight+'">\
       </div>\
        <script src="'+imScriptPath+'/promptwindow.js" type="text/javascript"></script>\
       	<script src="'+imScriptPath+'/flashobject_v2.js" type="text/javascript"></script>\
       	<script src="'+imScriptPath+'/openimwindow_v2.js" type="text/javascript"></script>\
       	<script type="text/javascript">\
       	var flashobj = null;\n\
      	var promptwindow = null;\n\
        im_initBuddylistWindow = function(){\n\
          n = navigator.userAgent.toLowerCase(),\n\
      	  nv = navigator.appVersion;\n\
      	  tt_ie = n.indexOf("msie") != -1 && document.all;\n\
       		flashobj = new FlashObject("'+imScriptPath+'/imwdkbuddylist.swf", "buddylistflash", "100%", "100%", "9", "#ffffff", false, "best");\n\
      		flashobj.imFlashType = "buddylist";\n\
      		flashobj.imScriptPath = "'+imScriptPath+'";\
       		flashobj.addParam("scale", "noscale");\n\
       		flashobj.addParam("menu", "true");\n\
       		flashobj.addParam("salign", "LT");\n\
       		flashobj.addParam("allowScriptAccess", "always");\n\
       		if(tt_ie)\n\
      		  flashobj.addParam("wmode", "Opaque");\n\
      		else\n\
      		  flashobj.addParam("wmode", "Window");\n\
       		flashobj.addVariable("sessionid", "'+encodeURIComponent(mysid)+'");\n\
       		flashobj.addVariable("myuid", "'+encodeURIComponent(myuid)+'");\n\
       		flashobj.addVariable("myname", "'+encodeURIComponent(myname)+'" );\n\
       		flashobj.addVariable("mytitle", "'+encodeURIComponent(titlestr)+'" );\n\
       		flashobj.addVariable("siteid", "'+encodeURIComponent(siteid)+'" );\n\
       		flashobj.addVariable("userparam", "'+encodeURIComponent(userparam?userparam:'')+'" );\
       		flashobj.write("imbuddylist");\n\
       		var promptwindow = new PromptWindow();\n\
        }\n\
        if(typeof(FlashObject)!="undefined")\n\
          im_initBuddylistWindow();\n\
     	</script>\n';
     if(!im_notshow_ad){
     	docstr +=
     	 '<div style="width=100%;height=6%">\
    	  <span style="float:left;width=85%;height:100%">\
    	    <a href="http://www.xiaoneng.net" target="_blank"><img border="0" src="'+imScriptPath+'/images/chatad_280x70.jpg"></a>\
    	  </span>\
    	  <span style="float:right;margin:0px;padding:0px;width=15%;height:100%">\
    	    <a href="#" onclick="javascript: im_popin_buddylist();return false;">\
    	      <img style="width:50px;height:22px;" border="0" src="'+imScriptPath+'/images/btpopin.jpg"></img>\
    	    </a>\
    	  </span>\
    	</div>';
     }
    docstr +=	
     '</body>\
     </html>';
    try{
      buddylistWindow.document.open("text/html", "replace");
      buddylistWindow.document.charset="utf-8";
      buddylistWindow.document.write(docstr);
    }
    catch(e){
      return null;
    }
    buddylistWindow.document.close();
    buddylistWindow.focus();
    return buddylistWindow;

}


//buddylist flash will call this function when user double click buddy name in buddylist
function imOpenChatWindow(destuid, destname, myuid, myname, mysid, siteid, userparam){
   if(typeof(im_openWebchatInPage)!="undefined"){
     im_openWebchatInPage(destuid, destname);
     return true;
   }
   else{
     if(flashobj)
       im_openWebchat(myuid, myname, mysid, siteid, destuid, destname, null, userparam, true, flashobj.imScriptPath, "")
   }
}


//webchat flash will call this function when user  click buddylist button
function OnWebchatOpenBuddylist(myuid, myname, mysid, siteid, userparam){
  if(typeof(wdkfloat)!="undefined"){
    if(wdkfloat)
      wdkfloat.showWindow();
  }
  else{
    if(flashobj)
      im_openBuddyList(myuid, myname, mysid, siteid, userparam, flashobj.imScriptPath);
  }
}
