window.addEvent('domready', function() {
	/**
	 * the left column accordion
	 */
	var triggers = $$('#left-column > ul > li > a');
	var data = $$('#left-column > ul > li > ul');
	var itemToOpen = 0;
	for (var i = 0; i < triggers.length; i++) {
		if (triggers[i].hasClass('open')) {
			itemToOpen = i;
			break;
		}
	}
	if (triggers.length && data.length) {
		new Accordion(triggers, data, {
			show: itemToOpen,
			duration: 150,
			alwaysHide: false,  // allow all sections to be hidden at once
			onActive: function(h) {
				h.addClass('open');
			},
			onBackground: function(h) {
				h.removeClass('open');
			}
		});
	}

	/**
	 * handle top tab box clicks
	 */
	$$('.top-tab-box > ul > li > a').addEvent('click', function(evt) {
		evt.stop();
		// highlight the selected tab
		$$('.top-tab-box > ul > li > a').removeClass('on');
		this.addClass('on');
		// display the selected block
		$$('.top-tab-box .pic-story').setStyle('display', 'none');
		var target = this.get('href').replace('#', '');
		$(target).setStyle('display', 'block');
	});

	/**
	 * rotate the event images
	 */
	new Jp_Showcase('events-show-case', {
		delay: 6000,
		duration: 500
	});

	/**
	 * handle site map toggling
	 */
	var siteMap = $$('#site-map').setStyle('display', 'none').set('load', { 'onComplete': function() {
		var originalHeight = siteMap.getStyle('height');

		siteMap.setStyles({ 'display': 'block', 'height': 0}).set('tween', {duration: 150});

		$$('a[rel=site-map]').set('href', 'javascript://').addEvent('click', function() {
			siteMap.tween('height', siteMap.getStyle('height') == '0px' ? originalHeight : 0);
		});
	}}).load('site_map.html');

	/**
	 * Handle galleries
	 */
	var largeImage = $$('#photo img');
	if (largeImage.length > 0) {
		new jp_ImageSwapper(largeImage[0], $$('#gallery a'), {previousLink: $$('.prev a')[0], nextLink: $$('.next a')[0], autoScrollToImage: true});
	}

    if (typeof DD_belatedPNG != 'undefined') {
    	//PNG fix
    	DD_belatedPNG.fix('.png_bg');
	}

	new ClearField('fldEntryYearGroup', ['(eg. Year 7)']);
	new ClearField('fldEntryYear', ['(eg. 2010)']);
	new ClearField('fldEntryTerm', ['(eg. Autumn)']);

	$$('form').each(function(e) {
		new jpForm(e, {
			inlineErrorMsg: false,
			valuesToConsiderNull: [
				'(eg. Year 7)',
				'(eg. 2010)',
				'(eg. Autumn)'
			]
		});
	});

	/*
	new jpForm('enquiry-form', {
		inlineErrorMsg: false,
		valuesToConsiderNull: [
			'(eg. Year 7)',
			'(eg. 2010)',
			'(eg. Autumn)'
		]
	});
	*/
});


var ClearField = new Class({
	element: null,
	nullvalues: [],
	cleared: false,
	initialValue : '',
	initialize: function(element, nullvalues){
		this.element = $(element);
		this.nullvalues = nullvalues || [];
		if (!this.element) {
			return;
		}

		this.initialValue = this.element.value;
		this.element.addEvent('focus', function() {
			this.clearDefault();
		}.bind(this));
		this.element.addEvent('blur', function() {
			this.setToDefault();
		}.bind(this));
	},
	getValue: function(actual) {
		var value = this.element.value;
		if (!actual) {
			var i = 0;
			while (i < this.nullvalues.length) {
				if (value == this.nullvalues[i]) {
					value = '';
					break;
				}
				i++;
			}
		}
		return value;
	},
	clearDefault: function() {
		if (this.element && !this.cleared) {
			this.element.value = '';
		}
		this.cleared = true;
	},
	setToDefault: function () {
		if (this.getValue(true) == '') {
			this.element.value = this.initialValue;
			this.cleared = false;
		}
	}
});

var jp_ImageSwapper = new Class({
	mainImage: null,
	thumbs: [],
	current: null,
	navigation: false,
	options: {
		previousLink: null,
		nextLink: null,
		autoScrollToImage: false
	},
	initialize: function(mainImage, thumbs, options) {
		this.setOptions(options);
		this.mainImage = $(mainImage);
		if (typeof thumbs.length != 'undefined') {
			thumbs.each(function(e) {
				e = $(e);
				if (e) {
					this.thumbs.push(e);
					new Image().src = e.get('href'); //preload in cache
				}
			}, this);
		}
		if (!this.mainImage || this.mainImage.tagName.toLowerCase() != 'img') {
			return;
		}
		this.setNavigation();
		this.setEvents();
	},
	setNavigation: function() {
		this.swap(this.mainImage.get('src'));
		this.options.previousLink = $(this.options.previousLink);
		this.options.nextLink = $(this.options.nextLink);
		if (this.options.previousLink && this.options.nextLink) {
			this.options.previousLink.addEvent('click', function(evt) {
				evt.stop();
				this.previous();
			}.bindWithEvent(this));
			this.options.nextLink.addEvent('click', function(evt) {
				evt.stop();
				this.next();
			}.bindWithEvent(this));
			this.navigation = true;
		}
	},
	setEvents: function() {
		this.thumbs.each(function(e) {
			e.addEvent('click', function(evt) {
				evt.stop();
				this.swap(e.get('href'));
			}.bindWithEvent(this));
		}.bind(this));
	},
	swap: function(src, action) {
		if (this.navigation) {
			for (var i = 0; i < this.thumbs.length; i++) {
				if (this.thumbs[i].get('href') == src) {
					break;
				}
			}

			var j = action ? i + action : i;
			if (action && j >= 0 && j < this.thumbs.length) {
				src = this.thumbs[j].get('href');
			}

			if (j <= 0) {
				this.options.previousLink.addClass('disabled');
			}
			else {
				this.options.previousLink.removeClass('disabled');
			}
			if (j + 1 >= this.thumbs.length) {
				this.options.nextLink.addClass('disabled');
			}
			else {
				this.options.nextLink.removeClass('disabled');
			}
		}

		this.mainImage.set('src', src);
		this.current = src;

		new Fx.Scroll(window, {duration: 100}).toElement(this.mainImage);
	},
	next: function() {
		this.swap(this.current, 1);
	},
	previous: function() {
		this.swap(this.current, -1);
	}
});
jp_ImageSwapper.implement(new Options);

