/*------------------------------------------------------------------
Project:		Save the Children
Author:			Jay Contonio
Last change:	2010-03-10
-------------------------------------------------------------------*/

$(document).ready(function() {
	init();
});

// Gets called onload
function init() {

	// Open links to external sites in a new tab/window
	$("a[href^='http']").attr('target','_blank');
	
	// Open RSS links in a new window
	$('a.subscribe').attr('target','_blank');

	// Add click event to the tagline on inside pages
	$('#headline h2').click(function() { location.href='/'; });

	// Adds a class of "last" to whatever LI
	$('.addLast li:last-child').addClass('last');

	// Anything with a class of autoClear gets the clearInputValue function
	$('.autoClear').focus(function() { clearInputValue($(this)); });

	// Any UL or OL with a class of "removeLastBorder" gets the border removed from it's last LI
	$('.removeLastBorder li:last-child').css({ 'border':'none' });

	// If we're in IE, let's remove the value attribute from the signup submit button because IE doesn't do negative text-indent properly.
	if(!jQuery.support.opacity) {
		$('#secondary #sign-up-button').val('');
	}
	// If we're Opera, add a body class of Opera
	if(jQuery.browser['opera'] == true) {
		$('body').addClass('opera');
	}

}

// This function takes an input, clears it's value on focus
// and sets it's value back if the user doesn't type anything
function clearInputValue(input)
{
	var originalValue = $(input).val();
	$(input).val('');
	$(input).blur(function() {
		if($(this).val() == '') {
			$(this).val(originalValue);
		} else {
			$(this).unbind('blur');
		}
	})
}

function validateEmail () {
	var str = $("input#sign-up_for_email_updates").val();
	var exp = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!exp.test(str))
	{
		alert("Oops! Please enter a valid e-mail address.");
		$("input#sign-up_for_email_updates").focus();
		return false;
	}
	$("form#emailsubscribe").attr('action','http://action.savethechildren.org/offsite-join.tcl');
	return true;
}

function validateJoin () {
	var str = $("input#email_updates").val();
	var exp = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!exp.test(str))
	{
		alert("Oops! Please enter a valid e-mail address.");
		$("input#email_updates").focus();
		return false;
	}
	var str = $("input#zip_code").val();
	if (str.length > 0)
	{
		var exp = /^((\d{5}-\d{4})|(\d{5}))$/;
		if (!exp.test(str))
		{
			alert("Oops! Please enter a valid ZIP Code, or leave blank.");
			$("input#zip_code").focus();
			return false;
		}
	}
	$("form#join_max").attr('action','http://action.savethechildren.org/offsite-join.tcl');
	return true;
}


String.prototype.ago = function()
{
	var d = Date.parse(this);
	var dateFunc = new Date();
	var timeSince = dateFunc.getTime() - d;
	var inSeconds = timeSince / 1000;
	var inMinutes = timeSince / 1000 / 60;
	var inHours = timeSince / 1000 / 60 / 60;
	var inDays = timeSince / 1000 / 60 / 60 / 24;
	var inYears = timeSince / 1000 / 60 / 60 / 24 / 365;

	// in seconds
	if(Math.round(inSeconds) == 1){
		return "1 second ago";
	}
	else if(inMinutes < 1.01){
		return Math.round(inSeconds) + " seconds ago";
	}
	// in minutes
	else if(Math.round(inMinutes) == 1){
		return "1 minute ago";
	}
	else if(inHours < 1.01){
		return Math.round(inMinutes) + " minutes ago";
	}
	// in hours
	else if(Math.round(inHours) == 1){
		return "1 hour ago";
	}
	else if(inDays < 1.01){
		return Math.round(inHours) + " hours ago";
	}
	// in days
	else if(Math.round(inDays) == 1){
		return "1 day ago";
	}
	else if(inYears < 1.01){
		return Math.round(inDays) + " days ago";
	}
	// in years
	else if(Math.round(inYears) == 1){
		return "1 year ago";
	}
	else
	{
		return Math.round(inYears) + " years ago";
	}
}

