//
// # Ready
//

$(function(){
	
	// # Subnav?
	if(exists($('nav.subnav'))){
		var active_class_item = $('nav.main li.active');
		var index = $('li').index(active_class_item);
		$('nav.subnav').addClass('subnav_'+even_or_odd(index+1));
	}
	
	// # Notice?
	if(exists($('div.alert'))){
		$('div.alert').click(function(){
			$(this).fadeOut();
			return false;
		});
	}
	
	// # Sponsors
	if(exists($('section.content div.sponsors'))){
		$('section.content div.sponsors:first').addClass('first-child');
	}
	
	// # Registration
	if(exists($('form#registration'))){
		
		$('form#registration').submit(function(){
			return registration_validation();
		});
		
		// # Type of
		$('form#registration input.type_of').click(function(){
			if($(this).val() == 'individual'){
				// # Hide the + add_person link
				$('form#registration div#add_person_link').slideUp(250);
				// # Remove any added people
				var i=0;
				$('form#registration div.people div.person').each(function(){
					if(i>0){
						$(this).slideUp(250, function(){
							$(this).remove();
						});
					}
					i++;
				});
				
			} else if($(this).val() == 'family' || $(this).val() == 'group'){
				// # Show the + add_person link
				$('form#registration div#add_person_link').slideDown(250);
			}
		});
		
		// # Links: Add Person
		$('form a.add_person').click(function(){
			$('div.people div.person:first-child').clone(false).appendTo('div#people').find('input').val('');
			regsitration_reorder($('div.people div.person'));
			registration_bind_remove_links();
			$('div.people div.person:last-child').css('display','none');
			$('div.people div.person:last-child').slideDown(250);
			$('div.people div.person:last-child input.first_name').select();
			try{ ie_add_person() }catch(e){} // # IE 6/7
			return false;
		});
		
		// # Links: Add Baby
		$('form#registration a.add_baby').click(function(){
			$('div.babies div.baby:first-child').clone(false).appendTo('div#babies').find('input').val('');
			regsitration_reorder($('div.babies div.baby'));
			registration_bind_remove_links();
			$('div.babies div.baby:last-child').css('display','none');
			$('div.babies div.baby:last-child').slideDown(250);
			$('div.babies div.baby:last-child input.name').select();
			try{ ie_add_baby() }catch(e){} // # IE 6/7
			return false;
		});
		
	}
	
	// # Comments
	if(exists($('form#comment'))){
		
		$('form#comment').submit(function(){
			return comment_validation();
		});
		
	}
	
	// # Tooltips (Not for IE6/7)
	if( !is_ie('6','7') ){
		$('div.tooltip').tipTip({ defaultPosition:'top' });
		$('a.tooltip').tipTip({ defaultPosition:'top' });
	}
	
	// # Cufon
	try{
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon.replace('h3');
		Cufon.replace('h4');
		Cufon.replace('header a', { hover: true });
		Cufon.replace('aside a', { hover: true });
		Cufon.replace('footer a', { hover: true });
	} catch(e){}
	
});


//
// # Utilities
//

function exists(element){
	return (element.length>0) ? true : false;
}

function even_or_odd(value){
	return (value % 2 == 0) ? 'even' : 'odd';
}

function trim(element){
	element.val( jQuery.trim(element.val()) );
}

function is_ie(){
	var args = is_ie.arguments;
	var result = false;
	if($.browser.msie){
		for(var i=0; i<args.length; i++){
			if($.browser.version.indexOf(args[i]) == 0){
				result = true;
				break;
			}
		}
	}
	return result;
}


//
// # Registration Helpers
//

function registration_bind_remove_links(){
	$('form a.remove').unbind();
	
	$('form a.remove').mouseover(function(){
		$(this).parent().addClass('remove');
	});
	
	$('form a.remove').mouseout(function(){
		$(this).parent().removeClass('remove');
	});
	
	$('form a.remove').click(function(){
		$(this).parent().parent().slideUp(350, function(){
			$(this).addClass('removed');
			regsitration_reorder($(this).parent().children());
			$(this).remove();
		});
		return false;
	});
}

function regsitration_reorder(elements){
	var i=0;
	elements.each(function(){
		if( !$(this).hasClass('removed') ){
			registration_order_inputs($(this).find('input'), i);
			registration_order_labels($(this).find('label'), i);
			i++;
		}
	});
}

function registration_order_inputs(elements, count){
	elements.each(function(){
		// # Renames input name such as people[0][first_name] to people[1][first_name]
		var name = $(this).attr('name').replace(/\[\d\]/, '['+count+']');
		$(this).attr('name', name);
		// # Renames input id such as people_0_first_name to people_1_first_name
		var id = $(this).attr('id').replace(/_\d_/, '_'+count+'_');
		$(this).attr('id', id);
	});
}

function registration_order_labels(elements, count){
	elements.each(function(){
		// # Renames label for such as people_0_first_name to people_1_first_name
		var f = $(this).attr('for').replace(/_\d_/, '_'+count+'_');
		$(this).attr('for', f);
	});
}


// 
// # Registration Validation
//

function registration_validation(){
	var valid = true;
	
	// # People
	var i=0;
	$('div.people div.person').each(function(){
		var first_name = $(this).find('input.first_name');
		var last_name = $(this).find('input.last_name');
		// # First set of names or a set with one only of the names filled in...
		if( i==0 || (validate_presence(first_name) || validate_presence(last_name)) ){
			if( ! validate_presence(first_name, "Enter your first name.") ) return valid = false;
			if( ! validate_presence(last_name, "Enter your last name.") ) return valid = false;
		}
		i++;
	});
	if( ! valid ) return false;
	
	// # Email
	if( ! validate_presence($('input#registration_email'), "Enter your email.") ) return false;
	if( ! validate_email($('input#registration_email'), 'Your email must be a valid email address.') ) return false;
	
	// # Phone
	if( ! validate_presence($('input#registration_phone'), "Enter your phone number.") ) return false;
	
	// # Address
	if( ! validate_presence($('input#registration_address'), "Enter your address.") ) return false;
	
	// # City
	if( ! validate_presence($('input#registration_city'), "Enter your city.") ) return false;
	
	// # Province_State
	if( ! validate_presence($('input#registration_province_state'), "Enter your province/state.") ) return false;
	
	// # Postal_Zip
	if( ! validate_presence($('input#registration_postal_zip'), "Enter your postal/zip code.") ) return false;
	
	// # Babies
	var i=0;
	$('div.babies div.baby').each(function(){
		var name = $(this).find('input.name');
		var birth_death = $(this).find('input.birth_death');
		var pronunciation = $(this).find('input.pronunciation');
		
		// # A set with one only of the names filled in...
		if( validate_presence(name) || validate_presence(birth_death) || validate_presence(pronunciation) ){
			if( ! validate_presence(name, "Please enter the baby's name.") ) return valid = false;
		}
		i++;
	});
	if( ! valid ) return false;
	
	// # Question/Answer
	if( ! validate_presence($('input#registration_answer'), "Please answer the security question.") ) return false;
	if( ! validate_value($('input#registration_answer'), 3, "Your answer to the security question is incorrect.") ) return false;
	
	// # Submit
	$('input#registration_submit').val('Please wait...');
	$('input#registration_submit').attr('disabled','disabledz');
	
	return true;
}


//
// # Comment Validation
// 

function comment_validation(){
	var valid = true;
	
	// # Name
	if( ! validate_presence($('input#comment_name'), "Enter your name.") ) return false;
	
	// # Email
	if( ! validate_presence($('input#comment_email'), "Enter your email.") ) return false;
	if( ! validate_email($('input#comment_email'), 'Your email must be a valid email address.') ) return false;
	
	// # Comment
	if( ! validate_presence($('textarea#comment_comment'), "Enter your comment.") ) return false;
	
	// # Question/Answer
	if( ! validate_presence($('input#comment_answer'), "Please answer the anti-spam question.") ) return false;
	if( ! validate_value($('input#comment_answer'), 3, "Your answer to the anti-spam question is incorrect.") ) return false;
	
	// # Submit
	$('input#comment_submit').val('Please wait...');
	$('input#comment_submit').attr('disabled','disabled');
	
	return true;
}


// 
// # Validators
// 

function validate_presence(element, message){
	trim(element);
	var valid = ( element.val() != "" ) ? true : false;
	if(!valid && message != null){
		alert(message);
		element.focus();
		element.select();
	}
	return valid;
}

function validate_email(element, message){
	trim(element);
	var pattern = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
	var valid = ( pattern.test( jQuery.trim(element.val()) ) ) ? true : false;
	if(!valid && message != null){
		alert(message);
		element.focus();
		element.select();
	}
	return valid;
}

function validate_checked(element, message){
	var valid = (element.attr('checked') == true) ? true : false;
	if(!valid && message != null){
		alert(message);
		element.focus();
	}
	return valid;
}

function validate_value(element, value, message){
	trim(element);
	var valid = (element.val() == value);
	if(!valid && message != null){
		alert(message);
		element.focus();
		element.select();
	}
	return valid;
}


