function FormatBox()
{
	var e = GetElement( "sitenamespan" ), 
		f = GetElement( "sitename" ); // div

	if ( e != null && f != null )
	{
		// if exceeds height, reduce font size
		if ( e.offsetHeight + 20 >= f.offsetHeight )
		{
			e.style.fontSize.match( /(\d+)/ );
			if ( RegExp.$1 != null )
				e.style.fontSize = (parseInt( RegExp.$1 ) / 2) + "pt";
		}

		e.style.position = "relative";
		e.style.top = ((f.offsetHeight - e.offsetHeight) / 2) + "px";
	}
}

function EqualHeight( a  )
{
	var bottompixel = 0,
		i,
		b = [],
		e = [];
		
	for ( i = 0 ; i < a.length ; i++ )
	{
		e[ i ] = GetElement( a[ i ] );
		b[ i ] = ParentOffset( e[ i ] );
		bottompixel = Math.max( bottompixel, b[ i ][ 1 ] + e[ i ].offsetTop + e[ i ].offsetHeight );
	}

	for ( i = 0 ; i < a.length ; i++ )
		e[ i ].style.height = (bottompixel - b[ i ][ 1 ] - e[ i ].offsetTop) + "px";

	return false;
}

function Trim( d )
{
	if ( typeof( d ) != 'string' )
		return d;
		
	while ( d.charAt( 0 ) == ' ' || d.charCodeAt( 0 ) == 160 )
		d = d.substr( 1 );
	while ( d.charAt( d.length - 1 ) == ' ' || d.charCodeAt( 0 ) == 160 )
		d = d.substr( 0, d.length - 1 );
			
	return d;
}

function MakeSponsorLine( n )
{
	var s = GetElement( "line" + n ).value
	
	if ( s == null || s == "" )
		return "&nbsp;";
		
	s = s.replace( /</g, "&lt;" ).replace( />/g, "&gt;" );
	
	if ( n == 2 )
	{
		var link = GetElement( "line2link" ).value;
		if ( link != null && link != "" )
		{
			link = Trim( link );
			if ( link != "" )
				s = "<a href=" + link + ">" + s + "</a>";
		}
	}

	if ( GetElement( "italic" + n ).checked )
		s = "<em>" + s + "</em>"
		
	if ( GetElement( "strong" + n ).checked )
		s = "<strong>" + s + "</strong>"
		
		
	return s;
}

function SponsorPreview()
{
	var previewdiv = GetElement( "previewdiv" );
	
	if ( GetElement( "radiosilver" ).checked )
	{
		GetElement( "radiosilver" ).checked = true;
		previewdiv.className = "sponsorstandard";
		GetElement( "photorow" ).style.display = "none";
	}
	else if ( GetElement( "radiogold" ).checked )
	{
		previewdiv.className = "sponsorlarge";
		GetElement( "photorow" ).style.display = "";
	}
	else
	{
		previewdiv.className = "sponsorplatinum";
		GetElement( "photorow" ).style.display = "";
	}
	
	for ( var i = 1 ; i <= 4 ; i++ )
	{
		var line = MakeSponsorLine( i );
		if ( GetElement( "preview" + i ).innerHTML != line )	
			GetElement( "preview" + i ).innerHTML = line;
	}
}

var tracking = null;
function Track( n, begin )
{
	if ( tracking > 0 )
	{
		clearInterval( tracking );
		tracking = null;
	}
		
	if ( begin )
		tracking = setInterval( "SponsorPreview();", 500 );
}

function ClassmateProfileDirectoryFields( b )
{
	if ( b )
	{
		// NONE clicked
		if ( GetElement( "directoryfield4" ).checked )
		{
			GetElement( "directoryfield1" ).checked = false;
			GetElement( "directoryfield2" ).checked = false;
			GetElement( "directoryfield3" ).checked = false;
		}
	}
	else
	{
		// something else clicked
		GetElement( "directoryfield4" ).checked = 
			!GetElement( "directoryfield1" ).checked && !GetElement( "directoryfield2" ).checked && !GetElement( "directoryfield3" ).checked;
	}
}

function SetClassmateProfileType( t )
{
	switch ( t )
	{
		default:	
			break;
		
		case 1:
		{
			$(".attendanceplan").show();
			$(".classmatesnamelink").hide();
			$(".classmatecommenttitle").show();
			$(".classmateeditprofilelink").show();
			break;
		}
	}
	
	CheckForCurrentUser();
}
	
function CheckForCurrentUser()
{
	$("input[name=iscurrentuser]").each(
		function()
		{
			var t = $(this),
				uid = t.val(),
				classmateentry = t.parents( ".classmateentry" );
				
			if ( classmateentry.size() == 0 )
				classmateentry = t.parents( ".classmateprofile" );
			
			classmateentry.find( ".classmateemail" ).hide(); // don't allow email send to self
			//classmateentry.append( "<div class=\"editmyprofile\"><a href=\"../main/myreunion.aspx?siteid=" + $("#siteid").val() + "&showprofile=1\">Edit My Profile</a></div>" );
			classmateentry.append( "<div class=\"editmyprofile\">" +
				"<a class=\"iconlink\" href=\"../main/myreunion.aspx?siteid=" + $("#siteid").val() + "&showprofile=1\">" +
				"<img src=\"/cqrfiles/templates/images/icons/" + $("input[name=icongroup]").val() + "/editmyprofile.png\" />" +
				"</a>" +
				"<a class=\"iconlink\" href=\"../main/myreunion.aspx?siteid=" + $("#siteid").val() + "\">" +
				"<img src=\"/cqrfiles/templates/images/icons/" + $("input[name=icongroup]").val() + "/myreunion.png\" />" +
				"</a>" +
				 "</div>" );
			$("#isthisyourprofile").hide();
		}
	);
}

function SponsorOn()
{
	$(".withad").show();
	return true;
}

function SponsorOff()
{
	$(".withad").hide();
	return true;
}

function SponsorPreview()
{
	var previewdiv = GetElement( "previewdiv" );
	
	if ( GetElement( "radiosilver" ).checked )
	{
		GetElement( "radiosilver" ).checked = true;
		previewdiv.className = "sponsorstandard";
		GetElement( "photorow" ).style.display = "none";
	}
	else if ( GetElement( "radiogold" ).checked )
	{
		previewdiv.className = "sponsorlarge";
		GetElement( "photorow" ).style.display = "";
	}
	else
	{
		previewdiv.className = "sponsorplatinum";
		GetElement( "photorow" ).style.display = "";
	}
	
	for ( var i = 1 ; i <= 4 ; i++ )
	{
		var line = MakeSponsorLine( i );
		if ( GetElement( "preview" + i ).innerHTML != line )	
			GetElement( "preview" + i ).innerHTML = line;
	}
}

var networkinghelpmessages = null;

function SocialNetworkingLinks()
{
	$("#socialnetworkingdiv").empty();
	$("#editsocialnetworking input").each(
		function()
		{
			var input = $(this),
				link = input.val();
			
			if ( link != null && link.indexOf( "." ) > 0 )
			{
				var plain = link.replace( /http:\/\/|https:\/\//gi, "" )
				$("#socialnetworkingdiv").append( "<a target=\"_blank\" href=\"http://" + plain + "\">" + plain + "</a> " );
			}
		}
	);
	
	$("a.networkhelp").click(
		function()
		{
			var t = $(this),
				name = t.find( "input" ).val();
		
			function DisplayHelp()
			{
				t.after( 
					"<div class=\"ibox networkhelpmessage\">" + 
					"<a href=\"#\" class=\"closer\"><img src=\"/cqrfiles/images/closebutton.gif\" /></a>" +
					networkinghelpmessages[ name ] + 
					"</div>" );
				t.hide();
				
				$( ".networkhelpmessage a.closer" )
					.hover(
						function() { $(this).find( "img" ).get( 0 ).src = "/cqrfiles/images/closebuttonhover.gif"; },
						function() { $(this).find( "img" ).get( 0 ).src = "/cqrfiles/images/closebutton.gif"; }
					)
					.click(
						function()
						{
							t.show();
							$(this).parent().remove();
							return false;
						}
					);
			}
				
			if ( networkinghelpmessages == null )
				Ajax( "myreunionhelp", "helpmessages", { },
					function( xml )
					{
						networkinghelpmessages = [];
						$(xml).find( "reply > helpmessage" ).each(
							function()
							{
								var t = $(this),
									name = t.attr( "name" ),
									message = t.find( "message" ).text();
									
								networkinghelpmessages[ name ] = message;
							}
						);
						setTimeout( DisplayHelp, 0 );
					}
				);
			else
				setTimeout( DisplayHelp, 0 );
			return false;
		}
	);
	
	return true;
}

function PreloadRegister()
{
	if ( $("input[name=issiteuser]").val() != "1" && $("input[name=preloadedprofiles]").val() == "1" )
	{
		var securesite = $("input[name=securesiteregister]").val(),
			icongroup = $("input[name=icongroup]").val();
		
		$(".classmateentry").each(
			function()
			{
				var t = $(this),
					namediv = $(this).find( ".classmatename" ),
					confirmed = (namediv.find( "input[name=confirmed]" ).val() == "1"),
					deceased = (namediv.find( "input[name=deceased]" ).val() == "1");
					
				if ( !confirmed && !deceased )
				{
					var firstname = namediv.find( "input[name=firstname]" ).val(),
						lastname = namediv.find( "input[name=lastname]" ).val(),
						identity = namediv.find( "input[name=identity]" ).val();
						
					t.append( 
						"<div class=\"preloadregister\">" +
							"<a href=\"#\" class=\"iconlink\" onclick=\"$(this).next().slideToggle();return false;\">" +
							(icongroup == null ? "This is me!" : "<img title=\"Click if this is you!\" src=\"/cqrfiles/templates/images/icons/" + icongroup + "/thisisme.png\" />") +
							"</a>" +
							"<div class=\"preloadregisternow ibox\">" +
								"<div class=\"h4\">Welcome!</div>" +
								"If this is you, we invite you to join your classmates so that you may participate on this site. To register, " +
								"<a class=\"registernow\" href=\"" + securesite + "&firstname=" + firstname + "&identity=" + identity + "&lastname=" + lastname + "\">click here</a>." +
							"</div>" +
						"</div>" );
				}
			}
		);
		
		$(".classmateentry a").hover(
			function()
			{
				$(this).css( { fontWeight: "bolder" } );
			},
			function()
			{
				$(this).css( { fontWeight: "normal" } );
			}
		);
	}
	else
	{
		$(".areyouaclassmatelink").hide();
	}
}

function DisplayClassmateMap( siteid )
{
	setTimeout(
		function()
		{
			var url = "http://maps" + siteid + ".classquest.com";
			
			window.open( url + "/main/classmap.aspx?siteid=" + siteid, "classmap", 
				"toolbar=0,scrollbars=1,titlebar=0,location=0,status=0,statusbar=0,menubar=0,resizable=1,width=890,height=450,left=50,top=50");
			return false;
		},
		0 );
	return false;
}

function AreYouAClassmateSetup()
{
	$("a.areyouaclassmatelink img").eq( 0 ).hover( 
		function()
		{
			var pos = $(this).position();
			$(this).parent().find( ".hoveron" ).css( { top: pos.top, left: pos.left } ).fadeIn();
		},
		function()
		{
		}
	);
	$("a.areyouaclassmatelink img.hoveron").hover( 
		function()
		{
		},
		function()
		{
			$(this).fadeOut();
		}
	);
}

