/* ws/fastSearch/static/js/page_prop.js */ /* UTF8-Côôkie */
/*
09/12/2009 - Handle add to/remove from request list buttons for property blocks in pages
*/

/*
Note: because a property may appear more than once in a page, we're using indexes rather that property id's for referencing properties!
*/

/*
Note: the class is instanciated only if request-list feature is used. Otherwise, only static methods are used by the request-make feature

*/

function	pagePropertiesClass(addText, removeText, makeReqText, viewReqText)
{
	var	self = this;

	//this.regProp = [];
	this.addText = addText;
	this.removeText = removeText;
	// 21/12/2009
	this.makeReqText = makeReqText;
	this.viewReqText = viewReqText;

	// Remember the cookie, we will periodically query it for changes
	this.requestedCookie = vkDom.getCookie('REQUESTED_PROPERTIES', '');
	// List of requested properties (loaded from cookie on startup...)
	this.requestedList = kigoList.createFromCSV(this.requestedCookie, ':');

	// List of props on the page
	this.props = [];

	vkDom.onLoad(
		function()
		{
			self.init();
		}
	);
}

pagePropertiesClass.prototype.registerRequestList = function(propId)
{
	this.props.push(propId);
}

pagePropertiesClass.prototype.init = function()
{
	var	self = this;

	// Setup periodic refresh
	setInterval(
		function()
		{
			var	newCookie = vkDom.getCookie('REQUESTED_PROPERTIES', '');

			if(newCookie != self.requestedCookie)
			{
				self.requestedList = kigoList.createFromCSV(self.requestedCookie = newCookie, ':');
				self.renderAll();
			}
		},
		2500
	);

	this.renderAll();
}


pagePropertiesClass.prototype.renderAll = function()
{
	// Renders button for all registered properties...
	// Done on init, but may also be called from a periodic cookie verification

	var	idx, a, inList;

	for(idx = 0; idx < this.props.length; idx++)
	{
		/*
		a = new kigoDom('RLT_'+idx);

		if(inList = this.requestedList.find(this.props[idx]))
			vkDom.addClass(a.domNode(), 'in_list');
		else
			vkDom.removeClass(a.domNode(), 'in_list');

		a.empty().append(
			kigoDom.create('span').append(
				inList ? this.removeText : this.addText
			)
		);
		*/

		inList = this.requestedList.find(this.props[idx]);

		if(this.addText.length && this.removeText.length)
		{
			a = new kigoDom('RLT_'+idx);

			if(inList)
				vkDom.addClass(a.domNode(), 'in_list');
			else
				vkDom.removeClass(a.domNode(), 'in_list');

			a.empty().append(
				kigoDom.create('span').append(
					inList ? this.removeText : this.addText
				)
			);
		}

		if(this.makeReqText.length && this.viewReqText.length)
		{
			a = new kigoDom('RVT_'+idx);

			if(inList)
				vkDom.addClass(a.domNode(), 'in_list');
			else
				vkDom.removeClass(a.domNode(), 'in_list');

			a.empty().append(
				kigoDom.create('span').append(
					inList ? this.viewReqText : this.makeReqText
				)
			);
		}
	}
}




pagePropertiesClass.prototype.toggleRequestList = function(idx)
{
	var	pos = this.requestedList.pos(this.props[idx]);

	if(pos != null)
		this.requestedList.remove(pos);
	else
		this.requestedList.add(this.props[idx]);

	vkDom.setCookie('REQUESTED_PROPERTIES', this.requestedCookie = this.requestedList.CSV(':'));
	
	// Always rerender all...
	this.renderAll();
}




// This one handles even "unregistered" properties by modifying the cookie directly
// STATIC
pagePropertiesClass.makeRequest = function(propId)
{
	var	list = kigoList.createFromCSV(vkDom.getCookie('REQUESTED_PROPERTIES', ''), ':');

	if(!list.find(propId))
	{
		list.add(propId);
		vkDom.setCookie('REQUESTED_PROPERTIES', list.CSV(':'));
	}

	return true;	// Default behaviour: go to requests page
}



