window.addEvent('domready', function() {
	//Popup timeout id
	var t = 0;

	/* Newsletter popup */
	var newsform = $('newsletter-form');
	newsform.fx = new Fx.Style(newsform, 'opacity', {duration: 500,transition: Fx.Transitions.linear}).addEvent('onComplete', toggleZ);
	var scroll = new Fx.Scroll(window, {duration: 400, transition: Fx.Transitions.quadInOut});

	//Add events to links
	var news_links = [$$('.item403')[0].getElement('a'), $$('.item283')[0].getElement('a')];

	news_links.each(function(el,i) {
		el.addEvent('click', showNewsletter);
		el.addEvent('click', closeSerchBox);
	});
	$('newsletter-box-close').addEvent('click', closeNewsletter);
	var search_link = $$('.item163')[0].getElement('a');
	search_link.addEvent('click', closeNewsletter);

	//Newsletter dropdown
	$('newsletter_category').addEvent('change', function() {
		var options = this.getChildren();
		var len = options.length;
		for(var n = 0; n < len; n++) {
			if(options[n].selected) {
				var value = (options[n].value == '')? 'select': options[n].value;
				$('newsletter_select').setText(value);
				break;
			}
		}
	});
	//Show newsletter form
	function showNewsletter(e) {
		scroll.toTop();
		newsform.setStyle('z-index', 1);
		newsform.fx.start(1);
	};

	//Hide newsletter form
	function closeNewsletter(e) {
		newsform.fx.start(0);
	}

	function toggleZ(el) {
		var z = el.getStyle('opacity');
		if(z == 0) {
			el.setStyle('z-index', 0);
		}
	}

	//Search box implementation
	var lnk = $('search-box');
	lnk.setStyles({display:"block", opacity: 0})
	//assign the effect and start
	lnk.fx = new Fx.Style(lnk, 'opacity', {duration: 500,transition: Fx.Transitions.linear});

	//fade in
	search_link.addEvent('click', function (event) {
		scroll.toTop();
		lnk.setStyle('z-index', 1);
		lnk.fx.start(1);
	});

	//set the close action
	$('search-box-close').addEvent('click', closeSerchBox);

	//set the close action
	function closeSerchBox(e) {
		lnk.fx.start(0);
	};

	//Newsletter submit
	$('newsletter_submit').addEvent('click', function(e) {
		new Event(e).stop();
		//Check form is valid
		if(document.formvalidator.isValid(newsform)) {
			//Add confirmation popup
			var terms = getTerms();
			var content = '<div class="newsletter-confirm popup-message">'+terms;
			content += '<div class="buttons"><a title="Cancel" class="arrow-button-left close">Cancel</a><a title="Proceed" class="arrow-button-right confirm">Proceed</a></div></div>'
			popup(content, 200);

			var confirm_link = $$('.newsletter-confirm .confirm');

			//Confirm
			confirm_link.addEvent('click', function(e) {
				var callback = function(responseText) {
					//Close confirmation popup and open success/failure
					document.boxB.close();
					if(responseText == 'success') {
						var content = '<div class="newsletterthanks"><p>Thank you for subscribing to our newsletter.</p></div>';
						var time = 3000;
						var height = 87;
						closeNewsletter();
					}
					else {
						var content = '<div class="popup-message error"><p>'+responseText+'</p></div>';
						var time = 3000;
						var height = 200;
					}
					popup(content, height);
					//Close final popup
					t = setTimeout('document.boxB.close()', time);
				};
				var opt = {
					method:'post',
					data:newsform,
					onComplete:callback,
					evalScripts:true
				};
				//Post form
				new Ajax(newsform.action, opt).request();
			});
		}
		//Invalid form
		else {
			if($('newsletter_email').hasClass('invalid')) var message = 'Please enter a valid email address.';
			else if($('newsletter_category').hasClass('invalid')) var message = 'Please select at least one newsletter to subscribe to.';
			var content = '<div class="popup-message error"><p>'+message+'</p></div>';
			popup(content, 200);
			t = setTimeout('document.boxB.close()', 3000);
		}
	});
});

function popup(content, height, width) {
	var len = $$('.cbContainer').length;
	if(len > 0) {
		for(var n = 0; n < len; n++) {
			$$('.cbContainer')[n].remove();
		}
		if(typeof t != 'undefined') clearTimeout(t);
	}

	document.boxB = new MooPrompt('', content, {
		buttons:0,
		width:width || 345,
		height:height,
		overlay:true,
		showCloseBtn:false
	});

	//Cancel
	$$('.popup-message .close').addEvent('click', function(e) {
		document.boxB.close();
	});

	//Hide proceed button
	if($defined($('confirm-terms'))) {
		var confirm_link = $$('.popup-message .confirm');
		confirm_link.setStyle('display', 'none');
		$('confirm-terms').addEvent('click', function() {
			if(this.checked) confirm_link.setStyle('display', 'inline-block');
			else confirm_link.setStyle('display', 'none');
		});
	}
}

function getTerms() {
	var terms_url = $$('.item373')[0].getElement('a').getProperty('href');
	var privacy_url = $$('.item383')[0].getElement('a').getProperty('href');
	var text = '<p>To proceed please tick the box to confirm that you have read our terms and accept that we may use information you give us as set out in the <a href="'+privacy_url+'" title="Privacy Policy" target="_blank">Privacy Policy</a> and agree to use our site in accordance with the <a href="'+terms_url+'" title="Terms & Conditions" target="_blank">Terms & Conditions</a> on this website.</p>';
	var confirm = '<p><input type="checkbox" name="confirm-terms" id="confirm-terms" /> <label for="confirm-terms">Confirm</label></p>';
	return text+confirm;
}