Event.observe(window,'load',CMS_InitMenu); 	// Tænder for mouseover-effekter på menuen
//CMS_AddOnloadEvent(CMS_InitLinks);	// Indsætter onclick-event på alle links, der har cms_url-attributen sat
/*CMS_AddOnloadEvent(function () {
	var loc = window.location.pathname;
	if ("/" != loc.substring( (loc.length -1),loc.length) && loc.indexOf('/',site_dir.length) === -1)
	{
		if (window.location.hash != "")		window.location = site_dir + window.location.hash;
		else								window.location = site_dir + "#" + window.location.pathname.substring(site_dir.length);
	}
	else
		setInterval(CMS_PageUpdate,300);
});		// Checker hver 500. millisekund om # er blevet ændret, fx ved tryk på "tilbage" knappen*/

CMS_AddOnloadEvent(CMS_InitSearch);

var CMS_SearchHighlighted = -1;
function CMS_InitSearch() 
{
	$('search_query').observe('focus',CMS_SearchFocus);
	$('search_query').observe('keyup',CMS_SearchChange);
	$('search_query').observe('blur',CMS_SearchBlur);
}


function CMS_SearchFocus(e)
{
	CMS_InputFocus('search_query','Indtast søgeord ...');
	
	CMS_SearchResults($('search_query').value);
}

function CMS_SearchClick()
{
	var options = $('searchResults').immediateDescendants();
//	window.location.hash = options[CMS_SearchHighlighted].readAttribute('cms_url');
	window.location = options[CMS_SearchHighlighted].readAttribute('href');
	
	$('search_query').blur();
}

function CMS_SearchChange(e)
{
	if ($('searchResults') && $('searchResults').select('a').size() > 0)
	{
		switch(e.keyCode)
		{
			case Event.KEY_RETURN:
				CMS_SearchClick();
				Event.stop(e);
			return;
			
			case Event.KEY_UP:
				var options = $('searchResults').immediateDescendants();
				if (CMS_SearchHighlighted >= 0)
				{
					options[CMS_SearchHighlighted].removeClassName('highlighted');
					CMS_SearchHighlighted -= 1;		
				}
				if (CMS_SearchHighlighted >= 0)
					options[CMS_SearchHighlighted].addClassName('highlighted');
				
				Event.stop(e);
			return;
			
			case Event.KEY_DOWN:
				var options = $('searchResults').immediateDescendants();
				if (CMS_SearchHighlighted+1 < options.size())
				{
					if (CMS_SearchHighlighted != -1)	options[CMS_SearchHighlighted].removeClassName('highlighted');
					CMS_SearchHighlighted += 1;
					options[CMS_SearchHighlighted].addClassName('highlighted');
				}
		
				Event.stop(e);			
			return;
			
			default:
				CMS_SearchResults($('search_query').value);
			return;
		}
	}
	else		CMS_SearchResults($('search_query').value);
}

function CMS_SearchHighlight(id)
{
	if ($(id))
	{
		if (!$(id).hasClassName('highlighted'))
		{
			$('searchResults').select('.highlighted').each(function(el) {el.removeClassName('highlighted');});
			$(id).addClassName('highlighted');
			CMS_SearchHighlighted = $(id).previousSiblings().size();
		}
	}
}

function CMS_SearchMouseover(e) 
{
	CMS_SearchHighlight(e.findElement('a'));
}

function CMS_SearchBlur(e)
{
	setTimeout(CMS_SearchHideResults,500);
}

function CMS_SearchResults(query)
{
	if ($('search_query').value.length > 0)		
	{
		if (!$('searchResults'))
		{
			$(document.body).insert(new Element('div',{'id' : 'searchResults'}).update('<img src="pics/ajax-loader.gif" width="32" height="32" />'),{'position' : 'bottom'});
			$('searchResults').observe('mouseover',CMS_SearchMouseover);
		}
	
		CMS_AjaxRequest('search.php','query='+query,'searchResults');

		CMS_SearchHighlighted = -1;
	}
	else
		CMS_SearchHideResults();
}

function CMS_SearchHideResults()
{
	if ($('searchResults'))
	{
		$('searchResults').stopObserving('mouseover',CMS_SearchMouseover);
		$('searchResults').remove();
	}
	
	CMS_SearchHighlighted = -1;
}






function CMS_InputFocus(id,def)
{
	if ($(id).value == def)		$(id).value = '';
	$(id).select();	
	$(id).observe('blur',function () {CMS_InputBlur(id,def);});
}

function CMS_InputBlur(id,def)
{
	if ($(id).value == '')		$(id).value = def;
	$(id).stopObserving('blur',function () {CMS_InputBlur(id,def);});
}

function CMS_InitMenu() {
	/*var preloadMenuHover = new Array();
	$$('#menuBox a').each(function(obj,i) {
		$(obj).observe('mouseover',CMS_MenuHover);
		$(obj).observe('mouseout',CMS_MenuBlur);
		
		preloadMenuHover[i] = new Image();
		preloadMenuHover[i].src = site_dir + 'design/menu/' + obj.readAttribute('cms_url') + '_hover.jpg';
	});*/
	var preload = new Array();
	preload[0] = new Image();
	preload[0].src = site_dir + 'pics/ajax-loader.gif';
	/*preload[1] = new Image();
	preload[1].src = site_dir + 'pics/ajax-loader-blue.gif';*/
	preload[2] = new Image();
	preload[2].src = site_dir + 'pics/arrow-left.gif';
}
function CMS_InitLinks() {
/*	$$('a[cms_url]').each(function(obj) {
		$(obj).writeAttribute('href','#' + $(obj).readAttribute('cms_url'));
	});*/
}

/*var CMS_Hash = "";
function CMS_PageUpdate() {
	if (window.location.hash != CMS_Hash)
	{
		var url = window.location.hash.substring(1);
		if (window.location.pathname.indexOf('/',site_dir.length) !== -1)
		{
			CMS_WindowRedirect(site_dir + "#" + url);
		}
		else
		{
			CMS_AjaxRequest('ajax.php','url='+url+'&target=contentsBox','contentsBox',true);
			CMS_AjaxRequest('ajax.php','url='+url+'&target=watermark');
			CMS_AjaxRequest('ajax.php','url='+url+'&target=leftPanel','leftPanel',false);
		
			CMS_Hash = window.location.hash;
		}
	}
}

function CMS_MenuHover(e) {
	var obj = e.findElement('a');
	$(obj).getElementsBySelector('img').each(function(image) {
		image.src = site_dir + 'design/menu/' + obj.readAttribute('cms_url') + '_hover.jpg';
	});
}
function CMS_MenuBlur(e) {
	var obj = e.findElement('a');
	$(obj).getElementsBySelector('img').each(function(image) {
		image.src = site_dir + 'design/menu/' + obj.readAttribute('cms_url') + '.jpg';
	});
}
*/
