/**
 * init()
 * Called in the body onload this function will initialise the search form we need.
 */
function init()
{	
	/* Search init */
	new Request({ url: '/modules/search_engine/tasks/search_build.php', 
			      method: 'get', 
			      onSuccess: function(responseText){ displaySearch(responseText); resultRequest(); },
			      onFailure: function(){ }
				}).send("init=true&sid="+Math.random()); 
};

/**
 * searchRequestHome()
 * Called in the body onload this function will initialise the search form we need.
 */
function searchRequestHome()
{	
	new Request({ url: '/modules/search_engine/tasks/search_build_home.php', 
			      method: 'get', 
			      onSuccess: function(responseText){ displaySearch(responseText); add_search_events(); },
			      onFailure: function(){ }
				}).send( $('dropdown_search') ); 
};

/**
 * init()
 * Called in the body onload this function will initialise the search form we need.
 */
function init_home()
{		
	new Request({ url: '/modules/search_engine/tasks/search_build_home.php', 
			      method: 'get', 
			      onSuccess: function(responseText){ displaySearch(responseText); add_search_events(); },
			      onFailure: function(){ }
				}).send("init=true"); 
};

/**
 * requestResults()
 * This method is a wrapper for the method used in the current IBE engine. We did this to prevent
 * the old IBE engine from breaking while developping the code improved one.
 */
function requestResults( page )
{
	var page = page || 0;
	
	searchRequest();
	resultRequest( page );
};

/**
 * searchRequest()
 * This function handles the request for search elements on every search page. To be able to get
 * the right searchform on initialisation we need to provide a MODE in which we define what search 
 * engine we're currently using.
 *
 * This function is also being called in the body onload of the index.php.
 */
function searchRequest()
{	
	/* Search init */
	new Request({ url: '/modules/search_engine/tasks/search_build.php', 
			      method: 'get', 
			      onSuccess: function(responseText){ displaySearch(responseText); },
			      onFailure: function(){ }
				}).send($('dropdown_search')); 
};

/**
 * resultRequest()
 * This function handles the request for results on every search page. To be able to get
 * the right results we need to provide a MODE in which we define what search engine we're 
 * currently using.
 */
function resultRequest( page )
{	
	var page = page || 0;
	
	/* This is where the magic happens. Creating a prototype AJAX object to handle AJAX requests */
	/* Adding the date to the parameter list is a workaround (hack...) to avoid IE caching the AJAX request 
	   Reference: http://mumrah.net/2008/07/from-the-archives-ajax-ie-caching-issue-recap/
	*/
	/* Search init */
	new Request({ url: '/modules/search_engine/tasks/result_build.php', 
			      method: 'get',
			      onSuccess: function(responseText){ displayResult(responseText); },
			      onFailure: function(){ }
				}).send("page="+page+"&sid="+Math.random());	
};

/**
 * displaySearch( resp )
 * Here we add the AJAX responsetext to the right div.
 */
function displaySearch( resp )
{
	$('search').innerHTML = resp;
};

/**
 * displayResult( resp )
 * Here we add the AJAX responsetext to the right div.
 */
function displayResult( resp )
{
	$('result').innerHTML = resp;
};

function displayInit()
{
	$('result').innerHTML = "<center>Maak een keuze uit bovenstaande opties.</center>";
};

function add_search_events()
{	
	$('reset_search_form').addEvent('click',function(e) {
		new Event(e).stop();
		init_home();
	});
	
	$('submit_search_form').addEvent('click',function(e) {
		new Event(e).stop();
		var urlQuery = $('dropdown_search').toQueryString();
		
		Shadowbox.open({
		        player:     'iframe',
		        title: 		'Zoek en boek',
		        content:    '/engine_ibe/PCRM/booking_engine/crm_engine/index.php?'+urlQuery,
		        width:      '940px'
	        });
	});
	
	$('advanced_search').addEvent('click', function(e) {
		new Event(e).stop();
		var urlQuery = $('dropdown_search').toQueryString();
		Shadowbox.open({
		        player:     'iframe',
		        title: 		'Zoek en boek',
		        content:    '/engine_ibe/PCRM/booking_engine/crm_engine/index.php?'+urlQuery+'&options=true',
		        width:      '940px'
	        });
	});
};
