/* SILVERBACK */
/* Action Link Functions
----------------------------------------------------------------------------- */


	$(document).ready(function() {

	});


/* FULL PAGE LOAD EVENT (fires when all page assets have loaded)
----------------------------------------------------------------------------- */


	$(window).load(function() {


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


// 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').length == 0) {

				$('<ul class="actions"><li class="modify"><a href="#" class="action flag">Flag for Review</a></li></ul>').insertAfter(this);

			} 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).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('input').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');
				return false;
	
			} 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');
				return false;
	
			}

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

				return false;
				
			}
			
			window.scrollTo(0,0);
	
		});
	
	});


// Toggle Reasoning Layer


	$('ul.actions a.flag').click(function() {

		$('form.flag div').hide();
		$(this).parent().parent().siblings('form.flag').children('div').show();

		var anchor = $(this).parent().parent().siblings('form.flag').attr('id');
		window.location.hash = anchor;

		$(this).parent().parent().remove();
		$('form.flag textarea').focus();
		return false;
	
	});
	

/* END FULL PAGE LOAD EVENT
----------------------------------------------------------------------------- */


	});