$(function() {
	$.preloadImages('/images/spinner.gif', '/images/button_add_to_collection.gif', '/images/button_remove_from_collection.gif');
	
	$('#filtyb,#fildb').bind('click', function(e) {
		e.stopPropagation();	
	});
	
	$(document).click(function(f) {
		$("#filtyb,#fildb").fadeOut();
	});
	
	$('#thankslink').click(function() {
		thankslink_listener();
	});
	
	function thankslink_listener() {
		if($('.spinner').size() != 0) {
			alert('Please wait for the other operation to complete...');
			return;
		}	
	
		if($('#thanksspinner').size() == 0) {
			$('#thanksspan').html('<span id="thanksspinner"><img src="/images/spinner.gif" width="16" height="16" class="spinner"></span>');
			$.get('/files/ajax-thanks/' + $('#file_id').val(),
			function(data) {
				data_split = data.split('|');
				status = data_split[0];
				switch(status) {
					case 'thanks_added':
						thanks_counter = data_split[1];
						if(thanks_counter == 1) {
							thanks_count = '1 time';
						} else {
							thanks_count = thanks_counter + ' times';
						}
						$('#thankscount').html('<a href="#nh" id="thankscountlink" title="See who thanked the uploader for this file"><strong>' + thanks_count + '</strong></a>');
						$('#thanksspan').html(' <img src="/images/icon_thanks_gray.gif" width="16" height="16" id="thanks_icon" alt="You have already thanked the uploader for this file..." title="You have already thanked the uploader for this file..."/>');
						$('#thankscountlink').click(function() {
							thank_you_count_click_listener();
						});
						message = ' - You have successfully thanked the uploader for this file.';
						break;
					case 'already_thanked':
						message = ' - You have already thanked the uploader for this file.';
						break;
					case 'file_does_not_exist':
						message = ' - File does not exist.';
						break;
					case 'own_file':
						message = ' - You can\'t thank yourself.';
						break;
					default:
						message = ' - Something went wrong.';
				}
				$('#thanksspinner').remove();
				// $('#thanksspan').html(message);
			});
		}
	}
	
	$('#addtocollectionbutton').click(function() {
		addtocollectionlistener();
	});
	
	function addtocollectionlistener() {
		if($('.spinner').size() != 0) {
			alert('Please wait for the other operation to complete...');
			return;
		}
		
		if($('#collectionspinner').size() == 0) {
			$('#collectionspan').after('<img src="/images/spinner_gray.gif" width="16" height="16" class="spinner" id="collectionspinner">');
			$.get('/files/ajax-collection-add/' + $('#file_id').val(),
			function(data) {
				switch(data) {
					case 'added':
						message = 'You have successfully added this file to your collection.';
						break;
					case 'already_in_collection':
						message = 'You have already added this file to your collection.';
						break;
					case 'file_does_not_exist':
						message = 'File does not exist.';
						break;
					case 'own_file':
						message = 'You can\'t add your own file to your collection.';
						break;
					default:
						message = 'Something went wrong.';
				}
				$('#collectionspinner').remove();
				$('#collectionspan').html('<a href="#nh" id="removefromcollectionbutton"><span>Remove from collection</span></a>');
				$('#removefromcollectionbutton').click(function() {
					removefromcollectionlistener();
				});
			});
		}
	}
	
	$('#removefromcollectionbutton').click(function() {
		removefromcollectionlistener();
	});
	
	function removefromcollectionlistener() {
		if($('.spinner').size() != 0) {
			alert('Please wait for the other operation to complete...');
			return;
		}
		
		if($('#collectionspinner').size() == 0) {
			$('#collectionspan').after('<img src="/images/spinner_gray.gif" width="16" height="16" class="spinner" id="collectionspinner">');
			$.get('/files/ajax-collection-remove/' + $('#file_id').val(),
			function(data) {
				switch(data) {
					case 'removed':
						message = 'You have successfully removed this file from your collection.';
						break;
					case 'not_in_collection':
						message = 'You didn\'t have this file in your collection.';
						break;
					case 'file_does_not_exist':
						message = 'File does not exist.';
						break;
					case 'own_file':
						message = 'You can\'t remove your own file.';
						break;
					default:
						message = 'Something went wrong.';
				}
				$('#collectionspinner').remove();
				$('#collectionspan').html('<a href="#nh" id="addtocollectionbutton"><span>Add to collection</span></a>');
				$('#addtocollectionbutton').click(function() {
					addtocollectionlistener();
				});
			});
		}
	}
		
	$('#thankscountlink').click(function() {
		thank_you_count_click_listener();
	});
	
	function thank_you_count_click_listener() {
		if($('.spinner').size() != 0) {
			alert('Please wait for the other operation to complete...');
			return;
		}
		
		if($('#latestthanksspinner').size() == 0) {
			thanksspan_contents = $('#thanksspan').html();
			$('#thanksspan').html('<span id="latestthanksspinner"><img src="/images/spinner.gif" width="16" height="16" class="spinner"></span>');
			$.get('/files/ajax-who-thanked/' + $('#file_id').val(),
			function(data) {
				$('#filtybc').html(data);
				$('#thanksspan').html(thanksspan_contents);
				$('#thankslink').click(function() {
					thankslink_listener();
				});
				$("#filtyb").css({
					left:$('#thankscountlink').offset().left - 165,
					top:$('#thankscountlink').offset().top + 20
				}).fadeIn();
			});
		}
	}
	
	$('#downloadcountlink').click(function() {
		if($('.spinner').size() != 0) {
			alert('Please wait for the other operation to complete...');
			return;
		}
		
		if($('#latestdownloadsspinner').size() == 0) {
			$('#downloadcountlink').after('<span id="latestdownloadsspinner"><img src="/images/spinner.gif" width="16" height="16" class="spinner"></span>');
			$.get('/files/ajax-who-downloaded/' + $('#file_id').val(),
			function(data) {
				$('#fildbc').html(data);
				$('#latestdownloadsspinner').remove();
				$("#fildb").css({
					left:$('#downloadcountlink').offset().left - 165,
					top:$('#downloadcountlink').offset().top + 20
				}).fadeIn();
			});
		}
	});
	
	$('#togglelinkingoptions').click(function() {
		if($('#linkingoptions').is(':visible')) {
			$('#linkingoptions').slideUp(function() {
				$('#togglelinkingoptions').html('Reveal linking options.');
			});
		}
		
		if(!$('#linkingoptions').is(':visible')) {
			$('#linkingoptions').slideDown(function() {
				$('#togglelinkingoptions').html('Hide linking options.');
			});
		}
	})
	
	$('.code_textarea').focus(function() {
		$(this).select();
	})
	
	$('.ctc').click(function() {
		$.copy($('#' + $(this)[0].id.split('_')[1]).val());
	})
});

jQuery.preloadImages = function() {
	for(var i = 0; i < arguments.length; i++) {
		jQuery('<img>').attr('src', arguments[i]);
	}
}
