
function actionInteract(){

	if (window.location.search != '') {
	
		get = window.location.search.replace('?', '');
		get = get.split('&');
		for (i = 0; get[i] != null; i++) {
			post = get[i].split('=');
			
			if (post[0] == 'id') {
				postId = post[1];
				
				$.post('/utilities/action', {
					'action': 'actionToCookie',
					'postId': postId
				}, function(data){
				
					Cookies.postUpdate(data.postId, 'new', data.source);
					
				}, 'json');
			}
		}
	}
	
	if ($('.blueLink[name=newAction]').length) {
		$('.blueLink[name=newAction]').unbind('mouseup');
		$('.blueLink[name=newAction]').mouseup(function(){
		
			var pathName = window.location.pathname.toLowerCase();
			var source = pathName.split('/');
			if (source[2] == null) {
				source[2] = 'internet';
			}
			source = source[1] + '/' + source[2];
			
			$.post('/utilities/action', {
				'action': 'newActionForm',
				'source': source
			}, function(data){
				$('body').append('<div id=actionTemp>');
				$('#actionTemp').css({
					'visibility': 'hidden',
					'position': 'fixed'
				});
				$('#actionTemp').text($('#contentMainContainer').html());
				$('#contentMainContainer').html(data.content);
				
				var newActionCancel = new button('.formButtonOver[name=actionFormMainCancel]', '.formButton[name=actionFormMainCancel]', null, null, function(){
					
					cancelNewAction();
					
				});
				var newActionSubmit = new button('.formButtonOver[name=actionFormMainSubmit]', '.formButton[name=actionFormMainSubmit]', null, null, function(){
					
					if (Cookies.get('userId')) {
						
						$.post('/utilities/user', {
							'action': 'checkRegistration'
						}, function(data){
							if (data.status == 'complete') {
								submitNewAction();
							}
							else {
								alert('Please complete registration.');
							}
						}, 'json');
					}
					else {
						alert('You must be signed in to post content.');
					}
				});
				
			}, 'json');
		});
	}
	
	if ($('.actionList').length) {
		$('.actionList').mouseover(function(){
			var postId = $(this).attr('name');
			$('.postRatingContainer[name=' + postId + ']').css({
				'visibility': 'visible'
			});
			
			$('.postRatingContainer[name!=' + postId + ']').css({
				'visibility': 'hidden'
			});
		});
		
		$('.actionList').mouseout(function(){
			var postId = $(this).attr('name');
			$('.postRatingContainer[name=' + postId + ']').css({
				'visibility': 'hidden'
			});
		});
	}
	
	if ($('.actionListParticipate,.actionListUnparticipate').length){
		actionListParticipate();
	}
	function actionListParticipate(){
		$('.actionListParticipate,.actionListUnparticipate').mousedown(function(){
			var postId = $(this).attr('name');
			
			$.post('/utilities/action', {
				'action': 'actionParticipate',
				'postId': postId
			}, function(data){
				if (data.status == 'success') {
					if (data.content == 'Unparticipate') {
						$('.actionListParticipate[name=' + postId + ']').replaceWith("<span class=actionListUnparticipate name=" + postId + ">" + data.content + "</span>");
						
					}
					else {
						$('.actionListUnparticipate[name=' + postId + ']').replaceWith("<span class=actionListParticipate name=" + postId + ">" + data.content + "</span>");
					}
					
					$('.actionListParticipate,.actionListUnparticipate').unbind('mousedown');
					actionListParticipate();
				}
			}, 'json');
		});
	}
	
	/* Get Action */
	if ($('.formButtonOver[name=actionTopicCommentSubmit]').length) {
		var actionCommentSubmit = new button('.formButtonOver[name=actionTopicCommentSubmit]', '.formButton[name=actionTopicCommentSubmit]', null, null, function(){
			submitNewActionComment();
			
		});
	}
	
	if ($('.actionTopicNavigation').length) {
		$('.actionTopicNavigation').mousedown(function(){//Interesting, this does not update the navigate button
			var postId = $('#actionTopicCommentForm').attr('name');
			var page = $(this).attr('name');
			
			$.post('/utilities/action', {
				'action': 'actionTopicNavigate',
				'postId': postId,
				'page': page
			}, function(data){
				$('.actionTopicCommentList').html(data.content);
				$('.pageNavigation').replaceWith(data.navigate);
				actionInteract();
			}, 'json');
		});
	}
	
	if ($('.actionTopicCommentListCell').length) {
		$('.actionTopicCommentListCell').mouseover(function(){
			var postId = $(this).attr('name');
			$('.actionTopicCommentReplyOpen[name=' + postId + ']').css({
				'visibility': 'visible'
			});
			
			$('.actionTopicCommentReplyOpen[name!=' + postId + ']').css({
				'visibility': 'hidden'
			});
			
			$('.postRatingContainer[name=' + postId + ']').css({
				'visibility': 'visible'
			});
			
			$('.postRatingContainer[name!=' + postId + '][title!=true][type!=commentReply]').css({
				'visibility': 'hidden'
			});
		});
		
		$('.actionTopicCommentListCell').mouseout(function(){
			var postId = $(this).attr('name');
			$('.actionTopicCommentReplyOpen[name=' + postId + ']').css({
				'visibility': 'hidden'
			});
			
			$('.postRatingContainer[name=' + postId + '][title!=true]').css({
				'visibility': 'hidden'
			});
		});
	}
	
	if ($('.actionTopicCommentReplyOpen').length) {
		$('.actionTopicCommentReplyOpen').mousedown(function(){
		
			var postId = $(this).attr('name');
			
			$.post('/utilities/action', {
				'action': 'commentReplyOpen',
				'postId': postId
			}, function(data){
				if ($('#actionTopicCommentReplyForm').length) {
					var formId = $('#actionTopicCommentReplyForm').attr('name');
					$('#actionTopicCommentReplyForm').replaceWith('<div class=actionTopicCommentReplyOpen name=' + formId + '>Reply</div>');
					actionInteract();
				}
				$('.actionTopicCommentReplyOpen[name=' + postId + ']').replaceWith(data.content);
				
				var newActionCancel = new button('.formButtonOver[name=actionTopicCommentReplyCancel]', '.formButton[name=actionTopicCommentReplyCancel]', null, null, function(){
					cancelActionCommentReply();
				});
				var newActionSubmit = new button('.formButtonOver[name=actionTopicCommentReplySubmit]', '.formButton[name=actionTopicCommentReplySubmit]', null, null, function(){
					submitActionCommentReply();
				});
				
			}, 'json');
			
		});
	}
	
	if ($('.formButtonOver[name=actionTopicCommentReplyCancel]').length) {//hmm
	
		var newActionCancel = new button('.formButtonOver[name=actionTopicCommentReplyCancel]', '.formButton[name=actionTopicCommentReplyCancel]', null, null, function(){
			cancelActionCommentReply();
		});
		var newActionSubmit = new button('.formButtonOver[name=actionTopicCommentReplySubmit]', '.formButton[name=actionTopicCommentReplySubmit]', null, null, function(){
			submitActionCommentReply();
		});
	}
	
	if ($('.actionTopicCommentReply').length) {
		$('.actionTopicCommentReply').mouseover(function(){
		
			var postId = $(this).attr('name');
			
			$('.postRatingContainer[name=' + postId + '][title!=true]').css({
				'visibility': 'visible'
			});
			
			$('.postRatingContainer[name!=' + postId + '][title!=true][type=commentReply]').css({
				'visibility': 'hidden'
			});
			
		});
		$('.actionTopicCommentReply').mouseout(function(){
		
			var postId = $(this).attr('name');
			
			$('.postRatingContainer[name=' + postId + '][title!=true][type=commentReply]').css({
				'visibility': 'hidden'
			});
			
		});
	}
	
	function submitNewActionComment(){
	
		var postId = $('#actionTopicCommentForm').attr('name');
		var content = $('.actionTopicCommentInput').attr('value');
		
		$.post('/utilities/action', {
			'action': 'submitActionComment',
			'postId': postId,
			'content': content
		}, function(data){
			if (data.status == 'success') {
				$('.actionTopicCommentList').append(data.content);
				$('.actionTopicCommentInput').replaceWith('<textarea class=actionTopicCommentInput></textarea>');
			}
		}, 'json');
	}
	
	function submitActionCommentReply(){
	
		var content = $('.subReplyTextarea').attr('value');
		var postId = $('#actionTopicCommentReplyForm').attr('name');
		
		$.post('/utilities/action', {
			'action': 'actionCommentReply',
			'postId': postId,
			'content': content
		}, function(data){
		
			if (data.status == 'success') {
			
				$('#actionTopicCommentReplyForm').replaceWith(data.content + '<div class=actionTopicCommentReplyOpen name=' + postId + '>Reply</div>');
				actionInteract();
			}
		}, 'json');
		
		var postId = $('#actionTopicCommentReplyForm').attr('name');
		
	}
	
	function cancelActionCommentReply(){
		var postId = $('#actionTopicCommentReplyForm').attr('name');
		$('#actionTopicCommentReplyForm').replaceWith('<div class=actionTopicCommentReplyOpen name=' + postId + '>Reply</div>');
		actionInteract();
	}
	
	function cancelNewAction(){
		var temp = $('#actionTemp').text();
		$('#contentMainContainer').html(temp);
		$('#actionTemp').remove();
		actionInteract();
	}
	
	function submitNewAction(){
	
		var digitFilter = /^[0-9]+$/g;
		
		var source = $('.actionFormMainSelect').attr('value');
		source = 'action/' + source.toLowerCase().replace(' ', '');
		
		var title = $('.actionFormMainInputSmall[name=title]').attr('value');
		var activationNumber = $('.actionFormMainInputSmall[name=activationNumber]').attr('value');
		
		var action = $('.actionFormMainInput[name=action]').attr('value');
		var reason = $('.actionFormMainInput[name=reason]').attr('value');
		var result = $('.actionFormMainInput[name=result]').attr('value');
		if (source != '' && title != '' && activationNumber != '' && digitFilter.test(activationNumber) && activationNumber >= 10 && activationNumber <= 10000 && action != '' && reason != '' && result != '') {
		
			$('.formButtonOver[name=actionFormMainSubmit]').unbind('mouseup');
			
			$.post('/utilities/action', {
				'action': 'submitNewAction',
				'source': source,
				'title': title,
				'activationNumber': activationNumber,
				'userAction': action,
				'reason': reason,
				'result': result
			}, function(data){
			
				if (data.status == 'success') {
					window.location = '/action?id=' + data.postId;
				}
				
				if (data.status == 'lowRating') {
					alert('User Rating too low');
				}
			}, 'json');
		}
		else {
			$('.actionFormMainErrorText[name=main]').text('*Please fill out form correctly');
			
			if (title == '') {
				$('.actionFormMainErrorText[name=title]').text('*Required');
			}
			
			if (activationNumber == '') {
				$('.actionFormMainErrorText[name=activationNumber]').text('*Required');
			}
			else {
			
				if (!digitFilter.test(activationNumber)) {
					$('.actionFormMainErrorText[name=activationNumber]').text('*Numbers Only');
				}
				
				if (activationNumber < 10) {
					$('.actionFormMainErrorText[name=activationNumber]').text('*Minimum 10');
				}
				
				if (activationNumber > 10000) {
					$('.actionFormMainErrorText[name=activationNumber]').text('*Maximum 10000');
				}
			}
			
			if (action == '') {
				$('.actionFormMainErrorText[name=action]').text('*Required');
			}
			
			if (reason == '') {
				$('.actionFormMainErrorText[name=reason]').text('*Required');
			}
			
			if (result == '') {
				$('.actionFormMainErrorText[name=result]').text('*Required');
			}
			
			$('.actionFormMainInputSmall').focus(function(){
				var name = $(this).attr('name');
				clearErrorText(name);
			});
			
			$('.actionFormMainInput').focus(function(){
				var name = $(this).attr('name');
				clearErrorText(name);
			});
			
			function clearErrorText(name){
				$('.actionFormMainErrorText[name=' + name + ']').text('');
				$('.actionFormMainErrorText[name=main]').text('');
			}
		}
	}
}

