var Search = new Class({
	
	initialize: function()
	{
		this.shown = false;
		$('searchInput').addEvent('focus', function(){
			if (this.value != 'Indtast søgeord') return false;
			this.value = '';
		});
		$('searchInput').addEvent('blur', function(){
			if (this.value != '') return false;
			this.value = 'Indtast søgeord';
		});
	},
	
	now: function()
	{
		if ($('searchInput').value.trim() == '' || $('searchInput').value.trim() == 'Indtast søgeord'){
			alert('Du skal indtaste noget før du søger.');
			return void(0);
		}
		$('searchForm').submit();
	},
	
	toggleAttribs: function()
	{
		(this.shown) ? this.hideAttribs() : this.showAttribs();
	},
	
	showAttribs: function()
	{
		this.shown = true;
		var el = $('searchMore');
		var left = el.offsetLeft + el.offsetWidth;
		var top = el.offsetTop + el.offsetHeight;
		var max = left - $('searchInput').offsetLeft;
		
		var div = new Element('div').setProperty('id', 'searchAttribs');
		div.setProperty('style', 'left: ' + left + 'px; top: ' + top + 'px;');
		div.injectInside($('menuSubBar'));
		var fxWidth = new Fx.Style($('searchAttribs'), 'width');
		var fxHeight = new Fx.Style($('searchAttribs'), 'height');
		var fxLeft = new Fx.Style($('searchAttribs'), 'left');
		fxWidth.start(max);
		fxHeight.start(50);
		fxLeft.start($('searchAttribs').offsetLeft, ( $('searchAttribs').offsetLeft - max ));
	},
	
	hideAttribs: function()
	{
		this.shown = false;
		var el = $('searchMore');
		var left = el.offsetLeft + el.offsetWidth;
		var top = el.offsetTop + el.offsetHeight;
		var max = left - $('searchInput').offsetLeft;
		var fxWidth = new Fx.Style($('searchAttribs'), 'width');
		var fxHeight = new Fx.Style($('searchAttribs'), 'height');
		var fxLeft = new Fx.Style($('searchAttribs'), 'left', {
			onComplete: function()
			{
				$('searchAttribs').remove();
			}
		});
		fxWidth.start(max, 0);
		fxHeight.start(0);
		fxLeft.start($('searchAttribs').offsetLeft, ( $('searchAttribs').offsetLeft + max));
	},
	
	hover: function(obj)
	{
		$(obj).setStyle('background', '#f7f7f7');
	},
	
	unhover: function(obj)
	{
		$(obj).setStyle('background', '#ffffff');
	},
	
	goTo: function(link)
	{
		window.location = link;
	},
	
	next: function(page)
	{
		var url = "?view=search&page=" + (page + 1);
		$('controlsFormUpper').action = url;
		$('controlsFormUpper').submit();
		$('controlsFormLower').action = url;
		$('controlsFormLower').submit();
	},
	
	prev: function(page)
	{
		var url = "?view=search&page=" + (page - 1);
		$('controlsFormUpper').action = url;
		$('controlsFormUpper').submit();
		$('controlsFormLower').action = url;
		$('controlsFormLower').submit();
	},
	
	gotoPage: function(page)
	{
		var url = "?view=search&page=" + page;
		$('controlsFormUpper').action = url;
		$('controlsFormUpper').submit();
		$('controlsFormLower').action = url;
		$('controlsFormLower').submit();
	}
	
});

var search;

Window.addEvent('domready', function(){
	
	search = new Search();
	
});
