// JavaScript Document
/*
Title:     Ark Church Volunteer Application general Javascript functions
Author:    Matt McCloskey, Kemso Design
Updated:   Jan 31, 2008
*/

var script_root = '/iserve/js/';
var site_root = '/iserve/';
var site_url = 'http://www.arkchurch.org/';

/* Optional Libraries
-----------------------------------------------------------------------------------------------------*/

// Flash Replacement
// (make sure to set h2 { display:block; } if you don't use this
/* document.write('<script type="text/javascript" src="'+site_root+'js/lib/flash_replacement.js"></script>'); */


/* Required Libraries
-----------------------------------------------------------------------------------------------------*/
// moo tools
document.write('<script type="text/javascript" src="'+script_root+'lib/mootools.js"></script>');

// window.domready
document.write('<script type="text/javascript" src="'+script_root+'lib/domready.js"></script>');

// round corners
/* document.write('<script type="text/javascript" src="'+script_root+'lib/roundcorners.js"></script>'); */

/* On Load Replacements
-----------------------------------------------------------------------------------------------------*/

ok_to_round = false;

function init(){
	
	new SmoothScroll();

	accordion = new Accordion('li h1', 'li .description', {
		opacity: true,
		duration: 400,
		transition: Fx.Transitions.Sine.easeOut,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#FEEF6D');
			toggler.getParent().setStyle('background-color', '#333');
			//updateRoundBackground(toggler.getParent());
		},
		onBackground: function(toggler, element){
			if(toggler){
				toggler.setStyle('color', '#fff');
				toggler.getParent().setStyle('background-color', '#272525');
				//updateRoundBackground(toggler.getParent());
				// remove form and notice
				var form = $E('form', toggler.getParent());
				if(form){
					new Fx.Style(form, 'opacity', {duration:400, onComplete:function(){
						form.remove();
					}}).start(1, 0);
				}
				var notice = $E('.notice', toggler.getParent());
				if(notice){
					new Fx.Style(notice, 'opacity', {duration:400, onComplete:function(){
						notice.remove();
					}}).start(1, 0);
				}
			}
		}
	}, $('opportunities'));
	
	//roundCorners($ES('li, a.signup', 'opportunities'));
	//roundCorners($ES('li', 'navigation'));
	
	
	//alternate($ES('li', 'opportunities'));
}


function signup(link, email){

	link = $(link);
	var fields = new Array("name", "phone", "email");
	var p = link.getParent();

	var form = $E('form', p);
	if(form){
		new Fx.Style(form, 'opacity', {duration:400, onComplete:function(){
			form.remove();
			accordion.display();
			accordion.display(p);
		}}).start(1, 0);
		
		var notice = $E('.notice', p);
		if(notice){
			new Fx.Style(notice, 'opacity', {duration:200, onComplete:function(){
				notice.remove();
			}}).start(1, 0);
		}
	}else{
	
		accordion.display();
	
		var form = new Element('form', {
			'action': 'formscripts/signup.php', 
			'method': 'post',
			'class': 'signupform'
		});
		// inputs
		for(var i = 1; i<=fields.length; i++){
			var fieldname = fields[i-1];
			new Element('label', {
				'for': fieldname
			}).setHTML(fieldname).injectInside(form);
			new Element('input', {
				'type': 'text',
				'name': fieldname,
				'id': fieldname
			}).injectInside(form);
		}
		// textarea
		new Element('label', {
			'for': 'comments'
		}).setHTML('Comments').injectInside(form);
		new Element('textarea', {
			'name': 'comments',
			'id': 'comments',
			'rows': '3',
			'cols': ''
		}).injectInside(form);
		
		// hidden email
		new Element('input', {
			'type': 'hidden',
			'name': 'recipient',
			'value': email
		}).injectInside(form);
		
		// hidden title
		new Element('input', {
			'type': 'hidden',
			'name': 'title',
			'value': $E('h1', p.getParent()).getText()
		}).injectInside(form);
		
		// submit
		new Element('button', {
			'type': 'submit'
		}).setHTML('Submit').injectInside(form);
		
		var clear = new Element('div', {'class': 'clearer'}).injectAfter(link);
		form.injectAfter(clear);
		
		accordion.display(p);
		
		
		form.addEvent('submit', function(e) {
			/**
			 * Prevent the submit event
			 */
			new Event(e).stop();
			
			$E('button', this).setHTML('<em>Sending...</em>');
			
			this.send({
				onComplete: function(){
					new Element('div', {'class': 'notice'}).setHTML("Look forward to having you a part of the team. Someone will be in contact with you right away.").injectAfter(form);
					form.setStyle('display', 'none');
				}
			});
		});
	}
}

function alternate(alternations){
	for(var i = 0; i<alternations.length; i++){
		var obj = $(alternations[i]);
		var myMod = i % 2;
		if (myMod == 0) {
			if(obj.hasClass('standard-fill')){
				obj.addClass('standard-fill-alt');
			}else{
				obj.addClass('odd');
			}
		}
	}
}
