$(document).ready(function() {

	// footer position

		var posFooter = function() {
			var sectionH = $('section#content').height();
			var footerH = $('footer#footer').height();
			var windowH = $(window).height();
			if (sectionH < (windowH - footerH - 70 - 80)) {
				$('footer#footer').css('position', 'absolute');
			} else {
				$('footer#footer').css('position', 'relative');
			}
		}

	// window load

		$(window).load(function() {
			posFooter();
		});

	// window resize

		$(window).resize(function() {
			posFooter();
		});

	// pledge ad rotator

		if($('aside#pledge_ad').text()) {
			var last = $('aside#pledge_ad span.quote_text').length + 1;
			var current = 2 + Math.floor(Math.random() * (last - 1));
			var rotate = function() {
				$('aside#pledge_ad span.quote_text').hide();
				$('aside#pledge_ad span.quote_text:nth-child(' + current + ')').fadeIn();
				var t = setTimeout(rotate, 8500);
				current++;
				if (current > last) { current = 2; }
			}
			rotate();
		}

	// profile grid handler

		$('div.profile_large a.wrapper').mouseenter(function() {
			$(this).children('div.more').fadeIn('fast');
		});
		$('div.profile_large a.wrapper').mouseleave(function() {
			$(this).children('div.more').fadeOut('fast');
		});

	// resize pledge form elements

		$(window).load(function () {
		    $('form#pledge input[type=text], form#pledge input[type=email]').each(function() {
		    	var thisEl = $(this);
		    	var valStart = $(thisEl).val();
		    	var valInit = $(thisEl).val();
		    	var resize = function(pad) {
		    		var valCur = $(thisEl).val();
					$(thisEl).after('<div class="fake_form">');
					$('div.fake_form').text(valCur);
					var w = $('div.fake_form').width() + 4 + pad;
					$(thisEl).width(w);
					$('div.fake_form').remove();
		        }
		    	// initial load
		        resize(0);
		        // on focus
		        $(thisEl).focus(function() {
		            var valCur = $(thisEl).val();
		            if (valCur == valInit) {
		                $(thisEl).val('');
		            }
	            	$(thisEl).removeClass('inputted');
		        });
				// on blur
		        $(thisEl).blur(function() {
		            if ($(thisEl).val() == '') {
		            	$(thisEl).val(valInit);
		            } else {
		            	valInit = $(thisEl).val();
		            }
		            resize(0);
					if ($(thisEl).val() != valStart) {
	            		$(thisEl).addClass('inputted');
	            	}
		        });
		        // on keystroke
		        $(thisEl).keydown(function() {
		            resize(30);
		        });
		    });
		});

    // submit form

	    $('aside#pledge_form div.wrapper form a').click(function() {
			$('aside#pledge_form div.wrapper form').submit();
	    });

	// profile image gallery

		$('aside#profile_gallery div.wrapper ul li img').each(function() {
			$(this).width(460);
		});
		$('aside#profile_gallery div.wrapper ul').before('<div class="tape_top">').before('<div class="tape_bottom">');
	
		var curImg = 0;	
		var nextImg = function() {
			$('aside#profile_gallery div.wrapper ul li').animate({
				opacity: 0
			}, 600, function() {
				$('aside#profile_gallery div.wrapper ul li:nth-child(' + curImg + ')').animate({
					opacity: 1
				}, 1000
				);
			});
			var t = setTimeout(nextImg, 10600);
			curImg++
			if (curImg > $('aside#profile_gallery div.wrapper ul li').length) {
				curImg = 1;
			}
		}
		nextImg();

});
