// ************************************************************************************
// BaseWebpartControl
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.Web.UI.Webparts");

// constructor
Competir.Web.UI.Webparts.BaseWebpartControl = function(element)
{
	Competir.Web.UI.Webparts.BaseWebpartControl.initializeBase(this, [element]);
	this._onApplicationLoadDelegate = Function.createDelegate(this, this._onApplicationLoad);
	this._UniqueID = "";
	this._ClientID = "";
	this._ClientIDSeparator = "";
	this._FKInstancia = 0;
	this._ClientVisible = true;
	this._ScriptFolder = "";
	this._ScriptFolderUri = "";
	this._XsltFolder = "";
	this._XsltFile = "";
	this._XsltFileUri = "";
	this._XsltParameters = new Competir.MiEmpresa.CustomProperties();
	this._Width = "";
	this._Height = "";
	this._FKInstanciaWebpart = 0;
	this._GUIDInstanciaWebpart = "";
	this._ContentsContainerClientID = "";
	this._ProgressIndicatorClientID = "";
	this._Properties = new Competir.MiEmpresa.CustomProperties();
	this._EnableAjax = true;
	this._ParentBaseWebpartControl = null;
	this._UseCache = false;
	this._CacheExpirationTimeSpan = "00:00:00";
};

// prototype
Competir.Web.UI.Webparts.BaseWebpartControl.prototype =
{
	// methods
	initialize: function()
	{
		Sys.Application.add_load(this._onApplicationLoadDelegate);
		this.add_propertyChanged(this._onPropertyChanged);
		this.toggleDisplay(this, this.get_ClientVisible());
		Competir.Web.UI.Webparts.BaseWebpartControl.callBaseMethod(this, "initialize");
	},
	dispose: function()
	{
		$clearHandlers(this.get_element());
		Sys.Application.remove_load(this._onApplicationLoadDelegate);
		this.remove_propertyChanged(this._onPropertyChanged);
		Competir.Web.UI.Webparts.BaseWebpartControl.callBaseMethod(this, "dispose");
	},
	getChildControls: function()
	{
		var rv = new Array();
		var c = Sys.Application.getComponents();
		for (var i = 0; i < c.length; i++)
		{
			if (Competir.Web.UI.Webparts.BaseWebpartControl.isInstanceOfType(c[i]))
			{
				if (c[i].get_ParentBaseWebpartControl() == this)
				{
					rv.push(c[i]);
				}
			}
		}
		return rv;
	},
	findChild: function(id)
	{
		var rv = null;
		if (id)
		{
			rv = $find(this.get_ClientID() + this.get_ClientIDSeparator() + id);
			if (!rv)
			{
				rv = $find(this.get_ClientID() + id);
			}
			if (!rv)
			{
				rv = $find(id);
			}
		}
		return rv;
	},
	getChild: function(id)
	{
		var rv = null;
		if (id)
		{
			rv = $get(this.get_ClientID() + this.get_ClientIDSeparator() + id);
			if (!rv)
			{
				rv = $get(this.get_ClientID() + id);
			}
			if (!rv)
			{
				rv = $get(id);
			}
		}
		return rv;
	},
	getContentsContainer: function()
	{
		var rv = null;
		if (this.get_ContentsContainerClientID() != "")
		{
			rv = $get(this.get_ContentsContainerClientID());
			if (!rv)
			{
				rv = $get(this.get_ClientID() + this.get_ContentsContainerClientID());
			}
		}
		else
		{
			rv = $get(this.get_id());
		}
		return rv;
	},
	getProgressIndicator: function()
	{
		var rv = null;
		if (this.get_ProgressIndicatorClientID() != "")
		{
			rv = $get(this.get_ClientID() + this.get_ProgressIndicatorClientID());
			if (!rv)
			{
				rv = $get(this.get_ProgressIndicatorClientID());
			}
		}
		return rv;
	},
	raiseEvent: function(id, args)
	{
		var events = this.get_events();
		if (events)
		{
			var f = events.getHandler(id);
			if (f)
			{
				f(this, args);
			}
		}
	},
	callFromBehavior: function(fnName, args)
	{
		var bhs = Sys.UI.Behavior.getBehaviors(this.get_element());
		for (var i = 0; i < bhs.length; i++)
		{
			var b = bhs[i];
			var m = eval("b." + fnName);
			if (m)
			{
				m.apply(b, args);
				break;
			}
		}
	},
	showWorkingProgress: function()
	{
		if (this.get_visible())
		{
			var obj = this.getProgressIndicator();
			if (obj)
			{
				Competir.Web.UI.show(obj);
			}
		}
	},
	hideWorkingProgress: function()
	{
		var obj = this.getProgressIndicator();
		if (obj)
		{
			Competir.Web.UI.hide(obj);
		}
	},
	toggleDisplay: function(obj, value)
	{
		if (!obj)
		{
			obj = this;
		}
		Competir.Web.UI.toggleDisplay(obj, value);
	},
	toggleImage: function(obj)
	{
		Competir.Web.UI.toggleImage(obj);
	},
	show: function(obj)
	{
		if (!obj)
		{
			obj = this;
		}
		Competir.Web.UI.show(obj);
	},
	hide: function(obj)
	{
		if (!obj)
		{
			obj = this;
		}
		Competir.Web.UI.hide(obj);
	},
	applyStyle: function(obj, name)
	{
		obj.className += " " + name;
	},
	removeStyle: function(obj, name)
	{
		obj.className = obj.className.replace(name, "");
	},
	update: function()
	{
		if (Competir.Web.UI.Webparts.ComponentList.isInstanceOfType(this) | Competir.Web.UI.Webparts.ContentViewer.isInstanceOfType(this))
		{
			this.renderContent();
		}
		var controls = this.getChildControls();
		for (var i = 0; i < controls.length; i++)
		{
			controls[i].update();
		}
	},
	mergeXsltParameters: function(cps)
	{
		if (!this.get_XsltParameters())
		{
			this.set_XsltParameters(cps);
		}
		for (var i = 0; i < cps.get_Count(); i++)
		{
			var cp = cps.getItem(i);
			if (this.get_XsltParameters().indexOfName(cp.get_Section(), cp.get_Name()) == -1)
			{
				this.get_XsltParameters().add(cp);
			}
			else
			{
				this.get_XsltParameters().getItemByName(cp.get_Section(), cp.get_Name()).set_Value(cp.get_Value());
			}
		}
	},

	// properties
	get_UniqueID: function()
	{
		return this._UniqueID;
	},
	set_UniqueID: function(value)
	{
		if (this._UniqueID !== value)
		{
			this._UniqueID = value;
			this.raisePropertyChanged("UniqueID");
		}
	},
	get_ClientID: function()
	{
		return this._ClientID;
	},
	set_ClientID: function(value)
	{
		if (this._ClientID !== value)
		{
			this._ClientID = value;
			this.raisePropertyChanged("ClientID");
		}
	},
	get_ClientIDSeparator: function()
	{
		return this._ClientIDSeparator;
	},
	set_ClientIDSeparator: function(value)
	{
		if (this._ClientIDSeparator !== value)
		{
			this._ClientIDSeparator = value;
			this.raisePropertyChanged("ClientIDSeparator");
		}
	},
	get_FKInstanciaWebpart: function()
	{
		return this._FKInstanciaWebpart;
	},
	set_FKInstanciaWebpart: function(value)
	{
		if (this._FKInstanciaWebpart !== value)
		{
			this._FKInstanciaWebpart = value;
			this.raisePropertyChanged("FKInstanciaWebpart");
		}
	},
	get_GUIDInstanciaWebpart: function()
	{
		return this._GUIDInstanciaWebpart;
	},
	set_GUIDInstanciaWebpart: function(value)
	{
		if (this._GUIDInstanciaWebpart !== value)
		{
			this._GUIDInstanciaWebpart = value;
			this.raisePropertyChanged("GUIDInstanciaWebpart");
		}
	},
	get_ClientVisible: function()
	{
		return this._ClientVisible;
	},
	set_ClientVisible: function(value)
	{
		if (this._ClientVisible !== value)
		{
			this._ClientVisible = value;
			this.raisePropertyChanged("ClientVisible");
		}
	},
	get_ScriptFolder: function()
	{
		return this._ScriptFolder;
	},
	set_ScriptFolder: function(value)
	{
		if (this._ScriptFolder !== value)
		{
			this._ScriptFolder = value;
			this.raisePropertyChanged("ScriptFolder");
		}
	},
	get_ScriptFolderUri: function()
	{
		return this._ScriptFolderUri;
	},
	set_ScriptFolderUri: function(value)
	{
		if (this._ScriptFolderUri !== value)
		{
			this._ScriptFolderUri = value;
			this.raisePropertyChanged("ScriptFolderUri");
		}
	},
	get_XsltFolder: function()
	{
		return this._XsltFolder;
	},
	set_XsltFolder: function(value)
	{
		if (this._XsltFolder !== value)
		{
			this._XsltFolder = value;
			this.raisePropertyChanged("XsltFolder");
		}
	},
	get_XsltFile: function()
	{
		return this._XsltFile;
	},
	set_XsltFile: function(value)
	{
		if (this._XsltFile !== value)
		{
			this._XsltFile = value;
			this.raisePropertyChanged("XsltFile");
		}
	},
	get_XsltFileUri: function()
	{
		return this._XsltFileUri;
	},
	set_XsltFileUri: function(value)
	{
		if (this._XsltFileUri !== value)
		{
			this._XsltFileUri = value;
			this.raisePropertyChanged("XsltFileUri");
		}
	},
	get_XsltParameters: function()
	{
		return this._XsltParameters;
	},
	set_XsltParameters: function(value)
	{
		if (Competir.MiEmpresa.CustomProperties.isInstanceOfType(value))
		{
			if (this._User !== value)
			{
				this._XsltParameters = value;
				this.raisePropertyChanged("XsltParameters");
			}
		}
		else
		{
			this._XsltParameters = new Competir.MiEmpresa.CustomProperties(value);
			this.raisePropertyChanged("XsltParameters");
		}
	},
	get_Width: function()
	{
		return this._Width;
	},
	set_Width: function(value)
	{
		if (this._Width !== value)
		{
			this._Width = value;
			this.raisePropertyChanged("Width");
		}
	},
	get_Height: function()
	{
		return this._Height;
	},
	set_Height: function(value)
	{
		if (this._Height !== value)
		{
			this._Height = value;
			this.raisePropertyChanged("Height");
		}
	},
	get_ContentsContainerClientID: function()
	{
		return this._ContentsContainerClientID;
	},
	set_ContentsContainerClientID: function(value)
	{
		if (this._ContentsContainerClientID !== value)
		{
			this._ContentsContainerClientID = value;
			this.raisePropertyChanged("ContentsContainerClientID");
		}
	},
	get_ProgressIndicatorClientID: function()
	{
		return this._ProgressIndicatorClientID;
	},
	set_ProgressIndicatorClientID: function(value)
	{
		if (this._ProgressIndicatorClientID !== value)
		{
			this._ProgressIndicatorClientID = value;
			this.raisePropertyChanged("ProgressIndicatorClientID");
		}
	},
	get_Properties: function()
	{
		return this._Properties;
	},
	set_Properties: function(value)
	{
		if (this._Properties !== value)
		{
			this._Properties = new Competir.MiEmpresa.CustomProperties(value);
			this.raisePropertyChanged("Properties");
		}
	},
	get_ParentBaseWebpartControl: function()
	{
		return this._ParentBaseWebpartControl;
	},
	set_ParentBaseWebpartControl: function(value)
	{
		if (this._ParentBaseWebpartControl !== value)
		{
			this._ParentBaseWebpartControl = value;
			this.raisePropertyChanged("ParentBaseWebpartControl");
		}
	},
	get_EnableAjax: function()
	{
		return this._EnableAjax;
	},
	set_EnableAjax: function(value)
	{
		if (this._EnableAjax !== value)
		{
			this._EnableAjax = value;
			this.raisePropertyChanged("EnableAjax");
		}
	},
    get_UseCache: function()
	{
	    return this._UseCache;  
	},
    set_UseCache: function(value)
	{
		if (this._UseCache !== value)
		{
			this._UseCache = value;
			this.raisePropertyChanged("UseCache");
		}
	},
	get_CacheExpirationTimeSpan: function()
	{
	    return this._CacheExpirationTimeSpan;  
	},
    set_CacheExpirationTimeSpan: function(value)
	{
		if (this._CacheExpirationTimeSpan !== value)
		{
			this._CacheExpirationTimeSpan = value;
			this.raisePropertyChanged("CacheExpirationTimeSpan");
		}
	},	
	
	// events
	add_onShow: function(handler)
	{
		this.get_events().addHandler("onShow", handler);
	},
	remove_onShow: function(handler)
	{
		this.get_events().removeHandler("onShow", handler);
	},
	add_onHide: function(handler)
	{
		this.get_events().addHandler("onHide", handler);
	},
	remove_onHide: function(handler)
	{
		this.get_events().removeHandler("onHide", handler);
	},
	add_onOperationStarted: function(handler)
	{
		this.get_events().addHandler("onOperationStarted", handler);
	},
	remove_onOperationStarted: function(handler)
	{
		this.get_events().removeHandler("onOperationStarted", handler);
	},
	add_onOperationSucceeded: function(handler)
	{
		this.get_events().addHandler("onOperationSucceeded", handler);
	},
	remove_onOperationSucceeded: function(handler)
	{
		this.get_events().removeHandler("onOperationSucceeded", handler);
	},
	add_onOperationFailed: function(handler)
	{
		this.get_events().addHandler("onOperationFailed", handler);
	},
	remove_onOperationFailed: function(handler)
	{
		this.get_events().removeHandler("onOperationFailed", handler);
	},

	// event delegates
	_onApplicationLoad: function(o)
	{
	},
	_onPropertyChanged: function(p)
	{
	},
	_onOperationStarted: function(sender, args)
	{
		this.raiseEvent("onOperationStarted", sender);
	},
	_onOperationSucceeded: function(sender, args)
	{
		this.raiseEvent("onOperationSucceeded", sender);
	},
	_onOperationFailed: function(sender, args)
	{
		this.raiseEvent("onOperationFailed", sender);
	}
};

// static methods
Competir.Web.UI.Webparts.BaseWebpartControl.getCurrent = function(typeName)
{
	var rv = null;
	var c = Sys.Application.getComponents();
	for (var i = 0; i < c.length; i++)
	{
		if (Object.getType(c[i]).getName() == "Competir.Web.UI.Webparts." + typeName)
		{
			rv = c[i];
		}
	}
	return rv;
};

// registration
Competir.Web.UI.Webparts.BaseWebpartControl.registerClass("Competir.Web.UI.Webparts.BaseWebpartControl", Sys.UI.Control);