/* $Id: dyp.js 15800 2009-03-27 13:38:54Z 16872 $ */
document.observe('dom:loaded', function(e) {
	// External links open in a new window
	$$('a[rel="external"]').invoke('writeAttribute', 'target', '_blank');
	
	if($('login_form')) {
		$('login_form_submit').observe('click', function(e) {
			Event.stop(e);
			new Ajax.Request( 'getSalt.php', {
				method: 'post',
				parameters: { 'email': $('email').value },
				onSuccess: function( response ) {
					json = response.responseJSON;
					salt = json.salt;
					$('hash').value = hex_sha1(hex_sha1($('password').value+salt)+$('tempsalt').value+$('timestamp').value);
						$('password').value = "";
					new Ajax.Request( 'dologin.php', {
						method: 'post',
						parameters: { 'email':$('email').value, 'hash':$('hash').value },
							onFailure: function( transport ) {
								location.href = $('login_form').action;
							},
							onSuccess: function(response) {
		     					if (response.responseJSON) {
		     						json = response.responseJSON;
      								if ( json.success ) {
      									$('loggedin_name').update(json.name);
      									
      									$('notloggedin').fade({duration: 1});
										$('loggedin').appear({delay: 1});
      								}
      								else {
      									location.href = $('login_form').action;
      								}
							}
						}
					});
				}
			})
		});
	}
	
	if($('footprint')) {
		$$('#footprint .content').reduce().wrap('div', { 'class': 'inner' }).wrap('div', { 'class': 'inner' });
	}
	
	if($('primary')) {
		if(!$(document.body).hasClassName('footprintless')) {
			$('primary').setStyle({
				/*@cc_on
				/*@if (@_jscript_version < 5.7)
					background: 'url(./images/dropshadow/nontrans/leftbottom.png) no-repeat left top',
				@else @*/
					background: 'url(./images/dropshadow/leftbottom.png) no-repeat left top',
				/*@end
				@*/					  
				padding: '14px 0 0' });
		}
		
		if($('tracking')) {
			$('tracking').setStyle({ 
				/*@cc_on
				/*@if (@_jscript_version < 5.7)
					background: 'url(./' + P_PATHPREFIX + 'images/dropshadow/nontrans/top.png) no-repeat left top',
				@else @*/
					background: 'url(./' + P_PATHPREFIX + 'images/dropshadow/top.png) no-repeat left top',
				/*@end
				@*/
				textIndent: '-9999px',
				margin: '0',
				height: '30px' });
		}
	}
	
	if($('secondary')) {
		if(!$(document.body).hasClassName('footprintless')) {
			$('secondary').insert({ top: '<div class="topleftcorner"></div>' });
		}
		else
		{
			$('main').setStyle({
				/*@cc_on
				/*@if (@_jscript_version < 5.7)
					background: 'url(./images/dropshadow/nontrans/righttop.png) no-repeat right top',
				@else @*/
					background: 'url(./images/dropshadow/righttop.png) no-repeat right top',
				/*@end
				@*/
				padding: '14px 0 0' });
		}
		
		$$('#secondary .content').reduce().wrap('div', { 'class': 'inner' }).wrap('div', { 'class': 'inner' });
		$('secondary').setStyle({
		/*@cc_on
		/*@if (@_jscript_version < 5.7)
				background: 'url(./images/dropshadow/nontrans/rightbottom.png) no-repeat left bottom',
			@else @*/
				background: 'url(./images/dropshadow/rightbottom.png) no-repeat left bottom',
		/*@end
		@*/
				padding: '0 0 82px' });
	}
	
	if($('main_nav') && $('map_tools')) {
		$('explore').observe('click', function(e) {
			Event.stop(e);
			if(!$('map_tools').visible()) {
				$(document.body).writeAttribute('id', 'exploring');
				new Effect.Parallel([new Effect.Appear($('map_tools')), 
					new Effect.Appear($('explore_hints'))], {duration: 0.5});
			}
			$('map_wrapper').scrollTo();
		});
		
		if(document.location.hash !== "#explore") {
			$('map_tools').hide();
			$('explore_hints').hide();
		}
		else {
			$(document.body).writeAttribute('id', 'exploring');
		}
		
		$('map_tools').insert({top: '<a id="close_map_tools" href="index.php">close</a>'});
		$('close_map_tools').observe('click', function(e) {
			Event.stop(e);
			$('map_tools').hide();
			$('explore_hints').hide();
		});
	}
	
	if ( $('privacy_link1') ) 
	{
		$('privacy_link1').observe( 'click', function(e)
			{
				Event.stop(e);
				window.open('privacy.php', 'Privacy', 'height=600,width=800,resizable=no,scrollbars=yes');
			});
	}
	
	if ( $('privacy_link2') )
	{
		$('privacy_link2').observe( 'click', function(e)
			{
				Event.stop(e);
				window.open('privacy.php', 'Privacy', 'height=600,width=800,resizable=no,scrollbars=yes');
			});
	}
	
	if ( $('join') )
	{
		$('join').observe( 'click', function()
		{
			if ( typeof(P_REQUESTURI) !== 'undefined' )
			{
				$('join').href += "?group="+P_REQUESTURI;
			}
		}
		);
	}
	
	if ( $('signup') )
	{
		$('signup').observe('click',function()
		{
			if ( typeof(P_REQUESTURI) !== 'undefined' )
			{
				$('signup').href += "?group="+P_REQUESTURI;
			}
		}
		);
	}
	
	var signinForm = $('signin');
	if(signinForm) {
		signinForm.observe('submit', function(e) {
			var groupSelect = $('group');
			groupSelect.writeAttribute('multiple', 'multiple').writeAttribute('size', '1').writeAttribute('name', 'group[]');
			var groupName = document.location.href.toQueryParams().group;
			groupSelect.insert('<option selected="selected" value="' + groupName + '">' + groupName + '</option>');
		});
	}
	
	/*@cc_on
	/*@if (@_jscript_version < 5.7)
		$$('.top_of_page + *').invoke('setStyle', { clear: 'right' });
		$$('#main_nav + h2').invoke('setStyle', { marginBottom: 0 });
	/*@end
	@*/
	
	var flippers = $$('.flipper');
	if(flippers.size() > 0) {
		flippers.invoke('observe', 'click', function(e) {
			Event.stop(e);
			var target = Event.element(e);
			var el = $(target.hash.substr(1));
			if(target.hasClassName('closed')) {
				el.show();
				target.removeClassName('closed');
			} else {
				el.hide();
				target.addClassName('closed');
			}
		});
	}
	
	var showprogressbuttons = $$('.showprogress');
	if(showprogressbuttons.size() > 0) {
		showprogressbuttons.invoke('observe', 'click', function(e) {
			Event.stop(e);
			var target = Event.element(e);
			var data = new String(target.href).toQueryParams();
			var footprints = $('footprinting85');
			footprints.SetVariable('/:pledge', data.pledge);
			footprints.SetVariable('/:goal', data.goal);
			footprints.Rewind();
			$$('.selected').invoke('removeClassName', 'selected');
			target.up().previous().down().addClassName('selected');
		});
	}
});

