AARP.Email = {
	__version__: ''
	, email_validation: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
	, mail_template: ''
	, generalStatus: ''
	, currentOverlay: ''
	, overlayToPrefix: {}
	, formToPrefix: {}
	
	, open: function( event )
	{
		var overlay = 'emailOverlay';
		var form = 'emailContentForm';
		var prefix = 'email';
		
		if ( arguments.length > 1 && arguments[1] )
			overlay = arguments[1];
		if ( arguments.length > 2 && arguments[2] )
			form = arguments[2];
		if ( arguments.length > 3 && arguments[3] )
			prefix = arguments[3];
		
		AARP.Email.currentOverlay = overlay;
		AARP.Email.currentForm = form;
		
		AARP.Email.overlayToPrefix[overlay] = prefix;
		AARP.Email.formToPrefix[form] = prefix;
		
		var objOverlay = $( overlay );
		objOverlay.style.display = 'inline';
		
		// event sent as parameter, so align with pointer
		/* if ( arguments[0] )
		{
			var ev = arguments[0];
			$( 'emailOverlay' ).style.top = ev.clientY;
			$( 'emailOverlay' ).style.left = ev.clientX;
		} */
		
		// NEW [2008-03-17 | kaiser] keep dialog within view.
		/*
		var winX = 0, winY = 0;
		var divW = 0, divH = 0;
		
		if ( window.innerHeight )
		{
			winX = window.innerWidth;
			winY = window.innerHeight;
			divW = objOverlay.innerWidth;
			divX = objOverlay.innerHeight;
		}
		else
		{
			winX = document.body.clientWidth;
			winY = document.body.clientHeight;
			divW = objOverlay.clientWidth;
			divX = objOverlay.clientHeight;
		}
		
		var midX = Math.floor( winX / 2 );
		var midY = Math.floor( winY / 2 );
		var divMidX = Math.floor( divX / 2 );
		var divMidY = Math.floor( divW / 2 );
		
		objOverlay.style.left = (midX - divMidX) + 'px';
		objOverlay.style.right = (midY - divMidY) + 'px';
		*/
		
		// NOTE: Position is deprecated in prototype 1.6--we're using 1.5 right now
		var cur = Position.cumulativeOffset( objOverlay );
		var off = Position.page( objOverlay );
		
		// only reposition if the object is far above (denoted by a negative position relative
		// to page, or cur[1]), or when object is far below (we'll say 200 pixels at least).
		if ( off[1] < 0 )
			objOverlay.style.top = (cur[1] - off[1]) + 'px';
		else if ( off[1] > 200 )
			objOverlay.style.top = '200px';
		
		Effect.Appear( overlay );
	}
	, send: function()
	{
		var prefix = 'email';
		
		if ( arguments.length > 0 && arguments[0] )
		{
			AARP.Email.currentForm = arguments[0];
			prefix = AARP.Email.formToPrefix[arguments[0]];
		}
		
		// this first check indicates that this is on a non-community page (presumably an article).
		// fill in fields if true.
		
		var ctypename = $( prefix + '.contentTypeName' );
		if ( ctypename && ctypename.value == "article" )
		{
			var em = $( prefix + '.from' ).value;
			var emc = AARP.daw.crypt_text( em );
			var url = AARP.page.protocol + '://' + AARP.page.host + ':' + AARP.page.port + AARP.page.path + '/' + AARP.page.file;
			//if ( AARP.page.is_authoring ) alert( "redirect=" + url );
			$( prefix + '.status' ).style.display = 'none';
			var req = $( prefix + '.required' );
			
			// repository id gives unique value
			//$( prefix + '.contentId' ).value = AARPAds.pgid;
			$( prefix + '.contentTitle' ).value = document.title;
			$( prefix + '.permalinkUrl' ).value = url;
			$( prefix + '.redirect' ).value = url + '?generalStatus=' + AARP.Email.generalStatus;
			
			// TODO: when form validation is built, use that instead of this
			var msgs = [];
			if ( !em.match( AARP.Email.email_validation ) )
				msgs.push( "From is invalid" );
			
			// split up recipients list and validate each email;
			// show the addresses that aren't valid.
			
			var tmp = $( prefix + '.to' ).value.trim().split( ',' );
			var recipients = [];
			var rinvalid = [];
			for ( var i = 0; i < tmp.length; i++ )
			{
				if ( tmp[i].match( AARP.Email.email_validation ) )
					recipients.push( AARP.daw.crypt_text( tmp[i].trim() ) );
				else
					rinvalid.push( tmp[i] );
			}
			if ( rinvalid.length > 0 )
				msgs.push( 'These recipients are invalid: ' + rinvalid.join( ", " ) );
			else
			{
				// limit # of recipients after processing copyme
				if ( $( prefix + '.copyme' ).checked ) recipients.push( emc );
				if ( recipients.length > 5 )
					msgs.push( "You can send emails up to 5 people at a time (including yourself). You currently have " + recipients.length ) + " people on your list";
			}
			
			if ( req && req.value.match( /emailContentMessage/ ) )
			{
				var _m = $( prefix + '.message' );
				if ( _m.value.trim() == '' )
					msgs.push( "Message must not be empty." );
			}
			
			if ( msgs.length > 0 )
			{
				Effect.Shake( AARP.Email.currentOverlay );
				$( prefix + '.status' ).innerHTML = "The following errors occured:<br />&ndash; " + msgs.join( '<br />&ndash; ' );
				$( prefix + '.status' ).style.display = 'block';
			}
			else
			{
				var realname = $( prefix + '.fromName' ).value.trim();
				if ( realname == '' ) realname = em;
				
				//$( prefix + '.email' ).disabled = true;
				$( prefix + '.email' ).value = em;
				$( prefix + '.realname' ).value = $( prefix + '.fromName' ).value = realname;
				$( prefix + '.recipients' ).value = recipients.join( ',' );
				$( prefix + '.mail_template' ).value = AARP.Email.mail_template;
				$( prefix + '.subject' ).value = 'AARP.org Article from ' + em;
				$( AARP.Email.currentForm ).submit();
			}
		}
	}
	
	, close: function()
	{
		if ( arguments.length > 0 )
			AARP.Email.currentOverlay = arguments[0];
		
		// hide error messages
		var prefix = AARP.Email.overlayToPrefix[AARP.Email.currentOverlay];
		$( prefix + '.status' ).style.display = 'none';
		
		Effect.Squish( AARP.Email.currentOverlay );
	}
}

AARP.Email.mail_template = AARP.daw.crypt_text( 'http://assets.aarp.org/aarp.org_/misc/formmail_email-tpl_article.html' );
AARP.Email.generalStatus = escape( 'Thanks! Your email has been sent.' );

AARP.Favorite = {
	__version__: ''
	
	// need two parameters: stringURL and articleTitle
	, Article: {
		submitURL: '/community/articleFavorite/submit.bt'
		, checkURL: '/community/articleFavorite/validate.bt'
		, open: function()
		{
			$( 'favorite.title' ).innerHTML = document.title;
			overlayConfirmation('favoriteContentOverlay');
		}
		, save: function()
		{
			if ( !AARP.User.isLoggedIn )
			{
				// TODO: make a nicer way of showing error/status message
				alert( "You must be logged in." );
				return;
			}
			
			// TODO: does one save the proxy path or the repository path?
			// TODO: is it right to be using document.title going forward?
			var params = 'articleURL=' + AARP.page.path + '/' + AARP.page.file
				+ '&articleTitle=' + escape( document.title )
				;
			
			new Ajax.Request(
				AARP.Favorite.Article.submitURL
				, { method: 'get', parameters: params, onSuccess: AARP.Favorite.Article.save__onSuccess }
			);
		}
		, save__onSuccess: function( response )
		{
			// TODO: check response for status code
			//alert(response.responseText.trim());
			AARP.generalStatus( 'This has been saved to your Favorite Articles.' );
			closeOverlayConfirmation();
		}
		, unsave: function()
		{
			
		}
		/*
		check that the current article is already saved? use Cookies to
		cache this info (maybe do it per session).
		*/
		// TODO: parameter check to see if an external method is explicitly forcing an action
		, check: function()
		{
			var page = AARP.page.path + '/' + AARP.page.file;
			var params = 'articleID=' + page + '&memberID=' + AARP.User.name;
			
			new Ajax.Request(
				AARP.Favorite.Article.checkURL
				, { method: 'post', data: params, onSuccess: AARP.Favorite.Article.check__onSuccess }
			);
			
			/* var article = Cookies.get( 'favorite.article.' + page;
			
			// not saved for this session
			if ( article == null )
			{
				
			} */
			
		}
		, check__onSuccess: function( response )
		{
			var val = response.responseText.trim().toLowerCase();
			if ( val == "true" )
			{
				// TODO: disable bookmark link and indicate that this is already saved
			}
		}
	}
}