var winFocus = true;

var chatConf = {
    sys:20000,  // system interval 10s
    osys:40000, // overtime interval 30s
    msg:5000,    // msg interval 3s		
    title:"开心交友",
    msgTitle:"【新消息】"
};
var chatAction = {
    open:false,  // status of interval
    active:true, // active or stop requst
    titleActive:false,
    lmsgt:0      // last get msg time
}
chatAction.request = function(){
    if(chatAction.open) return;
    chatAction.open = true;
    if($.chat.visible){
        $.chat.get_msg();
        clearInterval($.chat.get_msg_interval);
        $.chat.get_msg_interval = setInterval("$.chat.get_msg();", chatConf.msg);
    }else{
        $.chat.get_sys_msg_count();
        clearInterval($.chat.get_sys_count_interval);
        $.chat.get_sys_count_interval = setInterval("$.chat.get_sys_msg_count()", chatConf.sys);
    }
//    $("#debug").append("恢复轮询 chatAction.open:"+chatAction.open+"   winFocus:"+winFocus+"<br />");
}
chatAction.disrequest = function(){
    chatAction.open = false;
    clearInterval($.chat.get_msg_interval);
    $.chat.get_sys_msg_count();
    clearInterval($.chat.get_sys_count_interval);
    $.chat.get_sys_count_interval = setInterval("$.chat.get_sys_msg_count()", chatConf.osys);
//    $("#debug").append("延时轮询 chatAction.open:"+chatAction.open+"   winFocus:"+winFocus+"<br />");

}
chatAction.focus = function(e){
    winFocus=true;
//    $("#debug").append("获取焦点<br />");
    if($.chat.visible){
        chatAction.active = true;
        chatAction.request();
    }else{
        if(chatAction.active){chatAction.request();}else{return};
    }
}
chatAction.blur = function(e){
    if($.browser.msie){
		if(e.clientX < 0 || e.clientY < 0 || e.clientX > $("html").width() || e.clientY > $("html").height()){}else{return;}
	}
    winFocus=false;
//    $("#debug").append("失去焦点<br />");
}
chatAction.stopSys = function(){
    clearInterval($.chat.get_sys_count_interval);
    chatAction.active = false;
    chatAction.open = false;
    if($.chat.visible){
        $.chat.get_msg2();
    }
//    $("#debug").append("停止系统轮询<br />");
}
chatAction.activeTitle = function(){
    if(chatAction.titleActive) return;
    chatAction.titleActive = true;
    var title = chatConf.title;
    var msg = chatConf.msgTitle;
    clearInterval($.chat.active_title_interval);
    var count = 1;
    $.chat.active_title_interval = setInterval(function(){
        if(count++%2){
            //document.title = msg;
            //parent.document.title = msg;
        }else{
            //document.title = title;
            //parent.document.title = title;
        }
    },1000);
}
chatAction.stopTitle = function(){
    if(!chatAction.titleActive) return;
    chatAction.titleActive = false;
    clearInterval($.chat.active_title_interval);
}
chatAction.activeSys = function(){
    if(chatAction.active) return;
    chatAction.active = true;
    chatAction.open = true;
//    $("#debug").append("激活系统轮询<br />");
}
chatAction.init = function(){
    if($.browser.msie){
		$("html,body,div,dir,a,select,input,textarea,span").bind("blur",chatAction.blur)
            .bind("focus",chatAction.focus);
        $(document).bind("blur",chatAction.blur)
            .bind("focus",chatAction.focus);
	}else{
		$(window).bind("blur",chatAction.blur)
            .bind("focus",chatAction.focus);
	}
}

