if (!Hatena)
	alert("Load HatenaStar.js before loading HatenaStarProgressive.js!");

var HatenaStarProgressive = {
	Config: {
		timeout: 5,
		step: 25
	},

	ProgList: function(list, itv, step) {
		this.list = list;
		this.interval = itv;
		this.step = step;
	},

	commitButtons: function() {
		var pr = new HatenaStarProgressive.ProgList(Hatena.Star.Entry.pendingButtons, HatenaStarProgressive.Config.timeout, HatenaStarProgressive.Config.step);
		pr.proc = function(e) {
			e.showButtonsSuper();
			e.showStarsAsync();
			e.showCommentButton();
		}

		pr.next();
	},

	installHatenaStarHook: function(entry_obj) {
		var p = entry_obj.prototype;
		entry_obj.pendingButtons = [];

		// save original functions
		p.showCommentButtonSuper = p.showCommentButton;
		p.showStarsSuper = p.showStars;
		p.showButtonsSuper = p.showButtons;

		p.showStars = function() {
			this.showStarsAsync();
		};
		p.showStarsAsync = function() {
			this.showStarsAsync = p.showStarsSuper;
		};

		p.showCommentButton = function() {
			if (this.commentButton)
				this.showCommentButtonSuper();
		};

		p.showButtons = function() {
			entry_obj.pendingButtons.push(this);
		};

	}
};

HatenaStarProgressive.ProgList.prototype = {
	tick: function() {
		for (var i = 0;i < this.step && this.list.length > 0;i++)
		{
			if (this.proc) this.proc(this.list.shift());
		}

		this.next();
	},

	next: function() {
		if (this.list.length > 0)
		{
			var _this = this;
			setTimeout(function(){_this.tick()}, this.interval);
		}
	}
};

HatenaStarProgressive.installHatenaStarHook(Hatena.Star.Entry);
Hatena.Star.EntryLoader.addEventListener('load', HatenaStarProgressive.commitButtons);
