var isIE = navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Opera') == -1;
var isNotIE7 = isIE && navigator.userAgent.indexOf('MSIE 7.0') == -1;
var triggered = false;
var SlideList = new Class(
{
	initialize: function(menu, options)
	{
		this.setOptions(this.getOptions(), options);

		this.menu = $(menu), this.current = this.menu.getElement('li.current');
		this.menu.removeClass('no-javascript');

		this.menu.getElements('li').each(function(item){
			item.addEvent('mouseover', function(){ triggered = false; this.moveBg(item); }.bind(this));
			item.addEvent('mouseout', function(){  triggered = true; setTimeout(this.triggerBg.bind(this), 750);  }.bind(this));
			item.addEvent('click', function(event){ this.clickItem(event, item); }.bind(this));
		}.bind(this));

		this.back = new Element('li').addClass('background').adopt(new Element('div').addClass('left')).injectInside(this.menu);
		this.back.fx = this.back.effects(this.options);
		if(this.current) this.setCurrent(this.current);
	},

	triggerBg: function()
	{
		if(!triggered)
			return;

		this.moveBg(this.current);
	},

	setCurrent: function(el, effect)
	{
		this.back.setStyles({left: (el.offsetLeft)+'px', width: (el.offsetWidth)+'px'});
		(effect) ? this.back.effect('opacity').set(0).start(1) : this.back.setOpacity(1);
		this.current = el;
	},

	getOptions: function()
	{
		return {
			transition: Fx.Transitions.sineInOut,
			duration: 500, wait: false,
			onClick: Class.empty
		};
	},

	clickItem: function(event, item)
	{
		if(!this.current)
			this.setCurrent(item, true);
		this.current = item;
		this.options.onClick(new Event(event), item);
	},

	moveBg: function(to)
	{
		if(!this.current)
			return;

		this.back.fx.custom({
			left: [this.back.offsetLeft, to.offsetLeft],
			width: [this.back.offsetWidth, to.offsetWidth]
		});
	}
});

SlideList.implement(new Options);

function searchForm()
{
	var input = document.getElementById('searchtext');

	if(input)
	{
		input.onclick = function() {
			if(input.value == 'Search')
				input.value = '';
			else
				input.select();
		};
	};
};

function fixBrowserIssues()
{
	if(isIE && isNotIE7)
	{
		var externalLinks = $$('a[rel=external]');

		for(var i=0; i < externalLinks.length; i++)
			externalLinks[i].addClass('external');
	}
}

window.addEvent('domready', function() { new SlideList($E('ul', 'site-navigation'), {transition: Fx.Transitions.backOut, duration: 700 }); });
window.addEvent('domready', searchForm);
window.addEvent('domready', fixBrowserIssues);