$(document).ready(function() { 

	marginAutoFix();

	$('#age').click(function() {
		if ($('#age:checked').length) { 
			$('#age_parent').attr('checked', false);
		}
	});

	$('#age_parent').click(function() {
		if ($('#age_parent:checked').length) { 
			$('#age').attr('checked', false);
		}
	});
	
	$(window).resize(function(){marginAutoFix();});

	
}); 

function share(share, href){

	LeftPosition = (screen.width) ? (screen.width-780)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-570)/2 : 0;

	switch (share) {
		
		case 'facebook':

			var caption;
			caption = 'The first in a trailblazing new sci-fi series, The Lorien Legacies.';
			
			var publish = {
				method: 'stream.publish',
				attachment: {
					name: 'I Am Number Four',
					caption: caption,
					description: (
						'In the beginning we were a group of nine. ' +
						'There are six of us left. The first three were ' +
						'killed in the order of their numbers. ' +
						'They won\'t stop until they\'ve killed us all. ' +
						'I am number 4. ' +
						'I know that I am next. '
					),
					href: href,
					media: [
						{ 'type': 'image', 
						'src': href+'wp-content/themes/i_am_number_four/images/share/90x90.gif', 
						'href': href }
					]
				},
				action_links: [
					{ text: 'Find out more', href: href }
				],
				user_prompt_message: 'I Am Number Four, the first in a trailblazing new sci-fi series, The Lorien Legacies.'
			};

			FB.ui(publish);
			
		break;
		
		case 'twitter':
			window.open('http://twitter.com/home?status='+encodeURIComponent('I Am Number Four, the first in a trailblazing new sci-fi series, The Lorien Legacies. '+href),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;		

		case 'delicious':
			window.open('http://del.icio.us/post?url='+encodeURIComponent(href)+'&title='+encodeURIComponent('I Am Number Four, the first in a trailblazing new sci-fi series, The Lorien Legacies.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'diggit':
			window.open('http://digg.com/submit?url='+encodeURIComponent(href)+'&title='+encodeURIComponent('I Am Number Four, the first in a trailblazing new sci-fi series, The Lorien Legacies.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'reddit':
			window.open('http://reddit.com/submit?url='+encodeURIComponent(href)+'&title='+encodeURIComponent('I Am Number Four, the first in a trailblazing new sci-fi series, The Lorien Legacies.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'stumbleupon':
			window.open('http://www.stumbleupon.com/submit?url='+encodeURIComponent(href)+'&title='+encodeURIComponent('I Am Number Four, the first in a trailblazing new sci-fi series, The Lorien Legacies.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	
		
		case 'myspace':
			window.open('http://www.myspace.com/index.cfm?fuseaction=postto&u='+encodeURIComponent(href)+'&t='+encodeURIComponent('I Am Number Four, the first in a trailblazing new sci-fi series, The Lorien Legacies.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'google':
			window.open('http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+encodeURIComponent(href)+'&title='+encodeURIComponent('I Am Number Four, the first in a trailblazing new sci-fi series, The Lorien Legacies.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;			
		
	}
 
}

function shareVideo(share, legacy, href, vid){

	switch (legacy) {
	
		case 'underwater':
			legacy_text = 'Breathing Underwater';
		break;

		case 'fire':
			legacy_text = 'Controlling Fire';
		break;

		case 'lumen':
			legacy_text = 'Lumen';
		break;

		case 'telekinesis':
			legacy_text = 'Telekinesis';
		break;

		case 'weather':
			legacy_text = 'Controlling The Elements';
		break;
		
	}

	LeftPosition = (screen.width) ? (screen.width-780)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-570)/2 : 0;

	switch (share) {
		
		case 'facebook':
		
			var caption;
			caption = '{*actor*}\'s Lorien Legacy was '+legacy_text;
			
			var publish = {
				method: 'stream.publish',
				attachment: {
					name: 'What\'s your legacy?',
					caption: caption,
					description: (
						'Take the I Am Number Four Legacy Test and discover your Legacy. Will you have the power of Lumen like Number Four, or maybe the ability to control the elements like Number Six. It\'s time to find out...'
					),
					href: href+'legacy',
					media: [
						{ 'type': 'flash',
						'swfsrc': href+'embedplayer.swf?vid='+vid+'&player=fbplayer', 
						'imgsrc': href+'wp-content/themes/i_am_number_four/images/share/90x90_'+legacy+'.gif', 
						'width': '90', 
						'height': '90',
						'expanded_width': '400',
						'expanded_height': '314' }
					]
				},
				action_links: [
					{ text: 'Take The Legacy Test', href: href+'legacy' }
				],
				user_prompt_message: 'Take The Legacy Test and discover your powers.'
			};

			FB.ui(publish);
		
		break;
		
		case 'twitter':
			window.open('http://twitter.com/home?status='+encodeURIComponent('I took The Legacy Test for I Am Number Four and my Lorien Legacy is '+href+'legacy/?vid='+vid),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	
		
		case 'delicious':
			window.open('http://del.icio.us/post?url='+encodeURIComponent(href+'legacy/?vid='+vid)+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy. Watch the video of me and my powers!'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'diggit':
			window.open('http://digg.com/submit?url='+encodeURIComponent(href+'legacy/?vid='+vid)+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy. Watch the video of me and my powers!'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'reddit':
			window.open('http://reddit.com/submit?url='+encodeURIComponent(href+'legacy/?vid='+vid)+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy. Watch the video of me and my powers!'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'stumbleupon':
			window.open('http://www.stumbleupon.com/submit?url='+encodeURIComponent(href+'legacy/?vid='+vid)+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy. Watch the video of me and my powers!'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	
		
		case 'myspace':
			window.open('http://www.myspace.com/index.cfm?fuseaction=postto&u='+encodeURIComponent(href+'legacy/?vid='+vid)+'&t='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy. Watch the video of me and my powers!'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'google':
			window.open('http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+encodeURIComponent(href+'legacy/?vid='+vid)+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy. Watch the video of me and my powers!'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

	}
 
}

function marginAutoFix(){
	/*
	Need to ensure margins calculated by the browser when using {margin: 0 auto;} don't contain decimal pixel values
	http://fbexchange.net/questions/85/unable-to-interact-with-flash-settings-dialog-in-firefox
	*/
	var width =	$('body').width();
	var cntWidth = $('#site-container').width();
	var margin = parseInt((width-cntWidth)/2);
	if(margin < 0) {margin = 0;}
	$('#site-container').css({'margin':'0 '+margin+'px'});
	
}

function shareLegacy(share, legacy, href){

	LeftPosition = (screen.width) ? (screen.width-780)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-570)/2 : 0;

	switch (legacy) {
	
		case 'underwater':
			legacy_text = 'Breathing Underwater';
		break;

		case 'fire':
			legacy_text = 'Controlling Fire';
		break;

		case 'lumen':
			legacy_text = 'Lumen';
		break;

		case 'telekinesis':
			legacy_text = 'Telekinesis';
		break;

		case 'weather':
			legacy_text = 'Controlling The Elements';
		break;
		
	}	
	
	switch (share) {
		
		case 'facebook':
		
			var caption;
			caption = '{*actor*}\'s Lorien Legacy was '+legacy_text;
			
			var publish = {
				method: 'stream.publish',
				attachment: {
					name: 'What\'s your legacy?',
					caption: caption,
					description: (
						'Take the I Am Number Four Legacy Test and discover your Legacy. Will you have the power of Lumen like Number Four, or maybe the ability to control the elements like Number Six. It\'s time to find out...'
					),
					href: href+'legacy',
					media: [
						{ 'type': 'image', 
						'src': href+'wp-content/themes/i_am_number_four/images/share/90x90_'+legacy+'.gif', 
						'href': href+'legacy' }
					]
				},
				action_links: [
					{ text: 'Take The Legacy Test', href: href+'legacy' }
				],
				user_prompt_message: 'Take The Legacy Test and discover your powers.'
			};

			FB.ui(publish);
		
		break;

		case 'twitter':
			window.open('http://twitter.com/home?status='+encodeURIComponent('I took The Legacy Test for I Am Number Four and my Lorien Legacy is, '+legacy_text+'. '+href+'legacy'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;
		
		case 'delicious':
			window.open('http://del.icio.us/post?url='+encodeURIComponent(href+'legacy')+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy, '+legacy_text+'.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'diggit':
			window.open('http://digg.com/submit?url='+encodeURIComponent(href+'legacy')+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy, '+legacy_text+'.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'reddit':
			window.open('http://reddit.com/submit?url='+encodeURIComponent(href+'legacy')+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy, '+legacy_text+'.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'stumbleupon':
			window.open('http://www.stumbleupon.com/submit?url='+encodeURIComponent(href+'legacy')+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy, '+legacy_text+'.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	
		
		case 'myspace':
			window.open('http://www.myspace.com/index.cfm?fuseaction=postto&u='+encodeURIComponent(href+'legacy')+'&t='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy, '+legacy_text+'.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;	

		case 'google':
			window.open('http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+encodeURIComponent(href+'legacy')+'&title='+encodeURIComponent('I took The Legacy Test and discovered my Lorien Legacy, '+legacy_text+'.'),'sharer','toolbar=0,scrollbars=1,status=0,width=780,height=570,top='+TopPosition+',left='+LeftPosition);
			return false;
		break;

	}

}
