window.addEvent('domready', function()
{
	// Set default locale (need this for proper handling of Date)
	Locale.use('en-GB');
	
	// Setup accordions
	var accordionDisplay = -1;
	$$('.collapseMe').each(function(elem, index) {
		if (elem.hasClass('selected')) {
			accordionDisplay = index;
		}
	});
	
	var myAccordion = new Fx.Accordion($$('.collapsible'), '.collapseMe', {
		opacity: false,
		display: accordionDisplay,
		alwaysHide: true,
        onComplete:function() {
			// Fix cropped content issue
			if (this.target && this.target.getSize().y > 0) {
				this.target.setStyle('height','auto'); 
			}
        }, 
		onActive: function(toggler,el) {
			this.target = el;
			toggler.removeClass('closed').addClass('open');
		},
		onBackground: function(toggler,el) {
			toggler.removeClass('open').addClass('closed');
		}
	});
	
	// Add functionality for pod heading collapsing/expanding
	$$('.pod h4').each(function(item, index) {
		if (contentDiv = item.getNext()) {
			if (contentDiv.hasClass('contentWrapper') == false) {
				return false;
			}
			item.style.cursor = 'pointer';
			var myVerticalSlide = new Fx.Slide(contentDiv);
			item.addEvent('click', function(e) {
				e.stop();
				myVerticalSlide.toggle();
				item.toggleClass('collapsed');
			});
		}
	});

	// Init shop
	document.shopBasket = new ShopBasket;
	document.shopBasket.setController('/shop/basket');
	document.shopBasket.setPodElem(document.getElementById('shopPod'));
	document.shopBasket.setWrapperElem(document.getElementById('miniShopBasket'));
		
	// Init friendly forms
	$$('.pageForm .helper').setStyles({
		display: 'none'
	});
	$$('.pageForm input[type=text],' +
		'.pageForm input[type=password],' +
		'.pageForm select,' + 
		'.pageForm textarea').each(function(elem)
	{
		elem.blur();
		elem.addEvent('focus', function() {
			elem.getParent('li').getChildren('label').addClass('focus');
			elem.addClass('focus');
			elem.getParent('li').getChildren('.helper').reveal();
		});
		elem.addEvent('blur', function() {
			elem.getParent('li').getChildren('label').removeClass('focus');
			elem.removeClass('focus');
			elem.getParent('li').getChildren('.helper').dissolve();
		});
	});
	
	// Signup form email field placeholder
	$$('#signupEmail').addEvent('focus', function () {
		if (this.value == 'Email address') {
			this.value = '';
		}
	});
	$$('#signupEmail').addEvent('blur', function () {
		if (this.value == '') {
			this.value = 'Email address';
		}
	});

	// Sortable tables
	$$('table.sortme').each(function(tableElem)
	{
		new HtmlTable(tableElem, {
			sortable: true,
			sortIndex: null
		});
	});
});

function helpImages()
{
	var helpImages = $$('a.helpT');

	helpImages.each(function(item, index)
	{
		var tipz = new Tips('.helpT');
		item.store('tip:text', $('FullDescription').getNext('p').get('html'));
	});
}

function go(loc)
{
	document.location.href = loc;
}

function goHide(obj)
{
	obj.style.display = 'none';
}

function goShow(obj)
{
	obj.style.display = 'block';
}

function goFocus(obj)
{
	try {
		obj.focus();
	} catch (e) {
	}
}

function goDisable(obj)
{
	obj.disabled = true;
}

function goEnable(obj)
{
	obj.disabled = false;
}

function goClearField(obj, text)
{
	if (obj.value == text) obj.value = '';
}

function goFillField(obj, text)
{
	if (obj.value == '') obj.value = text;
}

function goClearFieldBg(elem)
{
	if (elem.value == '') {
		elem.style.backgroundImage = 'none';
	}
}

function goFillFieldBg(elem, bg)
{
	if (elem.value == '') {
		elem.style.backgroundImage = 'url(' + bg + ')';
	}
}
	
function goPopup(where)
{
	window.open(where);
}

function goPopupMinimal(loc,w,h)
{
	var leftVal = (screen.width / 2) - (w/2);
	var topVal = (screen.height / 2) - (h/2);
	window.open (loc, 'new_window', 'left=' +leftVal+ ',top=' + topVal + ',toolbar=no,width=' + w + ',height=' + h + ',status=no,resizable=yes,menubar=no,location=no,scrollbars=yes');
}

function goPopupImage(loc)
{
	var w = 640;
	var h = 480;
	var leftVal = (screen.width / 2) - (w/2);
	var topVal = (screen.height / 2) - (h/2);
	window.open (loc, 'new_window', 'left=' +leftVal+ ',top=' + topVal + ',toolbar=no,width=' + w + ',height=' + h + ',status=no,resizable=yes,menubar=no,location=no,scrollbars=no');
}
