$(document).ready(function(){
	// Redirects
	$('#donate2').click(function(){
		window.location.href = 'https://secure.yourpatriot.com/ou/mfcec/633/donate.aspx';
	});

	// Titles
	$('H1 SPAN').each(function() {
		if ($(this).html() == 'Events')  {
			$(this).parent().css('background-image', 'url(/Domains/mullen/images/h1_events.png)');
		}
		if ($(this).html() == 'Twitter')  {
			$(this).parent().css('background-image', 'url(/Domains/mullen/images/h1_twitter.png)');
		}
	});


	// Splash page stuff
	if (window.location.pathname == '/Home.aspx')  {
		// Twitter
		$(function() {  
			$.getJSON(
				'http://twitter.com/status/user_timeline/votewes.json?count=3&callback=?',
				function(d) {  
					$.each(d, function(i, item) {
						var html = '';
						html	= '<div id="newsArticle">'
							+ '<div class="newsDate">' + $.format.date(item.created_at, "MM/dd/yyyy") + '</div>'
							+ replaceURLWithHTMLLinks(item.text)
							+ '</div>';
						$('#twitterfeed').append(html);
					});
				}
			);  
		});
		$('#donate').click(function(){
			window.location.href = 'https://secure.yourpatriot.com/ou/mfcec/633/donate.aspx';
		});

		$('#volunteer').click(function(){
			window.location.href = 'https://secure.yourpatriot.com/ou/mfcec/volunteersignup.aspx';
		});
		$('#emailbutton').click(function(){
			window.location.href = 'mailto:contact@mullenforcongress.com';
		});
		$('#twitter').click(function(){
			window.location.href = 'http://twitter.com/votewes';
		});
		$('#facebook').click(function(){
			window.location.href = 'http://www.facebook.com/pages/Wes-Mullen/400836399017';
		});

		$('#issue1').click(function(){
			window.location.href = '/Meet_Wes.aspx';
		});
		$('#issue2').click(function(){
			window.location.href = '/Issues.aspx#fiscal';
		});
		$('#issue3').click(function(){
			window.location.href = '/Issues.aspx#economy';
		});
		$('#issue4').click(function(){
			window.location.href = '/Issues.aspx#healthcare';
		});
	}
	else  {
		$('#header').css("background-image", "url(/Domains/mullen/images/innerheader.jpg)");
		$('#header').css("height", "364");

		$('#donate').remove();
		$('#volunteer').remove();
		$('#emailbutton').remove();
		$('#twitter').remove();
		$('#facebook').remove();

		$('#issue1').remove();
		$('#issue2').remove();
		$('#issue3').remove();
		$('#issue4').remove();
		$('#firstname').remove();
		$('#lastname').remove();
		$('#email').remove();
	}




	// Newsletter
	$('#newslettersignup').click(function()  {
		if (isValidEmailAddress($('#newsletter').val()))  {
			$.post(
				'/Domains/mullen/files/newsletterhandler.php',
				'email=' + $('#newsletter').val(),
				function(data)  {
					$('#newsletter').removeClass('error');
					$('#newsletter').addClass('success');
					$('#newsletter').val('Success!');
					setTimeout(function() {
						$('#newsletter').val('Email Address');
						$('#newsletter').removeClass('success');
					}, 3000);
				}
			);
		}
		else  {
			$('#newsletter').addClass('error');
		}
		return false;
	});

	$('#newsletter').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});

	// Newsletter 2
	$('#signup').click(function()  {
		if (isValidEmailAddress($('#email').val()))  {
			$.post(
				'/Domains/mullen/files/newsletterhandler.php',
				'email=' + $('#email').val() + '&firstname=' + $('#firstname').val() + '&lastname=' + $('#lastname').val(),
				function(data)  {
					$('#email').removeClass('error');
					$('#signup').addClass('success');
					setTimeout(function() {
						$('#email').val('Email Address');
						$('#firstname').val('First Name');
						$('#lastname').val('Last Name');
						$('#signup').removeClass('success');
					}, 4000);
				}
			);
		}
		else  {
			$('#email').addClass('error');
		}
		return false;
	});

	$('#firstname').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});
	$('#lastname').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});
	$('#email').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});
});

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function replaceURLWithHTMLLinks(text) {
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	return text.replace(exp,"<a href='$1'>$1</a>"); 
}
