var _wink_flag = 0;
var _msg_type = "";
function star_wink() {
	switch(_wink_flag){
		case 0:
			document.title = '【新消息】' + _msg_type + " - 72Ren 拜师网";
			_wink_flag=1;
			break;
		case 1:
			document.title = '【　　　】' + _msg_type + " - 72Ren 拜师网";
			_wink_flag=0;
			break;
	}
	setTimeout("star_wink()", 500);
}

function showMsg(showWink, msgStatus) {
	if (!msgStatus) {
		commonFacade.loadMsgStatus(function(msgStatus){
			setTimeout (function() {showMsg (showWink, msgStatus);}, Math.round(Math.random() * 60000) + 30000);
		});
		return;
	} else {
	
			setTimeout (function() {showMsg(true);}, Math.round(Math.random() * 60000) + 30000);
			$("#newmsgs").hide();
			$("#nonewmsgs").show();
			
			$("#notlogined").hide();
			$("#logined").show();
			

		var haveNew = false;
		_msg_type = "";
		if(msgStatus.newShortMsg>0){
			$("#newmsgs_list > ul >li:nth-child(1) span").html(msgStatus.newShortMsg);
			$("#newmsgs_list > ul >li:nth-child(1)").show();
			haveNew = true;
			_msg_type += " 短消息 ";
		}
		
		if(msgStatus.newSysMsg>0){
			$("#newmsgs_list > ul >li:nth-child(2) span").html(msgStatus.newSysMsg);
		    $("#newmsgs_list > ul >li:nth-child(2)").show();
			haveNew = true;
			_msg_type += " 系统消息 ";
		}
		  
		
		
		$("#notlogined").hide();
		$("#logined").show();

	      if (haveNew) {
			   $("#nonewmsgs").hide();
		       $("#newmsgs").show();
				star_wink();
			    alarm("muyu.mp3");
		  }else{
			   $("#newmsgs").hide();
		       $("#nonewmsgs").show();
		  }
		  
	}
}

$(document).ready(function() {
	commonFacade.loadMsgStatus(function (msgStatus) {
     if (msgStatus) { //logined
			$("#logonUserName").html(msgStatus.logonInfo.name);
			$("#loadingTitle").hide();
			$("#loginedTitle").show();
			
			$("#loadingLogin").hide();
			$("#logined").show();

			$("#loginImage").show();
			$("#notloginImage").hide();
			
			showMsg(false, msgStatus);
		} else { //not logined
			$("#loadingTitle").hide();
			$("#notloginTitle").show();

			
			$("#loadingLogin").hide();
			$("#notlogin").show();

			$("#loginImage").hide();
			$("#notloginImage").show();
		}
	});
});

function ring(mp3File) {
	var url = "/sound/" + mp3File;
	var flashvars = {
		ringPath: url,
		ringVol: "80" // 0-100
	};
	var params = {
	  menu: "false",
	  wmode: "transparent"
	};
	var attributes = {
		id: "ring",
		name: "ring"
	};
	var newDiv = "__ringDiv" + Math.floor(Math.random() * 10000);
	$("body").prepend("<div id='" + newDiv + "'></div>");
	
	swfobject.embedSWF("/flash/ring.swf", newDiv, "1", "1", "8.0.0","/flash/expressInstall.swf",flashvars,params,attributes);
}

var alarming = false;
function alarm(mp3File) {
	if(!mp3File){mp3File="muyu.mp3";}
	var soundLength = 5000; //5 seconds
	if (!alarming) {
		alarming = true;
		ring(mp3File);
		setTimeout (function () {alarming = false;}, soundLength);
	} else {
		setTimeout (function () {alarm(mp3File);}, soundLength);
	}
}
