﻿//webchat flash will call this function when it receive a chat message
function im_onWebchatReceiveChatMsg(destuid, destnick, message, msgtype, chatid){
	 msgtype = (typeof(msgtype)=="undefined")?"0":msgtype;
	 if(promptwindow){
	    var promptMsg;
	 	  if(msgtype=="0")//common message
	 	    promptMsg = destnick+": "+message;
	 	  else if(msgtype=="1")//system message
	 	    promptMsg = message;
	 	  promptwindow.startPrompt(promptMsg, true);

	 	  if(im_chat_window_manager){
	 	    var chatWindow = im_chat_window_manager.getChatWindow(destuid);
	 	    if(chatWindow){
	 	      chatWindow.onReceiveMessage(message);
	 	    }
	 	  }
	 }
}


//处理webchat flash收到消息的事件通知，包括对方打开聊窗等
function im_onWebchatReceiveEvent(destuid, destnick, eventname, eventcontent){
    if(promptwindow){
      //promptcontent = unescape("%u5DF2%u7ECF%u6253%u5F00%u804A%u5929%u7A97%u53E3");
		  //promptwindow.startPrompt(destnick+promptcontent, true);
		  promptwindow.startPrompt(eventcontent, true);
	  }
	  //if(eventname=="DestOpenWindow"){
	  //}
}

//webchat flash will call this function when it get focus
function OnFlashFocus(){
	 promptwindow.handleFocus();
}

//webchat flash will call this function when it lost focus
function OnFlashBlur(){
	promptwindow.handleBlur();
}

PromptWindow = function(){
	this.isfocus = false;
	this.isIE = true;
	this.isprompting = false;
  this.timerID = null;
	this.promptMsg = null;
	this.promptMsg2 = null;
	this.originTitle = document.title;
	this.counter = 0;
	this.autostop = true;  //当前页面如果为焦点,自动停止闪动
};

_p = PromptWindow.prototype;
_p.init = function(){
	this.isIE = window.navigator.appName.indexOf("Microsoft") == 0;
	var _this = this;
	function _handleFocus(ev){
		_this.handleFocus(ev ||window.event);
	}
	function _handleBlur(ev){
		_this.handleBlur(ev || window.event);
	}

	//function __unload(ev){return beforePageClose();};
	if(_this.isIE){
		document.attachEvent("onfocusin", _handleFocus);
		document.attachEvent("onfocusout", _handleBlur);
	}else{
		window.addEventListener("focus", _handleFocus, false);
		window.addEventListener("blur", _handleBlur, false);
	}
};
_p.dispose = function(){

};

_p.handleFocus = function(ev){
//	if(im_logger)
//    im_logger.debug("获得焦点");
  this.isfocus = true;
  if(this.autostop)
     this.stopPrompt();

}

_p.handleBlur = function(ev){
 // if(im_logger)
 //   im_logger.debug("失去焦点"); 
  var newvar = 1;
  this.isfocus = false;
}

_p.startPrompt = function(promptMsg, autostop){
	if(this.isprompting || this.isfocus)
	   return;

	if(promptMsg && promptMsg.length>0){
		promptMsg = promptMsg.substring(0,15);
		this.promptMsg = "【"+promptMsg+"】"+" - "+this.originTitle;
		this.promptMsg2 = "";
		for(var i=0; i<promptMsg.length; i++){
		  this.promptMsg2+=" ";
		}
		this.promptMsg2 = "【"+this.promptMsg2+"】"+" - "+this.originTitle;
	}

	if(autostop!=null)
		 this.autostop = autostop;
  _this = this;
  var _changeWindowTitle = function(){ _this.changeWindowTitle(); };
	this.timerID = window.setInterval(_changeWindowTitle, 800);
	this.isprompting = true;

}

_p.changeWindowTitle = function(){
	this.counter++;
	if(this.counter%2==0)
	   //document.title = this.originTitle;
	   document.title = this.promptMsg2;
	else
	   document.title = this.promptMsg;
	try{
	  if(top!=self){
		  if(this.counter%2==0)
	      //top.document.title = this.originTitle;
	      top.document.title = this.promptMsg2;
	    else
	      top.document.title = this.promptMsg;
	  }
  }
  catch(e){
  }
}

_p.stopPrompt = function(){
	if(!this.isprompting)
	   return;

	window.clearInterval(this.timerID);
	this.timerID = null;
	this.counter = 0;
  document.title = this.originTitle;
  this.isprompting = false;
  try{
    if(top!=self)
  	   top.document.title = this.originTitle;
  }
  catch(e){
  }
  
}

im_flashGoUrl = null;
//called by buddylist Falsh
function setMyClientID(flashGoUrl){
	 im_flashGoUrl  = flashGoUrl;
}

var promptwindow = null;
if(typeof(promptwindow)=="undefined" || !promptwindow){
  promptwindow = new PromptWindow();
	promptwindow.init();
}