$(document).ready(function() {
	if ($('#footer').length > 0) footer_slide();
	if ($('a#button_join').length > 0) toggle_signup();
	if ($('#slideshow').length > 0) home_slideshow();
	if ($('#header').length > 0) header_w();
	
	// for short pages, ensure footer sticks to bottom
	drop_footer();
	$(window).bind("resize",drop_footer);
	$(window).bind("resize",header_w);
	if ($('li.flickr').length > 0) flickr_target();
	if ($('ol.commentlist').length > 0) number_comments();
});

function number_comments() {
	$('ol.commentlist li').each(function() {
		var i = $('ol.commentlist li').index(this)+1;
		$(this).prepend('<span class="num">'+i+'.</span>');
	});
}

function flickr_target() { $('li.flickr a:first').attr('target','_blank'); }

function header_w() {
	var w = $(window).width()-152;
	$('#header').css('width',w);
}

function home_slideshow() {
	n = 1;
	$('#slideshow img').each(function() {
		$(this).fadeTo(0,0);
	});
	$('#slideshow img:first').fadeTo(0,1);
	var t = $('#slideshow img').length;
	if (t > 1) setInterval('fade_img()',5000);
}
function fade_img() {
	var t = $('#slideshow img').length;
	$('#slideshow img').eq(n).fadeTo(1000,0);
	if (n < (t-1)) {
		n++;
	} else if (n >= (t-1)) {
		n = 0;
	}
	$('#slideshow img').eq(n).fadeTo(1000,1);
}
function slideup() { $('#email_signup').slideToggle(500); }
function toggle_signup() {
	$('a#button_join').click(function() {
		$('#email_signup').slideToggle(500);
		return false;
	});
	
	$('#email_submit').click(function() {
		var e = $('#email_address').val();
		var email_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!email_exp.test(e) || e == '' || e == ' ') {
			$('p.message').html('<em>Invalid Email Address</em>');
		} else {
			$.ajax({
				url:'/library/ajax/email-signup.php',
				data: ({email:e}),
				type: "POST",
				success: function(msg) {
					if (msg == 'Success') {
						$('#email_signup').html('<p class="msg">Thank you!</p>');
						setTimeout('slideup()',1500);
					} else {
						$('#email_signup').html('<p class="msg">'+msg+'</p>');
					}
				}
			});
		}
		return false;
	});
}
function drop_footer(e) {
	var h  = $(window).height()-230;
	var ch = $('#content_container').height();
	var nh = $('#content').height();
	var sh = $('#sidebar').height();
	if (nh < sh) $('#content').css('min-height',sh+'px');
	$('#content_container').css('min-height',h+'px');
}
function footer_slide() {
	$('a#more').click(function() {
		var t = ($(this).hasClass('less')) ? 0 : -430;
		var h = ($(this).hasClass('less')) ? 233 : 640;
		$('#footer').animate({ height:+h+'px',marginTop:+t+'px' },function() { $('a#more').toggleClass('less'); });
		return false;
	});
}
