var isProduction = true;

var _Root = '/';
var _FacebookApplicationId = 307934995200;

if (!isProduction) {
	_Root = '/twynkle/2.0/';
	_FacebookApplicationId = 114203071958001;
}

var _AjaxUrl = _Root + 'assets/php/ajax.php';
var _XmlUrl = _Root + 'assets/php/xml.php';
var _CanPostToFacebook = false;
var _Type = '';
var _AccountId = -1;
var _MovieId = -1;
var _ActorId = -1;
var _CategoryId = -1;
var _BackdropChanger;
var _InfoBubble;
var _MovieIdHovered = -1;
var _X = 0;
var _Y = 0;

jQuery.noConflict(); 
jQuery(document).ready(function() {
	
	jQuery(document).mousemove(function(e){
		_X = e.pageX;
		_Y = e.pageY;
	}); 
   
	if (jQuery('#home').length != 0) HomePage_Initialize();
	if (jQuery('#login').length != 0) LoginPage_Initialize();
	if (jQuery('#forgot_password #txtEmail').length != 0) ForgotPasswordPage_Initialize();
	if (jQuery('#reset_password #txtPassword').length != 0) ResetPasswordPage_Initialize();
	if (jQuery('#register').length != 0) RegisterPage_Initialize();
	if (jQuery('#movie').length != 0) MoviePage_Initialize();
	if (jQuery('#person').length != 0) PersonPage_Initialize();
	if (jQuery('#person_charts').length != 0) PersonChartsPage_Initialize();
	if (jQuery('#search').length != 0) SearchPage_Initialize();
	if (jQuery('#deep_search').length != 0) DeepSearchPage_Initialize();
	if (jQuery('#review_page').length != 0) ReviewPage_Initialize();
	if (jQuery('#account #txtUsername').length != 0)
		EditProfilePage_Initialize();
	else if (jQuery('#account #facebook').length != 0)
		ProfilePage_Initialize();
	if (jQuery('#change_password').length != 0) ChangePasswordPage_Initialize();
	if (jQuery('#change_avatar').length != 0) ChangeAvatarPage_Initialize();
	if (jQuery('#find_friends').length != 0) FindFriendsPage_Initialize();
	if (jQuery('#friend_requests').length != 0) FriendRequestsPage_Initialize();
	if (jQuery('#feedback_page').length != 0) jQuery('#name').focus();

	InitializeRatings();

	jQuery('.dig_deeper a').click(function() {
		return confirm('Are you sure you want to dig deeper for possibly better results?\r\n\r\nThis could take several minutes, but you\'ll be helping the site!');
	});
	
	// Display a movie info bubble if hovered
	InitializeMovieBubble();
	
	//InitializeFacebook();

})
function InitializeRatings() {

	jQuery('.my_rating>ul>li>a').unbind('mouseover');
	jQuery('.my_rating>ul>li>a').mouseover(Rating_MouseOver);

	jQuery('.my_rating>ul').unbind('mouseout');
	jQuery('.my_rating>ul').mouseout(Rating_MouseOut);

	jQuery('.my_rating>ul>li>a').unbind('click');
	jQuery('.my_rating>ul>li>a').click(Rating_Click);

}

// This function simply resets the bindings
function InitializeMovieBubble() {

	jQuery('ul.hoverable>li>a>img').unbind('mouseover');
	jQuery('ul.hoverable>li>a>img').mouseover(InitializeInfoBubble);

	jQuery('ul.hoverable>li>a>img').unbind('mouseout');
	jQuery('ul.hoverable>li>a>img').mouseout(AbortDisplayInfoBubble);
	jQuery('ul.hoverable>li>a>img').mouseout(InitializeInfoBubbleHide);

	jQuery('#movie_info_bubble').unbind('mouseout');
	jQuery('#movie_info_bubble').mouseout(InitializeInfoBubbleHide);

}
function InitializeInfoBubble() {
	
	_MovieIdHovered = jQuery(this).parent().parent()[0].id.split('_')[1];
	if (_MovieIdHovered == undefined) alert('Define the movie_id, so the info bubble can pop up here');
	_InfoBubble = setTimeout(DisplayInfoBubble, 1500)
	
}
function DisplayInfoBubble() {
	
	var bubble = jQuery('#movie_info_bubble');
	
	var x = _X + 20;
	var y = _Y + 20;
	if (y > 700) y = 700;
	
	bubble.css('left', x + 'px').css('top', y + 'px');
	jQuery('#movie_info_bubble').addClass('loading').html('');
	bubble.fadeIn('slow', LoadBubbleContent);
	
}
function LoadBubbleContent() {
	
	var body;

	var url = _AjaxUrl + '?action=loadbubble&movieid=' + _MovieIdHovered;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				
				body = '<h1>' + json.title + '<span>' + json.releaseYear + '</span></h1>';
				body = body + '<div id="bDirectors">Directed by ' + json.directors + '</div>';
				body = body + '<div id="bCover"><img src="' + json.cover + '" alt="' + json.title + '" /></div>';
				body = body + '<div id="bSynopsis">';
				if (json.synopsis == null)
					body = body + 'No synopsis';
				else
					body = body + json.synopsis;
				body = body + '</div>';
				body = body + '<div id="bActors"><h2>Cast</h2>';
				if (jQuery.trim(json.actors) == 0)
					body = body + '<span>No actors found</span>';
				else
					body = body + json.actors;
				body = body + '</div>';
				body = body + '<div id="bRating">';
				if (json.votes == 0)
					body = body + 'Be the first to vote!';
				else
					body = body + 'Average: ' + json.average + ' out of ' + json.votes + ' votes';
				body = body + '</div>';
				
				jQuery('#movie_info_bubble').removeClass('loading').html(body);
				
			}
			else {
				alert(json.message);
			}
		}
	);
	
}
function AbortDisplayInfoBubble() {
	clearTimeout(_InfoBubble);
	_MovieIdHovered = -1;
}
function InitializeInfoBubbleHide() {
	_InfoBubble = setTimeout(HideInfoBubble, 200)
}
function HideInfoBubble() {
	jQuery('#movie_info_bubble').fadeOut('slow');
}

function GetScreenResolution() {

	var url = _AjaxUrl + '?action=setscreenresolution&w=' + screen.width + '&h=' + screen.height + '&d=' + screen.colorDepth;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				//alert('Success');
			}
			else {
				alert(json.message);
			}
		}
	);

}

function HomePage_Initialize() {
	if (jQuery('#netflix_rentals').length > 0) HomePage_GetNetflixRentals();
}
function HomePage_GetNetflixRentals() {

	var li;
	
	var url = _AjaxUrl + '?action=netflixrentals';
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				
				for (var i = 0; i < json.movies.length; i++) {
					
					li = '<div class="title"><a href="' + _Root + 'movies/' + json.movies[i].id + '" title="' + json.movies[i].title + '">' + json.movies[i].title + '</a></div>';
					li += '<div class="image">';
					li += '<a href="' + _Root + 'movies/' + json.movies[i].id + '" title="' + json.movies[i].title + '"><img src="' + json.movies[i].coverUrl + '" alt="' + json.movies[i].title + '" /></a>';
					li += '<div class="completion ';
					
					if (json.movies[i].completion >= 100)
						li += 'complete';
					else if (json.movies[i].completion >= 75)
						li += 'three_quarters';
					else if (json.movies[i].completion >= 50)
						li += 'half';
					else if (json.movies[i].completion >= 25)
						li += 'one_quarter';
					else if (json.movies[i].completion >= 1)
						li += 'little';
					else if (json.movies[i].completion == 0)
						li += 'nothing';
					
					li += '">I have rated ' + json.movies[i].completion + '%';
					if (json.movies[i].completion >= 100) li += '<img class="complete" src="' + _Root + 'assets/images/complete.png" alt="I have completed rating this movie" />';
					li += '</div>';
					li += '</div>';
					
					if (i == 0) {
						jQuery('#netflix_rentals>ul>li').removeClass('loading');
						jQuery('#netflix_rentals>ul>li').attr('id', 'movie_' + json.movies[i].id).html(li);
					}
					else {
						jQuery('#netflix_rentals>ul>li:last').after('<li id="movie_' + json.movies[i].id + '">' + li + '</li>');
					}
					
				}
				InitializeMovieBubble();
				
			}
			else {
				alert(json.message);
			}
		}
	);
	
}
function LoginPage_Initialize() {
	
	jQuery('#txtUsername').focus();
	jQuery('#txtUsername').keypress(LoginPage_Submit);
	jQuery('#txtPassword').keypress(LoginPage_Submit);
	jQuery('#chkRememberMe').keypress(LoginPage_Submit);
	
}
function LoginPage_Submit(e) {

	if (e.keyCode == 13) {
		jQuery('#btnLogin').trigger('click');
		return false;
	}
	
}
function ForgotPasswordPage_Initialize() {

	jQuery('#txtEmail').focus();
	jQuery('#txtEmail').keypress(ForgotPasswordPage_Submit);

}
function ForgotPasswordPage_Submit(e) {

	if (e.keyCode == 13) {
		jQuery('#btnSend').trigger('click');
		return false;
	}
	
}
function ResetPasswordPage_Initialize() {
	
	jQuery('#txtPassword').focus();
	jQuery('#txtPassword').keypress(ResetPasswordPage_Submit);
	jQuery('#txtConfirmPassword').keypress(ResetPasswordPage_Submit);
	
}
function ResetPasswordPage_Submit(e) {

	if (e.keyCode == 13) {
		jQuery('#btnSave').trigger('click');
		return false;
	}

}
function RegisterPage_Initialize() {

	jQuery('#txtUsername').focus();
	jQuery('#txtUsername').keypress(RegisterPage_Submit);
	jQuery('#txtEmail').keypress(RegisterPage_Submit);
	jQuery('#txtPassword').keypress(RegisterPage_Submit);

}
function RegisterPage_Submit(e) {

	if (e.keyCode == 13) {
		jQuery('#btnRegister').trigger('click');
		return false;
	}
	
}

function MoviePage_Initialize() {
	
	_MovieId = jQuery('#movie>#info>#main_info>h1')[0].id.split('_')[1];
	
	GetCanPostToFacebook();
	
	jQuery('#backdrop>a>img').mouseover(function() {
		jQuery('#disable_backdrops').css('visibility', 'visible');
	});
	jQuery('#backdrop>a>img').mouseout(function() {
		jQuery('#disable_backdrops').css('visibility', 'hidden');
	});
	jQuery('#disable_backdrops').mouseover(function() {
		jQuery('#disable_backdrops').css('visibility', 'visible');
	});
	jQuery('#disable_backdrops').click(function() {
		return confirm('Are you sure you want to disable backdrops?\r\n\r\nYou can change this setting anytime from your profile.');
	});
	
	jQuery('#backdrop>a>img').click(Backdrop_Click);
	_BackdropChanger = setTimeout('Backdrop_Click()', 10000);
	
	jQuery('#trailers a').click(MoviePage_DisplayVideosWindow);
	jQuery('#videos a').click(MoviePage_DisplayVideosWindow);
	jQuery('#videos_window>ul>li>a').click(MoviePage_LoadVideo);
	
	jQuery('#favorite a').click(MoviePage_Favorite_Click);
	jQuery('#shelf a').click(MyShelf_Click);

	var categoryCount = jQuery('#categories>ul>li').length;
	if (categoryCount < 5) categoryCount = 5; // Making sure we always display at least 5 actors
	var actorCount = jQuery('#actors>ul>li').length;
	
	if (actorCount > categoryCount) {
		jQuery('#actors>ul>li:gt(' + (categoryCount - 1) + ')').addClass('secondary hidden');
		jQuery('#actors>ul').after('<div class="more"><a href="' + _Root + 'movies/' + _MovieId + '?allactors=1" title="View all ' + actorCount + ' actors">View all ' + actorCount + ' actors</a></div>');
	}
	
	jQuery('#movie #categories.list>ul>li>h3>a.description').click(ShowCategoryDescription);
	jQuery('#status .value>a').click(UpdateStatus);
	jQuery('#categories>.add>a').click(AddCategory);
	jQuery('#actors>.add>a').click(RecommendActor);
	
	jQuery('#actors>div.more').click(function() {

		jQuery(this).remove();
		jQuery('#actors>ul>li.hidden').show();
		
		return false;
	});
	
	jQuery('#add_category>input').keypress(AddCategory_KeyPress);

}
function AddCategory_KeyPress(e) {

	if (e.keyCode == 13) {
		
		jQuery(jQuery('.ui-dialog-buttonpane>button')[0]).trigger('click');
		
		return false;
	}

}
function GetCanPostToFacebook() {
	
	var url;
	if (_MovieId != -1)
		url = _AjaxUrl + '?action=canposttofacebook&movieid=' + _MovieId;
	else if (_PersonId != -1)
		url = _AjaxUrl + '?action=canposttofacebook&personid=' + _PersonId;

	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				_CanPostToFacebook = json.canPostToFacebook;
			}
			else {
				alert(json.message);
			}
		}
	);
	
}

function MoviePage_Favorite_Click() {
	
	var obj = jQuery(this).parent().find('a:last');
	
	var add = '0';
	switch (obj.text()) {
		
		case 'Add to my favorites':
			add = '1';
			break;
		
	}

	var url = _AjaxUrl + '?action=favoritemovie&movieid=' + _MovieId + '&add=' + add;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				
				if (add == '1') {

					obj.parent().find('a>img').attr('src', _Root + 'assets/images/favorite_remove.png');
					obj.parent().find('a').attr('href', _Root + 'profile/movies/favorites/remove/' + _MovieId).attr('title', 'Remove from my favorites');
					obj.text('Remove from my favorites');

				}
				else {

					obj.parent().find('a>img').attr('src', _Root + 'assets/images/favorite_add.png');
					obj.parent().find('a').attr('href', _Root + 'profile/movies/favorites/add/' + _MovieId).attr('title', 'Add to my favorites');
					obj.text('Add to my favorites');

				}
				
			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}

function MoviePage_DisplayVideosWindow() {

	jQuery('#videos_window>div').html('');
	
	switch (jQuery(this).parent().parent()[0].id) {
		
		case 'trailers':
			jQuery('#trailers_list').show();
			jQuery('#videos_list').hide();
			break;
		
		case 'videos':
			jQuery('#trailers_list').hide();
			jQuery('#videos_list').show();
			break;
		
	}

	jQuery("#videos_window").dialog({
		autoOpen: true,
		height: 600,
		width: 900,
		modal: true,
		buttons: {
			'Close': function() {
				jQuery(this).dialog('close');
			}
			// ,Cancel: function() {
				// jQuery(this).dialog('close');
			// }
		}
		// ,close: function() {
		// }
	});

	return false;
}
function MoviePage_LoadVideo() {
	
	var id = jQuery(this).attr('id').split('_')[1];

	var url = _AjaxUrl + '?action=getvideo&id=' + id;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				jQuery('#videos_window>div').html(json.code);
			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}
function ShowCategoryDescription() {
	
	var obj = jQuery(this);
	var categoryId = obj.parent().parent()[0].id.split('_')[1];

	var url = _AjaxUrl + '?action=getcategorydescription&id=' + categoryId;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				
				jQuery('#category_description>h1').html('"' + json.category + '" description');
				jQuery('#category_description>p').html(json.description);

				jQuery("#category_description").dialog({
					autoOpen: true,
					height: 300,
					width: 400,
					modal: true,
					buttons: {
						'Close': function() {
							jQuery(this).dialog('close');
						}
					}
				});
				
			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}
function UpdateStatus() {

	var url;
	
	setTimeout('jQuery(\'#update_status>input\').focus()',300);
	
	jQuery("#update_status").dialog({
		autoOpen: true,
		height: 220,
		width: 350,
		modal: true,
		buttons: {
			'Update': function() {
				
				url = _AjaxUrl + '?action=updatestatus&movieid=' + _MovieId + '&statusid=' + jQuery('#update_status>select').val();
				//prompt('url', url);return false;
				//window.open(url);return false;
				jQuery.getJSON(
					url,
					function(json) {
						if (json.success) {
							jQuery('#status>.value>a').html(json.status);
							jQuery('#update_status').dialog('close');
						}
						else {
							alert(json.message);
						}
					}
				);
				
			},
			'Close': function() {
				jQuery(this).dialog('close');
			}
		}
	});

	return false;
}
function AddCategory() {
	
	var category, url, li;
	
	setTimeout('jQuery(\'#add_category>input\').focus()',300);
	
	jQuery("#add_category").dialog({
		autoOpen: true,
		height: 220,
		width: 350,
		modal: true,
		buttons: {
			'Add category': function() {
				
				category = jQuery.trim(jQuery('#add_category>input').val());
				if (category.length == 0) {
					alert('Please enter a category');
					return false;
				}

				url = _AjaxUrl + '?action=addcategory&id=' + _MovieId + '&category=' + escape(category);
				//prompt('url', url);return false;
				//window.open(url);return false;
				jQuery.getJSON(
					url,
					function(json) {
						if (json.success) {

							li = '<h3><a href="' + _Root + 'categories/' + json.categoryId + '" title="' + json.category + '">' + json.category + '</a></h3>';
							li += '<div class="ratings">';
							li += '<div class="my_rating">';
							li += '<h4>My rating</h4>';
							li += '<ul id="movie_' + _MovieId + '_' + json.categoryId + '">';
							for (var i = 1; i <= 5; i++) {
								li += '<li id="movie_' + _MovieId + '_' + json.categoryId + '_' + i + '"><a href="' + _Root + 'movies/' + _MovieId + '/categories/' + json.categoryId + '/rate/' + i + '" title="' + i + ' stars">' + i + ' stars</a></li>';
							}
							li += '<li id="movie_' + _MovieId + '_' + json.categoryId + '_0" class="clear"><a href="' + _Root + 'movies/' + _MovieId + '/categories/' + json.categoryId + '/rate/0" title="Clear rating">Clear rating</a></li>';
							li += '</ul>';
							li += '</div> <!-- "my_rating" -->';
							li += '<div class="average_rating">';
							li += '<h4>Average rating</h4>';
							li += '<ul title="0 votes">';
							for (var i = 1; i <= 5; i++) {
								li += '<li>' + i + '</li>';
							}
							li += '</ul>';
							li += '<div class="votes">0 votes</div>';
							li += '</div> <!-- "average_rating" -->';
							li += '</div> <!-- "ratings" -->';

							jQuery('#categories>ul>li:last').after('<li>' + li + '</li>');
							InitializeRatings();
							
							jQuery('#add_category').dialog('close');
						}
						else {
							alert(json.message);
						}
					}
				);
	
			},
			'Close': function() {
				jQuery(this).dialog('close');
			}
			// ,Cancel: function() {
				// jQuery(this).dialog('close');
			// }
		}
		// ,close: function() {
		// }
	});

	return false;
}
function RecommendActor() {

	jQuery("#recommend_actor").dialog({
		autoOpen: true,
		height: 300,
		width: 400,
		modal: true,
		buttons: {
			'Recommend actor': function() {
				alert('Recommend');
				jQuery(this).dialog('close');
			},
			'Close': function() {
				jQuery(this).dialog('close');
			}
			// ,Cancel: function() {
				// jQuery(this).dialog('close');
			// }
		}
		// ,close: function() {
		// }
	});
	
	return false;
}

function PersonPage_Initialize() {
	_PersonId = jQuery('h1')[0].id.split('_')[1];
	GetCanPostToFacebook();
}

function PersonChartsPage_Initialize() {
	
	var myChart;

	var personId = jQuery('#person_id').val();
	var url = _XmlUrl + '?action=actorbankability&id=' + personId;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.ajax({
		type: 'GET',
		url: url,
		dataType: 'html',
		success: function(xml, status, request) {

			myChart = new FusionCharts(_Root + 'assets/FusionCharts/FCF_Line.swf', 'myChartId', '600', '500');
			//myChart = new FusionCharts(_Root + 'assets/FusionCharts/FCF_Area2D.swf', 'myChartId', '600', '500');
			//myChart = new FusionCharts(_Root + 'assets/FusionCharts/FCF_Bar2D.swf', 'myChartId', '600', '500');
			//myChart = new FusionCharts(_Root + 'assets/FusionCharts/FCF_Column3D.swf', 'divRevenue', '400', '300');
			myChart.setDataXML(xml);
			myChart.render('divRevenue');
			
		},
		error: function(xmlhttp, status, error) {
			alert('Error: ' + error);
		}
	});
	
}

function SearchPage_Initialize() {
	
	jQuery('#txtSearch').focus();
	jQuery('#txtSearch').keypress(function(e) {

		if (e.keyCode == 13) {
			jQuery('#btnSearch').trigger('click');
			return false;
		}
		
	});
	
	jQuery('#btnSearch').click(function(e) {
		return false;
	});
	
}

function DeepSearchPage_Initialize() {

	var url = _AjaxUrl + '?action=deepsearch';
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				location.href = _Root + 'search/results';
			}
			else {
				alert(json.message);
			}
		}
	);
	
}
function ReviewPage_Initialize() {
	
	_MovieId = jQuery('#movie_id').val();
	
	jQuery('#reviews>#write>a').unbind('click');
	jQuery('#reviews>#write>a').click(WriteReview);
	
	jQuery('#reviews>ul>li>.details>.approval>ul>.like>a').unbind('click');
	jQuery('#reviews>ul>li>.details>.approval>ul>.like>a').click(LikeReview);
	
	jQuery('#reviews>ul>li>.details>.approval>ul>.dislike>a').unbind('click');
	jQuery('#reviews>ul>li>.details>.approval>ul>.dislike>a').click(DislikeReview);
	
	jQuery('#reviews>ul>li>.details>.approval>ul>.inappropriate>a').unbind('click');
	jQuery('#reviews>ul>li>.details>.approval>ul>.inappropriate>a').click(FlagReview);
	
	jQuery('#reviews>ul>li>.details>.approval>ul>.delete>a').unbind('click');
	jQuery('#reviews>ul>li>.details>.approval>ul>.delete>a').click(DeleteReview);
	
}
function WriteReview() {
	
	if (jQuery('#write_review').length == 0) {

		jQuery('#reviews>ul>li:first').before('<li id="write_review" style="display: none;"><h3>Write a review</h3><textarea></textarea><div><input id="save" type="button" value="Save" /><input id="cancel" type="button" value="Cancel" /></div></li>');
		jQuery('#write_review').slideDown(500, function() {
			
			jQuery('#write_review>textarea').focus();
		
			jQuery('#write_review>div>#save').unbind('click');
			jQuery('#write_review>div>#save').click(SaveReview);
			
			jQuery('#write_review>div>#cancel').unbind('click');
			jQuery('#write_review>div>#cancel').click(CancelReview);

		});

	}
	else
		CancelReview();
	
	return false;
}
function SaveReview() {
	
	var li;
	var review = jQuery.trim(jQuery('#write_review>textarea').val());
	
	var url = _AjaxUrl + '?action=addreview&id=' + _MovieId + '&review=' + escape(review);
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {

				jQuery('#write_review>textarea').val('');
			
				li = '<li id="review_' + json.reviewId + '" style="display: none;">';
				li = li + '<blockquote>' + json.review + '</blockquote>';
				li = li + '<div class="details">';
				li = li + '<div class="owner"> ';
				li = li + 'Written by <a href="/twynkle/2.0/profile" title="View your profile">you</a> ' + json.displayDate;
				li = li + '</div> <!-- "owner" -->';
				li = li + '<div class="approval">';
				li = li + '<h3>Approval</h3>';
				li = li + '<ul>';
				li = li + '<li class="like"><a class="selected" href="/twynkle/2.0/movies/' + _MovieId + '/reviews/' + json.reviewId + '/like" title="I found this review helpful">I found this review helpful</a></li>\r\n';
				li = li + '<li class="dislike"><a href="/twynkle/2.0/movies/' + _MovieId + '/reviews/' + json.reviewId + '/dislike" title="I didn\'t find this review helpful">I didn\'t find this review helpful</a></li>\r\n';
				li = li + '<li class="inappropriate"><a href="/twynkle/2.0/movies/' + _MovieId + '/reviews/' + json.reviewId + '/inappropriate" title="This review is inappropriate, offensive, or non-applicable">This review is inappropriate, offensive, or non-applicable</a></li>\r\n';
				li = li + '<li class="delete"><a href="/twynkle/2.0/movies/' + _MovieId + '/reviews/' + json.reviewId + '/delete" title="Delete this review">Delete this review</a></li>\r\n';
				li = li + '</ul>';
				li = li + '<div class="likes_dislikes">';
				li = li + json.likes + ' out of ' + (json.likes + json.dislikes) + ' person likes this';
				li = li + '</div> <!-- "likes_dislikes" -->';
				li = li + '</div> <!-- "approval" -->';
				li = li + '</div> <!-- "details" -->';
				li = li + '</li>';
				
				jQuery('#reviews>ul>li:first').after(li);
				if (jQuery('#no_reviews').length == 0) {

					jQuery('#reviews>ul>li:first').slideUp(500, function() {
						jQuery(this).remove();
						jQuery('#review_' + json.reviewId).slideDown(500, ReviewPage_Initialize);
					});

				}
				else {
				
					jQuery('#no_reviews').slideUp(500, function() {

						jQuery('#reviews>ul>li:first').slideUp(500, function() {
							jQuery('#review_' + json.reviewId).slideDown(500, ReviewPage_Initialize);
						});

					});
				
				}

			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}
function CancelReview() {

	if (jQuery.trim(jQuery('#write_review>textarea').val()).length != 0) {
		if (!confirm('Are you sure you want to cancel writing your review?')) return false;
	}

	jQuery('#write_review').slideUp(500, function() {
		jQuery(this).remove();
	});
	
	return false;
}
function LikeReview() {
	
	var obj = jQuery(this);
	var reviewId = obj.parent().parent().parent().parent().parent()[0].id.split('_')[1];
	
	var url = _AjaxUrl + '?action=likereview&id=' + reviewId;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				
				obj.parent().parent().find('li>a').removeClass('selected');
				obj.parent().parent().find('.like>a').addClass('selected');
				
				if (json.likes + json.dislikes == 1)
					obj.parent().parent().parent().find('.likes_dislikes').html(json.likes + ' out of ' + (json.likes + json.dislikes) + ' person likes this');
				else
					obj.parent().parent().parent().find('.likes_dislikes').html(json.likes + ' out of ' + (json.likes + json.dislikes) + ' people like this');
				
			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}
function DislikeReview() {

	var obj = jQuery(this);
	var reviewId = obj.parent().parent().parent().parent().parent()[0].id.split('_')[1];

	var url = _AjaxUrl + '?action=dislikereview&id=' + reviewId;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {

				obj.parent().parent().find('li>a').removeClass('selected');
				obj.parent().parent().find('.dislike>a').addClass('selected');

				if (json.likes + json.dislikes == 1)
					obj.parent().parent().parent().find('.likes_dislikes').html(json.likes + ' out of ' + (json.likes + json.dislikes) + ' person likes this');
				else
					obj.parent().parent().parent().find('.likes_dislikes').html(json.likes + ' out of ' + (json.likes + json.dislikes) + ' people like this');

			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}
function FlagReview() {
	if (!confirm('Are you sure you want to flag this review as inappropriate, offense, or non-applicable?')) return false;

	var obj = jQuery(this);
	var reviewId = obj.parent().parent().parent().parent().parent()[0].id.split('_')[1];

	var url = _AjaxUrl + '?action=flagreview&id=' + reviewId;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {

				obj.parent().parent().find('li>a').removeClass('selected');
				obj.parent().parent().find('.inappropriate>a').addClass('selected');

				if (json.flags == 1)
					obj.parent().parent().parent().find('.likes_dislikes').html(json.flags + ' person has flagged this review as inappropriate');
				else
					obj.parent().parent().parent().find('.likes_dislikes').html(json.flags + ' people have flagged this review as inappropriate');

			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}
function DeleteReview() {
	if (!confirm('Are you sure you want to delete your review?')) return false;

	var obj = jQuery(this);
	var reviewId = obj.parent().parent().parent().parent().parent()[0].id.split('_')[1];

	var url = _AjaxUrl + '?action=deletereview&id=' + reviewId;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {

				obj.parent().parent().parent().parent().parent().fadeOut(1000, function() {
					 
					 jQuery(this).remove();
					 
					 if (json.reviewCount == 0) {
						jQuery('#reviews>ul').append('<li id="no_reviews" style="display: none;">No reviews</li>');
						jQuery('#no_reviews').slideDown(500);
					 }
					 
				});
				
			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}

function ProfilePage_Initialize() {
	
	/*
	jQuery('#facebook').click(function() {
		alert('Link Facebook');
		return false;
	});
	*/
	
	jQuery('#permenant_actions #delete_account').click(function() {
		return confirm('Are you sure you want to delete your account?\r\n\r\nTHIS CANNOT BE REVERSED!!!');
	});
	
}
function EditProfilePage_Initialize() {
	
	jQuery('#txtUsername').focus();

	jQuery('#txtUsername').keyup(EditProfilePage_CheckUsernameAvailability);

	jQuery('#txtUsername').keypress(EditProfilePage_Submit);
	jQuery('#txtFirstName').keypress(EditProfilePage_Submit);
	jQuery('#txtLastName').keypress(EditProfilePage_Submit);
	jQuery('#lstMonth').keypress(EditProfilePage_Submit);
	jQuery('#lstDay').keypress(EditProfilePage_Submit);
	jQuery('#lstYear').keypress(EditProfilePage_Submit);
	jQuery('#lstSex').keypress(EditProfilePage_Submit);
	jQuery('#txtEmail').keypress(EditProfilePage_Submit);
	
}
function EditProfilePage_CheckUsernameAvailability(e) {
	
	var obj = jQuery('#username_available');

	var username = jQuery.trim(jQuery('#txtUsername').val());
	var url = _AjaxUrl + '?action=usernameavailability&username=' + username;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				
				if (json.available) {
					obj.removeClass('not_available').html('Available');
				}
				else {
					obj.addClass('not_available').html('Not available');
				}
				
			}
			else {
				alert(json.message);
			}
		}
	);
	
}
function EditProfilePage_Submit(e) {

	if (e.keyCode == 13) {
		jQuery('#btnSave').trigger('click');
		return false;
	}
	
}
function ChangePasswordPage_Initialize() {
	
	jQuery('#txtCurrentPassword').focus();
	
	jQuery('#txtCurrentPassword').keypress(function(e) {
		if (e.keyCode == 13) {
			jQuery('#btnSave').trigger('click');
			return false;
		}
	});
	
	jQuery('#txtNewPassword').keypress(function(e) {
		if (e.keyCode == 13) {
			jQuery('#btnSave').trigger('click');
			return false;
		}
	});
	
	jQuery('#txtVerifyPassword').keypress(function(e) {
		if (e.keyCode == 13) {
			jQuery('#btnSave').trigger('click');
			return false;
		}
	});
	
}
function ChangeAvatarPage_Initialize() {
	jQuery('#txtUrl').focus();
}
function FindFriendsPage_Initialize() {
	
	jQuery('#txtEmail').focus();
	
	jQuery('#txtEmail').keypress(function(e) {
		if (e.keyCode == 13) {
			jQuery('#btnFind').trigger('click');
			return false;
		}
	});
	
	jQuery('#txtPassword').keypress(function(e) {
		if (e.keyCode == 13) {
			jQuery('#btnFind').trigger('click');
			return false;
		}
	});
	
	jQuery('#contacts ul>li.nonmember a').click(FindFriendsPage_Invite);
	jQuery('#contacts ul>li.member a').click(FindFriendsPage_RequestFriendship);

}
function FindFriendsPage_Invite() {
	
	var obj = jQuery(this).parent().parent();
	var email = obj.find('.email').html();

	obj.find('.status').html('Inviting...');

	var url = _AjaxUrl + '?action=invite&email=' + email;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				obj.addClass('invited');
				obj.find('.status').html('Invited');
			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}
function FindFriendsPage_RequestFriendship() {

	var obj = jQuery(this).parent().parent();
	var accountId = obj[0].id.split('_')[1];

	obj.find('.status').html('Requesting...');

	var url = _AjaxUrl + '?action=requestfriendship&id=' + accountId;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				obj.removeClass('member').addClass('requested');
				obj.find('.status').html('Awaiting confirmation');
			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}
function FriendRequestsPage_Initialize() {
	
	jQuery('#main_info>ul>li').mouseover(function() {
		jQuery(this).find('ul').addClass('visible');
	});

	jQuery('#main_info>ul>li').mouseout(function() {
		if (jQuery(this).find('ul>li').length != 0) jQuery(this).find('ul').removeClass('visible');
	});
	
	jQuery('.accept>a').click(FriendRequestsPage_Accept);
	jQuery('.decline>a').click(FriendRequestsPage_Decline);
	jQuery('.block>a').click(FriendRequestsPage_Block);
	
}
function FriendRequestsPage_Accept() {
	
	var obj = jQuery(this);
	var accountId = jQuery(this).parent().parent().parent()[0].id.split('_')[1];
	obj.parent().parent().fadeOut(300, function() {

		var url = _AjaxUrl + '?action=acceptfriend&id=' + accountId;
		//prompt('url', url);return false;
		//window.open(url);return false;
		jQuery.getJSON(
			url,
			function(json) {
				if (json.success) {
					obj.parent().parent().addClass('visible').html('Approved').fadeIn(300);
				}
				else {
					alert(json.message);
				}
			}
		);

	});
	
	return false;
}
function FriendRequestsPage_Decline() {
	
	var obj = jQuery(this);
	var accountId = jQuery(this).parent().parent().parent()[0].id.split('_')[1];
	obj.parent().parent().fadeOut(300, function() {

		var url = _AjaxUrl + '?action=declinefriend&id=' + accountId;
		//prompt('url', url);return false;
		//window.open(url);return false;
		jQuery.getJSON(
			url,
			function(json) {
				if (json.success) {
					obj.parent().parent().addClass('visible').html('Declined').fadeIn(300);
				}
				else {
					alert(json.message);
				}
			}
		);

	});
		
	return false;
}
function FriendRequestsPage_Block() {

	var obj = jQuery(this);
	var accountId = jQuery(this).parent().parent().parent()[0].id.split('_')[1];
	obj.parent().parent().fadeOut(300, function() {

		var url = _AjaxUrl + '?action=blockaccount&id=' + accountId;
		//prompt('url', url);return false;
		//window.open(url);return false;
		jQuery.getJSON(
			url,
			function(json) {
				if (json.success) {
					obj.parent().parent().addClass('visible').html('Blocked').fadeIn(300);
				}
				else {
					alert(json.message);
				}
			}
		);

	});
		
	return false;
}

function Backdrop_Click() {
	
	var obj = jQuery('#backdrop>a>img');
	var index = jQuery('#backdrop_settings>#backdrop_index').val();
	var total = jQuery('#backdrop_settings>#backdrop_total').val();
	var images = jQuery('#backdrop_settings>ul>li');
	
	if (total <= 1) return false;
	
	index++;
	if (index >= total) index = 0;
	var url = jQuery(images[index]).html();
	
	jQuery('#backdrop_settings>#backdrop_index').val(index);
	jQuery('#backdrop_settings>#backdrop_total').val(total);

	index++;
	if (index >= total) index = 0;
	jQuery('#backdrop_settings>#next_backdrop').attr('src', images[index].innerText);
	
	obj.fadeOut('fast', function() {
		obj.attr('src', url);
		obj.fadeIn('fast')
	});
	
	clearTimeout(_BackdropChanger);
	_BackdropChanger = setTimeout('Backdrop_Click()', 10000);
	
	return false;
}
function Rating_MouseOver() {
	
	var id = jQuery(this).parent()[0].id;
	var segments = id.split('_');
	var rating;

	switch (segments[0]) {
		
		case 'movie':
			_Type = 'movie';
			_MovieId = segments[1];
			_PersonId = -1;
			_CategoryId = segments[2];
			rating = segments[3];
			break;
		
		case 'person':
			_Type = 'person';
			_MovieId = segments[1];
			_PersonId = segments[2];
			_CategoryId = segments[3];
			rating = segments[4];
			break;
		
	}
	
	//alert('MovieId: ' + _MovieId + '\r\nPersonId: ' + _PersonId + '\r\nCategoryId: ' + _CategoryId + '\r\nRating: ' + rating);return false;
	
	LightStars(jQuery(this).parent().parent(), rating);
	
}
function Rating_MouseOut() {
	ResetStars(jQuery(this))
}
function Rating_Click() {

	// Remove this when you want to use ajax
	//return true;
	
	var obj = jQuery(this).parent().parent();

	var id = jQuery(this).parent()[0].id;
	var segments = id.split('_');
	obj.parent().parent().find('.saving').fadeIn('300');
	
	var url;
	var rating;
	switch (segments[0]) {
		
		case 'movie':
			_Type = 'movie';
			rating = segments[3];
			url = _AjaxUrl + '?action=ratemoviecategory&movieid=' + _MovieId + '&categoryid=' + _CategoryId + '&rating=' + rating;
			break;
		
		case 'person':
			_Type = 'person';
			rating = segments[4];
			url = _AjaxUrl + '?action=ratecastcategory&movieid=' + _MovieId + '&personid=' + _PersonId + '&categoryid=' + _CategoryId + '&rating=' + rating;
			break;
		
	}
	
	var oldRating = GetCurrentRating(jQuery(this).parent().parent());

	//alert('MovieId: ' + _MovieId + '\r\nPersonId: ' + _PersonId + '\r\nCategoryId: ' + _CategoryId + '\r\nRating: ' + rating + '\r\nOld rating: ' + oldRating);return false;

	// Displaying the new rating instantly, so the response time looks faster
	SetNewRating(obj, rating);
	
	if (_CanPostToFacebook && rating != 0) PostFacebook();
	
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {

				// We don't need to set the my_rating stars, because it was set up above, only set the average_rating stars
				SetNewAverage(obj.parent().parent().find('.average_rating>ul'), json.average, json.votes);
				obj.parent().parent().find('.saving').fadeOut('300');
	
			}
			else {

				// Since there was an error, reset the my_rating stars back to their original value
				SetNewRating(obj, oldRating);
				alert(json.message);
				obj.parent().parent().find('.saving').fadeOut('300');

			}
		}
	);

	return false;
}
function MyShelf_Click() {
	
	var obj = jQuery(this);
	
	var add = '0';
	switch (obj.text()) {
		
		case 'Add to my shelf':
			add = '1';
			break;
		
	}

	var url = _AjaxUrl + '?action=shelf&movieid=' + _MovieId + '&add=' + add;
	//prompt('url', url);return false;
	//window.open(url);return false;
	jQuery.getJSON(
		url,
		function(json) {
			if (json.success) {
				
				if (add == '1')
					obj.text('Remove from my shelf').attr('href', _Root + 'profile/movies/remove/' + _MovieId).attr('title', 'Remove from my shelf').css('background-image', 'url(' + _Root + 'assets/images/shelf_remove.png)');
				else
					obj.text('Add to my shelf').parent().find('a').attr('href', _Root + 'profile/movies/add/' + _MovieId).attr('title', 'Add to my shelf').css('background-image', 'url(' + _Root + 'assets/images/shelf_add.png)');
				
			}
			else {
				alert(json.message);
			}
		}
	);
	
	return false;
}

// The object that is passed in is the UL of the my_rating
function LightStars(obj, Rating) {

	var lis = jQuery(obj).find('li');
	lis.removeClass('selected');
	
	for (var i = 0; i < lis.length; i++) {
		if ((i + 1) <= Rating)
			jQuery(lis[i]).addClass('selected');
	}
	
}

// The object that is passed in is the UL of the my_rating
function ResetStars(obj) {

	var currentRating = GetCurrentRating(obj);

	var lis = jQuery(obj).find('li');
	lis.removeClass('selected');
	
	var isAverageRating = false;
	if (obj.parent().attr('class').indexOf('average_rating') != -1) {
		obj.find('>li').removeClass('quarter').removeClass('half').removeClass('three_quarters');
		isAverageRating = true;
	}

	for (var i = 1; i <= lis.length; i++) {
		
		if (isAverageRating) {
			
			if (currentRating >= i)
				lis.eq(i - 1).addClass('selected');
			else if (currentRating >= i - .25)
				lis.eq(i - 1).addClass('three_quarters');
			else if (currentRating >= i - .5)
				lis.eq(i - 1).addClass('half');
			else if (currentRating >= i - .75)
				lis.eq(i - 1).addClass('quarter');
			
		}
		else {
		
			if (i <= currentRating)
				jQuery(lis[i - 1]).addClass('selected');
			
		}
	}

}

// The object that is passed in is the UL of the my_rating
function SetNewRating(obj, Rating) {

	var classes = obj.attr('class').toLowerCase().split(' ');
	for (var i = 0; i < classes.length; i++) {
		if (classes[i].indexOf('_') != -1) {
		
			switch (classes[i].split('_')[0]) {
				
				case 'movie':
				case 'person':
					obj.removeClass(classes[i]);
					break;
				
			}
			
		}
	}
	
	obj.addClass(_Type + '_' + Rating);
	ResetStars(obj);

}

// The object that is passed in is the UL of the my_rating
function SetNewAverage(obj, Rating, Votes) {
	
	var votes = obj.parent().find('.votes');
	if (Votes == 1)
		votes.html(Votes + ' vote');
	else
		votes.html(Votes + ' votes');

	var classes = obj.attr('class').toLowerCase().split(' ');
	for (var i = 0; i < classes.length; i++) {
		if (classes[i].indexOf('_') != -1) {

			switch (classes[i].split('_')[0]) {
				
				case 'movie':
				case 'person':
					obj.removeClass(classes[i]);
					break;
				
			}
			
		}
	}

	obj.addClass(_Type + '_' + Rating);
	ResetStars(obj);

}

// The object that is passed in is the UL of the my_rating
function GetCurrentRating(obj) {
	
	var classes = obj.attr('class').toLowerCase().split(' ');
	for (var i = 0; i < classes.length; i++) {
		if (classes[i].indexOf('_') != -1) {
		
			switch (classes[i].split('_')[0]) {
				
				case 'movie':
				case 'person':
					return classes[i].split('_')[1];					
					break;
				
			}
			
		}
	}
	
	return -1;
	
}




/*******************************************************************************************************************************
										FACEBOOK SCRIPTS
******************************************************************************************************************************/

function InitializeFacebook() {
	
	/******************************************************************************************************************************************************************************
																http://fbrell.com/examples
	******************************************************************************************************************************************************************************/

	FB.init({
		appId  : _FacebookApplicationId,
		status: true,
		cookie: true,
		xfbml: true
	});

	/*
	jQuery('#facebook #link').click(LinkFacebook);
	jQuery('#facebook #unlink').click(UnlinkFacebook);
	jQuery('#facebook #logout').click(LogoutFacebook);
	*/

	// We are no longer using the javascript version of FB
	jQuery('#account #facebook #link').unbind('click');
	jQuery('#account #facebook #unlink').unbind('click');
	jQuery('#account #facebook #logout').unbind('click');

}

function LinkFacebook() {
	
	var obj = jQuery(this);
	
	FB.login(function(response) {

		// if we dont have a session, just hide the user info
		if (!response.session) return;
		
		// if we have a session, query for the user's profile picture and name
		FB.api(
			{
				method: 'fql.query',
				query: 'SELECT uid, first_name, last_name, contact_email, pic_big, timezone, birthday_date, sex FROM user WHERE uid = ' + FB.getSession().uid
			},
			function(response) {
				var user = response[0];
				
				var url = _AjaxUrl +
					'?action=registerwithfacebook' +
					'&id=' + user.uid + 
					'&first_name=' + escape(user.first_name) +
					'&last_name=' + escape(user.last_name) +
					'&email=' + escape(user.contact_email) +
					'&pic_url=' + escape(user.pic_big) +
					'&timezone=' + escape(user.timezone) +
					'&birthday=' + escape(user.birthday_date) +
					'&sex=' + escape(user.sex);
					
				//prompt('url', url);return false;
				//window.open(url);return false;
				jQuery.getJSON(
					url,
					function(json) {
						if (json.success) {
							
							var segments = location.href.split('/');
							if (jQuery.trim(segments[segments.length - 1].toLowerCase()) == 'profile') {
								
								obj.attr('id', 'unlink').attr('href', _Root + 'facebook/unlink').attr('title', 'Remove the link to your Facebook account').html('Unlink');

								jQuery('#facebook #link').unbind('click');
								jQuery('#facebook #link').click(LinkFacebook);
								
								jQuery('#facebook #unlink').unbind('click');
								jQuery('#facebook #unlink').click(UnlinkFacebook);
								
							}
							else
								location.href = _Root;
							
						}
						else {
							alert(json.message);
						}
					}
				);
				
			}
		);

	}, { perms: 'publish_stream,offline_access,email,user_about_me,user_interests,user_likes,read_friendlists' } );
	return false;
}
function UnlinkFacebook() {
	
	var obj = jQuery(this);
	
	FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {
	
		var url = _AjaxUrl + '?action=unlinkfacebook';
		//prompt('url', url);return false;
		//window.open(url);return false;
		jQuery.getJSON(
			url,
			function(json) {
				if (json.success) {

					obj.attr('id', 'link').attr('href', _Root + 'facebook/link').attr('title', 'Link your Facebook account').html('Link');
					
					jQuery('#facebook #link').unbind('click');
					jQuery('#facebook #link').click(LinkFacebook);
					
					jQuery('#facebook #unlink').unbind('click');
					jQuery('#facebook #unlink').click(UnlinkFacebook);
					
				}
				else {
					alert(json.message);
				}
			}
		);
		
	});
	
	return false;
}
function LogoutFacebook() {
	FB.logout();
	return false;
}
function ShareFacebook() {

	FB.ui({
		method: 'stream.share',
		u: location.href
	});

}
function PostFacebook() {
	
	if (!_CanPostToFacebook) return false;
	
	var id, message, title, description, url, coverUrl;

	var type = 'unknown';
	if (location.href.toLowerCase().indexOf('movies') != -1)
		type = 'movie';
	else if (location.href.toLowerCase().indexOf('actors') != -1)
		type = 'actor';
	
	type = jQuery.trim(type.toLowerCase());
	switch (type) {
		
		case 'movie':

			id = _MovieId;
			description = jQuery.trim(jQuery('#synopsis').html());
			url = 'http://twynkle.com/movies/' + id + '/users/' + _AccountId;
			imageUrl = jQuery.trim(jQuery('#cover>img').attr('src'));

			break;
		
		case 'actor':

			id = _PersonId;
			description = jQuery.trim(jQuery('#bio').html());
			description = description.replace(/(<([^>]+)>)/ig, '');
			if (description.toLowerCase().indexOf('biography') != -1) description = jQuery.trim(description.substring(9));
			url = 'http://twynkle.com/actors/' + id + '/users/' + _AccountId;
			imageUrl = jQuery.trim(jQuery('#headshot>img').attr('src'));

			break;
		
		default:
			// Not set up, so abort
			return false;

	}
		
	title = jQuery.trim(jQuery('#main_info>h1').html());
	message = 'I just rated ' + title + ' on Twynkle! Come and see what I thought!';
	if (imageUrl.toLowerCase().indexOf('no_cover') != -1) imageUrl = 'http://twynkle.com/assets/images/logo_star_large.png';
	//alert('Id: ' + id + '\r\nTitle: ' + title + 'Description: ' + description + '\r\nUrl: ' + url + '\r\nImage Url: ' + imageUrl);return false;
	
	var publish = {
		method:				'stream.publish',
		message:			message,
		attachment: {
			name:			'Twynkle | ' + title,
			caption:		'Twynkle.com',
			description:	description,
			href:			url,
			media:			[{ 
								'type':	'image', 
								'src':	imageUrl,
								'href':	url
							}] 
		},
		action_links: [
			{ text: 'Twynkle', href: 'http://twynkle.com/' }
		],
		user_prompt_message: 'Post a message to your friends'
   };

	FB.ui(publish, function(response) {
		if (response && response.post_id) {
			
			// The Facebook post was published successfully
			var url = _AjaxUrl + '?action=postedtofacebook&';
			switch (type) {
				
				case 'movie':
					url += 'movieid=' + id;
					break;
				
				case 'actor':
					url += 'personid=' + id;
					break;
				
			}
			//prompt('url', url);return false;
			//window.open(url);return false;
			jQuery.getJSON(
				url,
				function(json) {
					if (json.success) {
						//alert('Success');
					}
					else {
						alert(json.message);
					}
				}
			);
	
			_CanPostToFacebook = false;
		}
		else {
			_CanPostToFacebook = false;
		}
	});
		
}