Function.prototype.bind = function() {
	var method = this, args = jQuery.makeArray(arguments), object = args.shift();
	return function () { return method.apply(object, args.concat(jQuery.makeArray(arguments))); };
};

function Search(element, sSearchURI, sTemplate)
{
	this.sSearchURI = sSearchURI;
	this.sTemplate  = sTemplate;
	
	this.sQuery     = '';
	
	this.aSites     = new Array();
	this.aBlocks    = new Array();
	
	this._bLoading  = false;
	
	var aElements = $("#" + element);
	
	if (aElements.size() > 0)
	{
  	this.element = $(aElements.get());
  } // if
  else
	{
		throw(element + " not found!");
	} // else

} // function Search

/**
 * Fuegt eine Collection hinzu
 * 
 * @access public
 * @param String sSite
 * @return void
 */
Search.prototype.setSite = function(sSite)
{
	this.aSites.push(sSite);
} // function setSite

/**
 * Erzeugt den die QS Parameter fuer die Collections
 * 
 * @access private
 * @return String
 */
Search.prototype._getSiteParams = function()
{
	var sToReturn = '';
	
	for (var i=0; i < this.aSites.length; i++)
	{
		sToReturn+= '&site[]=' + this.aSites[i];
	} // for
	
	return sToReturn;
} // function _getSiteParams

/**
 * Fuegt einen Block hinzu
 * 
 * @access public
 * @param String sKey
 * @param String sValue
 * @return void
 */
Search.prototype.addBlock = function(sBlock)
{
	this.aBlocks.push(sBlock);
} // function addBlock

/**
 * Erzeugt den die QS Parameter fuer die Bloecke
 * 
 * @access private
 * @return String
 */
Search.prototype._getBlockParams = function()
{
	var sReturn = '';
	
	for (var i = 0; i < this.aBlocks.length; i++)
	{
		sReturn+= '&requiredfields[]=' + this.aBlocks[i];
	} // for
	
	return sReturn;
} // function _getBlockParams

/**
 * Erzeugt die Ausgabe des Templates
 * 
 * @access private
 * @param Object element
 * @param Object oJSON
 * @param bool bBlockmode
 * @return void
 */
Search.prototype._renderResults = function(element, oJSON, bBlockmode)
{
	if (element.hasTemplate() == 0)
	{
	  	element.setTemplateURL(this.sTemplate, null, {
	  		filter_data: false
	  	});
	} // if
  
	element.processTemplate({
		blockmode:  bBlockmode,
		num:        oJSON.num,
		q:          oJSON.q,
		resultNum:  oJSON.resultNum,
		results:    oJSON.results,
		navigation: oJSON.navigation
	});
} // function _renderResult

/**
 * Blaettern
 * 
 * @access public
 * @param int iStart
 * @param string sBlock
 * @param Object oBlock
 * @return void
 */
Search.prototype.goToPage = function(iStart, sBlock, oBlock)
{
	if (this._bLoading) return;
	this._bLoading = true;
	
	var sUri = this.sSearchURI + '?q=' + encodeURI(this.sQuery);
	sUri+= this._getSiteParams();
	sUri+= '&requiredfields[]=' + sBlock;
	sUri+= '&start=' + iStart;
	
	$.ajax({
		type: 'GET',
		url: sUri,
		dataType: 'json',
		success: function(oJSON) {
			this._renderResults($($(oBlock).parents('div.box_links').get(0)), oJSON, true);
			this._bLoading = false;
		}.bind(this)
	});
} // function searchBlock

/**
 * Fuehrt eine Suche durch
 * 
 * @access public
 * @return void
 */
Search.prototype.start = function(sQuery)
{
	if (this._bLoading) return;
	this._bLoading = true;
	
	this.sQuery = sQuery;
	
	var sUri = this.sSearchURI + '?q=' + encodeURI(this.sQuery);
	sUri+= this._getSiteParams();
	sUri+= this._getBlockParams();
	
	$.ajax({
		type: 'GET',
		url: sUri,
		dataType: 'json',
		success: function(oJSON) {
			this._renderResults(this.element, oJSON, false);
			this._bLoading = false;
		}.bind(this)
	});
} // function start
