//TSSearchController.js - 20081002 - Emile van Raaij, Topshare International BV
//
// Implements a controller for formatting and handling search requests that
// works with a tag that leans on Topshare.SearchHandler
// NOTE: since the interface to the Lucene search is NOT uniform for tops and nodes- we encapsulate the knowledge of
// 			the field fill-in in this class... This needs to be fixed by a redesign!

var ns_topshare;

if (!ns_topshare)
	ns_topshare ={};

if(!ns_topshare.ns_controllers)
	throw new Error('Missing dependency. Make sure Controllers.js is included.');

if(!ns_topshare.ns_searchcontrollers)
{
	ns_topshare.ns_searchcontrollers = {};

	ns_topshare.ns_searchcontrollers.SearchController = function (p_ctrl_id
		, p_targetName
		, p_targetType
		, p_contextId
		, p_resultsTag
		, p_searchType
		, p_viewContext)
	{
		// Init base class.
		this.superclass(p_ctrl_id, p_viewContext);

		this.tagExpanderUrl = '/tagexpander/asp/tagexpander.aspx?nbrNodeID=-1&tag=';
		this.targetName 	= p_targetName;		// The name of the container object, a Div, Span whatever
		this.targetType 	= p_targetType;		// Denounces frame or other object
		this.contextId 		= p_contextId;		// The base context from where to search
		this.resultsTag 	= p_resultsTag;		// The tag that performs the search
		this.searchType 	= p_searchType;		// 1 - top 2  -node
		
		// Added 08/12/08 R. Arts -- Extra param, we need to know whether or not to use the 
		// targetName or to repaint the page
		// defaults to false, this will not break existing implementations

		this.redirectToUrl = false;
		// Add custom handler
		this.AddHandler(ns_topshare.ns_controllers.SETSEARCHCONTEXT, this.HandleSwitchContext);
	};

	ns_topshare.ns_searchcontrollers.SearchController.prototype = new ns_topshare.ns_controllers.Controller();
	ns_topshare.ns_searchcontrollers.SearchController.prototype.superclass = ns_topshare.ns_controllers.Controller;
	ns_topshare.ns_searchcontrollers.SearchController.prototype.constructor = ns_topshare.ns_searchcontrollers.SearchController;

	ns_topshare.ns_searchcontrollers.SearchController.prototype.HandleSwitchContext = function (p_msg)
	{
		var p_nodeId = p_msg.argument[0];
		
		// Very ugly construction - since it introduces a dependency
		// 
		document.getElementById(this.ctrl_id + 'ancestorquery').value = p_nodeId;
	};

	// perform search
	ns_topshare.ns_searchcontrollers.SearchController.prototype.DoSearch = function (p_rawValue)
	{
		if(this.redirectToUrl == true)
		{
			// Invoking search through an url. We need to send out a navigate message
			var criteria = p_rawValue.split(' ');
			var nrCriteria = criteria.length;

			var params = new Array();
			params[0] = 4;
			params[1] = this.viewcontext;
			params[2] = true;
			params[3] = 'search';
			params[4] = this.searchType;
			params[5] = this.contextId;

			var query = '';
			
			for (var i = 0; i < nrCriteria; i++)
			{
				query = query + criteria[i];
				
				if (i != nrCriteria -1)
					query = query + ' ';
			}
			params[6] = query;

			var msg = this.ConstructMessage(ns_topshare.ns_controllers.NAVIGATE, params);
			this.SendMessage(msg);
		}
		else
		{
			this.ActivateSearch(p_rawValue);

			/*
		 var requestString = '@' + this.resultsTag;
			
			requestString += ' searchtype="' + this.searchType + '" ';			// type of search

			var queryParamField = 'contentquery';

			// depending on what we seek - fill right field.
			if(this.searchType == "1")
				queryParamField = 'fullcontentquery';

			if(this.contextId != null && this.contextId != '')
				requestString += ' ancestorquery="' + this.contextId + '" ';	// context within to search

			requestString += ' ' + queryParamField + '="' + p_rawValue+ '"';	// search value
			requestString += ' ' + this.resultsTag + '@';

			// This omits the Frames for now.
			// TODO: add later and factor this out in Utility class method.
			document.getElementById(this.targetName).innerHTML = this.LoadHtml(this.tagExpanderUrl + escape(requestString));
*/
			}
	};

	ns_topshare.ns_searchcontrollers.SearchController.prototype.SetRepaintOnSearch = function(p_value)
	{
		this.redirectToUrl = p_value;
	};
	
	ns_topshare.ns_searchcontrollers.SearchController.prototype.ActivateSearch = function(p_rawValue)
	{
			var requestString = '@' + this.resultsTag;
			requestString += ' searchtype="' + this.searchType + '" ';			// type of search

			var queryParamField = 'contentquery';

			// depending on what we seek - fill right field.
			if(this.searchType == "1")
				queryParamField = 'fullcontentquery';

			if(this.contextId != null && this.contextId != '')
				requestString += ' ancestorquery="' + this.contextId + '" ';	// context within to search

			requestString += ' ' + queryParamField + '="' + p_rawValue+ '"';	// search value
			requestString += ' ' + this.resultsTag + '@';

			// This omits the Frames for now.
			// TODO: add later and factor this out in Utility class method.
			document.getElementById(this.targetName).innerHTML = this.LoadHtml(this.tagExpanderUrl + escape(requestString));
		};
	
	
	
	// Treat query - this does nothing for now - but gets a tokenized and formatted
	// query to the search-tag.
	// This code should go to SearchTagHandler.cs - it does not belong in the webfiles, since it is not the
	// responsibility of the class.
	
	ns_topshare.ns_searchcontrollers.SearchController.prototype.TreatQuery = function (p_rawValue)
	{


		return query;
	};
	
}
