var ECVYSClass = new Class({
					
	initialize: function(){
		window.addEvent('domready', function(){
			this.ContentSlider();
			this.Search();
			this.content_fadein();
			this.GoogleMapLink();
			this.GoogleMapCloseLink();
			this.ApplicationForm();
		}.bind(this));
	},
		
	ContentSlider: function(){
		
		var ContentMiddle = $('content_middle');
		var ContentMiddleWithSub = $('content_middle_withsub');
		var CorrectContentMiddle;
		
		if (ContentMiddle != null) {
			CorrectContentMiddle = ContentMiddle;
		} else if (ContentMiddleWithSub != null) {
			CorrectContentMiddle = ContentMiddleWithSub;
		}
		
		if (CorrectContentMiddle != null) {
			var ContentArea = $('content_area');
			var WideContentArea = $('widecontent_area');
			var ContentAreaWithSub = $('content_area_withsub');
			var NewsletterList = $('newsletter_list');
			var CorrectContentArea;
			var SliderContainerHeight;
			var SliderFadeIn;
			
			if (ContentArea != null) {
				CorrectContentArea = ContentArea;
				SliderContainerHeight = 335;
				SliderFadeIn = false;
			} else if (WideContentArea != null) {
				CorrectContentArea = WideContentArea;
				SliderContainerHeight = 335;
				SliderFadeIn = true;
			} else if (ContentAreaWithSub != null) {
				CorrectContentArea = ContentAreaWithSub;
				SliderContainerHeight = 305;
				SliderFadeIn = true;
			} else if (NewsletterList != null) {
				CorrectContentArea = NewsletterList;
				SliderContainerHeight = 140;
				SliderFadeIn = true;
			}
			
			if (CorrectContentArea != null) {
				var ContentHeight = CorrectContentArea.getScrollSize().y;
				
				var SliderHeight = Math.round((SliderContainerHeight/100) * ((100/ContentHeight) * SliderContainerHeight));
				
				var ContentScrollMultiplier = Math.round((ContentHeight/SliderContainerHeight) + 0.5);
				
				if (SliderHeight < SliderContainerHeight) {
					var CreateSlideContainer = new Element('div', {'id' : 'contentsliderarea'});
					
					if (SliderFadeIn == true) {
						CreateSlideContainer.set('class', 'hide fadein')
					}
					
					CreateSlideContainer.injectTop(CorrectContentMiddle);
					
					var CreateSlider = new Element('div', {'id' : 'contentslider'});
					CreateSlider.injectTop($('contentsliderarea'));
					
					CreateSlider.setStyle('height', SliderHeight);
					CorrectContentArea.setStyle('height', SliderContainerHeight);
					
					var ScrollBarInitTop = CreateSlider.getPosition().y;
					
					//alert(ScrollBarInitTop)
					
					var ScrollDragInstance = new Drag.Move($('contentslider'), {
						container : $('contentsliderarea'),
						onDrag: function(el) {
							
							//alert(((CreateSlider.getPosition().y-ScrollBarInitTop) * ContentScrollMultiplier));
							
							CorrectContentArea.scrollTop = ((CreateSlider.getPosition().y-ScrollBarInitTop) * ContentScrollMultiplier);
						
						}.bind(this)
					});	
				}
			}
		}
	},
	
	Search: function(){
		
		var SearchBox = $('search_box');
		var SearchBoxValue;
				
		SearchBox.addEvents({
			'focus': function(){
				SearchBoxValue = SearchBox.get('value');
				if (SearchBoxValue == 'Search this site'){
					SearchBox.set('value', '');
				}
			},
			'blur': function(){
				SearchBoxValue = SearchBox.get('value');
				if (SearchBoxValue == '') {
					SearchBox.set('value', 'Search this site');
				};
			}
		});
		
	},
	
	content_fadein: function(){
		var FadeIn = $$('.fadein');
		
		if (FadeIn != null) {
			FadeIn.each(function(el){
				el.set('tween', {duration: 1000, transition: 'back:in'}).tween('opacity', 0, 1);
			})
		}
	},
	
	//ApplicationForm: function(){
	//	new FormCheck('application_form');
	//},
	
	ApplicationForm: function(){
		var AppForm = $('application_form');
		
		if (AppForm != null){
			var AppSubmitButton = $('submit');
			var OrganisationNameValid = false;
			var ContactNameValid = false;
			var AddressValid = false;
			var TelephoneValid = false;
			var EmailValid = false;
			var MemberSubscriptionsValid = false;
			var AimOfOrgValid = false;
			
			if (AppSubmitButton != null){
				
				AppForm.addEvent('submit', function(e){
					
					if ($('membership_organisation_name').get('value') != ''){
						$('label_organisation_name').setStyles({color:'#000000'});
						OrganisationNameValid = true;
					} else {
						$('label_organisation_name').setStyles({color:'#FF0000'});
						OrganisationNameValid = false;
					}
					
					if ($('membership_contact_name').get('value') != ''){
						$('label_contact_name').setStyles({color:'#000000'});
						ContactNameValid = true;
					} else {
						$('label_contact_name').setStyles({color:'#FF0000'});
						ContactNameValid = false;
					}
					
					if ($('membership_address').get('value') != ''){
						$('label_address').setStyles({color:'#000000'});
						AddressValid = true;
					} else {
						$('label_address').setStyles({color:'#FF0000'});
						AddressValid = false;
					}
					
					if ($('membership_telephone').get('value') != ''){
						$('label_telephone').setStyles({color:'#000000'});
						TelephoneValid = true;
					} else {
						$('label_telephone').setStyles({color:'#FF0000'});
						TelephoneValid = false;
					}
					
					if ($('membership_email').get('value') != '') {
						//var regex = "^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$";
						//var regexp = new RegExp(regex, 'i');
						//var text2 = $('membership_email').get('value');
						//text2 = text2.toLowerCase();
						//var x = text2.test(regexp);
						
						//if (x == true){
						//	$('label_email').setStyles({color:'#000000'});
						//	
						//	EmailValid = true;
						//} else {
						//	$('label_email').setStyles({color:'#FF0000'});
						//	
						//	EmailValid = false;
						//}
						$('label_email').setStyles({color:'#000000'});
						EmailValid = true;
					} else {
						$('label_email').setStyles({color:'#FF0000'});
						
						EmailValid = false;
					}
					
					if ($('membership_membersubscriptions').checked || $('membership_membersubscriptions2').checked) {
						$('p_youngpeople_membership').setStyles({color:'#000000'});
						
						MemberSubscriptionsValid = true;
					} else {
						$('p_youngpeople_membership').setStyles({color:'#FF0000'});
						
						MemberSubscriptionsValid = false;
					}
					
					if ($('membership_aoo').get('value') != ''){
						$('label_aoo').setStyles({color:'#000000'});
						AimOfOrgValid = true;
					} else {
						$('label_aoo').setStyles({color:'#FF0000'});
						AimOfOrgValid = false;
					}
					
					if (OrganisationNameValid == true && ContactNameValid == true && AddressValid == true && TelephoneValid == true && EmailValid == true && MemberSubscriptionsValid == true && AimOfOrgValid == true){
						
					} else {
						e.stop();	
					}
					
				})
				
			}
		}
	},
	
	GoogleMapLink: function(){
		if ($('google_map_link') != null){
			$('google_map_link').addEvent('click', function(e){
				e.stop();
				if ($('google_map_container').getStyle('visibility') == 'hidden'){
					$('google_map_container').set('tween', {duration: 1000, transition: 'back:in'}).tween('opacity', 0, 1);
				} else {
					$('google_map_container').set('tween', {duration: 1000, transition: 'back:in'}).tween('opacity', 1, 0);
				}
			})
		}
	},
	
	GoogleMapCloseLink: function(){
		if ($('google_close_map') != null){
			$('google_close_map').addEvent('click', function(e){
				e.stop();
				$('google_map_container').set('tween', {duration: 1000, transition: 'back:in'}).tween('opacity', 1, 0);
				$('google_map').set('tween', {duration: 1000, transition: 'back:in'}).tween('opacity', 1, 0);
			})
		}
	}
		
});

var FaderClass = new Class({

	initialize: function(options){
	
		// Get options from the passed options object
		this.Container = $(options.container);
		this.Rotate = options.rotate;
		this.elements = options.elements;
		this.Interval = options.interval;
		this.RotateLocation = 0;
		this.fadeLength = options.fadeLength;
		
		window.addEvents({
			"domready" : function(){
			
				// Get the container location, i.e. x, y and also include scroll height
				this.getContainerLocation();
				if(this.Rotate) {
					this.startRotation();		
				}
			
			}.bind(this),
			"resize" : function(){
		
				this.getContainerLocation();
		
			}.bind(this),
			"scroll" : function(){
		
				this.getContainerLocation();
		
			}.bind(this)
		});			
		
	},
	
	getContainerLocation : function() {
		this.ContainerLocation = this.Container.getCoordinates();
		this.ContaienrScroll = window.getScroll();
	},
	
	startRotation : function() {
		
		(function() {
		
			// Increment location
			if((this.RotateLocation+1)==this.elements.length){
				this.RotateLocation=0;
			} else {
				this.RotateLocation++;
			}
			
			// Make temp element and insert
			var NewLI = this.elements[this.RotateLocation].object;
			NewLI.setStyle('opacity', 0);
			NewLI.injectInside(this.Container);
			
			var OldLI = this.Container.getFirst();
			
			var myFadeFx = new Fx.Tween(OldLI, {duration: this.fadeLength, onComplete : function() {
				// Delete old div's
				this.DeleteOld();			
			}.bind(this)});
			myFadeFx.start('opacity', 1, 0);
			
			// Fade in and then get rid of overlay temp element
			var myFadeFx = new Fx.Tween(NewLI, {duration: this.fadeLength, onComplete : function() {
				// Delete old div's
				this.DeleteOld();			
			}.bind(this)});
			myFadeFx.start('opacity', 0, 1);
			
			
		}).periodical(this.Interval, this);
	
	},
	
	DeleteOld : function(iImage) {
		if(this.Container.getElements('li').length > 1) {
			this.Container.getFirst().dispose();
		}
	}
	
});


var ECVYS = new ECVYSClass();