// ************************************************************************************
// Context
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.MiEmpresa");

// constructor
Competir.MiEmpresa.Context = function(element)
{
	Competir.MiEmpresa.Context.initializeBase(this, [element]);
	this._onApplicationLoadDelegate = Function.createDelegate(this, this._onApplicationLoad);
	this._onDocumentReadyStateChangeDelegate = Function.createDelegate(this, this._onDocumentReadyStateChange);
	this._onPageBeforeUnloadDelegate = Function.createDelegate(this, this._onPageBeforeUnload);
    this._FKInstancia = "";
    this._AKInstancia = "";
    this._GUID = "";
    this._Name = "";
    this._ExecutionQueue = new Competir.MiEmpresa.ExecutionQueue(2);
	this._User = new Competir.MiEmpresa.Security.User();
};

// prototype
Competir.MiEmpresa.Context.prototype =
{
	// methods
	initialize: function()
	{
		Sys.Application.add_load(this._onApplicationLoadDelegate);
		if (window.addEventListener)
		{
			window.addEventListener("beforeunload", this._onPageBeforeUnloadDelegate, false);
		}
		else if (document.addEventListener)
		{
			document.addEventListener('beforeunload', this._onPageBeforeUnloadDelegate, false);
		}
		Competir.MiEmpresa.Context.callBaseMethod(this, "initialize");
	},
	dispose: function()
	{
		Sys.Application.remove_load(this._onApplicationLoadDelegate);
		Competir.MiEmpresa.Context.callBaseMethod(this, "dispose");
	},
	raiseEvent: function(id, args)
	{
		var events = this.get_events();
		if (events)
		{
			var f = events.getHandler(id);
			if (f)
			{
				f(this, args);
			}
		}
	},

	// properties
	get_FKInstancia: function()
	{
		return this._FKInstancia;
	},
	set_FKInstancia: function(value)
	{
		if (this._FKInstancia !== value)
		{
			this._FKInstancia = value;
			this.raisePropertyChanged("FKInstancia");
		}
	},
	get_AKInstancia: function()
	{
		return this._AKInstancia;
	},
	set_AKInstancia: function(value)
	{
		if (this._AKInstancia !== value)
		{
			this._AKInstancia = value;
			this.raisePropertyChanged("AKInstancia");
		}
	},
	get_Name: function()
	{
		return this._Name;
	},
	set_Name: function(value)
	{
		if (this._Name !== value)
		{
			this._Name = value;
			this.raisePropertyChanged("Name");
		}
	},
	get_GUID: function()
	{
		return this._GUID;
	},
	set_GUID: function(value)
	{
		if (this._GUID !== value)
		{
			this._GUID = value;
			this.raisePropertyChanged("GUID");
		}
	},
	get_User: function()
	{
		return this._User;
	},
	set_User: function(value)
	{
		if (!Competir.MiEmpresa.Security.User.isInstanceOfType(value))
		{
			value = new Competir.MiEmpresa.Security.User(value);
		}
		if (this._User !== value)
		{
			this._User = value;
			this.raisePropertyChanged("User");
			this.raiseEvent("onUserChange", Sys.EventArgs.Empty);
		}
	},
	get_ExecutionQueue: function()
	{
		return this._ExecutionQueue;
	},
	set_ExecutionQueue: function(value)
	{
		if (this._ExecutionQueue !== value)
		{
			this._ExecutionQueue = value;
			this.raisePropertyChanged("ExecutionQueue");
		}
	},

	// events
	add_onUserChange: function(handler)
	{
		this.get_events().addHandler("onUserChange", handler);
	},
	remove_onUserChange: function(handler)
	{
		this.get_events().removeHandler("onUserChange", handler);
	},

	// event delegates
	_onApplicationLoad: function(o)
	{
		if (document.readyState && document.readyState != "complete")
		{
			document.attachEvent("onreadystatechange", this._onDocumentReadyStateChangeDelegate);
		}
		else
		{
			var eq = this.get_ExecutionQueue();
			if (eq)
			{
				eq.start();
			}
		}
	},
	_onDocumentReadyStateChange: function()
	{
		if (document.readyState == "complete")
		{
			document.detachEvent("onreadystatechange", this._onDocumentReadyStateChangeDelegate);
			var eq = this.get_ExecutionQueue();
			if (eq)
			{
				eq.start();
			}
		}
	},
	_onPageBeforeUnload: function()
	{
		var eq = this.get_ExecutionQueue();
		if (eq)
		{
			eq.stop();
		}
	}
};

// static methods
Competir.MiEmpresa.Context.getCurrent = function()
{
	var rv = null;
	var c = Sys.Application.getComponents();
	for (var i = 0; i < c.length; i++)
	{
		if (Object.getType(c[i]).getName() == "Competir.MiEmpresa.Context")
		{
			rv = c[i];
		}
	}
	return rv;
};

// registration
Competir.MiEmpresa.Context.registerClass("Competir.MiEmpresa.Context", Sys.UI.Control);