$(document).ready(function() {

	/* SITE WIDE */
	Cufon.replace("h1", { fontFamily: "ampersand" });

	$('body').supersleight({shim: '/static/images/transparent.gif'});

	$('#site_search input[title], #newsletterForm input[title]').inputHints();
	
	$('#changer a').click(function() {
		var originalClass = $(this).attr('class');
		$('.img_1').removeClass('img_1').addClass(originalClass);
		$(this).removeClass(originalClass).addClass('img_1');
		$(this).next().removeClass(originalClass).addClass('img_1');
		return false;
	});
	
	$('#changer a').css({background:"white", opacity:"0"}); // cludge to fix IE evil hollow links
	
	$('#beer_finder select').change(function() {
		window.location = $(this).val();
	});
	

	/* GOOGLE ANALYTICS */

	debug_google_analytics_rules = false;
	
	$('a').click(function(){
		var a = $(this).attr('href');
		var u = parent.location.protocol+'//'+window.location.hostname;
		var t = (a.substring(0,u.length) == u || a.substring(0,1) == '/');
		if(!t) { var p = '/external/' + a;  _gaq.push(['_trackPageview',p]);  }
	});
	$('a.colorbox').attr('onClick', "_gaq.push(['_trackEvent', 'Video', 'Play', 'The brewing process']);");


	/* FORM VALIDATION */
	$('#contactForm').submit(function() {
		$(this).find('label.error').remove();
		var errors = false;
		$('input.req, textarea.req').each(function() {
			if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				$(this).after('<label class="error">'+$(this).attr('title')+'</label>');
				//$(this).val('');
				errors = true;
			}
		});
		if(!errors) {
			var email = $('#email').val();
			if(!(/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/.test(email))) {
				$('#email').after('<label class="error">'+$('#email').attr('title')+'</label>');
				errors = true;
			}
		}
		if (!errors) {
			return true;
		}
		return false;
	});

	$('#newsletterForm').submit(function() {
		var errors = false;
		var email = $('#email').val();
		if(!(/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/.test(email))) {
			$('#email').attr('value', $('#email').attr('title'));
			errors = true;
		}
		if (!errors) {
			return true;
		}
		$('#email').attr('value', 'Re-enter email address').addClass('error');
		return false;
	});

});

jQuery.fn.inputHints = function() {
   // hides the input display text stored in the title on focus
   // and sets it on blur if the user hasn't changed it.

   // show the display text
   $(this).each(function(i) {
       $(this).val($(this).attr('title'));
   });

   // hook up the blur & focus
   $(this).focus(function() {
       if ($(this).val() == $(this).attr('title'))
           $(this).val('');
   }).blur(function() {
       if ($(this).val() == '')
           $(this).val($(this).attr('title'));
   });
};
