var interactionMonitor = Class.create();
var monitor;
interactionMonitor.prototype = {
	/**
	 * Author:		Hans Duedal (Lintoo I/S)
	 * Email & URL:	hans@lintoo.dk www.lintoo.dk
	 * Copyright: 	2006 Lintoo I/S
	 *
	 */
	
	initialize: function() {
		this.showScreensaver = $('flashSaverContainer') ? true : false;
		this.timer = null;
		this.visitorTimeout = 60000; //in microseconds
		this.timeout = 120000; //in microseconds
		this.checkInterval = 5000; //in microseconds
		this.lastBeat = new Date();
		this.lastMove = null;
		this.screensaverEnabled = false;
		this.visitRegistered = refererHint ? false : true;
		this.flashObject = { };
		if ($('flashSaverContainer')) {
			$('flashSaverContainer').style.display = 'block';
			this.flashObject = {
				width: parseInt($('flashSaverContainer').scrollWidth),
				height: parseInt($('flashSaverContainer').scrollHeight),
				object: { }
			}
			$('flashSaverContainer').style.display = 'none';
		}
		//should the browser fire one of these three events perform a heartBeat
		Event.observe(document, 'click', this.heartbeat, false);
		Event.observe(document, 'mousemove', this.heartbeat, false);
		Event.observe(document, 'keypress', this.heartbeat, false);
		this.check(); //initialize timers
	},
	
	heartbeat: function(event) {
		monitor.lastBeat = new Date();
		if (monitor.screensaverEnabled) monitor.screensaverDisable();
		if (monitor.visitRegistered) monitor.visitRegistered = false;
	},
	
	check: function() {
		clearTimeout(this.timer);
		var diff = new Date() - this.lastBeat;
		if (diff >= this.visitorTimeout) {
			this.registerVisitor();
		}
		if (diff >= this.timeout) {
			this.checkScreenSaver();
		}
		this.timer = setTimeout(function() {monitor.check()},this.checkInterval);
	},
	
	registerVisitor: function()
	{
		if (this.visitRegistered) return void(0);
		if (navigation.grapQueryId(document.location.search) == 0) {
			var url = 'datasource/storeVisitor.php?client=' + this.getClientId() + '&id=NULL';
		} else {
			var url = 'datasource/storeVisitor.php?client=' + this.getClientId() + '&id=' + navigation.grapQueryId(document.location.search);
		}
		new Ajax.Request(url, {
				method: 'post',
				onSuccess: function() {monitor.visitRegistered = true},
				onFailure: function() {monitor.visitRegistered = true} // just pretend
			}
		);
	},
	
	checkScreenSaver: function() {
		if (navigation.grapQueryId(document.location.search) != 0) {
			navigation.home();
		} else {
			if (!this.screensaverEnabled && this.showScreensaver) this.getScreensaver();
		}
	},
	
	
	screensaverDisable: function() {
		if ($('flashSaverContainer')) {
			this.screensaverEnabled = false;
			$('flashSaverContainer').style.visibility = 'hidden';
			$('flashSaverContainer').style.display = 'none';
		}
	},
	
	screensaverEnable: function(properties) {
		if (this.showScreensaver) {
			if (this.flashObject.object instanceof SWFObject == false) {
				
				var src = properties.src;
				var title = properties.title;
				
				this.flashObject.object = new SWFObject(src,title,this.flashObject.width,this.flashObject.height,8,'#fff');
				this.flashObject.object.addParam('base','flash/screensavers/');
				this.flashObject.object.addParam('wmode','opaque');
				this.flashObject.object.addParam('menu','false');
				this.flashObject.object.write('flashSaver');
			}	
			$('flashSaverContainer').style.visibility = 'visible';
			$('flashSaverContainer').style.display = 'block';
			this.screensaverEnabled = true;
		}
	},
	
	getClientId: function()
	{
		if (currentClientId) return currentClientId;
		var queryId = navigation.grapQueryPart('client',document.location.search);
		if (queryId) return queryId;
		var queryId = navigation.grapQueryPart('infostander',document.location.search);
		if (queryId) return queryId;
		return false;
	},
	
	getScreensaver: function()
	{
		var clientID = this.getClientId();
		var url = 'screensaverInfo.php?clientID=' + clientID + '&noCache=' + parseInt(new Date().valueOf());
		var ajax = new Ajax.Request(url, {
			method: 'get',
			onComplete: function(transport)
			{
				var xml = transport.responseXML;
				var root = xml.getElementsByTagName('screensaver')[0];
				var title = root.getAttribute('name');
				var src = root.getAttribute('src');
				
				var props ={
					title: title,
					src: src
				};
				this.screensaverEnable(props);
			}.bind(this)
		});
	}
}

addLoadEvent(function() { //initialize and start object
	monitor = new interactionMonitor();
});
