
function PgEng() {
	this.device=new Array(); 
	this.dataSrcList=new Array();
	this.widgetList=new Array();
	this.loadedFiles = new Array();
	this.pollTimer = null;

//	this.initWgts();
};

PgEng.prototype.initWgts = function() {

};


PgEng.prototype.getDataSource = function(tgtName) {
	for (var i=0; i<this.dataSrcList.length; i++) {
		if (tgtName==this.dataSrcList[i].wgtName) {
			return this.dataSrcList[i];
		}
	}

};

PgEng.prototype.addDataSource = function(dataSrc) {
	this.dataSrcList[this.dataSrcList.length] = dataSrc;
};

PgEng.prototype.getDataSources = function() {
	return this.dataSrcList;
};

PgEng.prototype.addWidget = function(wgt) {
	this.widgetList[this.widgetList.length] = wgt;
};

PgEng.prototype.getWidget = function(wgtId){
	for (var i=0; i<this.widgetList.length; i++) {
		if (wgtId==this.widgetList[i].wgtId) {
			return this.widgetList[i];
		}
	}
};

PgEng.prototype.loadScript = function(url) {
	for (var i=0; i<this.loadedFiles.length; i++) {
		if (this.loadedFiles[i] == url)
			return;
	}

	document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + url + '"></script>');

	this.loadedFiles[this.loadedFiles.length] = url;
};

PgEng.prototype.startPoll = function() {
	this.pollUpdate();
	this.pollTimer = setInterval("pageEng.pollUpdate();",3000);
}

PgEng.prototype.stopPoll = function() {
	clearInterval(this.pollTimer);
}

PgEng.prototype.pollUpdate = function() {
		// use 'pageEng' var since this is a callback
	for (var i=0; i<pageEng.dataSrcList.length; i++) {
		if (pageEng.dataSrcList[i].isPollEnabled) {
			pageEng.dataSrcList[i].requestData();
		}
	}
}

/**
*	DataLink  class
*
*/
function DataLink() {
	this.tag = "";
	this.dataSrcName = "";
	this.dataSrc=null;
	this.readWrite="";
	this.numXforms = 0;
	this.tgtWgt = null;
};

DataLink.prototype.init = function(tagProps, wgt) {
	this.tgtWgt = wgt;
	if (tagProps != null) {
		if (tagProps.length >= 1) 
			this.dataSrcName = tagProps[0];
		if (tagProps.length >= 2) 
			this.tag = tagProps[1];
		if (tagProps.length >= 3) 
			this.readWrite = tagProps[2];
		if (tagProps.length >= 4) 
			this.addXform(tagProps[3]);
	}
};

DataLink.prototype.getDataSrcValue = function(){
	
	var value;
	if (this.dataSrc == null) 
		this.dataSrc = pageEng.getDataSource(this.dataSrcName);
		
	if (this.dataSrc != null) {
		// do the xform
		value = this.dataSrc.getFieldValue(this.tag);
		if (this.numXforms > 0) 
			value = this.doXform(value, "r");
	}
	return value;
};
DataLink.prototype.setWgtValue = function(newValue){

		// don't set if widget is write only
	if (this.readWrite != "w") {
		// do the xform
		var value = newValue;
		if (this.numXforms > 0) 
			value = this.doXform(value, "r");
		// pass it on to the widget
		this.tgtWgt.setFieldValue(this.tag, value);
	}	

};
DataLink.prototype.setDataSrcValue = function(newValue){
	
	if (this.dataSrc == null) 
		this.dataSrc = pageEng.getDataSource(this.dataSrcName);
		
	if (this.dataSrc != null) {
		// do the xform
		var value = newValue;
		if (this.numXforms > 0) 
			value = this.doXform(value, "w");
		// pass it on to the widget
		this.dataSrc.setFieldValue(this.tag, value);
	}

};
DataLink.prototype.addXform = function(xform) {
	if (this.numXforms==0)
		this.xform = new Array();
		
	this.xform[this.numXforms]=xform;
	this.numXforms++;
};

DataLink.prototype.doXform = function(value, readWrite) {
	var retValue = value;
	var strXform;
	
	for (i = 0; i < this.numXforms; i++) {
		strXform = this.xform[i];
		switch (strXform.slice(0,1)) {
			case 'b':
				bitIndex = parseInt(strXform.slice(1));
				mask = 0x0001 << bitIndex;
				// get bit/set bit
				if (readWrite=="w") {
					var dataSrc = pageEng.getDataSource(this.dataSrcName);
					if (dataSrc != null) {
							// when writing, we need to read existing value and 
							// change bit for the existing value (other bits should stay the same)
						var curValue = dataSrc.getFieldValue(this.tag);
						if (value > 0) 
							retValue = (curValue | mask);
						else 
							retValue = curValue & ~ mask;							
					}
				} else {
					retValue = (value & mask)>>bitIndex;					
				}
				break;
				
			case 's':
					// scale
				scale = parseFloat(strXform);
				if (readWrite == "w") {
					if (scale!=0)
						retValue = value/scale;
				} else {
					retValue = scale*value;
				}
				break;
				
			case 'o':
					// offset
				offset = parseFloat(strXform);
				if (readWrite == "w") {
					retValue = value-offset;
				} else {
					retValue = value+offset;
				}
				break;
		}
	}

	return retValue;
};

/**
*	Widget base class
*
*/
function Widget() {

};

Widget.prototype.init = function(wgtProps) {
};


Widget.prototype.setFieldValue = function(tag, newValue) {
	
};

Widget.prototype.getFieldValue = function(tag) {
	
};


/**
*	Data Source widget
*
*/
function DataSrc() {
	this.wgtName = "";
//	this.observers = new Array();
	this.isPollEnabled = false;
};

/*
DataSrc.prototype.addHndlr = function(wgt, varName) {
	this.observers[this.observers.length] = new Observer(wgt, varName);
};
*/
DataSrc.prototype.requestData = function() {
	
};

DataSrc.prototype.enablePoll = function(bEnable) {
	this.isPollEnabled = bEnable;
};

/**
*	Observer class
*
*/
function Observer(wgt, varName) {
	this.wgt = wgt;
	this.varName = varName;
};

// global vars
var pageEng = new PgEng();


