var YMI = {	

	churchLookup : function (churches)
	{
		var lookup_results = 0;
		var lookup_count = 0;
		jQuery('fieldset.signup').append('<div class="lookup" style="display: none"></div>');
		jQuery('input#church_name').keyup(function () {	
			lookup_count = 0;
			lookup_results = 0;
			jQuery('fieldset.signup .lookup').html('<h3><strong>Is your church listed here?</strong></h3><ul style="display: list"></ul><a href="#" class="close">x close</a>');
			var field_val = jQuery(this).attr('value');
			field_val = YMI.convertOrdinals(field_val);
			while(lookup_results < 6)
			{	
				if(churches[lookup_count].name.indexOf(field_val) > -1 && field_val.length > 0)
				{
					jQuery('fieldset.signup .lookup').show();
					jQuery('fieldset.signup .lookup ul').append('<li class="' + churches[lookup_count].id + '"><span>' + churches[lookup_count].name + '</span> - ' + churches[lookup_count].zip + '</li>');
					lookup_results++;
				}
				lookup_count < churches.length ? lookup_count++ : lookup_results = 6;
			}
		});
		jQuery('input:not(#church_name)').focus(function () {
			jQuery('fieldset.signup .lookup').hide();
		});
		jQuery('fieldset.signup .lookup a.close').live('click',function () {
			jQuery('fieldset.signup .lookup').hide();
			return false;
		});
		jQuery('fieldset.signup .lookup li').live('click', function () {
			jQuery('input#church_name').attr('value',jQuery(this).find('span').text());
			jQuery('input#church_id').attr('value',this.className);
			jQuery.getJSON("/churches/" + this.className + ".json",
				function(data){
				jQuery('input#church_street').attr('value',data.street);
				jQuery('input#church_phone').attr('value',YMI.nicePhone(data.phone));
				jQuery('input#church_city').attr('value',data.city);
				jQuery('select#state option').each(function () {
					if(data.state == jQuery(this).attr('value'))
					{
						jQuery(this).attr('selected','selected');
					}
				});
				jQuery('input#church_zip').attr('value',data.zip);
				jQuery('#one_col fieldset.signup table.hide').slideDown('fast');
			});
			jQuery('fieldset.signup .lookup').hide();
			return false;
		});
		jQuery('input#church_zip').keyup(function () {	
			lookup_count = 0;
			lookup_results = 0;
			jQuery('fieldset.signup .lookup').html('<h3><strong>Is your church listed here?</strong></h3><ul style="display: list"></ul><a href="#" class="close">x close</a>');
			var field_val = jQuery(this).attr('value');
			while(lookup_results < 6)
			{	
				if(churches[lookup_count].zip.indexOf(field_val) > -1 && field_val.length > 0)
				{
					jQuery('fieldset.signup .lookup').show();
					jQuery('fieldset.signup .lookup ul').append('<li class="' + churches[lookup_count].id + '"><span>' + churches[lookup_count].name + '</span> - ' + churches[lookup_count].zip + '</li>');
					lookup_results++;
				}
				lookup_count < churches.length ? lookup_count++ : lookup_results = 6;
			}
		});
		jQuery('input:not(#church_name)').focus(function () {
			jQuery('fieldset.signup .lookup').hide();
		});
		jQuery('fieldset.signup .lookup a.close').live('click',function () {
			jQuery('fieldset.signup .lookup').hide();
			return false;
		});
		jQuery('fieldset.signup .lookup li').live('click', function () {
			jQuery('input#church_name').attr('value',jQuery(this).find('span').text());
			jQuery.getJSON("/churches/" + this.className + ".json",
				function(data){
				jQuery('input#church_street').attr('value',data.street);
				jQuery('input#church_phone').attr('value',YMI.nicePhone(data.phone));
				jQuery('input#church_city').attr('value',data.city);
				jQuery('select#state option').each(function () {
					if(data.state == jQuery(this).attr('value'))
					{
						jQuery(this).attr('selected','selected');
					}
				});
				jQuery('input#church_zip').attr('value',data.zip);
				jQuery('#one_col fieldset.signup table.hide').slideDown('fast');
			});
			jQuery('fieldset.signup .lookup').hide();
			return false;
		});
	},

	nicePhone : function (number)
	{
		var nice_phone = number.replace(/^([0-9]{3})([0-9]{3})([0-9]{4})$/,'($1) $2-$3');
		return nice_phone;
	},

  convertOrdinals : function (ord)
	{
	  ord = ord.replace(/1st/i,'First');
	  ord = ord.replace(/2nd/i,'Second');
	  ord = ord.replace(/3rd/i,'Third');
	  ord = ord.replace(/4th/i,'Fourth');
	  ord = ord.replace(/5th/i,'Fifth');
	  ord = ord.replace(/6th/i,'Sixth');
	  ord = ord.replace(/7th/i,'Seventh');
    return ord;
	}
}