// ==UserScript==
// @name             Access Count Bar
// @namespace        http://espion.just-size.jp/archives/05/136155838.html
// @include          *
// ==/UserScript==

// Copyright (c) 2006, Kyosuke Takayama <support@mc.neweb.ne.jp>
// Distributed under the MIT license
// http://www.opensource.org/licenses/mit-license.php
// http://www.opensource.jp/licenses/mit-license.html

var t = document.contentType;
if(t != 'text/html' && t != 'application/xhtml+xml') return;
if(window != top) return;

var MAX_ACCESS = value('max_access', 100);

var BAR_IMG = {
   black:   'data:image/gif;base64,R0lGODdhAQABAIABAAAAAP///ywAAAAAAQABAAACAkQBADs=',
   gray:    'data:image/gif;base64,R0lGODdhAQABAIABAGZmZv///ywAAAAAAQABAAACAkQBADs=',
   blue:    'data:image/gif;base64,R0lGODdhAQABAIABAAAA/////ywAAAAAAQABAAACAkQBADs=',
   green:   'data:image/gif;base64,R0lGODdhAQABAIABAAD/AP///ywAAAAAAQABAAACAkQBADs=',
   red:     'data:image/gif;base64,R0lGODdhAQABAIABAP8AAP///ywAAAAAAQABAAACAkQBADs=',
};

function main() {
   var data  = new DataStore();
   var count = data.incremental((new Date()).getHours());

   var c = new Clearance(data);
   c.clear();

   var bar = new WORKBAR(data);
   bar.create();
   bar.draw();

   //var int = setInterval(function() { bar.draw(); }, 5*60*1000);
   bar.elm.addEventListener('dblclick', function() {
      document.body.removeChild(bar.elm);
      //clearInterval(int);
   } , false);

   if(count == MAX_ACCESS) {
      var msg = '\u3053\u306E\u6642\u9593\u5E2F\u306E\u30A2\u30AF\u30BB\u30B9\u6570\u304C' +
         count + '\u306B\u9054\u3057\u305F\u3088';
      alert(msg);
   }
}

function value(name, def) {
   var ret = GM_getValue(name);
   if(def && ret == undefined) {
      GM_setValue(name, def);
      ret = def;
   }
   return ret;
}

var Class = {
   create: function() {
      var newClass = function() {
         this.initialize.apply(this, arguments);
      }
      newClass.prototype.initialize = function() {}
      return newClass;
   }
};

Function.prototype.extend = function(newClass) {
   var proto = this.prototype;
   for(var property in newClass)
      proto[property] = newClass[property];
}

var WORKBAR = Class.create();

WORKBAR.extend({
   initialize: function(data) {
      this.data = data;
      this.elm;
   },

   create: function() {
      var div = document.createElement('div');
      document.body.appendChild(div);

      with(div.style) {
         width = '100%';
         textAlign = 'center';
         backgroundColor = '#FFFFFF';
         position = 'fixed';
         bottom = 0;
         left = 0;
         opacity = '0.5';
      }

      this.elm = div;
   },

   draw: function() {
      var now = (new Date).getHours();
      var wt  = '';
      for(var i = 0;i < 23;i++) {
         var img = (i % 2 == 0) ? BAR_IMG['gray'] : BAR_IMG['black'];
         if(i == now) img = BAR_IMG['red'];

         var num = this.data.get(i);
         wt += '<img src="'+img+'" width="3%" height="'+num+'px" title="'+i+' / '+num+'">';
      }

      this.elm.innerHTML = wt;
   }
});

var DataStore = Class.create();

DataStore.extend({
   initialize: function(prefix) {
      this._name = 'd_'+escape(prefix);
      this._lists = eval(GM_getValue(this._name)) || {};
   },

   get: function(n) {
      return (n != undefined) ?
         (this._lists[n] ? this._lists[n] : 0) : this._lists;
   },

   set: function(n, v) {
      this._lists[n] = v;
   },

   incremental: function(n) {
      var ret = this.get(n) + 1;
      this.set(n, ret);
      this.write();
      return ret;
   },

   write: function() {
      GM_setValue(this._name, this.get().toSource());
   }
});

var Clearance = Class.create();

Clearance.extend({
   initialize: function(data) {
      this.data  = data;
      this.cycle = 60*60*1000;
   },

   clear: function() {
      var time  = new Date();
      time = time - (time % this.cycle);

      if(time > this.data.get('last')) {
         var tmp = this.data.get('last') + this.cycle;
         this._set(tmp);

         var i = 0;
         while(time > tmp) {
            tmp += this.cycle;
            this._set(tmp);

            i++; if(i > 24) break;
         }

         this.data.set('last', time);
         this.data.write();
      }
   },

   _set: function(time) {
      var n = (new Date(time)).getHours();
      this.data.set(n, 0);
   }
});

main();

