var Navigation = new Class
({
	slideTime : 500,
	delayTime : 0,

	timeout : null,
	items : null,

    initialize : function(arrNavigation)
    {
        this.items = arrNavigation;
	
		this.items.each(function(objItem)
		{
			objSubNav = this.getSubNav(objItem);
		
			// only apply to first tier of list items
			if (objSubNav)
			{
				// set default slide
				objSubNav.set('slide', {duration: this.slideTime, transition: Fx.Transitions.Expo.easeOut});
			
				// hide nested lists
				objSubNav.slide('hide');
			}

			if (objItem.getParent('ul').id == 'navigation')
			{
				// toggle nested list visibility on mouseover
				objItem.addEvent('mouseover', function(objEvent)
				{
					objEvent.stop();
					
					objLink = objEvent.target;
					objSubNav = this.getSubNav(objLink);
					
					// set up a delay on mouseover
					this.timeout = setTimeout(function()
					{
						if (objSubNav != null) objSubNav.slide('in');
						objLink.addClass('active');
					
						this.clear(objLink);
	
					}.bind(this), this.delayTime);
	
				}.bind(this));
				
				// cancel the timeout on mouseout
				objItem.addEvent('mouseout', function()
				{			
					clearTimeout(this.timeout);
					if (!objItem.getParent().getElement('ul')) objItem.removeClass('active');
				
				}.bind([this, objItem]));
			}

		}, this);
    },

	getSubNav : function(objItem)
	{
		return objItem.getParent().getElement('ul');
	},

	clear : function(objKeep)
	{
		this.items.each(function(objLink)
		{
			objSubNav = this.getSubNav(objLink);
		
			if (objKeep != objLink)
			{
				if (objSubNav) objSubNav.slide('out');
				objLink.removeClass('active');
			}

		}, this);
	} 
    
});

var Registration = new Class
({
	form : null,
	message : null,
	submit : true,
	
    initialize : function(strForm, strMessage)
    {
		this.form = $(strForm);
		this.message = $(strMessage);
		
		if (this.message) this.message.style.display = 'none';
		if (this.form) this.form.addEvent('submit', this.submit.bind(this));
    },

    notify : function(strLabel, blnCustom)
    {
    	this.submit = false;
	    this.message.innerHTML == '';

	    strMessage = (blnCustom == true)? strLabel : '<strong>' + strLabel + '</strong> is a required field.';
    
    	if (this.message.innerHTML.indexOf(strLabel) == -1)
    	{    
			if (this.message.innerHTML == '') this.message.innerHTML = strMessage;
			else this.message.innerHTML += '<br />' + strMessage;
		}

		this.message.style.display = '';
		this.message.highlight('#EDF6E9');
    },
    
    submit : function()
    {
    	this.submit = true;
    
		// if required and blank
		if (this.form.first_name.value == '') this.notify('First Name');
		if (this.form.last_name.value == '') this.notify('Last Name');
		if (this.form.phone.value == '') this.notify('Telephone');
		//if (this.form.address1 && this.form.address1.value == '' && this.form.email.value == '' && this.form.phone.value == '') this.notify('Please leave at least one piece of <strong>contact information</strong>', true)
		
		return this.submit;
    }
    
});

var OnpulseMenu, RegisterForm, NewsTicker;

window.addEvent ('domready', function()
{
	OnpulseMenu = new Navigation($$('ul#navigation li a'));
	RegisterForm = new Registration('register', 'validation');
	if (typeof($$('p#ticker')[0]) != 'undefined') NewsTicker = new mooquee($$('p#ticker')[0]);
	
	// when the more link is clicked
	$$('.more-link').each(function(objMore)
	{
		objMore.onclick = function()
		{
			// remove it
			this.style.display = 'none';
		
			// enable the more content
			this.parentNode.parentNode.getElementsByTagName('div')[0].style.display = 'block';
		}
	});
	
	// kick off address post-code finder
	if ($('post_code')) postcodeFinder();
});