
var Notification=SingletonClass.create({initialize:function(){this._getDelay=this._delayBase;this._firstTime=true;this._queue=new Array();this._received=new Object();this._current=-1;this._active=false;this._transition=false;this._stopped=true;document.observe('dom:loaded',this._injectDOM.bindAsEventListener(this));},stop:function(){this._stopped=true;if(this._active)
this.hideNotification();else if(this._getTimeoutID>0){clearTimeout(this._getTimeoutID);this._getTimeoutID=0;}},showNext:function(){if(this._stopped)
return;if(!this._active){if(this._transition){setTimeout("Notification.get().showNext()",200);}
else{if(!this._flashObj){this._flashObj=getFlashObj('notification_flash','showNotification');if(!this._flashObj){setTimeout("Notification.get().showNext()",1000);return;}}
if(this._current<this._queue.length-1){this._current++;this._flashObj.showNotification(this._queue[this._current]);this._active=this._transition=true;new Effect.Move('notification_div',{x:parseInt(this._notification_div.style.left,10),y:document.viewport.getHeight()-94,mode:'absolute',duration:0.2,afterFinish:this._finishShowNext.bind(this)});this._hideTimeoutID=setTimeout("Notification.get().hideNotification()",this._autohideDelay);}}}},hideNotification:function(){if(this._active){if(this._transition){if(this._hideTimeoutID>0)
clearTimeout(this._hideTimeoutID);this._hideTimeoutID=setTimeout("Notification.get().hideNotification()",200);}
else{this._active=false;this._transition=true;new Effect.Move('notification_div',{x:parseInt(this._notification_div.style.left,10),y:document.viewport.getHeight()+this._hideOffset,mode:'absolute',duration:0.2,afterFinish:this._finishHideNotification.bind(this)});}}},_injectDOM:function(){Event.observe(document.onresize?document:window,"resize",this._onResize.bindAsEventListener(this));if(Prototype.Browser.WebKit)
this._hideOffset=-1;else if(Prototype.Browser.Gecko)
this._hideOffset=25;else
this._hideOffset=0;this._notification_div=$('notification_div');this._notification_div.setStyle({left:(document.viewport.getWidth()-392)+'px',top:(document.viewport.getHeight()+this._hideOffset)+'px'});this._notification_div.show();this._stopped=false;this._get();},_onResize:function(){if(!this._transition&&this._notification_div){var offset=this._active?-94:this._hideOffset;this._notification_div.setStyle({left:(document.viewport.getWidth()-392)+'px',top:(document.viewport.getHeight()+offset)+'px'});}},_get:function(){if(this._stopped)
return;var currentTime=new Date();this._lastGet=currentTime.getTime();new Ajax.Request('/index.php',{method:'post',asynchronous:true,parameters:{action:'notification',actionType:'ajax',subAction:'get'},onSuccess:this._get_onSuccess.bind(this)});},_get_onSuccess:function(transport){var response=transport.responseText.evalJSON();if(response.status=='Success'){var keys=Object.keys(response.data);var keysLength=keys.length;var newNotification=false;for(var i=0;i<keysLength;i++){if(!this._received[keys[i]]&&!(this._firstTime&&response.data[keys[i]].received)){this._queue.push(response.data[keys[i]].content);newNotification=true;}
this._received[keys[i]]=true;}
this._firstTime=false;if(newNotification){this._getDelay=this._delayBase;}
else{this._getDelay=Math.floor(this._getDelay*this._decay);if(this._getDelay>this._maxDelay)
this._getDelay=this._maxDelay;}
if(this._current<this._queue.length-1)
this.showNext();else
this._getTimeoutID=setTimeout("Notification.get()._get()",this._getDelay);}
else if(response.status=='Empty'){this._getDelay=Math.floor(this._getDelay*this._decay);if(this._getDelay>this._maxDelay)
this._getDelay=this._maxDelay;this._getTimeoutID=setTimeout("Notification.get()._get()",this._getDelay);}
else if(response.status=='Nologin'){if(this._getTimeoutID>0){clearTimeout(this._getTimeoutID);this._getTimeoutID=0;}}},_finishShowNext:function(){this._transition=false;},_finishHideNotification:function(){this._transition=false;if(this._current<this._queue.length-1){this.showNext();}
else{this._flashObj.showNotification('');var currentTime=new Date();var diff=currentTime.getTime()-this._lastGet;if(diff>this._getDelay)
this._get();else
setTimeout("Notification.get()._get()",this._getDelay-diff);}},_notification_div:null,_hideOffset:0,_firstTime:true,_flashObj:null,_received:null,_queue:null,_current:-1,_active:false,_transition:false,_hideTimeoutID:0,_getTimeoutID:0,_delayBase:15000,_getDelay:0,_maxDelay:300000,_autohideDelay:10000,_decay:1.15,_lastGet:0});