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

	// close on signup box
	$(document.body).getElements('.signup .close')
	.addEvent('click', function(ev){
	
		if (ev) ev.stop();
		this.getParent('.signup').tween('opacity', 0);
		(function(){
			this.getParent('.signup').setStyle('display', 'none');
		}.delay(800, this));
	
	});
	
	// close on signup box
	$(document.body).getElements('.signupclick')
	.addEvent('click', function(ev){
	
		if (ev) ev.stop();
		$(document.body).getElement('.signup')
		.setStyles({
			'display': 'block',
			'opacity': 0
		})
		.tween('opacity', 1);
	
	});
		
	$(document.body).getElement('.signup .signup-send')
	.addEvent('click', function(a){
		
		if(a) a.stop();
			
		if ($('fld-calculation').get('value') != 10) {
		
			$('fld-calculation').addClass('error');
		
		} else {	
			
			new Request.JSON({
				url: 'news/',
				data: { 
					method: 'post',
					sendto: $(document.body).getElement('.signup input[name="sendto"]').get('value'),
					firstname: $(document.body).getElement('.signup input[name="firstname"]').get('value'),
					surname: $(document.body).getElement('.signup input[name="surname"]').get('value'),
					email: $(document.body).getElement('.signup input[name="email"]').get('value'),
					interested: $(document.body).getElement('.signup textarea[name="interested"]').get('value'),
					receivenews: ($(document.body).getElement('.signup input[name="receivenews"]') ? $(document.body).getElement('.signup input[name="receivenews"]').get('value') : 'no')
				 }
			})
			.addEvent('success', function(j){
					
				if (j){
						
					if (j.code == 1) {
					
						boxHeight = $(document.body).getElement('.signup form').getStyle('height');
						$(document.body).getElement('.signup fieldset').tween('opacity', [1,0]);
						
						(function(){
							$(document.body).getElement('.signup form').set('morph', {
								duration: 500,
								transition: 'circ:out'
							})
							.morph({
								'height': 180,
								'min-height': 0
							});
	
							$(document.body).getElement('.signup .box p').set('html', 'Thanks for signing up to our news and offers mailing! We promise to keep you up to date!');
	
						}.delay(500, this));
					
					} else {
	
						(function(){
						
							$(document.body).getElement('.signup fieldset')
							.appendText('Sorry, there has been a problem with your signup. Please try again.','top');
						
							$(document.body).getElement('.signup form').set('morph', {
								duration: 500,
								transition: 'circ:out'
							})
							.morph({
								'height': 675
							});
	
						}.delay(500, this));
					
					}		
					
				}
			})
			.send();
		} 

	});
	
	// run banner	
	if ($(document.body).getElement('ul.promotop li.selected')) {
		$(document.body).getElements('ul.promotop li').setStyle('opacity', 0)
		$(document.body).getElements('ul.promotop').getFirst('li').setStyle('opacity', 1);
		runPromo.periodical(4000);
	}
		
  	$(document.body).getElements('[placeholder]')
  	.each(function(frm){
		placeholder = new Form.Placeholder(frm);
  	});

});

var runPromo = function(p) {

	// get current one, fade it out and remove selected.
		
	// current element with selected class
	current = $(document.body).getElement('ul.promotop li.selected');
	total = $(document.body).getElements('ul.promotop li').length;
	count = current.getAllPrevious('li').length + 1;
			
	// remove selected class on current li
	current
	.set('morph', {duration: 1000, transition: 'quart:in:out'})
	.morph({'opacity': 0})
	.removeClass('selected');

	// check to see if we're at the end
	// so we can loop round to start
	if (count >= total){
		count = 0;
	}
	
	// add selected to next image li
	$(document.body)
	.getElements('ul.promotop li')[count]
	.set('morph', {duration: 1000, transition: 'quart:in:out'})
	.morph({'opacity': 1})
	.addClass('selected');

}

