window.addEvent('domready', function () {
	if ($('carousel')) new Carousel($$('#carousel li'), {controls:false});
	if ($('contact-form')) {
		new Element('input', {type:'hidden', name:'antispam', value:'antispam'}).injectInside('contact-form');
		$('message').set('value', '').addEvent('click', function () {
			this.getPrevious().fade('hide');
		});
		$('contact-form').addEvent('submit', function (e) {
			var event = new Event(e);
			$$('#contact-form input').each(function (field) {
				if (field.get('value') == '' && field.id != 'phone') {
					field.setStyle('backgroundColor', '#f99');
					event.stop();
				} else {
					field.setStyle('backgroundColor', '#fff');
				}
			});
			
			var email = $('email').get('value');
			var regEx = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
			if (!regEx.test(email)) {
				$('email').setStyle('backgroundColor', '#f99');
				event.stop();
			}
		});
	}
	if ($('refer-a-friend'))	new Element('input', {type:'hidden', name:'antispam', value:'antispam'}).injectInside('refer-a-friend');
	if ($('thanks')) {
		var fade = new Fx.Tween('thanks', {duration:10000, transition: 'pow:in'});
			fade.start('opacity', 0);
	}
	
	if (Cookie.read('visited') == null)	new alert();
	
});

var Carousel = new Class({
	Implements: Options,
	options:{
		controls:true,
		controlsParent: $empty
	},
	
	initialize: function (screens, options) {
		this.setOptions(options);
		this.screens = screens.fade('hide');
		this.activeScreen = screens[0].fade('show');
		if (this.options.controls)	this.addControls();
		this.screens.getParent().addEvents({
			mouseover: function () {this.stop();}.bind(this),
			mouseout: function () {this.start();}.bind(this)
		});
		this.start();
	},
	start: function () {
		this.timer = (function () {
			this.activeScreen.fade('out');
			var pos = this.screens.indexOf(this.activeScreen);
				pos = (pos == this.screens.length-1) ? 0 : pos + 1;
			this.activeScreen = this.screens[pos].fade('in');
			if (this.options.controls) {
				this.controls.removeClass('on');
				this.controls[pos].addClass('on');
			}
		}.bind(this)).periodical(7000);
	},
	stop: function () {
		$clear(this.timer);
	},
	addControls: function (screens) {
		var controls = new Element('ul', {id:'carouselControls'});
		this.screens.each(function (screen) {
			controls.adopt(new Element('li').addEvent('click', function (e) {
				var control = new Event(e).stop().target;
				this.setScreen(control.getAllPrevious().length);
			}.bind(this)));
		}.bind(this));
		controls.getFirst().addClass('on');
		this.controls = controls.getChildren();
		$(this.options.controlsParent).adopt(controls);
	},
	setScreen: function (index) {
		this.stop();
		if (this.controls) {
			this.controls.removeClass('on');
			this.controls[index].addClass('on');
		}
		this.activeScreen.fade('hide');
		this.activeScreen = this.screens[index];
		this.activeScreen.fade('in');
		this.start();
	}
});

var alert = new Class({
	Implements:Options,
	options:{
		text:'Welcome'
	},
	initialize: function (options) {
		var myCookie = Cookie.write('visited', 'true');
		this.overlay = new Element('div').setStyles({
			width:'100%', height:'100%', position:'absolute', top:0, left:0, zIndex:1, opacity:0.8, background:'#000'
		}).injectInside(document.body);
		var html = '<img src="/wp-content/themes/wendy/images/wendy.jpg" alt="Wendy Alexander" width="202px" height="270px" style="float:left; padding:0px 20px 20px 0px;" /><h1>Wendy Alexander</h1><p>As of 23rd March 2011, the 3rd session of the Scottish Parliament dissolved due to the May 2011 election. Wendy Alexander ceased to be an MSP as of this date - accordingly her Parliamentary website ceased on this date.</p><p>Wendy Alexander served as a Member of the Scottish Parliament from Paisley North for 12 years, from the Parliament\'s inception in 1999 to March 2011.  She held a variety of Ministerial positions in the Scottish Cabinet including as Communities Minister and Enterprise, Lifelong Learning, Tourism and Transport Minister.</p><p>She chaired the Parliament\'s Finance Committee, served on the Economy Committee, and on the Economy, Energy and Tourism Committee. Finally from late 2010 until March 2011 she chaired the Holyrood committee of the Scotland Bill - considering the most far reaching constitutional changes since devolution.</p><p><a href="">Click here</a> if you wish to see some of her work during that time.</p>';
		this.window = new Element('div').setStyles({
			width:'60%', height:'50%', position:'absolute', top:'25%', left:'20%', zIndex:2, background:'#fff', overflowY:'auto', padding:20, textAlign:'left'
		}).set('html', html).injectInside(document.body);
		var self = this;
		this.window.getElements('a').addEvent('click', function (e) {new Event(e).stop(); self.window.dispose(); self.overlay.dispose();});
//		this.overlay.addEvent('click', function (e) {new Event(e).stop(); self.window.dispose(); self.overlay.dispose();});
	}
});
