/*

	woodstoc.js

	Contains main js for site.

	Created on 23/06/2010 by Andi Topping

	Changes:
		->
	

*/

window.addEvent('domready', function(){

	$('experts').setStyles({
		'opacity':0,
		'display': 'block'
	});

	$('ml').setStyles({
		'opacity':0,
		'display': 'block'
	});

	$('experts-bg').setStyles({
		'opacity':0,
		'display': 'block'
	});

	// sort out main nav animation
	// create the cookie if it doesn't exist
	// or read it if it does
	
	if (!Cookie.read('midnav')){
		Cookie.write('midnav', 0, {duration: 90});
	}
	
	if(Cookie.read('midnav') == 0) {
	
		$$('ul.midnav li')
		.setStyle('margin-top','38px')
		.each(function(el, i) {
			(function(){
				if (el.hasClass('selected')) { var liMargin = '3px'; } else { var liMargin = '0px'; }
				el.set('morph',{ duration: 1000, transition: 'bounce:out' });
				el.morph({
					'margin-top': liMargin
				});
			}.delay(100 * i));
		});	
				
		//$$('ul.links').setStyle('height',ulHeight+'px');
		Cookie.write('midnav', 1, {duration: 90});
		
	}
	
	// find out which footer li is the highest and then apply it to all of them

	var ulHeight = 0;
	
	$$('ul.links > li')
	.each(function(el){
		if (el.getSize().y > ulHeight) {
			ulHeight = el.getSize().y;
		}
		el.setStyle('height',ulHeight+'px');
	});
	
	
	// TURN OFF THE ANNOYING AUDIO!
	
	// create the cookie if it doesn't exist
	// or read it if it does
	if (!Cookie.read('audio')){
		Cookie.write('audio', 1, {duration: 90});
		$$('div.track').set('html','<embed src="http://www.woodstoc.com/birds.mp3" hidden="true" autostart="true" loop="true" volume="7" quality="low">');
		$$('div.audio img').set({
					'src':'images/audio-on.png',
					'alt':'Turn Audio Off'
				})
				.addClass('on');
	}
	
	// determine now what we do on domready
	// if cookie = 1 then we play the audio
	// otherwise we don't do anything
	if(Cookie.read('audio') == 1) {
		$$('div.track').set('html','<embed src="http://www.woodstoc.com/birds.mp3" hidden="true" autostart="true" loop="true" volume="7" quality="low">');
		$$('div.audio img').set({
					'src':'images/audio-on.png',
					'alt':'Turn Audio Off'
				})
				.addClass('on');
	}
	else {
		$$('div.audio img').set({
					'src':'images/audio-off.png',
					'alt':'Turn Audio On'
				})
				.addClass('off');
	}
	
	if($$('div.audio')) {
		$$('div.audio img')
		.addEvent('click', function(ev){
			if (this.hasClass('on')) {
				this.set({
					'src':'images/audio-off.png',
					'alt':'Turn Audio On'
				});
				this.removeClass('on').addClass('off');
				$$('div.track').set('html','');
				
				// update the cookie so that the audio doesn't happen on every page load
				Cookie.write('audio',0, {duration: 90});
		
			}
			else {
				this.set({
					'src':'images/audio-on.png',
					'alt':'Turn Audio Off'
				});
				this.removeClass('off').addClass('on');
				$$('div.track').set('html','<embed src="http://www.woodstoc.com/birds.mp3" hidden="true" autostart="true" loop="true" volume="7" quality="low">');
				
				// update the cookie so that the audio doesn't happen on every page load
				Cookie.write('audio',1, {duration: 90});
			}
		});
	}

	// metal boxes
	// display a box based on what metal box is clicked.
	
	if ($('box1') && $('box2')) {
		
		// add hover and click to box 1
		$('box1')
		.addEvent('mousedown', function(ev){
			if (!this.hasClass('selected')) {
				this.set('tween',{ duration: 100 }).tween('bottom', -2);
			}
		})
		.addEvent('click', function(ev){
			
			if ($('experts').getStyle('opacity') == 1) {
				$('experts')
				.tween('opacity',0);
				$('experts-bg')
				.tween('opacity',0);
			}
			else {
				$('experts')
				.tween('opacity',1);
				$('experts-bg')
				.tween('opacity',0.3);
			}
			
		});	
		
		$$('div.btn-close')
		.addEvent('click', function(a){
				$('experts')
				.tween('opacity',0);
				$('experts-bg')
				.tween('opacity',0);
				$('box1').set('tween',{ duration: 100 }).tween('bottom', 0);
		});
				
		// load form validation
		validateForm = new FormCheck("frm-ask-the-experts");


		$('experts').getElement('div.btn-submit')
		.addEvent('click', function(a){
				
			validateForm.onSubmit.bind(validateForm)();
		
		});
		
		 $('frm-ask-the-experts').addEvent('valid', function(){

		    $('frm-ask-the-experts').set('send', {
		    	
		    	onRequest: function(response) { 
			  	  $$('div.before').setStyle('display','none');
			  	  $$('div.after').setStyle('display','block');
				  $$('div.after').set('html','<h1>Nearly There</h1><h2>Please bare with us as we submit your details</h2>');
			    },
			  	onSuccess: function(success) { 
				  $$('div.after').set('html','<h1>Thank You</h1><p>Thanks for getting in touch! We\'ll be in contact with you as soon as we can!</p>');
			    },
		    	onFailure: function(fail) { 
				  $$('div.after').set('html','<h1>Sorry!</h1><p>We seem to have had a problem submitting your details! Please try again!</p>');
			    }
			    
			});
			$('frm-ask-the-experts').send();
		});

		// add hover and click to box 2
		$('box2')
		.addEvent('mousedown', function(ev){
			if (!this.hasClass('selected')) {
				this.set('tween',{ duration: 100 }).tween('bottom', -2);
			}
		})
		.addEvent('mouseleave', function(ev){
/*
			if (!this.hasClass('selected')) {
				this.set('tween',{ duration: 100 }).tween('bottom', 0);
			}
*/
		})
		.addEvent('click', function(ev){
			if ($('ml').getStyle('opacity') == 1) {
				$('ml')
				.tween('opacity',0);
				$('experts-bg')
				.tween('opacity',0);
			}
			else {
				$('ml')
				.tween('opacity',1);
				$('experts-bg')
				.tween('opacity',0.3);
			}
			
		});	
		
		$$('div.btn-close')
		.addEvent('click', function(a){
				$('ml')
				.tween('opacity',0);
				$('experts-bg')
				.tween('opacity',0);
				$('box2').set('tween',{ duration: 100 }).tween('bottom', 0);
		});
				
		
	}
	
	// do on mouse down instead
	$$('ul.midnav li')
	.addEvent('mousedown', function(ev){
		if (!this.hasClass('selected')) {
			$$('ul.midnav li.selected')
			.set('morph',{ duration: 100 }).morph({
				'background-image': 'url(images/links/bg-off.png)',
				'height':'31px',
				'margin-top': '0px',
				'width': '114px'
			}); 
			$$('ul.midnav li.selected a')
			.set('morph',{ duration: 100 }).morph({
				'color': 'rgb(75,75,75)'
			}); 
			this.set('morph',{ duration: 100 }).morph({
				'height': '28px',
				'margin-top': '3px'/*,
				'opacity': .5*/
			});    			
		}
	})
	.addEvent('mouseleave', function(ev){
		if (!this.hasClass('selected')) {
			$$('ul.midnav li.selected')
			.set('morph',{ duration: 100 }).morph({
				//'background-image': 'url(images/links/bg-on.png)',
				'background-image': 'url(images/links/bg-off.png)',
				'height':'28px',
				'margin-top': '3px',
				'width': '114px'
			}); 
			$$('ul.midnav li.selected a')
			.set('morph',{ duration: 100 }).morph({
				//'color': 'rgb(136,140,58)'
				'color': 'rgb(75,75,75)'
			}); 
			this.set('morph',{ duration: 100 }).morph({
				'height': '31px',
				'margin-top': '0px'/*,
				'opacity': 1*/
			});    			
		}
	})
	.addEvent('click', function(ev){
		location.href = this.getElement('a').getProperty('href');
	});
	
/*
	// run the rotator at the top of the page
	if($('promo')) {
		
		$$('div.promos').setStyle('opacity',0);
		$('p1')
		.setStyle('opacity',1)
		.addClass('current');

		$$('div.promos')
		.addEvent('click',function(a){
	
			// go to passed link
			if (a){
				$clear(runPromo);
			}
			else {
		
				$$('div.promos')
				.removeClass('current')
				.each(function(el){
					if (el.getStyle('opacity') == 1){
						el.tween('opacity', 0);
					}
				});
				
				$(this.get('id'))
				.setStyles({
					'display':'block',
					'opacity': 0
				}).addClass('current')
				.tween('opacity', 1);
			}	
		});
				
	    // run promo bar rotator
		if ($$('div.promos')) var runPromo = promo.periodical(5000);

	}
*/
	
	// choose image
	if ($$('table.page','table.gallery')) {
	
		$$('div.bigpics img').setStyle('opacity',0);
		if($$('div.description')) { $$('div.description').setStyle('opacity',0); }
		if($('b1')) { $('b1').setStyle('opacity',1); }
		if($('d1')) { $('d1').setStyle('opacity',1); }
		$$('td.smallpictures img')
		.addEvent('click', function(a) {
			$$('div.bigpics img').tween('opacity',0);
			$$('div.description').tween('opacity',0);
			$('b'+this.get('id')).tween('opacity',1);
			if($('d'+this.get('id'))) {
				$('d'+this.get('id')).tween('opacity',1);
			}
		});
		
	}
	
	window.fireEvent('resize');
	
});

window.addEvent('resize', function(){

	$('experts').setStyles({
		'top': (window.getSize().y / 2) - ($('experts').getSize().y / 2),
		'left': (window.getSize().x / 2) - ($('experts').getSize().x / 2)
	})

	$('ml').setStyles({
		'top': (window.getSize().y / 2) - ($('experts').getSize().y / 2),
		'left': (window.getSize().x / 2) - ($('experts').getSize().x / 2)
	})

});

// function to run promo bar
var promo = function(){

	var el = $('promo').getElement('div.promos.current');
	var count = el.getAllPrevious('div').length + 1;
		
	if (count > $$('div.promos').length){
		count = 1;
	}
	
/* 	console.log(count); */
		
	$$('div#p'+count).fireEvent('click');
	
}
