// ************************************************************************************
// IdeasAndComments
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.Web.UI.Webparts");

// constructor
Competir.Web.UI.Webparts.IdeasAndComments = function(element)
{
	Competir.Web.UI.Webparts.IdeasAndComments.initializeBase(this, [element]);
	this._onUserChangeDelegate = Function.createDelegate(this, this.onUserChange);
	this._Mode = "Regular";
	this._Entries = 0;
	this._Randomize = false;
	this._SortDirection = "Descending";
	this._UseCacheForQueryResults = false;
	this._QueryResultsCacheTTL = null;
	this._WarningBoxClientID = "";
	this._FormBoxClientID = "";
	this._TxtCommentClientID = "";
	this._AKInstanciaEstadoPublicacion = "Publicado";
	this._FKInstancia = 0;
	this._AKInstancia = "";
	this._WithCommunityStats = false;
};

// prototype
Competir.Web.UI.Webparts.IdeasAndComments.prototype =
{
	// methods
	initialize: function()
	{
		var objContext = Competir.MiEmpresa.Context.getCurrent();
		if (objContext)
		{
			objContext.add_onUserChange(this._onUserChangeDelegate);
		}
		Competir.Web.UI.Webparts.IdeasAndComments.callBaseMethod(this, "initialize");
	},
	dispose: function()
	{
		var objContext = Competir.MiEmpresa.Context.getCurrent();
		if (objContext)
		{
			objContext.remove_onUserChange(this._onUserChangeDelegate);
		}
		Competir.Web.UI.Webparts.IdeasAndComments.callBaseMethod(this, "dispose");
	},
	renderContent: function()
	{
		var o = new Competir.MiEmpresa.Operation("ideasandcomments.content.render");
		o.addParameter("ID", this.get_id());
		o.addParameter("Mode", this.get_Mode());
		o.addParameter("Entries", this.get_Entries());
		o.addParameter("Randomize", this.get_Randomize());
		o.addParameter("SortDirection", this.get_SortDirection());
		o.addParameter("UseCacheForQueryResults", this.get_UseCacheForQueryResults());
		o.addParameter("QueryResultsCacheTTL", this.get_QueryResultsCacheTTL());
		o.addParameter("Paginate", this.get_Paginate());
		o.addParameter("ActualPage", this.get_ActualPage());
		o.addParameter("PageLength", this.get_PageLength());
		o.addParameter("PageSetLength", this.get_PageSetLength());
		o.addParameter("PaginateXpression", this.get_PaginateXpression());
		o.addParameter("SortHelperXpression", this.get_SortHelperXpression());
		o.addParameter("SortHelperDataType", this.get_SortHelperDataType());
		o.addParameter("SortHelperOrder", this.get_SortHelperOrder());
		o.addParameter("FKInstancia", this.get_FKInstancia());
		o.addParameter("AKInstancia", this.get_AKInstancia());
		o.addParameter("WithCommunityStats", this.get_WithCommunityStats());
		o.addParameter("ScriptFolder", this.get_ScriptFolder());
		o.addParameter("XsltFolder", this.get_XsltFolder());
		o.addParameter("XsltFile", this.get_XsltFile());
		o.addParameter("XsltParametersXml", this.get_XsltParameters().toXml(), false);
		o.addParameter("XsltFileUri", this.get_XsltFileUri());
		o.addParameter("ScriptFolderUri", this.get_ScriptFolderUri());
		o.addListener(this);
		Competir.MiEmpresa.Context.getCurrent().get_ExecutionQueue().enqueue(o);
	},
	postComment: function()
	{
		if (this.getValue() != "")
		{
			var usr = Competir.MiEmpresa.Context.getCurrent().get_User();
			if (usr && usr.get_UserType() && usr.get_UserType().AKInstancia != "TipoUsuario.Anonymous")
			{
				var o = new Competir.MiEmpresa.Operation("comments.post");
				o.addParameter("ID", this.get_id());
				o.addParameter("FKInstancia", this.get_FKInstancia());
				o.addParameter("Text", this.getValue());
				o.addParameter("AKInstanciaEstadoPublicacion", this.get_AKInstanciaEstadoPublicacion());
				o.addListener(this);
				Competir.MiEmpresa.Context.getCurrent().get_ExecutionQueue().enqueue(o);
			}
			else
			{
				alert("Para dejar un comentario debes ser un usuario registrado.");
			}
		}
	},
	changePageIndex: function(index)
	{
		this.set_ActualPage(index);
		this.renderContent();
	},
	getValue: function()
	{
		var rv = "";
		if (this.get_TxtCommentClientID() != "")
		{
			var objTxtComment = this.getChild(this.get_TxtCommentClientID());
			if (objTxtComment)
			{
				rv = objTxtComment.value;
			}
		}
		return rv;
	},

	// properties
	get_Mode: function()
	{
		return this._Mode;
	},
	set_Mode: function(value)
	{
		if (this._Mode !== value)
		{
			this._Mode = value;
			this.raisePropertyChanged("Mode");
		}
	},
	get_Entries: function()
	{
		return this._Entries;
	},
	set_Entries: function(value)
	{
		if (this._Entries !== value)
		{
			this._Entries = value;
			this.raisePropertyChanged("Entries");
		}
	},
	get_Randomize: function()
	{
		return this._Randomize;
	},
	set_Randomize: function(value)
	{
		if (this._Randomize !== value)
		{
			this._Randomize = value;
			this.raisePropertyChanged("Randomize");
		}
	},
	get_SortDirection: function()
	{
		return this._SortDirection;
	},
	set_SortDirection: function(value)
	{
		if (this._SortDirection !== value)
		{
			this._SortDirection = value;
			this.raisePropertyChanged("SortDirection");
		}
	},
	get_UseCacheForQueryResults: function()
	{
		return this._UseCacheForQueryResults;
	},
	set_UseCacheForQueryResults: function(value)
	{
		if (this._UseCacheForQueryResults !== value)
		{
			this._UseCacheForQueryResults = value;
			this.raisePropertyChanged("UseCacheForQueryResults");
		}
	},
	get_QueryResultsCacheTTL: function()
	{
		return this._QueryResultsCacheTTL;
	},
	set_QueryResultsCacheTTL: function(value)
	{
		if (this._QueryResultsCacheTTL !== value)
		{
			this._QueryResultsCacheTTL = value;
			this.raisePropertyChanged("QueryResultsCacheTTL");
		}
	},
	get_WarningBoxClientID: function()
	{
		return this._WarningBoxClientID;
	},
	set_WarningBoxClientID: function(value)
	{
		if (this._WarningBoxClientID !== value)
		{
			this._WarningBoxClientID = value;
			this.raisePropertyChanged("WarningBoxClientID");
		}
	},
	get_FormBoxClientID: function()
	{
		return this._FormBoxClientID;
	},
	set_FormBoxClientID: function(value)
	{
		if (this._FormBoxClientID !== value)
		{
			this._FormBoxClientID = value;
			this.raisePropertyChanged("FormBoxClientID");
		}
	},
	get_TxtCommentClientID: function()
	{
		return this._TxtCommentClientID;
	},
	set_TxtCommentClientID: function(value)
	{
		if (this._TxtCommentClientID !== value)
		{
			this._TxtCommentClientID = value;
			this.raisePropertyChanged("TxtCommentClientID");
		}
	},
	get_AKInstanciaEstadoPublicacion: function()
	{
		return this._AKInstanciaEstadoPublicacion;
	},
	set_AKInstanciaEstadoPublicacion: function(value)
	{
		if (this._AKInstanciaEstadoPublicacion !== value)
		{
			this._AKInstanciaEstadoPublicacion = value;
			this.raisePropertyChanged("AKInstanciaEstadoPublicacion");
		}
	},
	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_WithCommunityStats: function()
	{
		return this._WithCommunityStats;
	},
	set_WithCommunityStats: function(value)
	{
		if (this._WithCommunityStats !== value)
		{
			this._WithCommunityStats = value;
			this.raisePropertyChanged("WithCommunityStats");
		}
	},

	// event delegates
	_onApplicationLoad: function(o)
	{
		if (this.get_Mode() == "Regular")
		{
			this.renderContent();
		}
	},
	_onOperationStarted: function(sender, args)
	{
		throw Error.notImplemented("IdeasAndComments._onOperationStarted no se ha implementado en la aplicación.");
	},
	_onOperationSucceeded: function(sender, args)
	{
		throw Error.notImplemented("IdeasAndComments._onOperationSucceeded no se ha implementado en la aplicación.");
	},
	_onOperationFailed: function(sender, args)
	{
		throw Error.notImplemented("IdeasAndComments._onOperationFailed no se ha implementado en la aplicación.");
	},
	onUserChange: function(sender, args)
	{
		var objFormBox = this.getChild(this.get_FormBoxClientID());
		if (objFormBox)
		{
			var objWarningBox = this.getChild(this.get_WarningBoxClientID());
			var usr = sender.get_User();
			if (usr && usr.get_UserType() && usr.get_UserType().AKInstancia != "TipoUsuario.Anonymous")
			{
				Competir.Web.UI.show(objFormBox);
				if (objWarningBox)
				{
					Competir.Web.UI.hide(objWarningBox);
				}
			}
			else
			{
				Competir.Web.UI.hide(objFormBox);
				if (objWarningBox)
				{
					Competir.Web.UI.show(objWarningBox);
				}
			}
		}
	}
};

// registration
Competir.Web.UI.Webparts.IdeasAndComments.registerClass("Competir.Web.UI.Webparts.IdeasAndComments", Competir.Web.UI.Webparts.BasePaginatorControl);