$(document).ready(function() {
	$(document).click(hideAllHideableWindows);
	$('#loginButton').click(openLoginBox);
	$('.hideable').click(function(e) { e.stopPropagation(); });
	$('.fieldWithDefault').focus(fieldWithDefaultReceivedFocus);
	$('.fieldWithDefault').blur(fieldWithDefaultLostFocus);
	$('.fieldWithDefaultPasswordPlaceholder').focus(passwordFieldWithDefaultReceivedFocus);
	$('.fieldWithDefaultPassword').blur(passwordFieldWithDefaultLostFocus);
});

function openLoginBox(e)
{
	e.preventDefault();
	e.stopPropagation();
	$('.boxOpenLogin').show();
}

function hideAllHideableWindows(e) {
	$('.hideable').hide();
}

function fieldWithDefaultReceivedFocus(e) {
	if ($(e.target).data('default') == undefined) $(e.target).data('default', $(e.target).attr('value'));
	if ($(e.target).attr('value') == $(e.target).data('default')) $(e.target).attr('value', '');
}

function fieldWithDefaultLostFocus(e) {
	if ($(e.target).attr('value') == '') $(e.target).attr('value', $(e.target).data('default'));
}

function passwordFieldWithDefaultReceivedFocus(e) {
	$(e.target).hide();
	$('#'+e.target.id.replace('_placeholder','')).show();
	$('#'+e.target.id.replace('_placeholder','')).focus();
}

function passwordFieldWithDefaultLostFocus(e) {
	if ($(e.target).attr('value') == '')
	{
		$(e.target).hide();
		$('#'+e.target.id+'_placeholder').show();
	}
}

// popup stuff
$(document).ready(function() {
	$('.wishlist-box-open').click(loadWishlistPopup);
	$('.users-box-open').click(loadUsersPopup);
	$('.settings-box-open').click(loadSettingsPopup);
});

function destroyPopupContent()
{
	$('#popup-box').html('');
}

function loadWishlistPopup(e)
{
	e.preventDefault();
	$('.loading').show();
	$('#popup-box').load($(this).attr('href'), openWishlistPopup);
}

function openWishlistPopup()
{
	$('#popup-box a.remove').click(loadWishlistRemove);
	$('.loading').hide();
	$('#popup-box').lightbox_me({ centered: true, onClose: destroyPopupContent });
}

function loadWishlistRemove(e)
{
	e.preventDefault();
	$('.loading').show();
	$.get($(this).attr('href'), displayRemove);
}

function displayRemove(data)
{
	$('.loading').hide();
	$('#popup-box #'+data).hide('vertical');
}

function loadUsersPopup(e)
{
	e.preventDefault();
	$('.loading').show();
	$('#popup-box').load($(this).attr('href'), openUsersPopup);
}

function openUsersPopup()
{
	$('.loading').hide();
	$('#popup-box a.remove').click(loadUsersRemove);
	$('#popup-box').lightbox_me({ centered: true, onClose: destroyPopupContent });
}

function loadUsersRemove(e)
{
	e.preventDefault();
	$('.loading').show();
	$.get($(this).attr('href'), displayRemove);
}

function loadSettingsPopup(e)
{
	e.preventDefault();
	$('.loading').show();
	$('#popup-box').load($(this).attr('href'), openSettingsPopup);
}

function openSettingsPopup()
{
	$('.loading').hide();
	$('#popup-box form').ajaxForm(processSettingsFormSubmit);
	$('#popup-box a.tab').click(loadSettingsPopupTab);
	$('#popup-box input.submit').click(submitSettingsForm);
	$('#popup-box').lightbox_me({ centered: true, onClose: destroyPopupContent });
}

function loadSettingsPopupTab(e)
{
	e.preventDefault();
	$('.loading').show();
	$('#popup-box a.tab').removeClass('hover');
	$(this).addClass('hover');
	$('#popup-box #tab-content').hide();
	$('#popup-box #tab-content').load($(this).attr('href'), changeSettingsPopupTab);
}

function changeSettingsPopupTab()
{
	$('.loading').hide();
	$('#popup-box form').ajaxForm(processSettingsFormSubmit);
	$('#popup-box #tab-content').show();
}

function submitSettingsForm(e)
{
	$('.loading').show();
	$('#popup-box #user_language_language *').attr('selected', 'selected');
	var form = $(this).parents('#popup-box').find('form');
	$(form).submit();
}

function processSettingsFormSubmit(data)
{
	if (data != '0')
	{
		//alert('Your settings were successfully saved.');
		location.reload();
	} else {
		alert('There was a problem saving your settings. Please check your form entries and try again.')
		$('.loading').hide();
	}
}

// wishlist stuff
$(document).ready(function() {
	$('.ajax-wishlist').hover(showLabel, hideLabel);
	$('.ajax-wishlist').click(wishlistAjaxLoad);
});
function showLabel() { $(this).children('small').show(); }
function hideLabel() { $(this).children('small').hide(); }

function wishlistAjaxLoad(e) {
	e.preventDefault();
	var id = $(this).attr('id');
	$('.ajax-wishlist').unbind();
	$('.loading').show();
	$('#movie'+id).load($(this).attr('href'), wishlistAjaxDisplay);	
}
function wishlistAjaxDisplay() {
	$('.ajax-wishlist').hover(showLabel, hideLabel);
	$('.ajax-wishlist').click(wishlistAjaxLoad);
	$('.loading').hide();
}

// search stuff
var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();

$(document).ready(function() {
  $('#term_header').keyup(function(key) {
		input = this;
	  if(input.value.length == 0)
		{
			$('#results_header').hide();
		} else {
	    delay(function(){
				if (input.value.length > 0)
				{
				  $('#results_header').load($(input).parents('form').attr('action'), { term: input.value }, updateResultsHeader);
				} else {
					$('#results_header').hide();
				}
	    }, 350);
		}
  });
	$('#searchform_header').submit(function() {
		var val = $.trim($('#term_header').val());
		$('#term_header').val(val.replace('.', ''));
	});
});

function updateResultsHeader() {
	$('#results_header').show();
	if($('#term_header').val() == '') { $('#results_header').hide(); }
}

// voting and reporting stuff
$(document).ready(function() {
	$('ul.review-listing li').hover(showVoteBar, hideVoteBar);
	$('.reports button').click(castVote);
	$('p.spoiler').click(function(){ $(this).hide(); });
});

function showVoteBar() {
	$(this).find('div.reports').css('visibility', 'visible');
}
function hideVoteBar() {
	$(this).find('div.reports').css('visibility', 'hidden');
}

function castVote(e) {
	e.preventDefault();
	if ($(this).parent('div').hasClass('reporting') && !$(this).hasClass('select')) {
		var confirmed = false;
		if ($(this).hasClass('spoiler')) confirmed = confirm('Spoiler reports should be sent when a review contains too much information about a movie, compromising the experience of watching it. If that is what you intend to report, please click \'OK\'.');
		if ($(this).hasClass('offensive')) confirmed = confirm('Offensive review reports should be sent when a review is written in a way that might upset other users - for example, racist remarks, direct attacks against other users, and so on. If that is what you intend to report, please click \'OK\'.');
		if ($(this).hasClass('spam')) confirmed = confirm('Spam reports should be sent when a review contains commercial messages and misleading links. If that is what you intend to report, please click \'OK\'.');
		if (confirmed)
		{
			$('.loading').show();
			$('.reports button').unbind('click');
			$(this).parent('div').load($(this).val(), updateVoteHTMLWithMessage);			
		}
	} else {
		$('.loading').show();
		$('.reports button').unbind('click');
		$(this).parent('div').load($(this).val(), updateVoteHTML);
	}
}
function updateVoteHTML() {
	$('.reports button').click(castVote);
	$('.loading').hide();
}
function updateVoteHTMLWithMessage() {
	updateVoteHTML();
	alert('Your report has been registered. We\'ll investigate the issue in the next 24 hours and take the necessary measures. Thanks for taking the time to report this review - your support is appreciated!');
}
