function openLightbox()
{
	$.openDOMWindow({ 
		borderSize:'0',
		width:'619',
		height:'440',
		modal:1,
		overlayOpacity:'75',
		windowPadding:0,
		windowSource:'iframe',
		windowSourceURL:'/tell-a-friend.php' 
	}); 
	return false;
}
function openBadgesLightbox()
{
	$.openDOMWindow({ 
		borderSize:'0',
		width:'640',
		height:'700',
		modal:1,
		overlayOpacity:'75',
		windowPadding:0,
		windowSource:'iframe',
		windowSourceURL:'/download-badges.php' 
	}); 
	return false;
}
function closeLightbox()
{
	$.closeDOMWindow();
	return false;
}
function openFBConnect()
{
	var fb_id = '';
	var name = '';
	var city = '';
	var state = '';
	$.ajax({
		type: 'POST',
		url: '/includes/ajax_funcs.php',
		data: 'action=cookie',
		success: function(result)
		{
			if(result == 'signed') // change to 'signed' to check for cookie
			{
				showCookiePage();
			}
			else
			{
				FB_RequireFeatures(["XFBML"], function()
				{
					FB.Facebook.init('23ea78454426d6d34d5f2097a7306568', "/xd_receiver.htm");
					FB.ensureInit(function()
					{
						FB.Connect.get_status().waitUntilReady(function(status)
						{
							switch (status)
							{
								case FB.ConnectState.connected:
									userIsLoggedIn();
								break;

								case FB.ConnectState.appNotAuthorized:
								case FB.ConnectState.userNotLoggedIn:
									FB.Connect.requireSession(function()
									{
										userIsLoggedIn();
									});
								break;
							}
						});
					});
				});
			}
		}
	});
	return false;
}
function userIsLoggedIn()
{
	if(FB.Connect.get_loggedInUser() != null)
	{
		FB.Facebook.apiClient.users_getInfo(FB.Connect.get_loggedInUser(), new Array('name','current_location','hometown_location','affiliations'), function(result)
		{
			if(result != null)
			{
				var current_location = result[0]['current_location'];
				var hometown_location = result[0]['hometown_location'];
				var affiliations = result[0]['affiliations'];
				fb_id = result[0]['uid'];
				name = result[0]['name'];

				$.ajax({
					type: 'POST',
					url: '/includes/ajax_funcs.php',
					data: 'action=fb&id=' + fb_id,
					success: function(result)
					{
						if(result == 'signed')
						{
							showCookiePage();
						}
						else
						{
							if(current_location)
							{
								city = current_location['city'];
								state = current_location['state'];
							}
							else if(hometown_location)
							{
								city = hometown_location['city'];
								state = hometown_location['state'];
							}
							else
							{
								if(affiliations != '')
								{
									var num = affiliations.length;
									for(var i=0; i<num; i++)
									{
										if(affiliations[i]['type'] == 'region')
										{
											location = affiliations[i]['name'].split(',');
											city = location[0];
											state = location[1].replace(/^\s+|\s+$/g,"");
										}
									}
								}
							}
							FB.Connect.streamPublish('', {'name':'I Raised My Hand For Chocolate Milk!','href':'http://www.raiseyourhand4milk.com','description':'I know that milk is important for children\'s health and kids will drink more when it\'s chocolate. Do you agree? Come sign the petition and raise your hand to show your support and help reinforce the importance of giving kids nutrient-rich options like lowfat chocolate milk at home and in school.','media':
							  [{'type': 'image',
							    'src':  'http://www.raiseyourhand4milk.com/images/raiseyourhand.png', 
							    'href': 'http://apps.facebook.com/raiseyourhandformilk/'}]}, '', '', 'Tell your friends...',
							function()
							{
								if(document.getElementById('flash'))
								{
									var url = 'url=http://www.raiseyourhand4milk.com/?v=signed';
								}
								else
								{
									var url = 'url=http://www.raiseyourhand4milk.com/facebook/?v=signed'
								}
								$.ajax({
									type: 'POST',
									url: '/includes/friend_invite.php',
									data: url,
									success: function(result)
									{
										var dialog = new FB.UI.FBMLPopupDialog('Invite your friends to join Raise Your Hand for Milk', result, true, true);
										dialog.setContentWidth(625);
										dialog.set_placement(FB.UI.PopupPlacement.topCenter);
										dialog.show();
									}
								});
							}
							);
							insertIntoDB(fb_id, name, '', city, state, '', 'facebook');
						}
					}
				});
			}
			else
			{
				FB.Connect.forceSessionRefresh();
				FB.Connect.logout(function()
				{
					alert('Facebook encountered an error, please try again. If you continue to see this error, try refreshing your browser.');
				});
			}	
		});
	}
	else
	{
		FB.Connect.forceSessionRefresh();
		FB.Connect.logout(function()
		{
			alert('Facebook encountered an error, please try again. If you continue to see this error, try refreshing your browser.');
		});
	}
}
function insertIntoDB(fb_id, name, email, city, state, opt, signed_from)
{
	$.ajax({
		type: 'POST',
		url: '/includes/ajax_funcs.php',
		data: 'action=cookie',
		success: function(result)
		{
			if(result == 'signed') // change to 'signed' to check for cookie
			{
				showCookiePage();
			}
			else
			{
				$.ajax({
					type: 'POST',
					url: '/includes/ajax_funcs.php',
					data: 'action=insert&fb_id=' + fb_id + '&name=' + name + '&email=' + email + '&city=' + city + '&state=' + state + '&opt=' + opt + '&signed_from=' + signed_from,
					success: function(result)
					{
						returnTheAmount();
					}
				});
			}
		}
	});
}
function returnTheAmount()
{
	$.ajax({
		type: 'POST',
		url: '/includes/ajax_funcs.php',
		data: 'action=get',
		success: function(result)
		{
			document.getElementById('flash').sendToFlash(result);
		}
	});
}
function showCookiePage()
{
	$.ajax({
		type: 'POST',
		url: '/includes/ajax_funcs.php',
		data: 'action=get',
		success: function(result)
		{
			document.getElementById('flash').checkForCookie(result);
		}
	});
}
function positionFooter()
{
	if($('#container').height() + $('#footer').height() < $(window).height())
	{
		$('#footer').css({'position':'absolute','top':parseInt($('body').height() - $('#footer').height()) + 'px'}).addClass('footerIE6');
	}
	else
	{
		$('#footer').css({'position':'relative','top':'0px'}).removeClass('footerIE6');
	}
}
function showYoutubeVideo()
{
	$('#video-container').html('<object width="342" height="281"><param name="movie" value="http://www.youtube.com/v/RjKFlusW-Xc&rel=0&autoplay=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/RjKFlusW-Xc&rel=0&autoplay=1" type="application/x-shockwave-flash" wmode="transparent" width="342" height="281"></embed></object>');
	return false;
}
function showSmallYoutubeVideo()
{
	$('#video-container-sm').html('<object width="240" height="205"><param name="movie" value="http://www.youtube.com/v/RjKFlusW-Xc&rel=0&autoplay=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/RjKFlusW-Xc&rel=0&autoplay=1" type="application/x-shockwave-flash" wmode="transparent" width="240" height="205"></embed></object>');
	return false;
}
$(document).ready(function()
{
	positionFooter();
	$(window).scroll(function()
	{
		positionFooter();
	});
	$(window).resize(function()
	{
		positionFooter();
	});
})