Array.prototype.remove = function(from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	this.length = from < 0 ? this.length + from : from;
	return this.push.apply(this, rest);
};
(function($){
	$.BrowserInfo = {
	    IsIE: /*@cc_on!@*/false,
	    IsIE6Under: /*@cc_on!@*/false && (parseInt(navigator.userAgent.toLowerCase().match(/msie (\d+)/)[1], 10) <= 6),
	    getWindowSize : function(){//定义浏览器窗口的宽度和高度
			return {
				width: window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth),
				height: window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight)
			};
		}
	};
	//ary.hasOwnProperty(key); 
	$.toastrNotify = {
		backupTitle : document.title,//备份标题
		scheduleQueue:[],//调度队列信息,消息时间最老的记录,放最前面
		scheduleQueueUuid:[],
		maxTipCount:3,//最大tip显示
		tipsArrayIndex : 0,
		tipItemIndex : 0,
		interval : [],//调度器对象
		timer:null,//标题闪烁的调度器
		ajaxCallbackTimer:null,//消息推送后告知服务端的调度器
		ajaxMessageQueue:[],//消息告知服务端的数组
		ajaxCallbackFunc:null,//回调函数
		messageFields:{
			uuid:'uuid',
			title:'title',
			sender:'sender_name',
			content:'content',
			type:'type',
			date:'date'
		},
		intervalTimer : 5000,//显示时间 单位毫秒
		tipWidth : 300,//提示框宽度
		tipHeight : 118,//提示框高度
		tipMarginBottom:12,//tip之间的间隔
		tipMarginRight:10,//tip右侧间隔
		tipToolBar : null,//关闭所有层 jq对象
		currentTipsArray : [],//提示框数组,存储的是html jq对象
		
		//渲染消息提示框
		resizeTips : function(){//设置消息提示框的位置 只供内部JS调用
			var ie6 = $.BrowserInfo.IsIE6Under;
			var wsize = $.BrowserInfo.getWindowSize();
			var top= 0;
			var tag = 0;
			var arrayLength = $.toastrNotify.currentTipsArray.length;
			for(var i=0;i<arrayLength;i++){
				var o = $($.toastrNotify.currentTipsArray[i]);
				if(o){
					tag++;
					var bottom = i * ($.toastrNotify.tipMarginBottom + $.toastrNotify.tipHeight);
					o.css({ position: (ie6)? "absolute" : "fixed",height:$.toastrNotify.tipHeight,  right: 5, bottom: bottom,'z-index' : "99999999" });
					$("body").append(o);
				}
			}
			//最上方的工具条
			if(tag>0){
				if($.toastrNotify.tipToolBar!=null){
					var _height = arrayLength * ($.toastrNotify.tipMarginBottom + $.toastrNotify.tipHeight) + 25 - $.toastrNotify.tipMarginBottom ;
					$($.toastrNotify.tipToolBar).css({ width:($.toastrNotify.tipWidth+2), position: (ie6)? "absolute" : "fixed", height:_height, right: 5, bottom: 0,'z-index' : "99999990" });
					$($.toastrNotify.tipToolBar).fadeIn(800);
					$.toastrNotify.startBlinkTitle('【新消息提醒】');
				}
			}else{
				$($.toastrNotify.tipToolBar).hide();
				$.toastrNotify.stopBlinkTitle();
			}
		},
		tipTypeMapping:{
			"0":{name:'系统消息',css:'fa-bell-o info'},
			"1":{name:'站内信',css:'fa-envelope-o message'}
		},
		//系统消息;站内信
		closeTip : function(index){//关闭消息提示框 只供内部JS调用
			$("#jqthiscurrenttips"+index).fadeOut(800,function(){
				//800毫秒后弹出,并重新渲染
				for(var i=0;i<$.toastrNotify.currentTipsArray.length;i++){
					if($("#jqthiscurrenttips"+index).html()==$.toastrNotify.currentTipsArray[i].html()){
						$("#jqthiscurrenttips"+index).remove();//从DOM,删除当前元素
						//从数组移除
						$.toastrNotify.currentTipsArray.remove(i);
						$.toastrNotify.tipsArrayIndex --;
						break;
					}
				}
				$.toastrNotify.resizeTips();
				$.toastrNotify.openTip();//尝试打开
			});
		},
		closeTipByInterval : function(index){//通过调度任务关闭tip
			$.toastrNotify.closeTip(index);
			clearInterval($.toastrNotify.interval[index]);
		},
		closeAllTip : function(){//关闭所有提示框
			$('.tip-item[id^="jqthiscurrenttips"]').remove();//以jqthiscurrenttips开头
			$.toastrNotify.tipsArrayIndex = 0;
			$.toastrNotify.currentTipsArray = [];
			if($.toastrNotify.scheduleQueue.length == 0){
				$($.toastrNotify.tipToolBar).hide();
				$.toastrNotify.resizeTips();
			}else{
				$.toastrNotify.openTip();//尝试打开
			}
			
		},
		startAjaxCallback:function(){
			if($.toastrNotify.ajaxCallbackTimer == null){
				if($.toastrNotify.ajaxMessageQueue && $.toastrNotify.ajaxMessageQueue.length > 0){
					function noticeServer(){
						if($.toastrNotify.ajaxMessageQueue.length == 0){
							$.toastrNotify.endAjaxCallback();
							return;
						}
						if(typeof $.toastrNotify.ajaxCallbackFunc =='function'){
							var uuidArray = [];
							for(var i=0;i<$.toastrNotify.ajaxMessageQueue.length;i++){
								var queueObj = $.toastrNotify.ajaxMessageQueue[i];
								var isCallback = queueObj.isCallback == false ? false : true;//默认需要回调
								if(isCallback == true){
									uuidArray.push(queueObj[$.toastrNotify.messageFields.uuid]);
								}
							}
							var str  = uuidArray.join(',');
							$.toastrNotify.ajaxCallbackFunc(str);
						}
						$.toastrNotify.ajaxMessageQueue = [];
					}
					$.toastrNotify.ajaxCallbackTimer = setInterval(noticeServer, 1000);
				}
			}
		},
		endAjaxCallback:function(){
			if($.toastrNotify.ajaxCallbackTimer!=null){
	             clearInterval($.toastrNotify.ajaxCallbackTimer);
	             $.toastrNotify.ajaxCallbackTimer = null;
			}
		},
		openTip:function(){
			if($.toastrNotify.tipsArrayIndex < $.toastrNotify.maxTipCount){
				var popedObj = $.toastrNotify.scheduleQueue.shift();//从最前面拿
				$.toastrNotify.scheduleQueueUuid.shift();
				if(popedObj){
					$.toastrNotify.ajaxMessageQueue.push(popedObj);
					$.toastrNotify.startAjaxCallback();//启动通知进程
					var type = popedObj[$.toastrNotify.messageFields.type],title = popedObj[$.toastrNotify.messageFields.title],content = popedObj[$.toastrNotify.messageFields.content],isAutomaticClose = popedObj.isAutomaticClose ? popedObj.isAutomaticClose:false;
					var sender_name = popedObj[$.toastrNotify.messageFields.sender] ? popedObj[$.toastrNotify.messageFields.sender]:'';
					var syscreatedate = popedObj[$.toastrNotify.messageFields.date] ? popedObj[$.toastrNotify.messageFields.date] :'';
					/************组装提示框开始***************/
					var divobj = $("<div class='tip-item' style='width:"+$.toastrNotify.tipWidth+"px;height:"+$.toastrNotify.tipHeight+"px;display:none;' id='jqthiscurrenttips"+$.toastrNotify.tipItemIndex+"'></div>");
					var tableStrArray = [];
					tableStrArray.push('<div class="tip-item-title">');
					tableStrArray.push('	<div class="tip-icon fa '+$.toastrNotify.tipTypeMapping[type].css+'">');
					tableStrArray.push('	</div>');
					tableStrArray.push('	<div>');
					tableStrArray.push(' 		<span onclick="$.toastrNotify.closeTipByInterval('+$.toastrNotify.tipItemIndex+');" class="tip-close jqcurrentindextag'+$.toastrNotify.tipItemIndex+'">×</span>');
					tableStrArray.push(' 		<span class="tip-title-main">'+$.toastrNotify.tipTypeMapping[type].name+(title? ':'+title : '')+'</span>');
					var extraTitle = type=='1' ?  (sender_name ? sender_name+'&emsp;&emsp;'+syscreatedate :syscreatedate) :syscreatedate;
					tableStrArray.push('		<span class="tip-title-extra">'+extraTitle+'</span>');
					tableStrArray.push('	</div>');
					tableStrArray.push('	<div style="clear: both;"></div>');
					tableStrArray.push('</div>');
					tableStrArray.push('<div class="tip-item-content"');
					var uuid = popedObj[$.toastrNotify.messageFields.uuid] ? popedObj[$.toastrNotify.messageFields.uuid] :'';
					if(uuid){
						tableStrArray.push(' id="'+uuid+'" ');
					}
					tableStrArray.push('><span>');
					tableStrArray.push('	'+content);
					tableStrArray.push('</span></div>');
					
					divobj.append(tableStrArray.join(''));
					
					/************组装提示框结束***************/
					$.toastrNotify.currentTipsArray[$.toastrNotify.tipsArrayIndex] = divobj;
					var ie6 = $.BrowserInfo.IsIE6Under;
					var b = $(document.body);
					var w = $(window);
					var ie6scroll = function(){ 
						divobj.css({ top: getWindowScrollOffset() }); 
					};
					$.toastrNotify.resizeTips();
					if(ie6) w.scroll(ie6scroll);
					//绑定window的resize事件
					w.resize($.toastrNotify.resizeTips);
					$(divobj).fadeIn(800);
					if(isAutomaticClose){
						var timeVal = popedObj.automaticCloseTime;
						if(!timeVal){
							timeVal = $.toastrNotify.intervalTimer;
						}
						$.toastrNotify.interval[$.toastrNotify.tipItemIndex] = setInterval("$.toastrNotify.closeTipByInterval('"+$.toastrNotify.tipItemIndex+"')",timeVal);
					}
					$.toastrNotify.tipsArrayIndex ++;
					$.toastrNotify.tipItemIndex++;

					setTimeout(arguments.callee,10);
				}
			}
		},
		showTip : function(jparam){//打开消息提示框
			/**************组装关闭所有层开始**********************/
			if($.toastrNotify.tipToolBar==null){
				var sobj =$("<div class='tip-container' style='display:none;'></div>");

				var closeAll=$("<div class='tip-title' title='关闭全部'><span style='float:right;'>关闭全部</span></div>");
				closeAll.bind("click",function(){
					$.toastrNotify.closeAllTip();
				});
				sobj.append(closeAll);
				$.toastrNotify.tipToolBar = sobj;
				/**************组装关闭所有层结束**********************/
				$("body").append(sobj);
			}
			//type,title,content,isAutomaticClose
			var isNotExist = false;
			if($.toastrNotify.messageFields.uuid){
				var curUUid = jparam[$.toastrNotify.messageFields.uuid];
				if(curUUid){
					if($.inArray(curUUid, $.toastrNotify.scheduleQueueUuid) == -1){
						if($('#'+curUUid).length == 0){
							isNotExist = true;
						}
					}
				}else{
					isNotExist = true;
				}
			}else{
				isNotExist = true;
			}
			if(isNotExist == true){
				$.toastrNotify.scheduleQueue.push(jparam);
				$.toastrNotify.scheduleQueueUuid.push(curUUid);
			}
			$.toastrNotify.openTip();//尝试打开
		},
		 //桌面通知
	    browserNotify : function(title,body){
	        if (window.Notification) {
	            //弹出窗体消息
	            var popNotice = function() {
	                if (Notification.permission == "granted") {
	                    var notification = new Notification(title,{
	                        body: body,
	                        icon: 'https://www.bugclose.com/oss/20/4a/a9/f2b23c66113f.png'
	                    });
	                    setTimeout(notification.close.bind(notification), 5000);
	                    //点击消息
	                    notification.onclick = function() {
	                        
	                        notification.close();    
	                    };
	                }
	            };
	            //接受消息
	            if (Notification.permission == "granted") {
	                popNotice();
	            } else if (Notification.permission != "denied") {
	                Notification.requestPermission(function (permission) {
	                  popNotice();
	                });
	            }
	        }
	    },
	    isDocumentHidden:function(){
	    	 var hidden = false;
	         if (typeof document.hidden !== "undefined") {
	             hidden = document.hidden;
	         } else if (typeof document.mozHidden !== "undefined") {
	             hidden = document.mozHidden;
	         } else if (typeof document.msHidden !== "undefined") {
	             hidden = document.msHidden;
	         } else if (typeof document.webkitHidden !== "undefined") {
	             hidden = document.webkitHidden;
	         }
	         return hidden;
	    },
	    startBlinkTitle:function(title){
	    	$.toastrNotify.stopBlinkTitle();
	         function blink(){
	             //网页当前状态判断
	             //显示消息
	             document.title = document.title == $.toastrNotify.backupTitle? title: $.toastrNotify.backupTitle;
	         }
	         blink();
	         $.toastrNotify.timer = setInterval(blink, 1000);
	    },
	    stopBlinkTitle:function(){
	    	 if($.toastrNotify.timer != null){
	             document.title = $.toastrNotify.backupTitle;
	             clearInterval($.toastrNotify.timer);
	             $.toastrNotify.timer = null;
	         }
	    }
	};
})(jQuery);