$(document).ready(function(){
	// Redirects
	$('#books').click(function(){
		window.location.href = '/Books.aspx';
	});
	$('#about').click(function(){
		window.location.href = '/About.aspx';
	});
	$('#blog').click(function(){
		window.location.href = '/Blog.aspx';
	});
	$('#home').click(function(){
		window.location.href = '/Home.aspx';
	});
	$('#news').click(function(){
		window.location.href = '/News.aspx';
	});

	$('#events').click(function(){
		window.location.href = '/Events.aspx';
	});
	$('#contact').click(function(){
		window.location.href = '/Contact.aspx';
	});



	$('.twitter').click(function(){
		window.open('http://twitter.com/#!/EE_Knight');
	});
	$('#facebook').click(function(){
		window.open('http://www.facebook.com/pages/E-E-Knight/42434890434');
	});
	$('#myspace').click(function(){
		window.open('http://www.myspace.com/e_e_knight');
	});
	$('#livejournal').click(function(){
		window.open('http://eeknight.livejournal.com/');
	});
	$('#digg').click(function(){
		window.open('http://digg.com/submit?url=http%3A//www.eeknight.com');
	});



	// Scroller
	$("#scroller").easySlider({
		auto: true,
		continuous: true,
		pause: 15000,
		speed: 800,
		controlsShow: false
	});


	// Newsletter
	$('#signup').click(function()  {
		if (isValidEmailAddress($('#email').val()))  {
			$.post(
				'/Domains/eeknight/files/newsletterhandler.php',
				'email=' + $('#email').val(),
				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/EE_Knight.json?count=3&callback=?',
			function(d) {  
				//$.each(d.reverse(), function(i, item) {
				var item = d[0];
					var html = '';
					html	= '<div class="item">'
						+ '<h2>' + $.format.date(item.created_at, "MM/dd/yyyy hh:mm") + '</h2>'
						+ replaceURLWithHTMLLinks(item.text)
						+ '</div>';
					$('#twitterfeed').html(html);
				//});
			}
		);
	});


	$("#blogcontainer").rssfeed('http://eeknight.livejournal.com/data/rss', {
		header: false,
		limit: 10
	});

});

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>"); 
}
