$(document).ready(function(){
	// Redirects
	$('#donate').click(function(){
		window.open('https://secure.yourpatriot.com/ou/ryan/donate.aspx');
	});
	$('#volunteer').click(function(){
		window.open('https://secure.yourpatriot.com/ou/ryan/volunteersignup.aspx');
	});
	$('#tellafriend').click(function(){
		window.location.href = '/Tell_A_Friend.aspx';
	});
	$('#contact').click(function(){
		window.location.href = '/Contact.aspx';
	});


	$('#facebook').click(function(){
		window.open('http://www.facebook.com/#!/pages/Paul-Ryan/89309491492');
	});
	$('#twitter').click(function(){
		window.open('http://twitter.com/#!/ryan4congress');
	});
	$('#youtube').click(function(){
		window.open('http://www.youtube.com/ryan4congress');
	});


	// Scroller
	$("#scroller").easySlider({
		auto: true,
		continuous: true,
		pause: 10000,
		speed: 800,
		controlsShow: false
	});


	// Newsletter
	$('#signup').click(function()  {
		if (isValidEmailAddress($('#email').val()))  {
			var emailvalue = $('#email').val();
			$('#email').val('Processing...');
			$.post(
				'/Domains/paulryan/files/newsletterhandler.php',
				'email=' + emailvalue,
				function(data)  {
					$('#email').removeClass('error');
					$('#email').addClass('success');
					$('#email').val('Success!');
					setTimeout(function() {
						$('#email').val('Email Address');
						$('#email').removeClass('success');
					}, 3000);
				}
			);
		}
		else  {
			$('#email').addClass('error');
		}
		return false;
	});

	$('#email').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});


	// Twitter
	$(function() {
		$.getJSON(
			'http://twitter.com/status/user_timeline/ryan4congress.json?count=3&callback=?',
			function(d) {  
				$.each(d.reverse(), function(i, item) {
					var html = '';
					html	= '<div class="item">'
						+ '<h2>' + $.format.date(item.created_at, "MM/dd/yyyy hh:mm") + '</h2>'
						+ replaceURLWithHTMLLinks(item.text)
						+ '</div>';
					$('#tweets').prepend(html);
				});
			}
		);
	});

	// Endorsements
	$("#newsstream #AllContent").each(function()  {
		jQuery.getFeed({
			url: '/site/RssOutput.aspx?id=1609',
			success: function(feed) {
				for(var i = 0; i <= 2; i++) {

					// ... more
					var morelink = '';
					if (feed.items[i].description.length > 200)  {
						morelink =  '... <a href="' + feed.items[i].link + '">more</a>';
					}

					// print
					$('#newsstream #AllContent').append(
						'<div id="newsArticle">'
						+ '<h2>' + feed.items[i].title + '</h2>'
						+ feed.items[i].description.substring(0,200)
						+ morelink
						+ '</div>'
					);
				}
			}
		});
	});

	// Scroll to the content (except Home)
	if (window.location.pathname != '/Home.aspx')  {
		var target_offset = $("#pagecontent").offset();
		var target_top = target_offset.top;
		$('html, body').animate({scrollTop:target_top}, 500);
	}
});

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>"); 
}
