$(function($) {

	$('#flagContent-dialog').dialog({
		autoOpen: false,
		width: 490,
		resizable: false,
		modal: true,
		zIndex : 12000,
		overlay: {
			opacity: 0.5,
			background: 'black'
		}
	});	

	
	var flagContent = function(obj) {
		$('#flagContent-dialog input[type=checkbox]').each(function(i){
			this.checked = false;
		});
		$('#flagComment').val('');
		$('#flagContent-dialog').dialog('open');
		
		var cId = $(obj).attr('contentId');
		var cType = $(obj).attr('contentType');
		
		$('#flagContentId').attr('value', cId);
		$('#flagContentType').attr('value', cType);		
	};
	
	$(".flagContent").live('click', function(event) {
		event.preventDefault();
		event.stopPropagation();
		var elem = this;
		loginCheck(function(){flagContent(elem);});		
	});

	
	$('#submitFlag').click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		
		var fId = 0;
		if($('#offensiveFlag').attr('checked')) {
			fId += 1;
		}
		if($('#duplicateFlag').attr('checked')) {
			fId += 2;
		}
		if($('#spamFlag').attr('checked')) {
			fId += 4;
		}
		if($('#otherFlag').attr('checked')) {
			fId += 8;
		}
		if($('#inaccurateFlag').attr('checked')) {
			fId += 16;
		}

		var data = {
			contentId: $('#flagContentId').attr('value'),
			contentType: $('#flagContentType').attr('value'),
			comment: $('#flagComment').attr('value'),
			flagId: fId 
		};

		$.post(baseUrl+'admin/flag/flag-content', data, function(data) {
			if(data[0].flag=="success") {
				alert('This content has been flagged.');
			} else {
				alert('Error flagging content.');
			}}, 'json');
		
		$('#flagContent-dialog').dialog('close');
	});
	
});

