/* FLAG ACTIONS
----------------------------------------------------------------------------- */


function flagger() {


// Add Flag Action Links Unless User is a Moderator or Author


	$('form.flag').each(function() {

		if ($(this).parent().hasClass('widget-network')) {} else {
	
			if ($(this).siblings('ul.actions').hasClass('friend') || $(this).siblings('ul.actions').hasClass('author') || $(this).siblings('ul.actions').hasClass('moderator')) {
			
			} else {
			
				$(this).siblings('ul.actions').append('<li class="modify"><a href="#" class="action flag">Flag for Review</a></li>');
		
			}
			
		}
	
	});
	
	
// Create Flag Reasoning Layer
	
	
	$('form.flag div').each(function() {

		$(this).addClass('flag');
		$(this).prepend('<h4>Flag for Review</h4>');
		$(this).children('select').wrapAll('<label></label>');
		$(this).find('label:first').prepend('<em>Reason</em>');
		$(this).children('textarea').wrapAll('<label></label>');
		$(this).find('label:last').prepend('<em>Comments</em>');
		$(this).append('<a href="#" class="action flag">Send to a Moderator</a>');
		$(this).children('select').change(function() {
		
			if ($('form.flag option:selected').val() == "other") {
				$('form.flag em:last').text('Please detail your reason for flagging this below');		
			} else {
				$('form.flag em:last').text('Comments');
				$('form.flag strong').remove();
			}
		
		});	
	
		$(this).children('a').click(function() {
		
			var form = $(this).parent().parent().attr('id');

			// Reasoning Validation
	
			if ($(this).parent().find('select')[0].selectedIndex == 0 && $('#' + form + ' select + strong').length == 0) {
	
				$('<strong>Required</strong>').insertAfter('#' + form + ' select');
	
			} else if ($(this).parent().find('option:selected').val() == "other" && $(this).parent().find('textarea').val() == 0 && $('#' + form + ' textarea + strong').length == 0) {
				
				$('<strong>Required</strong>').insertAfter('#' + form + ' textarea');
	
			}

			if ($(this).parent().find('select')[0].selectedIndex == 0 || $(this).parent().find('option:selected').val() == "other" && $(this).parent().find('textarea').val() == 0) {

				return false;
				
			} else {

				$('form#' + form).submit();

			}

			return false;
		
		});
	
	});
	
	
// Toggle Reasoning Layer
	
	
	$('ul.actions a.flag').click(function() {

		//$('form.flag div').hide();
		$(this).parent().parent().siblings('form.flag').children('div').toggle();
		return false;
	
	});
		
}