$(document).ready(function(){

	$('div.full-text').hide();
	var readmore_orig = $('p.readmore a').html();

	$('p.readmore a').click(function(e){
		$this = $(this);
		e.preventDefault();
		$('div.full-text').css('width', $('div.full-text').parent().width()+'px');
		$('div.full-text').slideToggle(300, function(){
			if ( $('p.readmore a').html() == readmore_orig ) {
				$('p.readmore a').html('Close text');
			} else {
				$('p.readmore a').html(readmore_orig);
// 				$target = $this;
// 				st = $target.offset().top - 100;
// 				$('html, body').animate({ scrollTop: st}, 300);
			}
		});
	});

	
// 	adjustHeights = function() {
// 		if( $('div.level3').length > 0 ) {
// 			var fullHeight = $('div.level3').offset().top + $('div.level3').outerHeight();
// 			$('div.level1').height( fullHeight );
// 			$('div.level2').height( fullHeight - $('div.level2').offset().top );
// 		} else if( $('div.level2').length > 0 ) {
// 			var fullHeight = $('div.level2').offset().top + $('div.level2').height();
// 			$('div.level1').height(fullHeight).css('padding-bottom',0);
// 		}
// 	}
// 	setTimeout(adjustHeights,100);
	
	
	function adjustHeights() {
	
		var maxHeight = 0;

		var level = 1;
		
		while( $('div.level'+level).length > 0 ) {
			$currentLevel = $('div.level'+level);
			currentHeight = $currentLevel.height() + parseInt($currentLevel.css('top')) + parseInt($currentLevel.css('padding-top'));
			if(currentHeight > maxHeight) maxHeight = currentHeight;
			level++;
			
			console.log(maxHeight);
		}
	
		var level = 1;
		while( $('div.level'+level).length > 0 ) {
			$currentLevel = $('div.level'+level);
			
			var newHeight = maxHeight - ( parseInt($currentLevel.css('top')) + parseInt($currentLevel.css('padding-top')) );
			
			$currentLevel.css('height', newHeight );
			level++;
		}
	
	}
	
	adjustHeights();
	
	
	function hidePaypal() {
		$('div.paypal_form').hide();
		$('a.paypal_link').show();
		$('a.paypal_link').click(function(){
			var pp_id = $(this).attr('href');
			var theForm = $(pp_id).children('form');
			var theForm = theForm[0];
			$(theForm).submit();
			return true;
		});
	};	

	hidePaypal();
	


	function validateEmail(email){
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		if( filter.test(email) ) return true;
		else return false;
	}

	
	$('#id_newsletter').submit(function(e) {
		$('p#message').html('');
		// e.preventDefault();
		if(! validateEmail( $('#id_email').attr('value') ) ) {
			$('p#message').html('Please enter a valid email address.');
			return false;
		} else {
			return true;
// 			var postdata =  $("#id_newsletter").serialize();
//       		$.post('http://lists.internationalprojectspace.org/?p=subscribe&id=8', postdata, function(data) {
// 				console.log(data);
//         	});
		}
	});
	
	
});
