$(function () {
	var body =  $('body');
	// Create an overlay to capture click events that happen off of the signup form.
	var clickOverlay = $('<div>&nbsp;</div>').css({
		height: body.height(),
		width: body.width(),
		position: 'absolute',
		top: '0',
		left: '0',
		zIndex: '2'
	}).attr('id', 'newsletter-signup-overlay');
	if ($.browser.msie) {
		clickOverlay.css({
			filter:'alpha(opacity=02)',
			backgroundColor:'#fff'
		});
	}

	//functions declared as local variables so they don't pollute global namespace
	var hideSignup = function (event) {
		$('#signup-form').hide();
		$('#newsletter-signup a#signup-link').css({backgroundColor: '#527186', color: '#fff'});
		// clickOverlay.remove();
		$.cookie('signup-seenit', 'true')
	}
	var showSignup = function (event) {
		$('#signup-form').show();
		$('#newsletter-signup a#signup-link').css({backgroundColor: '#fff', color: '#527186'});
		// clickOverlay.prependTo(body).bind('click', hideSignup); // hide signup when they click off of it.
		return false;
	}
	
	var toggleSignup = function (event) {
		if($('#signup-form:visible').length == 1) {
			hideSignup();
		} else {
			showSignup();
		}
		return false
	}
	
	//Bind events to appropriate links.
	$('#signup-link').bind('click', toggleSignup).append(' &darr;');
	// $('#signup-close').bind('click', hideSignup);

	//Make the 'Email Address' label in the input field dissapear and reappear when appropriate.
	$('#signup-form input').bind(
		'focus', 
		function () {
			var selector = 'label[for='+ $(this).attr('name') +']';
			if(this.value == $(selector).html()){
				this.value='';
				this.style.color='#000';
			}
		}
	).bind(
		'blur',
		function () {
			var selector = 'label[for='+ $(this).attr('name') +']';
			if(this.value==''){
				this.style.color='#aaa';
				this.value= $(selector).html();
			}
		}
	);

	
	$('#signup-form label[for=email]').hide();
	$('#signup-form form').bind('submit', function () {$.cookie('signup-seenit', 'true')});
	//Show the signup form automatically if they've never seen it before.
	window.showSignup = showSignup
	if ($.cookie('signup-seenit')!='true') {
		setTimeout("showSignup();", 2000);
	}
	$('#newsletter-signup input').each(function () {
		var selector = 'label[for='+ $(this).attr('name') +']';
		if (this.value == "") {
			this.value = $(selector).html();
			$(this).addClass('greyed');
		}
	});
	
});
