// auto check for Athena

// Setting
AthenaCheck.interval     = 600;	// checked interval [second]
AthenaCheck.request_url  = 'athena.cgi?_mode=auto_check';	// URL requested
AthenaCheck.confirm_text = '';	// confirm-text at update
								// ex: 'Thanks to you, this page was updated to latest information.\nDo you reload ?';

// Class
function AthenaCheck (q) {
	if (!window.HttpRequest) return null;
	if (!q) q = {};	// no argument ?

	// this.property
	this.interval     = (q.interval - 0 || AthenaCheck.interval) * 1000;
	this.request_url  =  q.request_url  || AthenaCheck.request_url;
	this.confirm_text =  q.confirm_text || AthenaCheck.confirm_text;
	this.confirming   = false;

	// last-modified of this file
	this.modified = ( new Date( document.lastModified ) ).getTime();

	// set function
	this.funcs = [];
	this.funcs[4] = function (self) {
		return function (req) { self.received(req); }
	}(this);

	// to first check
	this.check();
	return this;
}


// check
AthenaCheck.prototype.check = function () {
	if (!window.HttpRequest) return null;
	this.timer && clearTimeout(this.timer);	// clear timer

	// times
	this.now      = ( new Date() ).getTime();
	this.expire   = (this.modified + this.interval) - this.now;

	// request
	if (this.expire <= 0) {
		this.req = new HttpRequest('GET', this.request_url, this.funcs);
		this.expire = this.interval;
	}

	// set timer
	this.timer = setTimeout( function (self) {
		return function (e) { self.check() };
	}(this), this.expire);
};


// request
AthenaCheck.prototype.received = function (req) {
	if (this.confirming || req.status != 200) return;
	var source = req.responseText;	// last-modified \n update-time
	var values = source.match(/([0-9]+)[^0-9]*$/);
	var update = (values && values[1] - 0) || 0;	// update-time

	// update ?
	if (update * 1000 <= this.modified + this.interval / 10) return;

	// confirm page reload
	this.confirming = true; 	// prevent confirming more times
	if (!this.confirm_text || confirm(this.confirm_text)) location.reload(true);
	else clearTimeout(this.timer);
	this.confirming = false;
};




// get attributes by text in this script
AthenaCheck._get_attributes_by_text = function (file) {
	var text = '';
	var scripts = document.getElementsByTagName('script');
	var reg = new RegExp('\\b'+ file +'\\b');
	for (var i = scripts.length; i--;) {
		var c = scripts[i];
		if (!c.ran && c.src && c.src.match(reg)) {
			c.ran = true;	// this script already ran
			text = c.firstChild && c.firstChild.nodeValue || c.innerHTML;	// get text
			break;
		}
	}
	if (!text || text.match(/^\s*$/)) return;

	// parse
	var tmp = {};
	try {
		reg = new RegExp(	// z
			'"[^\"]*(?:""[^\"]*)*"'+'|'+
			"'[^\']*(?:''[^\']*)*'"+'|'+
			'([_0-9A-Za-z]+)'+		// name
			'(?:'+'[ \t]*[=:][ \t]*'+'(?:'+
				'"([^\"]*(?:""[^\"]*)*)"'+'|'+	// v1
				"'([^\']*(?:''[^\']*)*)'"+'|'+	// v2
				'([^,;*\\/\\[\\]\\s]*)'+		// v3
			'))?', 'g'
		);
		text.replace(reg, function (z, name, v1, v2, v3) {
				if (name) tmp[name] =
					v1 && v1.replace(/""/g, '\"') ||
					v2 && v2.replace(/''/g, "\'") ||
					v3 || '';
		});
	} catch (e) { return; }
	return tmp;
};


// new
AthenaCheck.run = function () {
	var q = AthenaCheck._get_attributes_by_text('AthenaCheck.js');
	if (q) new AthenaCheck(q);
};
AthenaCheck.run();
