var signupFieldSelectTotal = 5;
var signupFieldSelectCount = 0;

var signupRegionSelectTotal = 3;
var signupRegionSelectCount = 0;

// jQuery
$(document).ready(function(){
	
	// adjust the aside so it's always in view
	$(window).scroll(
		function(){
			var absoluteTop = 27;
			var fixedTop = 12;
			var offset = 135;
			var asideHeight = $('aside').height();
			var docHeight = $(document).height();
			var footerHeight = $('body > footer').height();
			var footerOffset = $('body > footer').offset();
			var footerTop = footerOffset.top;
			var winHeight = $(window).height();
			var right = ($('aside').hasClass('right')) ? true : false;
			if (right){
				var pageOffset = $('#page > .wrapper').offset();
				var xoffset = pageOffset.left + 760;
			}
			if ($(this).scrollTop() > offset){
				// don't overlap the footer
				if ($(this).scrollTop() > (footerTop-asideHeight-(2*fixedTop))){
					if (right){
						$('aside').css({'position':'absolute', 'left':'auto', 'right':'0', 'top':'auto', 'bottom':fixedTop+'px'});
					} else {
						$('aside').css({'position':'absolute', 'top':'auto', 'bottom':fixedTop+'px'});
					}
				} else {
					if (right){
						$('aside').css({'position':'fixed', 'left':xoffset+'px', 'right':'auto', 'top':fixedTop+'px', 'bottom':'auto'});
					} else {
						$('aside').css({'position':'fixed', 'top':fixedTop+'px', 'bottom':'auto'});
					}
				}
			} else {
				if (right){
					$('aside').css({'position':'absolute', 'left':'auto', 'right':'0', 'bottom':'auto', 'top':absoluteTop+'px'});
				} else {
					$('aside').css({'position':'absolute', 'bottom':'auto', 'top':absoluteTop+'px'});
				}
			}
		}
	);
	
	// home signup button
	$('#home .signup-next').click(function(){
		$('body > header ul').fadeOut(500);
		$('#home, body > footer .columns').slideUp(750, "easeOutQuart", function(){
			$('#home-slideshow').cycle('stop');
			$('#signupsequence').slideDown(750, "easeOutQuart");
		});
	});
	$('#home p.signup-next').hover(
		function(){
			$(this).find('span.off').css({'display':'none'});
			$(this).find('span.on').css({'display':'block'});
		},
		function(){
			$(this).find('span.on').css({'display':'none'});
			$(this).find('span.off').css({'display':'block'});
		}
	);
	
	/* FIELD selection */
	$('#signupsequence-fields .diagram p.signup-next').click(function(){
		if ($(this).hasClass('active')){
			$('#signupsequence-fields .diagram p.diagram-error').remove();
			$('#signupsequence-set').animate({'left':'-960px'}, 750, "easeOutCubic");
			
			// add the chosen fields to the hidden input on the join form
			var fieldsSelected = Array();
			$('#signupsequence-fields header li').each(function(){
				fieldsSelected.push($(this).attr('class'));
			});
			var fieldsSelectedString = fieldsSelected.join(',');
			$('input#joinfields').val(fieldsSelectedString);
		}
	});
	$('#signupsequence-fields .diagram li').click(function(){
		var regionID = $(this).attr('id');
		var regionName = $(this).text();
		if ($(this).hasClass('selected')){
			$(this).removeClass('selected');
			$('#signupsequence-fields header li.'+regionID).remove();
			$('#signupsequence-fields .diagram p.diagram-error').click();
			signupFieldSelectCount--;
		} else {
			if (signupFieldSelectCount < signupFieldSelectTotal){
				$(this).addClass('selected');
				$('#signupsequence-fields header ul').append('<li class="'+regionID+'">'+regionName+'</li>');
				signupFieldSelectCount++;
			} else {
				$('#signupsequence-fields .diagram p.diagram-error').remove();
				$('#signupsequence-fields .diagram').append('<p class="diagram-error" style="display:none;">Thank you for selecting your focus fields.<br />Click NEXT to proceed.</p>');
				$('#signupsequence-fields .diagram p.diagram-error').fadeIn(250);
			}
		}
		if (signupFieldSelectCount == 0){
			//$('#signupsequence-fields header p span').show();
			$('#signupsequence-fields .diagram p.signup-next').removeClass('active');
		} else {
			//$('#signupsequence-fields header p span').hide();

			$('#signupsequence-fields .diagram p.signup-next').addClass('active');
			if (signupFieldSelectCount == signupFieldSelectTotal){
				$('#signupsequence-fields .diagram p.signup-next strong').fadeIn(250);
			} else {
				$('#signupsequence-fields .diagram p.signup-next strong').fadeOut(250);
			}
		}
	});
	/* REGION selection */
	$('#signupsequence-regions .diagram p.signup-next').click(function(){
		if ($(this).hasClass('active')){
			$('#signupsequence-regions .diagram p.diagram-error').remove();
			$('#signupsequence-set').animate({'left':'-1920px'}, 750, "easeOutCubic");
			
			// add the chosen regions to the hidden input on the join form
			var regionsSelected = Array();
			$('#signupsequence-regions header li').each(function(){
				regionsSelected.push($(this).attr('class'));
			});
			var regionsSelectedString = regionsSelected.join(',');
			$('input#joinregions').val(regionsSelectedString);
			
			var socialIframeSrc = $('#signupsequence-signin iframe').attr('src');
			var socialIframeSrcAdd = '&fields='+$('input#joinfields').val()+'&regions='+regionsSelectedString;
			$('#signupsequence-signin iframe').attr('src', socialIframeSrc+encodeURIComponent(socialIframeSrcAdd));
		}
	});
	$('#signupsequence-regions .diagram li').click(function(){
		var regionID = $(this).attr('id');
		var regionName = $(this).text();
		if ($(this).hasClass('selected')){
			$(this).removeClass('selected');
			$('#signupsequence-regions header li.'+regionID).remove();
			$('#signupsequence-regions .diagram p.diagram-error').click();
			signupRegionSelectCount--;
		} else {
			if (signupRegionSelectCount < signupRegionSelectTotal){
				$(this).addClass('selected');
				$('#signupsequence-regions .diagram p.signup-next').removeClass('active');
				$('#signupsequence-regions header ul').append('<li class="'+regionID+'">'+regionName+'</li>');
				signupRegionSelectCount++;
			} else {
				$('#signupsequence-regions .diagram p.diagram-error').remove();
				$('#signupsequence-regions .diagram').append('<p class="diagram-error" style="display:none;">Thank you for selecting your focus regions.<br />Click NEXT to proceed <em>(almost done!)</em></p>');
				$('#signupsequence-regions .diagram p.diagram-error').fadeIn(250);
			}
		}
		/*if (signupRegionSelectCount == 0){
			$('#signupsequence-regions header p span').show();
		} else {
			$('#signupsequence-regions header p span').hide();	
		}*/
		if (signupRegionSelectCount == signupRegionSelectTotal){
			$('#signupsequence-regions .diagram p.signup-next').addClass('active');
			$('#signupsequence-regions .diagram p.signup-next strong').fadeIn(250);
		} else {
			$('#signupsequence-regions .diagram p.signup-next').removeClass('active');
			$('#signupsequence-regions .diagram p.signup-next strong').fadeOut(250);
		}
	});
	$('p.diagram-error').live('click', function(){
		$(this).fadeOut(250, function(){
			$(this).remove();
		});
	});
	
	// confirmation checkbox logic
	$('#joinform .checkbox-columns input:checkbox').change(function(){
		var total = signupRegionSelectTotal;
		if ($(this).closest('div.checkbox-columns').hasClass('fields')){
			total = signupFieldSelectTotal;
		}
		var count = 0;
		$(this).closest('div.checkbox-columns').find('input:checkbox').each(function(){
			if ($(this).attr('checked') == "checked"){
				count++;
			}
		});
		$(this).closest('div.checkbox-columns').find('input:checkbox').each(function(){
			if (count == total){
				if ($(this).attr('checked') != "checked"){
					$(this).attr('disabled', true);
					$(this).siblings('label').addClass('disabled');
				}
			} else {
				$(this).attr('disabled', false);
				$(this).siblings('label').removeClass('disabled');
			}
		});
	});
	
	// invest button
	$('.investbutton').hover(
		function(){
			$(this).stop(true,false).animate({'width':'200px'}, 500, "easeOutCubic");
		},
		function(){
			$(this).stop(true,false).animate({'width':'95px'}, 500, "easeOutCubic");
		}
	);
	$('form.investnowform').submit(function(){
		var price = $(this).find('input.price').val();
		if (price < 25 || isNaN(price)){
			alert("InVestments must be a minimum of $25.");
			return false;
		}
	});
	
	// aside sections toggle
	$('aside div > a').click(function(){
		$(this).closest('div').addClass('active');
		$(this).closest('div').siblings('div').each(function(){
			$(this).removeClass('active');
		});
		return false;
	});
	
	// FAQs toggle
	$('ul.faqs h3').click(function(){
		if ($(this).hasClass('active')){
			$(this).removeClass('active');
			$(this).siblings('div').slideUp(500, "easeOutCubic");
		//	window.location.hash = 'none';
		//	return false;
		} else {
			$(this).addClass('active');
			$(this).siblings('div').slideDown(500, "easeOutCubic");
		//	window.location.hash = $(this).closest('li').attr('id');
		}
	});
	
	// info tooltips
	$('span.infotooltip').click(function(){
		var pageOffset = $('#page').offset();
		var pageLeft = pageOffset.left;
		var triggerOffset = $(this).offset();
		$('#infotooltip').remove();
		$('#page').append('<blockquote id="infotooltip"><span class="close">close</span><div></div></blockquote>');
		$('#infotooltip div').html($(this).html());
		var tipHeight = $('#infotooltip').outerHeight();
		var tipWidth = $('#infotooltip').outerWidth();
		var newTop = triggerOffset.top-(tipHeight+130);
		var newLeft = triggerOffset.left-(pageLeft+(tipWidth/2)-9);
		$('#infotooltip').css({'top':newTop+'px', 'left':newLeft+'px'});
		$('#infotooltip').fadeIn('250');
	});
	$('#infotooltip .close').live('click',function(){
		$('#infotooltip').remove();
	});
	
	// textarea character count
	$('.charcountset textarea').keyup(function(){
		var limit = 250;
		var charcount = $(this).val().length;
		var remaining = ((limit - charcount) > 0) ? limit-charcount : 0;
		$(this).siblings('p.charcount').html('Characters remaining: '+remaining);
		if (charcount > limit){
			$(this).val($(this).val().substr(0, limit));
 		}
	});

	// password change (hide/show) new password fields
	$('#passwordchange input#joinpasswordcurrent').keyup(function(){
		var charcount = $(this).val().length;
		if (charcount > 0){
			$('#passwordchange .newpasswords').removeClass('empty');
			$('#passwordchange .newpasswords input').each(function(){
				$(this).attr('disabled', false);
			});
		} else {
			$('#passwordchange .newpasswords').addClass('empty');
			$('#passwordchange .newpasswords input').each(function(){
				$(this).attr('disabled', true);
			});
		}
	});
	
	// make sure a pledged amount falls within range
	$('#pledgeform').submit(function(){
		var currentPledge = parseInt($('input#pledge-amount').val());
		var maxPledge = parseInt($('input#pledge-max').val());
		var minPledge = parseInt($('input#pledge-min').val());
		
		if (!$('#pledgetermsconfirm').is(':checked')){
			alert("You must confirm your pledge and agree to the terms and conditions.");
			return false;
		} else if (currentPledge < minPledge){
			alert("Pledges must be a minimum of $"+minPledge+".");
			$('input#pledge-amount').val(minPledge);
			return false;
		} else if (currentPledge > maxPledge){
			alert("Your pledge cannot exceed the remaining requested amount of $"+maxPledge+".");
			$('input#pledge-amount').val(maxPledge);
			return false;
		}
	});
	
	$('a#pledge-initiate').click(function(){
		$('#pledge-confirm-set').show();
		$(this).hide();
		return false;
	});
	
	$('.donate').click(function() {
		$('form#paypaldonate').submit();
	});
	
});

function checkSettingsSelections(){	
	var rtotal = signupRegionSelectTotal;
	var rcount = 0;
	var ftotal = signupFieldSelectTotal;
	var fcount = 0;
	$('#joinform .checkbox-columns.regions input:checkbox').each(function(){
		if ($(this).attr('checked') == "checked"){
			rcount++;
		}
	});
	$('#joinform .checkbox-columns.fields input:checkbox').each(function(){
		if ($(this).attr('checked') == "checked"){
			fcount++;
		}
	});
	$('#joinform .checkbox-columns.regions input:checkbox').each(function(){
		if (rcount == rtotal){
			if ($(this).attr('checked') != "checked"){
				$(this).attr('disabled', true);
				$(this).siblings('label').addClass('disabled');
			}
		} else {
			$(this).attr('disabled', false);
			$(this).siblings('label').removeClass('disabled');
		}
	});
	$('#joinform .checkbox-columns.fields input:checkbox').each(function(){
		if (fcount == ftotal){
			if ($(this).attr('checked') != "checked"){
				$(this).attr('disabled', true);
				$(this).siblings('label').addClass('disabled');
			}
		} else {
			$(this).attr('disabled', false);
			$(this).siblings('label').removeClass('disabled');
		}
	});
};
