// JavaScript Document
PropertyMonitor = Class.create({
		initialize: function (prop, delay, callback)
		{
			// vars
			this.property = (prop != undefined)? prop: null;
			this.timerDelay = (delay > 0)? delay: 0.2; // seconds
			this.onChange = (typeof(callback) == "function")? callback: null;
			this.executer = null;
			
			this.propertyValue = this.getPropertyValue();
			
			this.initiateTimer();
		},
		initiateTimer: function ()
		{
			this.executer = new PeriodicalExecuter(this.monitorFunction.bind(this), this.timerDelay);
		},
		start: function ()
		{
			this.propertyValue = this.getPropertyValue();
			this.initiateTimer();
		},
		stop: function ()
		{
			if(this.executer != null) {
				this.executer.stop();
			}
		},
		monitorFunction: function ()
		{
			var temp = this.getPropertyValue();
			if(this.propertyValue != temp) {
				this.propertyValue = temp;
				
				this.handlePropertyChange();
			}
		},
		getPropertyValue: function ()
		{
			var temp;
			if(typeof(this.property) == "function") {
				temp = this.property();
				
				if(typeof(temp) == "object") {
					temp = Object.toJSON(temp);
				}
			}
			else if (typeof(this.property) == "string") {
				if(this.property.length > 0) {
					eval("var temp = " + this.property);
					
					if(typeof(temp) == "object") {
						temp = Object.toJSON(temp);
					}
				}
			}
			return temp;
		},
		handlePropertyChange: function ()
		{
			
			if(this.onChange != null) {
				var evtObj = {};
				evtObj.property = this.property;
				evtObj.value = this.propertyValue;
				
				this.onChange(evtObj);
			}
		}
	});
