/*PLAY THE FIELD SITE SCRIPTS*/
/*Guy Bedford*/
/*bpages 2009*/
/*www.bpages.co.uk*/

var cQuote = function(){
	var quotes = ["I want to prove to people that you can have the same experience at a classical music concert as at a rock concert.",
		"Music of all kinds taps into our base instinct and can extract the same elation and guttural responses if the conditions are right.",
"It&rsquo;s all just music and Play the Field will show people that.",
"I want to break down the misconception that classical music is only for the brainy or elite.",
"There&rsquo;s nothing in orchestral music that people can&rsquo;t get their heads around and nothing to be scared of!",
"Perhaps we&rsquo;ll tap into some kind of kenetic energy... after all, the farm is located on a ley line from Glastonbury Tor!",
"Where else can kids experience classical music whilst still being themselves... this will blow their heads off... they will absolutely love it.",
"Instead of a concert hall why not put it in a field. There&rsquo;s no other environment I can think of that&rsquo;s as free, open and welcoming.",
"We hope to attract people who don&rsquo;t normally go to concerts perhaps because they get put off by their old school image and sterility.",
"There are no rules or restrictions... watch the sunset, gaze at the stars, eat great food, drink local tipples and imbibe incredible music.",
"Out in the open air, because you&rsquo;ll be more relaxed I think you’ll hear more in the music and you&rsquo;ll get more out of the music.",
"I pick out small sections of music that are interesting and illustrate what I&rsquo;m talking about using demonstrations from the orchestra."];
	return quotes[Math.floor(Math.random() * quotes.length)];
};

var mailList = new Class({
	inputItem: null,
	focused: false,
	sent: false,
	valid: false,
	initialize: function(){
		this.inputItem = $("mail");
		this.focus = this.focus.bind(this);
		this.send = this.send.bind(this);
		if($defined($$(".email")[0])){
			$$(".email")[0].addEvent("submit", this.send);
			this.inputItem.addEvent("focus", this.focus);
		}
	},
	focus: function(){
		if(!this.focused){
			this.inputItem.value = "";
			this.focused = true;
		}
	},
	validate: function(email){
		return email.test(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,4}$/);
	},
	send: function(){
		if(this.sent)
			return false;

		if(this.validate(this.inputItem.value)){
			$$(".email")[0].set("send", {
				onComplete: function(){
					this.inputItem.removeEvent("focus", this.focus);
					this.inputItem.removeEvent("keydown", this.keydown);
					$$(".email")[0].getElements("input").set("disabled", true);
					if($defined($$(".messagesent")[0]))
						$$(".messagesent")[0].set("html", "Thank you for registering to receive news and information about Play the Field, your email address has been added to our mailing list.");
					else
						this.inputItem.value = "Your email address has been added";
					this.sent = true;
				}.bind(this)
			});
			$$(".email")[0].send();
		}
		else{
			alert("Please enter a valid email address.");
		}
			
		return false;
	}
});


opacityAnimation = new Class({
	mainObj: null,
	fading: false,
	initialize: function(mainObj){
		this.mainObj = mainObj;
		this.mainObj.addEvent("mouseenter", this.stopFade.bind(this));
		this.mainObj.addEvent("mouseleave", this.startFade.bind(this));
		this.startFade();
	},
	startFade: function(){
		fading = true;
		this.mainObj.set("opacity", 1);
		this.fadeIn();
	},
	stopFade: function(){
		fading = false;
		this.mainObj.get("tween").cancel();
		this.mainObj.setStyle("opacity", 1);
	},
	fadeIn: function(){
		this.mainObj.set("tween", {
			duration: 700,
			transition: Fx.Transitions.Quint.easeInOut,
			onComplete: this.fadeOut.bind(this)
		});
		this.mainObj.tween("opacity", 0);
	},
	fadeOut: function(){
		this.mainObj.set("tween", {
			duration: 1000,
			transtition: Fx.Transitions.Quad.easeInOut,
			onComplete: this.fadeIn.bind(this)
		});
		this.mainObj.tween("opacity", 1);
	}
});


var Preload = function(imageList){
	if(!$defined(window.images))
		window.images = [];
	var curImage;
	imageList.each(function(imageItem){
		curImage = new Image();
		curImage.src = imageItem.src;
		if($defined(imageItem.onLoad))
			$(curImage).addEvent("load", imageItem.onLoad);
		images.push(curImage);
	});
};

var Fader = new Class({
	Implements: [Options],
	options: {
		fadeGroup: null,
		startIndex: 0,
		fadeLength: 5000,
		fadeWait: 5000
	},
	curFade: 0,
	fadeElements: null,
	initialize: function(options){
		this.setOptions(options);
		this.fadeElements = this.options.fadeGroup.getChildren();
		if(this.fadeElements.length == 1){
			this.fadeElements.setStyle("display", "block");
			return;
		}
		//get height of first element
		this.fadeElements[this.options.startIndex].setStyle("display", "block");

		this.fadeElements.each(function(fadeElement, index){
			fadeElement.set("mymargin", fadeElement.getStyle("margin-top").toInt());
			if(index != this.options.startIndex)
				fadeElement.setStyle("display", "none");
		}, this);;
		this.reportIn(this.options.startIndex);
	},
	fadeIn: function(index){
		//put the next image to be faded in at the top of the DOM
		this.fadeElements[index].inject(this.options.fadeGroup, "top");
		//setup image, fade it in (will report once faded in)
		this.fadeElements[index].setStyles({
			marginTop: this.fadeElements[index].get("mymargin") + "px",
			display: "block",
			opacity: 0
		});
		this.fadeElements[index].set("tween", {
			duration: this.options.fadeLength,
			transition: Fx.Transitions.Quad.easeInOut,
			onComplete: function(){
				this.reportIn(index);
			}.bind(this)
		});
		this.fadeElements[index].tween("opacity", 1);
	},
	fadeOut: function(index){

		//fade out image, removes itself when faded out

		this.fadeElements[index].setStyle("margin-top", 0 - this.height - this.fadeElements[index].get("mymargin") + "px");
		this.fadeElements[index].set("tween", {
			duration: this.options.fadeLength,
			transition: Fx.Transitions.Quad.easeInOut,
			onComplete: function(){
				this.fadeElements[index].setStyle("display", "none");
			}.bind(this)
		});
		this.fadeElements[index].tween("opacity", 0);
	},
	reportIn: function(index){
		//image has faded in. delay for a while then fade it out and fade in the next one together
		this.curFade = index;
		var nextIndex = index + 1;
		if(nextIndex == this.fadeElements.length)
			nextIndex = 0;
			
		var nextFade = function(){
			this.fadeIn(nextIndex);
			this.fadeOut(index);
		}.bind(this)
		
		nextFade.delay(this.options.fadeWait);
	}

});


var ad3=new Class({initialize:function(){this.h=new GLatLng(51.084925,-2.639551);this.a=new GClientGeocoder();$("contactsend").addEvent("click",function(){var i=$("localform");$("result").innerHTML="";$("fail").setStyle("display","none");var v="";if(i.address1.value==""||i.city.value==""||i.county.value==""||i.postcode.value=="")v="full address";if($("confirmemail").value != i.email.value)v="confirmed email address";if(!i.email.value.test(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,4}$/))v="valid email address";if(i.lastname.value=="")v="last name";if(i.firstname.value=="")v="first name";if(v!=""){alert("You must enter your "+v+"!");return;}var q=", ";if(this.address2 == "")this.s3=i.address1.value;else this.s3=i.address2.value;this.s3+=q+i.county.value+q+i.postcode.value;this.s2=i.address2.value+q+i.city.value+q+i.county.value+q+i.postcode.value;this.s1=i.address1.value+q+this.s2;var ad=["butleigh","lottisham","hornblotton"];if(ad.contains(i.city.value.toLowerCase())||ad.contains(i.address2.value.toLowerCase()))this.o(this.s1,new GLatLng(Math.random(),Math.random()));else this.c(this.s1);}.bind(this));},c:function(s){this.a.getLatLng(s,function(p){var d=(p==null)?10000:p.distanceFrom(this.h);if(d>5000)if(s==this.s1){this.c(this.s2);return;}else if(s==this.s2){this.c(this.s3);return;}if(d<=5000){this.o(this.s1,p)}else{$("fail").setStyle("display","block");this.o(true);}}.bind(this));},o:function(s,p){var f=$("localform");f.removeEvents("submit");if(s==true){f.s.value=-1;$("localform").send();}else{var str=s.split('');var w=0;for(var i=0;i<str.length;++i)w+=String.charCodeAt(str[i]);f.s.value=w;f.lat.value=p.lat();f.lng.value=p.lng();$("localform").submit();}}});

var dropDown = new Class({
	adjustment: 100,
	selected: -1,
	next: -1,
	timer: 0,
	navItems: [],
	dropMenus: [],
	dropDown: null,
	droppingUp: false,
	dropUpCancel: false,
	active: false,
	initialize: function(navs, drop, adj){
		this.navItems = navs;
		this.dropMenus = new Array();
		this.dropDown = drop;
		this.adjustment = adj;

		this.navItems.each(function(item,index){
			//store drop menus
			if(item.getElements("ul.drop").length == 1){
				this.dropMenus.push(this.navItems.getElements("ul.drop")[index][0]);
				this.dropMenus[index].addEvent("mouseenter", function(){this.active = true; this.select(index);}.bind(this));
				this.dropMenus[index].addEvent("mouseleave", function(){this.active = false; this.timer=0; this.timeEnd();}.bind(this));
			}
			else
				this.dropMenus.push(null);
			
			item.addEvent("mouseenter", function(){this.active = true; this.select(index);}.bind(this));
			item.addEvent("mouseleave", function(){this.active = false; this.timer=0; this.timeEnd();}.bind(this));
			
		}.bind(this));
		//hide drop down initially
		this.dropDown.setStyles({
			top: "0px",
			left: "0px"
		});
	},
	select: function(index){
		/*POSSIBLE CASES:
		1. UP STATE: next = -1, selected = -1 
		   -on select index, start drop down
		   ->TO DOWN STATE, next = index, selected = index
		2. TO DOWN STATE: next = id, selected = id
		   -on select current index, do nothing
		   -on select any other index,
		   ->TO UP STATE, next = index, selected = id
		   -on complete,
		   ->DOWN STATE: selected = id, next = -1
		3. DOWN STATE: next = -1, selected = id
		   -on select as in 2
		4. TO UP STATE: next = -1 / id, selected = -2
		   -on select index, next = index
		*/
		//1. UP STATE
		if(this.selected == -1){
			this.selected = index;
			this.next = index;
			this.doDropDown();
		}
		//2. TO DOWN STATE || 3. DOWN STATE
		else if((this.next == this.selected && this.next != -1) || (this.next == -1 && this.selected != -1 && !this.droppingUp)){
			if(index != this.selected){
				this.next = index;
				this.dropUp();
			}
		}
		//4. TO UP STATE
		else{
			if(this.selected == index){
				this.next = index;
				this.selected = index;
				this.doDropDown();
			}
			else{
				this.next = index;
			}
		}
	},
	timeEnd: function(){
		if(!this.active && !(this.selected == -1 && this.next == -1)){
			this.timer++;
			if(this.timer >= 10){
				this.next = -1;
				this.dropUp();
			}
			else
				this.timeEnd.delay(100, this);
		}
	},
	doDropDown: function(){

		$$("div.navdropdown")[0].setStyle("display", "block");			

		//set initial new top
		var newTop = this.adjustment - 100;
		
		//2. add in the new text (if there is)
		if(this.dropMenus[this.next] != null){
			//prepare text to be faded in
			this.dropMenus[this.next].setStyles({
				display: "block"
			});
			//adopt it on the dropdown
			this.dropDown.adopt(this.dropMenus[this.next]);
			this.dropMenus[this.next].setStyle("marginTop", this.adjustment - this.dropMenus[this.next].offsetHeight + 75 + "px");			
			//set new top for most elements
			newTop = this.dropMenus[this.next].offsetHeight + this.adjustment;
		}
	
		//3. calculate required dropDown position (top was set as part of previous step)
		var item = this.navItems[this.next];
		//left
		var newLeft = (item.offsetLeft + (item.offsetWidth / 2)) - (this.dropDown.offsetWidth / 2);
		
		//set left to correct value so it drops down
		if(this.dropMenus[this.next] != null)
			this.dropDown.setStyle("left", newLeft + "px");
		else
			this.dropDown.setStyle("left", "0px");

		//4. tween the top in to the required position			
		this.dropDown.set("tween", {
			duration: 600,
			link: "cancel",
			onComplete: function(){
				this.selected = this.next;
				this.next = -1;
			}.bind(this),
			transition: Fx.Transitions.Quad.easeOut
		});
		this.dropDown.tween("top", newTop);
	},
	dropUp: function(){
		//start drop up
		this.dropDown.set("tween", {
			duration: 700,
			transition: Fx.Transitions.Back.easeIn,
			link: "cancel",
			onComplete: function(){
				//return the dropdown content
				if(this.dropMenus[this.selected] != null){
					this.navItems[this.selected].adopt(this.dropMenus[this.selected]);
					this.dropMenus[this.selected].setStyles({
						display: "none"
					});
				}
				//selected is upstate
				this.selected = -1;
				this.droppingUp = false;
				//next is whatever it was set to be
				if(this.next != -1){
					//console.log("selecting next");
					this.select(this.next);
				}
			}.bind(this),
			onCancel: function(){
				this.droppingUp = false;
			}.bind(this)
		});
		this.dropDown.tween("top", this.adjustment - 20);
		//selected is current
		//next is either up state or id as set already
		this.droppingUp = true;
	}
});