/*
 * General
 */

$(document).ready(function(){
	
	//This might eventually get the correct intro data based off of what the location it is.
	
	objects();
	Cookies.cookieToTabs('general');
	statisticsMenuInteract();
	pageOrientation();
	chromeIntegration();
	
});

function chromeIntegration(){
	var browser = new browserDetection();
	if (browser.name == 'Chrome'){
		
		if (Cookies.get('userId')){
			var userId = Cookies.get('userId');
			var userKey = Cookies.get('userKey');
			$.post('/utilities/chrome', {
				'action' : 'refresh',
				'userId' : userId,
				'userKey' : userKey
			}, function(data){
				if (data.status == 'valid'){
					Cookies.set('userId', userId, 60*24*5);
					Cookies.set('userKey', userKey, 60*24*5);
				}
				else{
					Cookies.set('userId', 'nothing', -1);
					Cookies.set('userKey', 'nothing', -1);
				}
			}, 'json');
		}
	}
}


function pageOrientation(){

	var source = window.location.pathname.toString();
	var sourceString = source.replace(/(\/)(\w*)(\/*)(\w*)(\/*)(.*)/g,"$2$3$4");
	var sourceRoot = source.replace(/(\/)(\w*)(\/*)(\w*)(\/*)(.*)/g,"$2");
	
	var sourceTitle = source.replace(/(\/)(\w*)(\/*)(\w*)(\/*)(.*)/g,"$2 | Beneverywhere").capitalize();
	
	$('input[name="source"]').attr('value', sourceString);
	
	if(sourceRoot == ''){
		$('title[name=title]').text('Beneverywhere');
	}
	else{
		$('title[name=title]').text(sourceTitle);
	}
	
	if (sourceRoot == 'discussion'){
		loadDiscussion();
	}
	
	if (sourceRoot == 'action'){
		loadDiscussion();
		//actionInteract();
	}
	
	if (sourceRoot == 'settings'){
		settingsInteract();
	}
}

/*
 * Functions
 */

function menuMove(){
	
	var x;
	var y;
		
		$("#statisticsMenuRight").mousedown(function(e){
			x = Cookies.cookieToPosition('menuPosition').x;
			y = Cookies.cookieToPosition('menuPosition').y;
		
		x1 = e.clientX;
		y1 = e.clientY;
		
		window.onmousemove = function(e){
			if (x1 != 0 || y1 != 0){
			
				dx = e.clientX - x1;
				dy = e.clientY - y1;
			
				x = x + dx;
				y = y + dy;
			
				$("#statisticsMenu").css({
					'left' : x,
					'top' : y
				});
			}
		
				x1 = e.clientX;
				y1 = e.clientY;
				
				Cookies.positionToCookie('menuPosition', x, y);
			}
	});
	
	$("#statisticsMenuRight").mouseup(function(){
		window.onmousemove = null;
	});
	
	//Footer
	$("#statisticsMenuFooter").mousedown(function(e){
		
			x = Cookies.cookieToPosition('menuPosition').x;
			y = Cookies.cookieToPosition('menuPosition').y;
			
			x1 = e.clientX;
			y1 = e.clientY;
			
			window.onmousemove = function(e){
				if (x1 != 0 || y1 != 0) {
				
					dx = e.clientX - x1;
					dy = e.clientY - y1;
					
					x = x + dx;
					y = y + dy;
					
					$("#statisticsMenu").css({
						'left': x,
						'top': y
					});
				}
				
				x1 = e.clientX;
				y1 = e.clientY;
				
				Cookies.positionToCookie('menuPosition', x, y);
			}
	});
	
	$("#statisticsMenuFooter").mouseup(function(){
		window.onmousemove = null;
	});
	
}

function loadDiscussion(){
	mainDiscussionInteract();
	
}

function replyFormInteract(){
	
	$('#replyFormHome').mousedown(function(){ //Consider splitting this function up into smaller parts featuring one that would hold just the menuTabs
		
		if ($('#contentAllContainer').attr('name') == 'crapWrapper'){
			
			$('#contentAllContainer').css({
				'border' : 'none',
				'min-height' : '0',
				'width' : '100%',
			});
			
			$('#top').css({
				'height' : '0'
			});
			
			$('#footer_container').css({
				'height' : '0'
			});
		}
		
		replyWindowSwitch('discussion', null);
		
		$('#comMode').attr('name', 'discussion');
		
		document.body.scrollTop = $('#userStatus').attr('userScrollTop');
		
	});
	
	$('#replyFormClose').mousedown(function(){
		
		if ($('#contentAllContainer').attr('name') == 'crapWrapper'){
			
			$('#contentAllContainer').css({
				'border' : 'none',
				'min-height' : '0',
				'width' : '100%',
			});
			
			$('#top').css({
				'height' : '0'
			});
			
			$('#footer_container').css({
				'height' : '0'
			});
		}
		
		replyWindowSwitch('discussion', null);
		
		$('#comMode').attr('name', 'discussion');
		
		document.body.scrollTop = 0;
		
		postNumber = $(this).attr('name');

		$(".statisticsTabs[name="+postNumber+"]").remove();
			
		Cookies.tabsToCookie();
		
		
	});
}

function contentFormSubmit(){
	
	var postLoop;
	
	var valid = true;
	
	if (postLoop != null) {
		clearInterval(postLoop);
	}
	
	if ($('#content_form_title').length && $('#content_form_title').val().length > 50) {
		alert('Title contains ' + $('#content_form_title').val().length + 'characters, must not be more than 50.');
		
		valid = false;
	}
	
	if (valid == true) {
		
		postLoop = setInterval(function(){
		
			if ($('.uploadTarget[name=replyTarget]').contents().find('#postComplete').length) {
				clearInterval(postLoop);
				postLoop = null;
				
			
				if ($('#comMode').attr('name') == 'reply') {//Could change the whole comMode thing into the user status tag
				
					$('#replyList').append($('.uploadTarget[name=replyTarget]').contents().find('#postComplete').html());
					replyDiscussionInteract();
				}
				else {
				
					$('#replyList').prepend($('.uploadTarget[name=replyTarget]').contents().find('#postComplete').html());
					
					var source = window.location.pathname.toString();
					var sourceString = source.replace(/(\/)(\w+)(\/*)(\w*)(\/*)(.*)/g,"$2$3$4");
					
					Cookies.postUpdate($('.uploadTarget[name=replyTarget]').contents().find('.postReply').attr('name'), 'new', sourceString);
				}
				
				$('#content_form_textarea').val("");
				
				$('#content_form_title').val("");
				
				$('#contentFormFileUnder').replaceWith("<input type=file name=image id=contentFormFileUnder></input>");
				
				$('#contentFormFileOver').text('');
				$('.uploadTarget[name=replyTarget]').contents().find('#postComplete').remove();
				
				$('#contentFormFileClose').css({
					'visibility': 'hidden'
				});
				
				if ($('#comMode').attr('name') == 'reply') {
					document.body.scrollTop = document.body.scrollHeight;
				}
				
				
				menuInteract();
				mainDiscussionInteract();
			}
			
			if ($('.uploadTarget[name=replyTarget]').contents().find('#fileExists').length) {
				clearInterval(postLoop);
				postLoop = null;
			
				alert('Image Exists');
				$('.uploadTarget[name=replyTarget]').contents().find('#fileExists').remove();
				
				menuInteract();
				mainDiscussionInteract();
			}
			
		}, 50);
		
	}
}

function mainDiscussionInteract(){
	
	$('#contentMainContainer');
	
	/*
	 * Content Form
	 */
	
	$('#contentFormFileUnder').change(function(){
		$('#contentFormFileOver').text($('#contentFormFileUnder').val());
				
		$('#contentFormFileClose').css({
			'visibility' : 'visible'
		});
				
	});
		
	$('#contentFormFileClose').mousedown(function(){
		$('#contentFormFileClose').css({
			'visibility' : 'hidden'
		});
		
		$('#contentFormFileUnder').replaceWith('<input id=contentFormFileUnder type=file name=image></input>');
		
		$('#contentFormFileOver').text('');
		
		mainDiscussionInteract();
	});
	
	
	
	$('#contentFormSubmit').mouseup(function(){
		
		$('#contentFormSubmitImage').css({
			'background-image' : 'url(/images/site/forms/menuButton.png)'
		});
		
		if (Cookies.get('userId')){
			contentFormSubmit();
		}
		else{
			alert('You must be signed in to post content.')
		}
	});
	
	
	$('#contentFormSubmit').mousedown(function(){
		
		$('#contentFormSubmitImage').css({
			'background-image': 'url(/images/site/forms/menuButtonDown.png)'
		});
	
		$('#contentFormSubmit').mouseover(function(){
			$('#contentFormSubmitImage').css({
				'background-image': 'url(/images/site/forms/menuButtonDown.png)'
			});
		});
		
		$('#contentFormSubmit').mouseout(function(){
			
			$('#contentFormSubmitImage').css({
				'background-image': 'url(/images/site/forms/menuButton.png)'
			});
		});
		
		$(document).mouseup(function(){
			
			$('#contentFormSubmit').unbind('mouseover');
			
			$('#contentFormSubmit').unbind('mouseout');
			
			$(document).unbind('mouseup');
		});
	});
	
	/* Forum Topics */
	
	//Integration with Mozilla browser
	$('.postRatingUp').mouseover(function(){
		var name = $(this).attr('name');
		
		$('.postRatingUp[name='+name+']').css({
			'background-image' : 'url(/images/site/vote/VoteUpOver.png)'
		});
	});
	$('.postRatingUp').mouseout(function(){
		var name = $(this).attr('name');
		
		$('.postRatingUp[name='+name+']').css({
			'background-image' : 'url(/images/site/vote/VoteUp.png)'
		});
	});
	
	$('.postRatingDown').mouseover(function(){
		var name = $(this).attr('name');
		
		$('.postRatingDown[name='+name+']').css({
			'background-image' : 'url(/images/site/vote/VoteDownOver.png)'
		});
	});
	$('.postRatingDown').mouseout(function(){
		var name = $(this).attr('name');
		
		$('.postRatingDown[name='+name+']').css({
			'background-image' : 'url(/images/site/vote/VoteDown.png)'
		});
	});
	//Integration with Mozilla browser
	
	function postReply(){
		
		//alert('freedom');
		
		$(".postReply").unbind('mousedown');
		
		$(".postReply").mousedown(function(){

			var postNumber = $(this).attr("name");
			
			var mode = 'new';
			
			source = $(this).attr('source');
			
			
			/* Procedure */
			
			openWindow(postNumber, mode, source);
		});
	}
	
	postReply();
		
		
	$('.replyListContainer').mouseover(function(){
		
		postId = $(this).attr('name');
		$('.postRatingContainer[name!='+postId+'],.postReplyContainer[name!='+postId+']').css('visibility', 'hidden');
		$('.postRatingContainer[name='+postId+'],.postReplyContainer[name='+postId+']').css('visibility', 'visible');
	});
	
	$('.replyListContainer').mouseout(function(){
		postId = $(this).attr('name');
		//voteTimeOut = setTimeout("$('.postRatingContainer[name='+postId+']').css('visibility', 'hidden')", 500);
		$('.postRatingContainer[name='+postId+'],.postReplyContainer[name='+postId+']').css('visibility', 'hidden');
	});
	
	$('#replyWindow').mouseover(function(){
		
		postId = $(this).attr('name');
		$('.postRatingContainer[name!='+postId+']').css('visibility', 'hidden');
		$('.postRatingContainer[name='+postId+']').css('visibility', 'visible');
	});
	
	$('#replyWindow').mouseout(function(){
		postId = $(this).attr('name');
		//voteTimeOut = setTimeout("$('.postRatingContainer[name='+postId+']').css('visibility', 'hidden')", 500);
		$('.postRatingContainer[name='+postId+']').css('visibility', 'hidden')
	});
	
	$('.postRatingUp').mousedown(function(){
		
		var postId = $(this).attr("name");
		var source = $(this).attr('source');
		
		if ($('.postRatingDown[name=' + postId + ']').attr('status') != 'rating') {
		
			setTimeout(function(){
			
			
				$.post("/utilities/rating", {
					'action': 'rateUp',
					'postId': postId,
					'source': source
				}, function(data){
					
					if (data.updateStatus == 'voteNeutral') {
						var userRatingInt = parseFloat($('#statsTabUserText').text());
						
						userRatingInt++;
						
						$('#statsTabUserText').text(userRatingInt);
					}
					
				}, 'json');
			
			$('.postRatingDown[name='+postId+']').attr('status', 'done');
			
			}, 100);
			
			$('.postRatingDown[name='+postId+']').attr('status', 'rating');
		}
		
		
		
	});
	
	$('.postRatingDown').mousedown(function(){
		
		var postId = $(this).attr("name");
		var source = $(this).attr('source');
		
		if ($('.postRatingDown[name=' + postId + ']').attr('status') != 'rating') {
		
			setTimeout(function(){
			
				$.post("/utilities/rating", {
					'action': 'rateDown',
					'postId': postId,
					'source' : source
				}, function(data){
					
					if (data.updateStatus == 'voteDown') {

						var userRatingInt = parseFloat($('#statsTabUserText').text());
						
						userRatingInt--;
						
						$('#statsTabUserText').text(userRatingInt);
					}
					
				}, 'json');
				
				$('.postRatingDown[name='+postId+']').attr('status', 'done');
				
			}, 100);
			
			$('.postRatingDown[name='+postId+']').attr('status', 'rating');
		}
		
		
		
	});
	
	$('.pageNavigate').mousedown(function(){
	
		var source = window.location.pathname.toString();
		var sourceString = source.replace(/(\/)(\w+)(\/)(\w+)(\/*)(.*)/g, "$2/$4");
		var pageName = $(this).attr('name');
		
		$.post("/"+sourceString, {
			'action': 'getPosts',
			'source': sourceString,
			'getPage': pageName
		}, function(data){
			
			$('#content').replaceWith(data);
			
			pageOrientation();
		});
	}); 
}

function replyDiscussionInteract(){
	
	var replyTimeout;
	
	$('.subReplyPost').mousedown(function(){
		
		var replyId = $(this).attr('name');
		var source = $('.postRatingContainer[name='+replyId+']').attr('source');
		
		$('.subReplyContainer').remove();
		$('.subReplyPost[name='+replyId+']').css('visibility','hidden');
		
		subReplyOpen(replyId, source);
	});
	
	$('.replyListContainer').mouseover(function(){
		var name = $(this).attr('name');
		if (!$('.subReplyContainer[name='+name+']').length){
			$('.subReplyPost[name='+name+']').css('visibility', 'visible');
		}
		$('.subReplyPost[name!='+name+']').css('visibility', 'hidden');
		
	});
	
	$('.replyListContainer').mouseout(function(){
		var name = $(this).attr('name');
		
		$('.subReplyPost[name='+name+']').css('visibility', 'hidden');
	});
	
	$('.subReply').mouseover(function(){
		replyId = $(this).attr('name');
		
		$('.subPostRatingContainer[name='+replyId+']').css('visibility', 'visible');
		
	});
	
	$('.subReply').mouseout(function(){
		replyId = $(this).attr('name');
		
		$('.subPostRatingContainer[name='+replyId+']').css('visibility', 'hidden');
		
	});
}

function subReplyOpen(replyId, source){
	
	$('.replyListContainer[name='+replyId+']').append(//This will be replaced with a version more reliant on AJAX
	'<div class=subReplyContainer name='+replyId+'>'+
		'<form action=/discussion/post method=post enctype=multipart/form-data target=subPostTarget>'+
			'<div class=contentFormText>Content</div><textarea class=subReplyTextarea name=content></textarea>'+
			'<br>'+
			'<br>'+
			'<br>'+
			'<br>'+
			'<br>'+
			'<br>'+
			'<br>'+
			'<br>'+
			'<div class=contentFormText>Image</div><div class=subReplyImageContainer>'+
				'<div class=subReplyImageClose></div>'+
				'<input class=subReplyImage type=file name=image></input>'+
				'<div class=subReplyImageUnder></div>'+
			'</div>'+
			'<br>'+
			'<br>'+
			'<input type=hidden name=source value='+source+'></input>'+
			'<input type=hidden name=type value=reply></input>'+
			'<input type=hidden name=action value=post></input>'+
			'<input type=hidden name=subReplyId value='+replyId+'></input>'+
			'<input type=hidden name=replyId value='+$('#replyWindow').attr('name')+'></input>'+
			'<div id=subReplyCancel>Cancel</div>'+
			'<div id=subReplySubmitContainer>'+
				'<input id=subReplySubmit type=submit></input>'+
				'<div id=subReplySubmitImage>Submit</div>'+
			'</div>'+
		'</form>'+
	'</div>'
	);
	
	//pageOrientation();//Could Eventually create a form orientation function.
	
	//Create function that will change the input value to the correct source of the sub Reply
	
	subReplyInteract();
}

function subReplyInteract(){
	
	$('.subReplyImage').change(function(){
		$('.subReplyImageUnder').text($('.subReplyImage').val());
				
		$('.subReplyImageClose').css({
			'visibility' : 'visible'
		});
				
	});
	
	$('.subReplyImageClose').mousedown(function(){
		$('.subReplyImage').replaceWith('<input class=subReplyImage type=file></input>');
		$('.subReplyImageUnder').text('');
		$('.subReplyImageClose').css('visibility', 'hidden');
		
		$('.subReplyImage').change(function(){
			$('.subReplyImageUnder').text($('.subReplyImage').val());
			
			$('.subReplyImageClose').css({
				'visibility': 'visible'
			});
			
		});
	});
	
	//Sub Reply Cancel button interaction
	
	$('#subReplyCancel').mouseup(function(){
		
		$('#subReplyCancel').css({
			'background-image' : 'url(/images/site/forms/menuButton.png)'
		});
		
		$('.subReplyContainer').remove();
	});
	
	
	$('#subReplyCancel').mousedown(function(){
		
		$('#subReplyCancel').css({
			'background-image': 'url(/images/site/forms/menuButtonDown.png)'
		});
	
		$('#subReplyCancel').mouseover(function(){
			$('#subReplyCancel').css({
				'background-image': 'url(/images/site/forms/menuButtonDown.png)'
			});
		});
		
		$('#subReplyCancel').mouseout(function(){
			
			$('#subReplyCancel').css({
				'background-image': 'url(/images/site/forms/menuButton.png)'
			});
		});
		
		$(document).mouseup(function(){
			
			$('#subReplyCancel').unbind('mouseover');
			
			$('#subReplyCancel').unbind('mouseout');
			
			$(document).unbind('mouseup');
		});
	});
	
	
	
	var postLoop = null;
	
	$('#subReplySubmit').mouseup(function(){
		$('#subReplySubmitImage').css({
			'background-image' : 'url(/images/site/forms/menuButton.png)'
		});
		
		if (postLoop != null){
			clearInterval(postLoop);
		}
		
		var replyId = $('.subReplyContainer').attr('name');
		
		postLoop = setInterval(function(){
		
			if ($('.uploadTarget[name=subPostTarget]').contents().find('#postComplete').length) {
				clearInterval(postLoop);
				postLoop = null;
				
				if (!$('.subRepliesContainer[name=' + replyId + ']').length) {
					$('.replyContainer[name=' + replyId + ']').append('<br>' +
					'<br>' +
					'<div class=subRepliesContainer name=' +
					replyId +
					'></div>');
				}
				
				$('.subRepliesContainer[name='+ $('.uploadTarget[name=subPostTarget]').contents().find('#subReplyId').html() +']').append($('.uploadTarget[name=subPostTarget]').contents().find('#postComplete').html());
				
				$('.subReplyTextarea').val('');
				
				$('.subReplyImage').replaceWith('<input class=subReplyImage type=file name=image></input>');
				
				$('.subReplyImageUnder').text('');
				
				$('.uploadTarget[name=subPostTarget]').contents().find('#postComplete').remove();
				$('.uploadTarget[name=subPostTarget]').contents().find('#subReplyId').remove();
				
				$('.subReplyImageClose').css({
					'visibility': 'hidden'
				});
				
				$('.subReplyContainer').remove();
				
				
				subReplyInteract();
				replyDiscussionInteract();
				mainDiscussionInteract();//Could do further research and trim this down
			}
			
			if ($('.uploadTarget[name=subPostTarget]').contents().find('#fileExists').length) {
				clearInterval(postLoop);
				postLoop = null;
			
				alert('Image Exists, also this popup will be replaced with something less intrusive in the near future.');
				$('.uploadTarget[name=subPostTarget]').contents().find('#fileExists').remove();
				
				
			}
			
		}, 50);
		
	});
	
	$('#subReplySubmit').mousedown(function(){
		
		$('#subReplySubmitImage').css({
			'background-image': 'url(/images/site/forms/menuButtonDown.png)'
		});
	
		$('#subReplySubmit').mouseover(function(){
			$('#subReplySubmitImage').css({
				'background-image': 'url(/images/site/forms/menuButtonDown.png)'
			});
		});
		
		$('#subReplySubmit').mouseout(function(){
			
			$('#subReplySubmitImage').css({
				'background-image': 'url(/images/site/forms/menuButton.png)'
			});
		});
		
		$(document).mouseup(function(){
			
			$('#subReplySubmit').unbind('mouseover');
			
			$('#subReplySubmit').unbind('mouseout');
			
			$(document).unbind('mouseup');
		});
	});
}

function menuInteract(){
		
	$("#statisticsMenuClose").mousedown(function(){
		
		Cookies.positionToCookie('menuPosition', 0, 72);
	
		$("#statisticsMenu").css({
			'visibility' : 'hidden',
			'left' : 0,
			'top' : 72
		});
		
		$('.statisticsTabPosts').css({
			'visibility' : 'hidden'
		});
		
		if (!$('#menuFormOpen').length){
			$('body').append('<div id=menuFormOpen></div>');
		}
		
		$('#statisticsMenuFormCache').remove();
	});
	
	$("#statisticsMenuMin").mousedown(function(){
		if($('#statisticsMenuFormContainer').length){
			menuCache.statisticsSet();
			
			if (!$('#menuFormOpen').length){
				$('body').append('<div id=menuFormOpen></div>');
			}
		}
		
		tabMenuSwitch("minDown", null);
	});
	
	$('#statisticsMenuHome').mousedown(function(){
		
		replyWindowSwitch('discussion', null);
		
		$('#comMode').attr('name', 'discussion');
		
		document.body.scrollTop = $('#userStatus').attr('userScrollTop');
		
		if ($('#contentAllContainer').attr('name') == 'crapWrapper'){
			
			$('#contentAllContainer').css({
				'border' : 'none',
				'min-height' : '0',
				'width' : '100%',
			});
			
			$('#top').css({
				'height' : '0'
			});
			
			$('#footer_container').css({
				'height' : '0'
			});
		}
		
	});
	
	$("#statisticsMenuExpand").mousedown(function(){
		
		tabMenuSwitch("expandDown", $('#replyWindow').attr('name'));
		
		if ($('#statisticsMenu').attr('menuMode') == 'list') {
			
			statisticsTabsInteract();
		}
		
	});
}

function statisticsTabsInteract(){
	//Interaction
	//call this function up whenever you need to interact with the tabs
		
	$(".statisticsTabs").mouseover(function(){
		postNumber = $(this).attr('name');
		$(".statisticsTabClose[name="+postNumber+"]").css({
			'visibility' : 'visible'
		});
	});
		
	$(".statisticsTabs").mouseout(function(){
		postNumber = $(this).attr('name');
		$(".statisticsTabClose[name="+postNumber+"]").css({
			'visibility' : 'hidden'
		});
	});
		
	$(".statisticsTabClose").mousedown(function(){
		postNumber = $(this).attr('name');

		$(".statisticsTabs[name="+postNumber+"]").remove();
			
		Cookies.tabsToCookie();
		
	});
		
	$(".statisticsTabName").mousedown(function(){
		postNumber = $(this).attr('name');
		mode = 'update';
		source = $(this).text();
		
		openWindow(postNumber, mode, source);
	});
}

function statisticsMenuInteract(){
	
	/*
	 * Variables
	 */
	
	var x;
	var y;
	
	/*
	 * Functions
	 */
	
	/*
	 * Procedures
	 */
	
	statsTabList();
	
	function statsTabList(){
		$("#statsTabList,#statsTabListNew").mousedown(function(){
		
			if ($("#statisticsMenu").attr('menuMode') == 'action') {
			
				$("#statisticsMenu").attr({
					'menuMode': 'list'
				});
				
				$("#statisticsMenu").css({
					'visibility': 'visible'
				});
				
				tabMenuSwitch('expandDown', null);
				
				statisticsTabsInteract();
				
				mainDiscussionInteract();
				
			}
			else {
			
				$("#statisticsMenu").attr({
					'menuMode': 'list'
				});
				
				if ($("#statisticsMenu").css("visibility") == "hidden") {
					$("#statisticsMenu").css({
						'visibility': 'visible'
					});
					
					tabMenuSwitch('expandDown', null);
					
					statisticsTabsInteract();
					
					mainDiscussionInteract();
				}
				else {
					$("#statisticsMenu").css({
						'visibility': 'hidden'
					});
					
					$('.statisticsTabPosts').css({
						'visibility': 'hidden'
					});
					
					Cookies.cookieToTabs('general');
				}
			}
			
			x = 34;
			y = 82;
			
			$("#statisticsMenu").css({
				'left': x,
				'top': y
			});
			
			Cookies.positionToCookie('menuPosition', x, y);
			
			menuMove();
			
		});
	}
	
	$('#statsTabAction,#statsTabActionNew').mousedown(function(){
		
		if ($("#statisticsMenu").attr('menuMode') == 'list') {
			
			$("#statisticsMenu").attr({
				'menuMode' : 'action'
			});
		
			$("#statisticsMenu").css({
				'visibility': 'visible'
			});
			
			tabMenuSwitch('expandDown', null);
			
		}
		else {
			if ($("#statisticsMenu").css("visibility") == "hidden") {
				
				$("#statisticsMenu").attr({
					'menuMode' : 'action'
				});
				
				$("#statisticsMenu").css({
					'visibility': 'visible'
				});
				
				tabMenuSwitch('expandDown', null);
				
				menuInteract();
			}
			else {
				$("#statisticsMenu").css({
					'visibility': 'hidden'
				});
				
				$('.statisticsTabPosts').css({
					'visibility': 'hidden'
				});
			}
		}
		
		x = 34;
		y = 112;
		
		$("#statisticsMenu").css({
			'left' : x,
			'top' : y
		});
		
		Cookies.positionToCookie('menuPosition',x, y);
		
		menuMove();
		
	});
	
	$('#statsTabUser').mouseover(function(){
		$('#statsTabUserText').css({
			'left' : -1
		});
	});
	
	$('#statsTabUser').mouseout(function(){
		$('#statsTabUserText').css({
			'left' : 0
		});
	});
	
	
}

function actionStatisticsOpen(mode, page){
	$.post('/utilities/widget',{
		'action' : 'actionStatisticsOpen',
		'mode' : mode,
		'page' : page
	}, function(data){
		
		if (data.status == 'success'){
			
			if (mode != 'initial'){
				$('#statisticsMenuCore').replaceWith(
				'<div id=statisticsMenuCore>' +
				data.content +
				'</div>'
				);
				$('#actionStatisticsLower').replaceWith(data.options);
			}
			else{
				$('#statisticsMenuCore').append(data.content);
				$('#statisticsMenu').append(data.options);
			}
			
			actionStatisticsInteract();
			mainDiscussionInteract().postReply();
		}
		
	}, 'json');
}

function actionStatisticsInteract(){
	
	$('.actionStatisticsTab').mouseover(function(){
		var postId = $(this).attr('name');
		var source = $(this).attr('source');
		
		$('.actionStatisticsTabClose[name='+postId+'][source='+source+']').css({
			'visibility' : 'visible'
		});
	});
	
	$('.actionStatisticsTab').mouseout(function(){
		
		var postId = $(this).attr('name');
		var source = $(this).attr('source');
		
		$('.actionStatisticsTabClose[name='+postId+'][source='+source+']').css({
			'visibility' : 'hidden'
		});
	});
	
	$('.actionStatisticsTabClose').mousedown(function(){
	
		var postId = $(this).attr('name');
		
		$.post('/utilities/action', {
			'action': 'actionParticipate',
			'postId': postId
		}, function(data){
			if (data.status == 'success') {
				$('.actionStatisticsTab[name='+postId+']').remove();
			}
		}, 'json');
		
	});
	
	$('.actionStatisticsControlAlertContainer').mousedown(function(){
		var name = $(this).attr('name');
		
		$.post('/utilities/widget',{
			'action' : 'actionStatisticsControlAlert',
			'name' : name
		}, function(data){
			
			if(data.status == 'success'){
				
				$('.actionStatisticsControlAlert[name='+name+']').css({
					'background-image' : 'url('+data.content+')'
				});
			}
			
			if (data.status == 'registerMobile'){
				alert('Please register your mobile device in the Settings section.');
			}
			
		}, 'json');
		
	});
	
	$('.actionStatisticsPagesOption').mousedown(function(){
		
		var mode = $(this).attr('name');
		var page = 1;
		
		actionStatisticsOpen(mode, page);
	});
	
	$('.actionStatisticsPagesListUnit,.darkBlueLink[group=actionStatisticsPagesListUnit]').mousedown(function(){
		var mode = $('.darkBlueLink[group=actionStatisticsPagesOption]').attr('name');
		var page = $(this).attr('name');
		
		actionStatisticsOpen(mode, page);
	});
}




function replyWindowSwitch(mode, postNumber){
	if(mode == 'reply'){
		
		if (!$('#contentAllCache').length){
			$('body').append('<div id=contentAllCache></div>');
			$('#contentAllCache').html($('#contentAllContainer').html());
		}
	}
	
	if (mode == 'discussion'){
		if ($('#contentAllCache').length) {
			$('#contentAllContainer').html($('#contentAllCache').html());
			$('#contentAllCache').remove();
			pageOrientation();
		}
	}
	
	//pageOrientation();
	//mainDiscussionInteract();
}







function tabMenuSwitch(mouse, postId){
	
	if (mouse == "minDown"){
		
		$("#statisticsMenu").html(
		"<div id=statisticsMenuHeader>"+
			"<div id=statisticsMenuClose></div>"+
			"<div id=statisticsMenuExpand></div>"+
			"<div id=statisticsMenuRight></div>"+
		"</div>"
		);
		
		$("#statisticsMenu").css({
			'width' : 83,
			'min-height' : 31,
			'max-height' : 31
		});
		
		$("#statisticsMenuHeader").css({
			'height' : 31,
			'background-image' : 'url(/images/site/statisticsMenu/menuCloseBar.png)',
			'border' : 'none'
		});
		
		$("#statisticsMenuRight").css({
			'width' : 34,
			'height' : 31,
			'left' : 47
		});
		
	}
	else{
		if (mouse == "expandDown") {
		
			$('#statisticsMenu').html('<div id=statisticsMenuHeader>' +
			'<div id="statisticsMenuTL"></div>' +
			'<div id="statisticsMenuTC"></div>' +
			'<div id="statisticsMenuClose"></div>' +
			'<div id="statisticsMenuMin"></div>' +
			'<div id="statisticsMenuHome"></div>' +
			'<div id="statisticsMenuRight">' +
			'<div id="statisticsMenuTR"></div>' +
			'</div>' +
			'</div>' +
			'<div id="statisticsMenuCore"></div>' +
			'<div id="statisticsMenuFooter">' +
			'<div id="statisticsMenuFooterLeft"></div>' +
			'<div id="statisticsMenuFooterCenter"></div>' +
			'<div id="statisticsMenuFooterRight">' +
			'</div>' +
			'</div>');
			
			$("#statisticsMenuHeader").css({
				'height': 20,
				'width': 300,
				'background-image': 'none',
				'border-bottom': '1px #a9a9a9 solid'
			});
			
			switch ($('#statisticsMenu').attr('menuMode')){
				
				case 'list':
				Cookies.cookieToTabs('specific');
				break;
				
				case 'action':
				actionStatisticsOpen('initial', 1);
				break;
				
			}
			
			$("#statisticsMenu").css({
				'width': 300,
				'min-height': 200,
				'max-height': 600
			});
			
			$('#statisticsMenuTC').css({
				'height': 20,
				'width': 270
			});
			
			$("#statisticsMenuRight").css({
				'width': 230,
				'height': 20,
				'left': 70
			});
			
			$("#statisticsMenuCore").css({ //Could possibly automate this
				'width': 298,
				'min-height': 200,
				'max-height': 350,
				'cursor': 'pointer'
			});
			
			$('#statisticsMenuFooter').css({
				'width': 300
			});
			
			$("#statisticsMenuFooterCenter").css({
				'width': 270,//Eventually this will be a dynamic function
				'height': 20,
				'background-image': 'url(/images/site/statisticsMenu/BottomBarCenter.png)',
				'background-repeat': 'repeat-x'
			});
			
		}
	}
	
	menuMove();
	menuInteract();
}


/*
 * Variables
 */

/*
 * Procedures
 */


/*
 * 
 * Window
 * 
 */



	/*
	 * Functions
	 */

	function openWindow(postNumber, mode, source){
	
		$('#userStatus').attr('userScrollTop', document.body.scrollTop);
		
		var sourceString = source.toLocaleLowerCase().replace(/(\s)/g, "").replace(/\|/g, "/");
		
		var sourceRoot = sourceString.replace(/(\/*)(\w*)(\/*)(\w*)(\/*)(.*)/g, "$2");
		
		if (sourceRoot == 'discussion') {
			var requestLocation = '/discussion/post';
			var action = 'getPost';
		}
		
		if (sourceRoot == 'action') {
			var requestLocation = '/utilities/action';
			var action = 'getAction';
		}
		
		$.post(requestLocation, {
			'action': action,
			'source': sourceString,
			'postId': postNumber
		}, function(data){
		
			if ($('#comMode').length) {
				$('#comMode').remove();
				
			}
			
			$('body').append('<div id=comMode name=reply></div>'); //This will be switched to a universal information tag. Named something like windowInfo
			document.body.scrollTop = 0;
			
			Cookies.postUpdate(postNumber, mode, source); //Investigate this and expand it into the action and possibly writing sections.
			
			replyWindowSwitch('reply', postNumber);
			
			$("#contentAllContainer").html(data);
			
			if ($('#contentAllContainer').attr('name') == 'crapWrapper') {
			
				$('#contentAllContainer').css({
					'border-left': '4px solid #eceef4',
					'border-right': '4px solid #eceef4',
					'min-height': '800',
					'width': '1000',
				});
				
				$('#top').css({
					'height': '180'
				});
				
				$('#footer_container').css({
					'height': '180'
				});
			}
			
			if (sourceRoot == 'action'){
				actionInteract();
			}
			
			replyFormInteract();
			mainDiscussionInteract();
			replyDiscussionInteract();
			
			source = source.toLocaleLowerCase().replace(/(\s)/g, "").replace(/\|/g, "/");
			
			$('input[name="source"]').attr('value', sourceString);
		
		});
	}
	
		
	/*
	 * Variables
	 */
		

	/*
	 * Procedures level 1
	 */
	
	
	// Create two functions, follow and reply
	//There will be only one window active at any time. 
	//Clicking on any of the window tabs will swap out the content in the reply window with the corresponding content in the tab
	//Eventually there may be a function that would preload content
	
	function objects(){
		
		String.prototype.capitalize = function() {
			return this.replace(/\b[a-z]/g, function(matchChar){
				return matchChar.toLocaleUpperCase();
			});
		}
		
		/*
		 * Variables
		 */
			
		Cookies = {
			set : function(key, value, minsToExpire){
				
				var host = window.location.hostname.toString();
				hostname = host.replace(/www\./g, "");
				host = "domain=" + '.' + hostname + ";";
				var expires = "";
				if (minsToExpire) {
					var date = new Date();
					date.setTime(date.getTime()+(minsToExpire*60*1000));
					expires = "; expires="+ date.toGMTString();
				}
				
				var browser = new browserDetection();
				
				if (browser.name == 'Chrome'){ //Chrome browser
				
					if (Cookies.get('chromeFrame') && key != 'userId' && key != 'userKey' && key != 'test') { // Chrome Frame
						localStorage.setItem(key, value);
					}
					else {
						
						if (Cookies.get('chromeFrame') && localStorage.getItem('chromeFrameTag') != null && key != 'test') {
							var tag = '--' + localStorage.getItem('chromeFrameTag');
							key = key + tag;
						}
						
						chromeCookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + expires + "; path=/;";// + host;
					
						if (hostname != 'beneverywhere.net') {
							
							var source = window.location.pathname.toString();
							var path = source.replace(/(\/)(\w*)(\/*)(\w*)(\/*)(.*)/g, "$4");
							
							document.cookie = chromeCookie;
							
						}
						else {
							document.cookie = chromeCookie;
						}
					}
					
				}
				else{
					document.cookie = encodeURIComponent(key)+"="+ encodeURIComponent(value) + expires + "; path=/;" + host;
				}
			},
			
			get: function(key){
				
				var chromeFrame = false;
				var nameCompare = 'chromeFrame' + "=";
				var cookieArray = document.cookie.split(';');
				for (var i = 0; i < cookieArray.length; i++) {
					var aCrumb = cookieArray[i].split("=");
					var currentKey = decodeURIComponent(aCrumb[0]);
					if ('chromeFrame' == currentKey || " " + 'chromeFrame' == currentKey) {
						var chromeFrame = true;
					}
				}
				
				if (chromeFrame == true && key != 'chromeFrame' && key != 'userId' && key != 'userKey' && key != 'test') {
					return localStorage.getItem(key);
				}
				else {
					
					if (chromeFrame == true && localStorage.getItem('chromeFrameTag') != null && key != 'chromeFrame' && key != 'test'){//Interesting. It seems that I should include the localStorage thing pronto
						var tag = '--' + localStorage.getItem('chromeFrameTag');
						var key = key + tag;//This seems to be the road block
					}
					
					var nameCompare = key + "=";
					var cookieArray = document.cookie.split(';');
					for (var i = 0; i < cookieArray.length; i++) {
						var aCrumb = cookieArray[i].split("=");
						var currentKey = decodeURIComponent(aCrumb[0]);
						if (key == currentKey || " " + key == currentKey) {
							return decodeURIComponent(aCrumb[1]);
						}
					}
				}
			},
			
			postUpdate : function(postNumber, mode, source){ //This might eventually generate a reply count or not based off of whether or not it is a post
				
				var menuTabsNumber = [];
				var menuTabsSection = [];
				
				if (mode == 'new') {
					var location = source;
					
					var locationString = location.replace(/(\/)*(\w+)(\/)(\w+)(\/*)(.*)/g, "$2 | $4").capitalize();
				}
				
				if (mode == 'update'){
					locationString = source;
				}
				
				var tabsArray;
				var isValid = 'yes';
				
				
					
				$.post("/utilities/widget", {
					'action' : 'postUpdate',
					'postId' : postNumber,
					'locationString' : locationString
				}, function(data){
					
					replyCount = data.replyCount;
						
					if(!Cookies.get('menuTabs')){
						
						menuTabs = postNumber+'.'+locationString+'.'+ replyCount;
						
						Cookies.set('menuTabs', menuTabs, 60*24*5);
						
					}
					else{
					
						menuTabs = Cookies.get('menuTabs').split(',');
					
						var index = 0;
				
						while(menuTabs[index]){
					
							if (postNumber == menuTabs[index].split('.')[0]){
								isValid = 'no';
								menuTabs[index] = postNumber+'.'+locationString+'.'+replyCount;
								
								Cookies.set('menuTabs', menuTabs, 60*24*5);
							}
							
							if (index > 20){
								isValid = 'no';
							}
						
							index++
						}
					
						menuTabs[index] = postNumber+'.'+locationString+'.'+replyCount;
					}
						
					if (isValid == 'yes'){
							
						Cookies.set('menuTabs', menuTabs, 60*24*5);
					}
						
				},'json');
			},
			
			tabsToCookie : function(){
				
				var menuTabs = []; //This could possible run into some problems without the array indicator up front
				var menuTabsNumber;
				var menuTabsSection;
				var menuTabsCount;
				var index = 0;
				
				while($(".statisticsTabs").eq(index).length){
					
					menuTabsNumber = $(".statisticsTabs").eq(index).attr("name");
					menuTabsSection = $(".statisticsTabName").eq(index).text();
					
					
					newIndex = 0;
					while(menuTabsNumber != Cookies.get('menuTabs').split(',')[newIndex].split('.')[0]){
						
						newIndex++;
					}
					
					if (menuTabsNumber == Cookies.get('menuTabs').split(',')[newIndex].split('.')[0]){
						menuTabsCount = Cookies.get('menuTabs').split(',')[newIndex].split('.')[2];
					}
					
					menuTabs[index] = menuTabsNumber+'.'+menuTabsSection+'.'+menuTabsCount;
					
					index++;
				}
				
				Cookies.set('menuTabs', menuTabs, 60*24*5);
			},
			
			cookieToTabs : function(mode){
				
				if (!Cookies.get('menuTabs')){
				}
				else{
					
					if (Cookies.get('chromeFrame')) {
						var array = {
							'action': 'clipTopics',
							'menuTabs': Cookies.get('menuTabs')
						}
					}
					else {
						var array = {
							'action': 'clipTopics'
						}
					}
					
					$.post("/utilities/widget", 
					array, 
					function(data){
						
						var menuTabsNumber = data.menuTabsNumber;
						var menuTabsSection = data.menuTabsSection;
						var menuTabsCount = data.menuTabsCount;
						var repliesCount = data.repliesCount;
						var postRating = data.postRating;
						
						var newPosts = 'no';
						
						var ratingColor;
						
						var menuTabs = [];
						
						var index = 0;
						
						while(menuTabsNumber[index]){
								
							menuTabs[index] = menuTabsNumber[index]+'.'+menuTabsSection[index]+'.'+menuTabsCount[index];
								
							index++;
						}
						//The issue seems to be related to not synchronising the menuTabsCookie
						if (menuTabsNumber[0]){
							Cookies.set('menuTabs', menuTabs, 60*24*5);
						}
						
						index = 0;
						
						while (menuTabsNumber[index]){
					
							replyId = menuTabsNumber[index];
						
							if (repliesCount[index] > menuTabsCount[index]){
								newPost = "style=visibility:visible";
								
								var nameTag = '<div class=blueLink>' + menuTabsSection[index] + '</div>';
								
								newPosts = 'yes';
							}
							else{
								newPost = "style=visibility:hidden";
								
								var nameTag = menuTabsSection[index];
							}
							
							if (parseFloat(postRating[index]) < 0){
								ratingColor = 'style=color:#e87875';
							}
							else{
								ratingColor = '';
							}
							
							if (mode != 'general'){
								
								if (index == 0){
							
									tabsHTML =
									"<div class=statisticsTabs id=statisticsTab" + index + " name=" + replyId + ">" +
										"<div class=statisticsTabClose name="+replyId+"></div>" +
										"<div class=statisticsTabName name="+replyId+">"+nameTag+"</div>" +
										"<div class=statisticsTabRating name="+replyId+" "+ratingColor+">"+postRating[index]+"</div>" +
									"</div>";
								}
								else{
									tabsHTML = tabsHTML +
									"<div class=statisticsTabs id=statisticsTab" + index + " name=" + replyId + ">" +
										"<div class=statisticsTabClose name="+replyId+"></div>" +
										"<div class=statisticsTabName name="+replyId+">"+nameTag+"</div>" +
										"<div class=statisticsTabRating name="+replyId+" "+ratingColor+">"+postRating[index]+"</div>" +
									"</div>";
								}
							}
					
							index++;
						}
						
						if (newPosts == 'yes'){
							
							$('#statsTabList').css('visibility','hidden');
							$('#statsTabListNew').css('visibility','visible');
						}
						else{
							$('#statsTabList').css('visibility','visible');
							$('#statsTabListNew').css('visibility','hidden');
						}
						
						if (mode != 'general'){
							$('#statisticsMenuCore').html(tabsHTML);//But what if there is no statistics menu core?
							var tabsCheck = setInterval(function(){
								if ($('.statisticsTabs').length){
									statisticsTabsInteract();
									clearInterval(tabsCheck);
								};
							},25);
						}
					}, 'json');
				}
			},
			
			positionToCookie : function(cookie, x, y){//
				
				positionArray = [x,y];
				
				Cookies.set(cookie, positionArray, 60*24*5);
				//alert('derp');
				
			},
			
			cookieToPosition : function(cookie){
				
				positionArray = Cookies.get(cookie).split(",");
				
				 X = parseFloat(positionArray[0]);
				 Y = parseFloat(positionArray[1]);
				 
				 return{
				 	x : X,
					y : Y
				 }
			}
		}
		
		
		
		menuCache = {
	
		set: function(){
			$("#statisticsMenuCache").html($('#statisticsMenuCore').html());
		},
		
		get: function(){
			$('#statisticsMenuCore').html($('#statisticsMenuCache').html());
		},
		
		statisticsSet: function(){
			if ($('#statisticsMenuFormCache').length){
				$('#statisticsMenuFormCache').remove();
			}
			
			if ($('#statisticsMenuFormTextCache').length){
				$('#statisticsMenuFormTextCache').remove();
			}
			
			$("body").append("<div id=statisticsMenuFormCache></div>");
			$("body").append("<div id=statisticsMenuFormTextCache></div>");
			
			$('#statisticsMenuFormCache').html($('#statisticsMenuFormContainer').html());
			$('#statisticsMenuFormTextCache').html($('#statisticsMenuFormText').attr('value'));
			
			$('#statisticsMenuFormContainer').remove();
		},
		
		statisticsGet: function(){
			
			$('#statisticsMenuFormImageStatus').css({
				'visibility' : 'hidden'
			});
			
			$('#statisticsMenuCore').after("<div id=statisticsMenuFormContainer></div>");
			
			
			$('#statisticsMenuFormContainer').html($('#statisticsMenuFormCache').html());
			
			$('#statisticsMenuFormText').attr({
				'value' : $('#statisticsMenuFormTextCache').html()
			});
			
			
			$('#statisticsMenuFormCache').remove();
			$('#statisticsMenuFormTextCache').remove();
		}
	}	
		
	}
	
	
	
	
	
	
	
	
	
	/*
	 * Ethics
	 */
	
	function loadEthics(){
		ethicsInteract();
	}
	
	
	function ethicsInteract(){
		
		$('#ethicsMenuNew').mousedown(function(){
			
			if (!$('#ethicsCache').length){
				$('body').append('<div id=ethicsCache></div>');
				$('#ethicsCache').html($('#contentMainContainer').html());
				$('#contentMainContainer').html(//There should be a way to search for dependencies (including multiple ones) while on this page
				/*
				 
				 Notes:
				 
				 When someone clicks on a new dependency, it should display a preview with the capacity for people to quote specific threads.
				 Include a technical explaination and a simplified everyday explaination
				 */
					
					'<div id=ethicsNewContainer>'+
						'New Ethics'+
						'<form>'+
							'Title: <textarea id=ethicsNewTitle></textarea>'+
							'<br>'+
							'Justification: <textarea id=ethicsNewJustification></textarea>'+
							'<br>'+
							'Conclusion: <textarea id=ethicsNewConclusion></textarea>'+
							'<br>'+
							'<div id=ethicsNewDependency>New Dependency</div>'+
							'<br>'+
							'<div id=ethicsNewCancel>Cancel</div> <div id=ethicsNewPreview>Preview</div>'+
						'</form>'+
					'</div>'
				);
				
			}
			
			ethicsInteract();
		});
		
		$('#ethicsNewCancel').mousedown(function(){
			$('#contentMainContainer').html($('#ethicsCache').html());
			$('#ethicsCache').remove();
					
			ethicsInteract();
		});
		
		$('#ethicsNewPreview').mousedown(function(){
		
			if ($('#ethicsNewTitle').attr('value') != '' && $('#ethicsNewJustification').attr('value') != '' && $('#ethicsNewConclusion').attr('value') != '') {
			
				//do something useful be sure to link with json
				
				//Very important, this thing should be autosaved just in case something awful happens
				
				var testEthics = 
				'<div id=ethicsContainer>' +
				
				'<div class=ethicsContentTitle name=title>' +
					$('#ethicsNewTitle').attr('value') +
				'</div>' +
				'<div class=ethicsContentContainer name=title></div>'+
				
				'<br>' +
				
				'<div class=ethicsContentTitle>Justification</div>' +
				'<br>' +
				'<div class=ethicsContentContainer name=justification>'+
					$('#ethicsNewJustification').attr('value') +
				'</div>' +
				
				'<br>' +
				
				'<div class=ethicsContentTitle>Conclusion</div>' +
				'<br>' +
				'<div class=ethicsContentContainer name=conclusion>'+
					$('#ethicsNewConclusion').attr('value') +
				'</div>' +
				
				'<br>' +
				
				'<div class=ethicsContentTitle>Dependencies</div>' +
				'<br>' +
				'<div class=ethicsContentContainer name=dependencies>'+
					$('#ethicsNewDependencies').text() +
				'</div>' +
				
				'<br>' +
				
				'<div id=ethicsNewEdit>Edit</div>' +
				'<div id=ethicsNewSubmit>Submit</div>' +
				'</div>';
				
				$('#contentMainContainer').html(testEthics);
				
				ethicsInteract();
			}
		});
		
		$('#ethicsNewEdit').mousedown(function(){
			$('#contentMainContainer').html(
			'<div id=ethicsNewContainer>' +
			'New Ethics' +
			'<form>' +
			'Title: <textarea id=ethicsNewTitle>' +
			$('.ethicsContentTitle[name=title]').text() +
			'</textarea>' +
			'<br>' +
			'Justification: <textarea id=ethicsNewJustification>' +
			$('.ethicsContentContainer[name=justification]').text() +
			'</textarea>' +
			'<br>' +
			'Conclusion: <textarea id=ethicsNewConclusion>' +
			$('.ethicsContentContainer[name=conclusion]').text() +
			'</textarea>' +
			'<br>' +
			'<div id=ethicsNewDependency>New Dependency</div>' +//Clear this up mon'
			'<br>' +
			'<div id=ethicsNewCancel>Cancel</div> <div id=ethicsNewPreview>Preview</div>' +
			'</form>' +
			'</div>');
			
			ethicsInteract();
		});
		
		$('#ethicsNewSubmit').mousedown(function(){
			$.post('/ethics/post', {
				'action': 'newEthic',
				'title': $('.ethicsContentTitle[name=title]').text(),
				'justification': $('.ethicsContentContainer[name=justification]').text(),
				'conclusion': $('.ethicsContentContainer[name=conclusion]').text(),
				'dependencies': $('#ethicsDependenciesList').text()
			}, function(data){
				alert('thanks for your submission');
			});
			
			ethicsInteract();
			
		});
		
	}
	
	
	