var $j = jQuery;

jQuery.fn.toggleDefault = function(){
	jQuery(this).each(function(){
		jQuery(this).bind('focus', function(){
			if (jQuery.trim(jQuery(this).val()) == this.defaultValue){
				jQuery(this).val('');
			}
		}).bind('blur', function(){
			if (jQuery.trim(jQuery(this).val()) == ''){
				jQuery(this).val(this.defaultValue);
			}
		});
	});
}

function sameHeight(c, scope){
	if (scope){
		$j(scope).each(function(){
			var set = $j(this).find(c);
			
			sameHeight(set);
		});
	}
	else {
		var h = 0;
		
		$j(c).each(function(){
			var th = $j(this).height()
			
			h = (th > h)? th : h;
		});
		
		$j(c).height(h);	
	}
}

function vCenterImg(container){
	$j(container).each(function(){
		var h1 = $j(this).innerHeight();
		var h2 = $j(this).find('img').outerHeight();
		$j(this).find('img').css('paddingTop', Math.floor((h1 - h2) / 2));
	});
}

$j(function(){
	$j('#siteSearchTxt, #siteNewsletter input.text').toggleDefault();
	
	$j('#siteCustomerLogin input.text, #ekmResponseEmailAddress').each(function(){
		$j(this).bind('focus', function(){
			$j(this).css('color', '#482548');
		}).bind('blur', function(){
			$j(this).css('color', '#D1C8D1');
		});
	});
});

function prettyDate(date){
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		
		dayDiff = Math.floor(diff / 86400);
			
	if ( isNaN(dayDiff) || dayDiff < 0)
		return 'Seconds ago';
	
	if (diff < 60)
		return 'Less than a minute ago';
		
	if (diff < 120)
		return '1 minute ago';
		
	if (diff < 3600)
		return Math.floor( diff / 60 ) + ' minutes ago';
		
	if (diff < 7200)
		return '1 hour ago';
		
	if (diff < 86400)
		return Math.floor( diff / 3600 ) + ' hours ago';
		
	if (dayDiff == 1)
		return 'Yesterday';
		
	if (dayDiff < 7)
		return dayDiff + ' days ago';
		
	if (dayDiff < 31)
		return Math.ceil( dayDiff / 7 ) + ' weeks ago';
	
	if (dayDiff >= 31)
		return 'Over a month ago';
}

google.load("feeds", "1");

function googleFeedsLoaded(){
	var feed = new google.feeds.Feed("http://twitter.com/statuses/user_timeline/21092088.rss");
	
	feed.load(function(result) {
		if (!result.error) {
			var container = $j("#siteTwitterFeed");
			var RE_URL = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g;
			
			container.empty();
			
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
				
				var date = new Date(entry['publishedDate']);
				
				var contentHtml = entry['content'];
				var dateText = prettyDate(date);
				
				contentHtml = contentHtml.replace(/^\w+:/, '<strong>$&</strong>');
				
				contentHtml = contentHtml.replace(RE_URL, '<a href="$&">$&</a>');
				
				container.append('<p class="siteTwitterPost">' + contentHtml + '</p>');
				container.append('<p class="siteTwitterDate">' + dateText + '</p>');
			}
		}
		else {
			$j("#siteTwitterFeed").append('<p>Oops! There was an error while retrieving our twitter feed. Sorry.</p>');
		}
	});
}

google.setOnLoadCallback(googleFeedsLoaded);