// AControl class (derivative of AWidget)
var AControl = Class.create({
	initialize: function() {}
});

AControl.$ = function(control) {
	if(typeof(control)=="string") return $(control).JSControl;
	return control;
}

AControl.AddDefaultHandler = function(sEventName, sControlID) {
	var oWindow = AControl.GetParentWindow(sControlID);
	if (oWindow) oWindow.AddDefaultHandler(sEventName, sControlID);
}

AControl.GetParentWindowInstanceID = function(oControl) {
	var r = AControl.GetParentWindowElement(oControl);
	return (r ? r.readAttribute('instanceid') : null);
}

AControl.GetBoundingFrame = function(elNode) {
	var n = elNode;
	while (true) {
		n = n.parentNode;

		if (!n) return undefined;
		if (n.nodeName != 'DIV') continue;

		if (n.getAttribute('type') != "gridFrame") continue;
		if (n.getAttribute('height-limit') == 'stretch') continue;
		else break;
	}
	return $(n);
}

AControl.GetParentWindowElement = function(oControl) {
	if (oControl === GWindowManager.ID() ||
		(typeof(oControl.getAttribute) != 'undefined' &&
			oControl.getAttribute('id') == GWindowManager.ID())) return null; // WindowManager has no parent window
	var elpCurrentNode = $(oControl);
	// .parentNode should be here to make it working for windows itself (and a little bit faster)
	// but this breaks tabbars/hpath (cannot find bug there)
	//if (!elpCurrentNode) return null;
	//elpCurrentNode = $(elpCurrentNode.parentNode);
	while (elpCurrentNode) {
		if (typeof(elpCurrentNode.getAttribute) != 'undefined') {
			//LogL("AControl::GetParentWindow(): Traversing " +
				//elpCurrentNode.nodeName + ", ID="+elpCurrentNode.getAttribute('id'));
			if (elpCurrentNode.nodeName == 'DIV' &&
				elpCurrentNode.getAttribute('eleclass') == 'wnd') {
				return $(elpCurrentNode);
			}
		}
		elpCurrentNode = elpCurrentNode.parentNode;
	}
	//LogL("AControl::GetParentWindow(): ERROR: Cannot find parent window for '"+oControl+"'! ");
	return null;
}

AControl.GetParentWindow = function(oControl) {
	var r = AControl.GetParentWindowElement(oControl);
	return (r ? r.JSControl : null);
}

AControl.GetParentWindowID = function(sControlID) {
	var lidx=sControlID.lastIndexOf('_');
	return sControlID.substr(0, lidx);
}

// =============================================
var DataPlaceholder = Class.create({
initialize: function(element) {
	this.element = $(element);
	this.element.JSControl = this;
	this.ID_ = this.element.readAttribute('id');
},
SetValue: function(value) {
	//LogL("DataPlaceholder SetValue: "+value+",type="+typeof(value)+", isInt"+(value instanceof Int)+", IsInt64"+(value instanceof Int64));
	this.value=value;
},
Value: function() {
	//LogL("DataPlaceholder Value: "+this.value);
	if(typeof(this.value)=='undefined') return null;
	if(typeof(this.value)=='string') return this.value;
	if(this.value instanceof String) return this.value.toString();
	// otherwise it should be our kosher type (Int, Double, Optional, etc)
	return this.value;//.Value();
},
ID: function() { return this.ID_; }
});

//==============================================
var Refresher = Class.create({
initialize: function(winID, evtType) {
	this.winID=winID;
	this.evtType=evtType;
	$(this.winID).JSRefresher=this;
	this.sInstance=$(this.winID).JSControl.InstanceID();
	this.sRootInstance=document.getElementById("wMain").JSControl.InstanceID();
	SubscribeOnEvent(evtType, this.sRootInstance, this.Refresh, this);
},
Refresh: function(evt) {
	//LogL("Event");
	if (!$(this.winID) || $(this.winID).JSControl.InstanceID() != this.sInstance) {
		UnsubscribeFromEvent(this.evtType, this.sRootInstance, this.Refresh, this);
		//LogL("Unsubscribed from event "+this.evtType+", winID="+this.winID);
		return;
	}
	document.getElementById(this.winID).JSControl.Refresh();
}

});


