;(function(global){var lastId=-1;var self={onVisible:function(callback){var support=self.isSupported();if(!support||!self.hidden()){callback();return support;}
var listener=self.change(function(e,state){if(!self.hidden()){self.unbind(listener);callback();}});return listener;},change:function(callback){if(!self.isSupported()){return false;}
lastId+=1;var number=lastId;self._callbacks[number]=callback;self._listen();return number;},unbind:function(id){delete self._callbacks[id];},afterPrerendering:function(callback){var support=self.isSupported();var prerender='prerender';if(!support||prerender!=self.state()){callback();return support;}
var listener=self.change(function(e,state){if(prerender!=state){self.unbind(listener);callback();}});return listener;},hidden:function(){return!!(self._doc.hidden||self._doc.webkitHidden);},state:function(){return self._doc.visibilityState||self._doc.webkitVisibilityState||'visible';},isSupported:function(){return self._doc.hidden!==undefined||self._doc.webkitHidden!==undefined;},_doc:document||{},_callbacks:{},_change:function(event){var state=self.state();for(var i in self._callbacks){self._callbacks[i].call(self._doc,event,state);}},_listen:function(){if(self._init){return;}
var event='visibilitychange';if(self._doc.webkitVisibilityState){event='webkit'+event;}
var listener=function(){self._change.apply(self,arguments);};if(self._doc.addEventListener){self._doc.addEventListener(event,listener);}else{self._doc.attachEvent(event,listener);}
self._init=true;}};if(typeof(module)!='undefined'&&module.exports){module.exports=self;}else{global.Visibility=self;}})(this);