$(document).ready(function(){ 
	
	$('input.field').each(function() {
		var default_value = this.value;
		$(this).focus(function(){
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function(){
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	
	$(".post .more-link").hover(
	  function () {
	  	$(this).parent().parent().addClass('hover');
	  }, 
	  function () {
	    $(this).parent().parent().removeClass('hover');
	  }
	);
	
	$(".post h2.post-title a").hover(
	  function () {
	  	$(this).parent().parent().parent().find('.post-entry').addClass('hover');
	  }, 
	  function () {
	    $(this).parent().parent().parent().find('.post-entry').removeClass('hover');
	  }
	);
		
	$(".dsq-comment:last-child").addClass('last');
	
	// $("#disqus_thread").hover(
	//   function () {
	//   	$('#content').animate({width:'660px'},400);
	// 	$('#disqus_thread').animate({width:'280px'},400);
	//   }
	// );
	
	// $('.single #sidebar').live("click", function(){
	// 	 	$('#content').animate({width:'600px'},400);
	// 		$('#disqus_thread').animate({width:'340px'},400);
	// });


	// var $sidebar   = $('.single #sidebar'),
	//         $window    = $(window),
	// 		offset     = $sidebar.offset(),
	// 		topPadding = 40;
	// 
	// $window.scroll(function() {
	//         if ($window.scrollTop() > offset.top) {
	// 			
	//             $sidebar.stop().animate({
	//                 marginTop: $window.scrollTop() - offset.top + topPadding
	//             });
	//         } else {
	//             $sidebar.stop().animate({
	//                 marginTop: 0
	//             });
	//         }
	//  });
	// 
	// var navHeight = $("#navigation").outerHeight();
	// $("#container").css("padding-top",navHeight);
	// 
	// $(function() {
	//  $(window).scroll(function(){
	//  	var scrollTop = $(window).scrollTop();
	//  	if(scrollTop != 0)
	//  		$('#navigation').stop().animate({'opacity':'0.2'},400);
	//  	else	
	//  		$('#navigation').stop().animate({'opacity':'.9'},400);
	//  });
	//  
	//  $('#navigation').hover(
	//  	function (e) {
	//  		var scrollTop = $(window).scrollTop();
	//  		if(scrollTop != 0){
	//  			$('#navigation').stop().animate({'opacity':'.9'},400);
	//  		}
	//  	},
	//  	function (e) {
	//  		var scrollTop = $(window).scrollTop();
	//  		if(scrollTop != 0){
	//  			$('#navigation').stop().animate({'opacity':'0.2'},400);
	//  		}
	//  	}
	//  );
	//  });
	
	
	// Linkify
	String.prototype.linkify = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
			return m.link(m);
		});
	};

	// Hashtag
	String.prototype.parseHashtag = function() {
		return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
			var tag = t.replace("#","%23")
			return t.link("http://twitter.com/#!/search/"+tag,'_blank');
		});
	};

	// Usernames
	String.prototype.parseUsername = function() {
		return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
			var username = u.replace("@","")
			return u.link("http://twitter.com/"+username,'_blank');
		});
	};

	// Relative Time
	function relative_time(time_value) {
	   var parsed_date = Date.parse(time_value);
	   var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	   var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);

	   if(delta < 60) {
		
	       return 'just said it';
	   } else if(delta < 120) {
	       return 'a minute ago';
	   } else if(delta < (45*60)) {
	       return (parseInt(delta / 60)).toString() + ' minutes ago';
	   } else if(delta < (90*60)) {
	           return 'an hour ago';
	       } else if(delta < (24*60*60)) {
	       return 'earlier today';
	   } else if(delta < (48*60*60)) {
	       return '1 day ago';
	   } else {
	       return (parseInt(delta / 86400)).toString() + ' days ago';
	   }
	}
	var dayArray = new Array();
	var today;
	var countMe = 0;

	// Twitter JSON
	$.getJSON('http://twitter.com/status/user_timeline/danmartell.json?count=40&callback=?', function(data){
					
		// Populate the Array w/ Dates	
		$.each(data, function(i, item){
			dayArray.push(relative_time(item.created_at)); 
		});
		
		// Save the First Date
		today = "today";
		
		// Cycle Through dayArray and add +1 if today equals a date in dayArray
		$.each(dayArray, function (){
			if(this == 'just said it' || this == 'a minute ago' || this == 'an hour ago' || this == 'earlier today'){
				countMe++;
			}
			else {
			}
		});
		$('#header p.tweet_count strong').html(countMe);
	
	});
	
	// Twitter JSON
	$.getJSON('http://twitter.com/status/user_timeline/danmartell.json?count=3&callback=?', function(data){
		$.each(data, function(i, item){
			$('div.twitter').append('<div class="tweet"><p class="post">' + item.text.linkify().parseHashtag().parseUsername()+'</p><p class="meta">tweeted '+relative_time(item.created_at)+'</p>'+'</div>');	
	    });				  		
	});
	
	// Start Lightbox
	$('a[rel="newsletter"]').click(function(){
		$('#newsletter').css('display','block');
		// $('.lightbox .modal').width(0).height(0);
		$('#newsletter .overlay').css('opacity','0');
		$('#newsletter .modal').css('opacity','0');

		$('#newsletter .modal').animate({opacity:'1'},400);	
		$('#newsletter .overlay').animate({opacity:'.8'},200);
	});
	
	// Close Lightbox
	$('#newsletter .modal a.close, #newsletter .overlay').click(function(){		
		$('#newsletter .overlay, #newsletter .modal').animate({opacity:'0'},250, function(){
			$('#newsletter').css('display','none');	
		});		
	});
	

});


