$(document).ready(function()
{
	socialNavSetup();
	addThis();
	checkImages();
	
	// Slides - http://www.slidesjs.com/
	$("#featured-posts").slides({
		effect: 'slide, fade', // first effect val sets effect for next/prev buttons, second for pagination
		play: 8000,
		pause: 2500,
		slideSpeed: 1000,
		hoverPause: true
	});
	
	// Auto fill
	$('.gform_wrapper #input_1_1').val('Enter your email address').autofill();
	
	// Post image class
	$('.entry img').addClass('post-img');
	
	// Add onclick tracking for blog roll links
	$('.blogroll a[title="Driving the Midwest"]').attr('onclick', '_gaq.push(["_trackEvent", "sidebar", "offsite_click", "midwest"]);' );
	$('.blogroll a[title="Driving the Heartland"]').attr('onclick', '_gaq.push(["_trackEvent", "sidebar", "offsite_click", "heartland"]);' );
	$('.blogroll a[title="Driving the Southeast"]').attr('onclick', '_gaq.push(["_trackEvent", "sidebar", "offsite_click", "southeast"]);' );
	$('.blogroll a[title="Driving the Northeast"]').attr('onclick', '_gaq.push(["_trackEvent", "sidebar", "offsite_click", "northeast"]);' );
	$('.blogroll a[title="The Future Is Electric"]').attr('onclick', '_gaq.push(["_trackEvent", "sidebar", "offsite_click", "the_future_is_electric"]);' );
	$('.blogroll a[title="Link to General Motors Company site"]').attr('onclick', '_gaq.push(["_trackEvent", "sidebar", "offsite_click", "gm"]);' );
});

// Cufon Font Replacement
Cufon.replace('.widget h3');

// SPECIFIC JS FUNCTIONS

function checkImages()
{
	$('.author-image-lg img').each(function()
	{
		if($(this).height() < 115)
		{
			$(this).css({ marginLeft: '-' + parseInt(($(this).width() / 2) / 2) + 'px' }).parent().parent().addClass('tall');
		}
	});
	$('.author-image-sm img').each(function()
	{
		if($(this).height() < 85)
		{
			$(this).css({ marginLeft: '-' + parseInt(($(this).width() / 2) / 2) + 'px' }).parent().parent().parent().addClass('tall');
		}
	});
	$('.author-thumb img').each(function()
	{
		if($(this).height() < 85)
		{
			$(this).css({ marginLeft: '-' + parseInt(($(this).width() / 2) / 2) + 'px' }).parent().parent().addClass('tall');
		}
	});
}

function addThis()
{
	$(".addThis").attr("href", "http://www.addthis.com/bookmark.php?v=20");
	$(".addThis").hover(function() {
        return addthis_open(this, '', location.href, document.title);  
    }, function() {
        addthis_close();
    });
    $(".addThis").click(function() {
        return addthis_sendto();
    });
    $(".addThisEmail").click(function() {
        return addthis_open(this, 'email', location.href, document.title);  
    });
}

function socialNavSetup()
{
	$('#social-nav').css({top:'-10px'}, 200);
	$('#functional-nav li.connect').hover(function()
	{
		$('#social-nav').animate({top:'35px'}, 200).parent().addClass('current_page_item');
	}, function()
	{
		$('#social-nav').animate({top:'-10px'}, 200).parent().removeClass('current_page_item');
	});
}

function submitNewsletterForm()
{
	$('#modules .success').hide();
	$('#modules .error').hide();
	$.ajax({
		type: 'POST',
		url: '/wp-content/themes/gmwest/includes/ajax_funcs.php?type=newsletter',
		data: $('#newsletter-form').serialize(),
		success: function(data)
		{
			if(data == 'success')
			{
				$('#modules .success').show();
				$('#newsletter-form').hide();
			}
			else
			{
				$('#modules .error').html(data).show();
			}
		}
	});
	return false;
}

// GLOBAL JS FUNCTIONS

//function autoFill(id, text, startColor, endColor)
//{
//	$(id).unbind().css({ color: startColor }).attr({ value: text }).focus(function()
//	{
//		if($(this).val()==text)
//		{
//			$(this).val("").css({ color: endColor });
//		}
//	}).blur(function()
//	{
//		if($(this).val()=="")
//		{
//			$(this).css({ color: startColor }).val(text);
//		}
//	});		
//}

(function($)
{
	$.fn.autofill = function(options)
	{
		options = $.extend({
			startColor: '#999',
			endColor: '#000'
		}, options);
		
		$(this).each(function()
		{
			if(!$(this).is('input')) return;
			var text = $(this).val();
			$(this).css({ color: options.startColor }).focus(function()
			{
				if($(this).val() == text)
				{
					$(this).val('').css({ color: options.endColor });
				}
			}).blur(function()
			{
				if($(this).val() == '')
				{
					$(this).css({ color: options.startColor }).val(text);
				}
			});
		});
	};
})(jQuery);

function ajaxLightbox(the_url, the_data, title)
{
	$.ajax({
		type: 'POST',
		url: the_url,
		data: the_data,
		success: function(data)
		{
			openLightbox(data, title);
		}
	});
	return false;
}

function openLightbox(content, title)
{
	$('#lightbox').overlay(
	{
	   	expose:
		{
			color: '#000', 
	       	loadSpeed: 200, 
	       	opacity: 0.7 
	   	},
	   	api: true
	}).load();
	$('#lightbox .title').html(title);
	$('#lightbox .content').html(content);
	$('#lightbox .close').html('Close');
	return false;
}

function closeLightbox()
{
	$('#lightbox').close();
}
