// JavaScript Document

var CMS_Search = Class.create();

CMS_Search.prototype = {
	input: false,
	options: {},
	selected: -1,
	removeNow: false,
	
	request: {},
	resultContainer: false,
	
	
	initialize : function(input,options)
	{
		this.input = $(input);
		
		this.options = Object.extend({
			wrappers : []
		}, options);
		
		this.resultContainer = new Element('div').addClassName('CMS_SearchResultContainer');
		this.resultContainer.observe('mouseover',this.mouseover.bindAsEventListener(this));
		
		$(input).observe("focus",this.change.bindAsEventListener(this))
				.observe("keyup",this.change.bindAsEventListener(this))
				.observe("keydown",this.key.bindAsEventListener(this))
				.observe("blur",this.blur.bindAsEventListener(this));
		
	},
	
	key : function(event) 
	{
		var action = false;
		
		if (event.keyCode == Event.KEY_RETURN)		action = "click";
		else if (event.keyCode == Event.KEY_UP)		action = "up";
		else if (event.keyCode == Event.KEY_DOWN)	action = "down";
		else if (event.keyCode == Event.KEY_TAB)	action = (event.shiftKey) ? "up" : "down";

	
		if (action === false)
			return false;

		var results = this.resultContainer.select('tr[onclick]');
		var num_of_results = results.size();

		switch(action)
		{
			case "click":
				if (num_of_results == 1)	this.selected = 0;
				
				if (this.selected > -1)
					$(results[this.selected]).onclick.call();
				event.stop();
			return;
			
			case "up":
				if (this.selected > -1)
				{
					results[this.selected].removeClassName('highlight');
					this.selected -= 1;		
				}
				else if (num_of_results > 0)
					// Spring automatisk til det sidste resultat
					this.selected = num_of_results-1;
				
				if (this.selected >= 0)
				{
					results[this.selected].addClassName('highlight');
				}
				event.stop();

				this.resultContainer.stopObserving('mouseover');
				setTimeout((function() {
					this.resultContainer.observe('mouseover',this.mouseover.bindAsEventListener(this));
				}).bind(this),100);
			break;
			
			case "down":
				if (this.selected != -1)	results[this.selected].removeClassName('highlight');
				this.selected += 1;
				
				if (this.selected < num_of_results)
				{
					results[this.selected].addClassName('highlight');
				}
				else
					// Spring automatisk til søgefeltet
					this.selected = -1;
				
				event.stop();
				
				this.resultContainer.stopObserving('mouseover');
				setTimeout((function() {
					this.resultContainer.observe('mouseover',this.mouseover.bindAsEventListener(this));
				}).bind(this),100);
			break;
		}

		if (this.selected < 1)
			this.resultContainer.scrollTop = 0;
		else if (results[this.selected].offsetTop < this.resultContainer.scrollTop+20)
			this.resultContainer.scrollTop = results[this.selected].offsetTop-20;
		else if (results[this.selected].offsetTop > this.resultContainer.scrollTop + 100)
			this.resultContainer.scrollTop = results[this.selected].offsetTop - 100;

	},
	
	change : function(event)
	{
		if (event.type == "focus")
		{
			this.options.wrappers.invoke('addClassName','CMS_SearchActive');
			this.input.activate();
		}
		else
		{
			switch(event.keyCode)
			{
				case 16:
				case Event.KEY_TAB:
				case Event.KEY_LEFT:
				case Event.KEY_RIGHT:
				case Event.KEY_RETURN:
				case Event.KEY_UP:
				case Event.KEY_DOWN:	
				return;
			}
		}

		if (this.input.value.length < 2)
		{
			if (this.resultContainer.up())
			{
				this.resultContainer.update("").remove();
			}
			return false;
		}
		
		var microtime = CMS_Microtime();
		this.request.microtime = microtime;
		this.removeNow = false;
		
		if (!this.resultContainer.up())
		{
			var LoadingDiv = new Element('div').addClassName('CMS_SearchLoading').update("<img src='"+_common_url+"gfx/ajax-loader.gif' width='16' height='16' />");
			this.resultContainer.update(LoadingDiv);
			this.input.insert({'after' : this.resultContainer});
		
		}
		else if (!this.resultContainer.down(".CMS_SearchLoading"))
			this.resultContainer.setOpacity(0.3);
				
		var ajaxOptions = {
			method: 'post',
			parameters: "module=search&query="+CMS_UrlEncode(this.input.value),
			onCreate : (function (response)
				{
					if (typeof(this.request.transport)!="undefined" && this.request.transport.readyState < 4)
						this.request.transport.abort();
					
					this.request.transport = response.transport;
				}).bind(this),
			onSuccess : (function (response)
				{
					if (this.request.microtime == microtime /*&& $(container_id)*/)
					{
						this.resultContainer.setOpacity(1.0);
			
						this.selected = -1;
						this.resultContainer.scrollTop = 0;
						this.resultContainer.update(response.responseText.stripScripts());
						response.responseText.evalScripts();
					}
				}).bind(this),
			onFailure : (function (response)
				{
					if (this.request.microtime == microtime /*&& $(container_id)*/)
					{
						this.resultContainer.setOpacity(1.0);
						this.selected = -1;
					}
				}).bind(this)
		};
						
		setTimeout((function() {
			if (this.request.microtime == microtime)
			{
				new Ajax.Request(_site_url + 'ajax.php',ajaxOptions);
			}
		}).bind(this),200);		
	},
	
	
	blur : function(e)
	{
		this.removeNow = true;
		setTimeout((function() {
			if (this.resultContainer.up() && this.removeNow)
			{
//				this.resultContainer.stopObserving('mouseover');
				this.resultContainer.remove();
			}
		}).bind(this),200);

		this.options.wrappers.invoke('removeClassName','CMS_SearchActive');
	},
	
	mouseover : function(event)
	{
		var tr = event.findElement('tr');
		if ($(tr) && $(tr).readAttribute('onclick'))
		{
			if (!$(tr).hasClassName('highlight'))
			{
				this.resultContainer.select('.highlight').invoke("removeClassName","highlight");
				$(tr).addClassName('highlight');
			}
			this.selected = $(tr).previousSiblings().size() -1;
		}
	}
};
