$(function(){window.notification_timers={},window.doNotification=function(type,nid,title,encoding,timeout,src,actions,nID,onAction,onClose){console.debug("doNotification",type,nid,title,encoding,timeout,src,actions,nID,onAction,onClose);var nID="notification"+nid,a=$('<div id="'+nID+'" class="alert '+type+'"><img class="notification_icon" id="notification_icon'+nID+'"><span class="title">'+title+'</span><span class="message">'+encoding+'</span><div class="dismiss">&#215;</div></div>'),encoding=$(".notifications");if(encoding.prepend(a),actions){var notification_buttons=$('<div class="notification_buttons"></div>');a.append(notification_buttons);for(var i=0;i<actions.length;i+=2){var action_id=actions[i],notification_button=actions[i+1],notification_button=window._notification_button(nid,action_id,notification_button,onAction,onClose);notification_buttons.append(notification_button)}}return encoding.prepend(a),src&&(encoding=src[0],src=src[3],"png"==encoding&&(src="data:image/"+encoding+";base64,"+Utilities.ArrayBufferToBase64(src),$("#notification_icon"+nID).attr("src",src))),a.on("click",".dismiss",function(){window.cancelNotificationTimer(nid),a.removeClass("visible").addClass("hidden"),a.on("transitionend webkitTransitionEnd",$.debounce(250,function(){a.trigger("dismissed"),a.remove()})),onClose&&onClose(nid,3,"user clicked dismiss")}),setTimeout(function(){a.addClass("visible")},1),timeout&&(a.data("timeLeft",timeout),window.notification_timers[nid]=setInterval(function(){var tleft=a.data("timeLeft")-1;0===a.data("timeLeft")?(a.find(".dismiss").trigger("click"),onClose&&onClose(nid,1,"timeout")):(a.find("sec").text(tleft),a.data("timeLeft",tleft))},1e3)),a},window._notification_button=function(nid,action_id,notification_button,onAction,onClose){notification_button=$('<div class="notification_button" id=notification"'+action_id+'">'+notification_button+"</div>");return notification_button.on("click",function(){window.closeNotification(nid),onAction&&onAction(nid,action_id),onClose&&onClose(nid,3,"user clicked action")}),notification_button},window.cancelNotificationTimer=function(nid){var timer=window.notification_timers[nid];timer&&(window.clearInterval(timer),delete window.notification_timers[nid])},window.cancelNotificationTimers=function(){for(var nid in window.notification_timers)window.cancelNotificationTimer(nid)},window.closeNotification=function(nID){window.cancelNotificationTimer(nID);nID="notification"+nID;$(".notifications").find("#"+nID).find(".dismiss").trigger("click")},window.clearNotifications=function(){window.cancelNotificationTimers(),$(".notifications").find(".dismiss").trigger("click")},window.removeNotifications=function(){window.cancelNotificationTimers(),$(".notifications").empty()}});