$(document).ready(function() {
	// Safer email links
	$('a[@rel=email]').each(function() {
		var strReplace = '[at-no-candy-spam]';
		$(this).html($(this).html().replace(strReplace, '@')).attr('href',$(this).attr('href').replace(strReplace, '@'));
	});

	// Open external links in new windows
	$('a[@rel=external]').click(function() {
		window.open($(this).attr('href'));
		return false;
	});	
	
	// Subscribe to newsletter
	$('#subscribeSidebar').submit(function() {
		// get the value of email
		var address = $('#address').val();
		if (address) {
			// Hide the intro copy
			$('#newsletterIntro').hide();
			// hide the success message
			$('#success').hide();
			// display loading info
			$('#loader').show();
			$.ajax({
				type: 'POST',
				cache: false,
				url: '/newsletter/subscribe.php',
				data: "address=" + address,
				success: function(response) {
					if (response == true) {
						$('#loader').hide();
						$('#success').show();
					} else {
						$('#loader').hide();
						$('#newsletterIntro').show();
						alert('There was an error adding your email address.');
					}
				}
			});
		}
		return false;
	});
	
	$('#selectState').change(function() { 
		var thisState = $(this).children('option:selected').attr('value');
		if (thisState != 'Choose a state:') {
			$('#loading').show();
			$('#retailContent').hide();
			$.get('/retail/view.php', { state: thisState }, function(data) {
				$('#loading').hide();
				$('#retailContent').html(data).show();
			});
		}
		return false;
	 });
});
