$(function() {
	
	// Find all links with the class 'report_content' and initiate a modal 
	$("a.report_content_link").removeAttr('href').click(function(){
		
		var current = $(this);
		
		if (current.parents('.comment').size() > 0) {
			var target_type = 'comment';
			var target_id = current.parents('.comment').attr('data-id');
		} else if (current.parents('.subcomment').size() > 0) {
			var target_type = 'comment';
			var target_id = current.parents('.subcomment').attr('data-id'); 
		} else if (current.parents('#article_column').size() > 0) {
			var target_type = 'article';
			var target_id = current.parents('#article_column').attr('data-id'); 
		}
		
		var data = {			
		}
		
		$.ajax({
			'url' : '/report_content/' + target_type + '/' + target_id + '/', 
			'type' : 'GET',
			'data' : data,
			'success' : function(data) {
				ajaxSuccess(data);
			},
			'error' : function() {
				ajaxError();
			},
			'dataType': 'html'
					
		});
		
		function ajaxSuccess(data) {
						
			current.qtip({
				content: {
					title: {
						text: 'Report Inappropriate Content',
						button: 'Close'
					},
					text: data
				},
				position: {
					target: $(document.body),
					corner: 'center'
				},
				show: {
					ready: true
				},
				hide: false,
				api: {
					beforeShow: function() {
						$('#qtip-blanket').fadeIn(this.options.show.effect.length);
					},
					beforeHide: function() {
						$('#qtip-blanket').fadeOut(this.options.hide.effect.length);
					},
					onHide: function() {
						current.qtip("destroy");
					}
				},
				style: {
					width: {
						max: 700
					},
					border: {
						width: $.js_style.tt_border,
						radius: $.js_style.tt_radius,
						color: $.js_style.tt_border_colour
					},
					title: {
						backgroundColor: $.js_style.tt_title_background,
						color: $.js_style.tt_title_colour
					}
				}
				
			});
		}
		
		function ajaxError() {
			alert('There was an error communicating with the server, please try again later');
		}
		
	});	
	
	$('<div id="qtip-blanket">').css({
		position: 'fixed',
		top: 0,
		left: 0,
		height: $('#view').css('height'),
		width: '100%',
		opacity: 0.7,
		backgroundColor: '#000000',
		zIndex: 5000	
	}).appendTo(document.body).hide();
	
});
