var quickSearchSetup = new Class({
    initialize: function() {
	},
	setup: function()
	{
		elements = $$('#searchBar', '#searchPod');
		elements.each(function(ele) {
			if (Browser.Engine.trident && Browser.Engine.version < 6) {
				// IE6 or 7
				ele.addEvent('submit', function(e) {
					if (! checkPostcode(ele.postcode.value)) {
						e.stop();
						alert('Please enter a full, valid postcode');
					}
				});
			} else {
				var arrow = createArrowPopup(ele, 'Please enter a full, valid postcode');
				ele.addEvent('submit', function(e) {
					if (! checkPostcode(ele.postcode.value)) {
						e.stop();
						arrow.fade('in');
					} else {
						arrow.fade('out');
					}
				});
				ele.getElements('input[name=postcode]').each(function(ele) {
					ele.addEvent('click', function(e) {
						arrow.fade('out');
					});
				});
			}
		});
	}
});

var popupSetup = new Class({
    initialize: function() {
	},
	setup: function()
	{
		elements = $$('a[rel=external]');
		elements.each(function(ele) {
			ele.addEvent('click', function(e) {
				e.stop();
				if (ele.hasClass('smallPopup')) {
					var w=400;
					var h=480;
					var leftVal = (screen.width / 2) - (w/2);
					var topVal = (screen.height / 2) - (h/2);
					window.open (ele.getProperty('href'), 'new_window', 'left=' +leftVal+ ',top=' + topVal + ',toolbar=no,width=' + w + ',height=' + h + ',status=no,resizable=yes,menubar=no,location=no,scrollbars=yes');
				} else {
					window.open(ele.getProperty('href'));
				}
			});
		});
	}
});

var autoClearSetup = new Class({
    initialize: function() {
	},
	setup: function()
	{
		$$('input.autoclear',
		   'textarea.autoclear').each(function(elem) {
			elem.addEvent('focus', function() {
				if (elem.value == elem.alt) {
					elem.value = '';
				}
			});
			elem.addEvent('blur', function() {
				if (elem.value == '') {
					elem.value = elem.alt;
				}
			});
		});
	}
});

function createArrowPopup(parent, text)
{
	var arrow = new Element('div', {'class': 'largeArrow'});
	arrow.fade('hide');
	var txt = new Element('p', {'html': text});
	arrow.adopt(txt);
	parent.adopt(arrow);
	return arrow;
}

function checkPostcode(postcode)
{
	var postcode = postcode.replace(/\s/g, '');
	var rx = new RegExp('[^A-Za-z0-9]+');
	if (rx.test(postcode) ||
		postcode.length < 5 || postcode.length > 7) {
		return false;
	} else {
		return true;
	}
}

var accordionSetup = new Class({
    initialize: function() {
		this.display = -1;
	},
	setup: function()
	{
		var caller = this;
		var myAccordion = new Accordion($$('.collapsible>legend'), '.collapseMe', {
			opacity: false,
			display: caller.display,
			alwaysHide: true,
			onActive: function(toggler) {
				toggler.addClass('on');
			},
			onBackground: function(toggler) {
				toggler.removeClass('on');
			}
		});
	}
});

var friendlyFormSetup = new Class({
    initialize: function() {
	},
	setup: function()
	{
		$$('.friendlyForm .inputField').each(function(elem) {
			elem.blur();
			elem.addEvent('focus', function() {
				elem.getParent('li').getChildren('label').addClass('focus');
				var helpers = elem.getParent('li').getChildren('.helper');
				if (helpers.length) {
					var helper = helpers[0];
					helper.setStyles({
						display:'block',
						opacity: 0
					});
					helper.set('tween').fade(1);
					helper.style.display = 'block';
				}
				elem.addClass('focus');
			});
			elem.addEvent('blur', function() {
				elem.getParent('li').getChildren('label').removeClass('focus');
				var helpers = elem.getParent('li').getChildren('.helper');
				if (helpers.length) {
					var helper = helpers[0];
					helper.set('tween',{
						onComplete: function() {
							helper.style.display = 'none';
						}
					}).fade(0);
				}
				elem.removeClass('focus');
			});
		});
	}
});

var addToBasketSetup = new Class({
    initialize: function() {
	},
	setup: function()
	{
		elements = $$('.productForm');
		elements.each(function(ele) {
			var termsCheck = ele.getElement('.termsCheck');
			if (termsCheck) {
				var termsText = ele.getElement('.termsText');
				var sButton = ele.getElement('.addToBasket');
				sButton.addEvent('click', function(e) {
					if (! termsCheck.checked) {
						e.stop();
						termsText.highlight();
					}
				});
			}
		});
	}
});

var accordions = new accordionSetup;
var quickSearches = new quickSearchSetup;
var popups = new popupSetup;
var autoClears = new autoClearSetup;
var friendlyForms = new friendlyFormSetup;
var addToBasket = new addToBasketSetup;

window.addEvent('domready', function() {
	friendlyForms.setup();
	popups.setup();
	accordions.setup();
	quickSearches.setup();
	autoClears.setup();
	addToBasket.setup();
});