// 
//  application.js
//  Seabourne Website
//  
//  Created by Jay Contonio on 2011-03-28
//  Copyright 2011 Seabourne Consulting. All rights reserved.
// 


function hideOverlayLabels(formItem)
{
	if($(formItem).val() != '') {
		var id = $(formItem).attr('id');
		$('label[for=' + id + ']').hide();
	}
}

function parseResults(data)
{
	results = data;
	//log(results);
	var output = '';
	//var settings_len = <%= (widget_instance.settings and widget_instance.settings['display_items']) ? widget_instance.settings['display_items'] : 5 %>;
	var len = 5;//settings_len < results.length ? settings_len : results.length;
	for (var i = 0; i < len; i++) {
		message = results[i];
		var clas = (i === (len - 1)) ? ' class="end"' : '';
		var date = message.created_at.split(' ')[0] + ' ' + message.created_at.split(' ')[1] + ' ' + message.created_at.split(' ')[2];
		output += '<li' + clas + '><p>' + message.text.linkify() + '<time date="' + message.created_at + '">' + date + '</time></p></li>';
	}
	$(".twitterList").empty().append(output);
}

function getTwitterStatus()
{
	$.ajax({ url:"http://twitter.com/status/user_timeline/seabourneinc.json", success: parseResults, dataType:'jsonp'} );
}

window.onload = function()
{
	
	// When the page loads, hide labels for any input text that has a value
	// ie: This happens when a user fills out the contact form, submits, then hits back
	$('.action_form .form-item .overlay').each(function() {
		hideOverlayLabels(this);
	});
	
	// When a user tabs from an input field, hide the label if there is a value in the input
	$('.action_form .form-item .overlay').blur(function(e) {
		hideOverlayLabels(this);
	});
	
	// Add the last class to the last breadcrumb LI because the WP plugin doesnt do this nice
	$('.breadcrumb li:last-child').addClass('last');
	
}
