$(document).ready(function() {
	// Vars 
	var fblikebox = $('#fblike-real');
	var fblikeli = $("#top-nav-fblike");
	var twitterli = $('#top-nav-twitter');
	var twitterfollow = $('#twitter-topnav');
	var latestTweet = $('#latest-tweet');
	
	// Enable the homepage slideshow
	$('#homepage-slideshow #homepage-slides').cycle({
		fx: 'blindY', 
		speed: 1500,
		timeout: 9000,
		next:   '#slideshow-next',
		prev:	'#slideshow-prev',
		pager: '#slideshow-pager',
		pagerAnchorBuilder: function(index, ele) {
			return '<a href="#">&nbsp;</a>';
		},
		cleartypeNoBg: true
	});
	
	// Add a SPAN around every other word in H1
	colorizeHeader();
	
	// Add JS for "Contact Us" tab
	$('#contact-us-widget').css({'display':'block', 'top': ($(window).height()-$('#contact-us-widget').height())/2+'px'});
	$('#homepage #contact-us-widget').css('top', '500px');
	$("a#open-contact-us").nyroModal();

	// Add onfocus handler to remove "label" from text fields
	$("#contactform form input[type='text'], #contactform-medium input[type='text'], #contactform-medium textarea").bind('focus', clearLabel);
	$("#contactform form input[type='text'], #contactform-medium input[type='text'], #contactform-medium textarea").bind('blur', resetLabel);
	$("#contactform form").bind('submit', validateShortform);
	$("#contactform-medium form").bind('submit', validateMediumform);
	
	// Setup social networking stuff
	fblikeli.hover(function(){ fblikebox.show(); }, function() { fblikebox.hide(); });
	twitterli.hover(function(){ twitterfollow.show(); }, function(){ twitterfollow.hide(); });
	$.ajax({
		url:'proxy.php?action=latesttweet',
		success: function(data,status,XHR) { latestTweet.html(data[0].text); }
	 });
});


// Clean the label from an event target if the value in the field is the same as the "name" attribute
// converted to lowercase and with spaces replaced by underscores
function clearLabel(e) {
	var ele = $(e.target);
	if( ele.attr('class') == ele.val().toLowerCase().replace(' ', '_') )
	{
		ele.val('');	
	}
}

// Reset the label of a field to a processed version of it's "name" attribute
function resetLabel(e) {
	var ele = $(e.target);
	if( ele.val() == "" )
	{
		ele.val(ele.attr('class').replace('_',' ').capitalize());	
	}
}

// Validate input to the shortform
function validateShortform(e)
{
	var form = $(e.target);
	var empty = false;
	$("#contactform input[type='text']").each(function() {
		// If value of field is empty or equal to default value then flat field as empty
		if( $(this).val() == "" || $(this).val() == $(this).attr('class').replace('_',' ').capitalize() )
		{
			empty = true;
		}
	});
	
	// If there is an empty param then error. Else, inject Nutshell "contact" data into form and allow submit.
	if( empty == true )
	{
		alert("Please fill out all fields.");
		e.preventDefault();
	}
	else
	{
		// Inject "Contact" data into form for Nutshell CRM. Account data is already being posted.
		injectNutshellContactData(form);
	}
	
	return true;
}

// Validate input to the medium form
function validateMediumform(e) {
	var form = $(e.target);
	var empty = false;
	$("#contactform-medium input[type='text']").each(function() {
		// If value of field is empty or equal to default value then flat field as empty
		if( $(this).val() == "" || $(this).val() == $(this).attr('class').replace('_',' ').capitalize() )
		{
			empty = true;
		}
	});
	
	if( $('#description').val() == "" || $('#description').val() == 'Description' )
	{
		empty = true;
	}
	
	if( empty == true )
	{
		alert("Please fill out all fields.");
		e.preventDefault();
	}
	else
	{
		// Inject "Contact" data into form for Nutshell CRM. Account data is already being posted.
		injectNutshellContactData(form);
	}

return true;
}

function injectNutshellContactData(form)
{
	// Make sure form exists.
	if( form.length > 0 )
	{
		// Get the contact name, phone, email and create corresponding Contact variants
		form.append( jQuery('<input />').attr( 	{type: 'hidden', 
											 	name: 'contact[name]', 
												value: jQuery('input.name', form).val()})
		);

		form.append( jQuery('<input />').attr( 	{type: 'hidden', 
											 	name: 'contact[email]', 
												value: jQuery('input.email', form).val()})
		);

		form.append( jQuery('<input />').attr( 	{type: 'hidden', 
											 	name: 'contact[phone]', 
												value: jQuery('input.phone', form).val()})
		);
	}
}

// Add SPAN around every other word in header
function colorizeHeader()
{
	str = '';
	pageHead = $("#body h1");
	words = pageHead.text().split(' ');
	for(var i=0; i<words.length; i++)
	{
		if( i % 2 == 0 )
		{
			str += "<span>"+words[i]+"</span> ";	
		}
		else
		{
			str += words[i] + " ";
		}
	}
	pageHead.html(str);
}

// Add prototype for capitalize function
String.prototype.capitalize = function(){
	return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};
