

function TitleBlinker(delay) {
	this.blinkTitle;
	this.timer;
	this.counter;
	this.delay = delay;
	this.focus = true;
	this.originalTitle = document.title;
	
	window.onfocus = new Delegate(this, this.onFocus);
	window.onblur = new Delegate(this, this.onBlur);
	window.onfocusout = new Delegate(this, this.onBlur);
}

TitleBlinker.prototype.blink = function(strMsg) {
	if(!this.focus) {
		this.reset();
		
		if(this.originalTitle != document.title) {
			this.originalTitle = document.title;
		}
		
		this.blinkTitle = strMsg;
		this.counter = 0;
		this.changeTitle();
	}
};


TitleBlinker.prototype.changeTitle = function() {
	this.counter++;
	
	if(this.counter % 2 == 0) {
		document.title = this.originalTitle;
	} else {
		document.title = this.blinkTitle;
	}
	
	clearTimeout(this.timer);
	this.timer = setTimeout(new Delegate(this, this.changeTitle), this.delay);
};

TitleBlinker.prototype.reset = function() {
	this.counter = -1;
	this.changeTitle();
	clearTimeout(this.timer);
};

TitleBlinker.prototype.onFocus = function() {
	this.focus = true;
	this.reset();
};

TitleBlinker.prototype.onBlur = function() {
	this.focus = false;
};

