var GLOBAL_DIR = "/mybrainnet";

/**  ON LOAD  **/

jQuery(document).ready(function($){
	//Service Comments
	$('.comment-form-textbox').autogrow({maxHeight: 200, minHeight: 80, lineHeight: 16});
	$('.nicetextbox').autogrow({maxHeight: 250, minHeight: 90, lineHeight: 16});
	
	dropShadows();
	$('.comment-form').submit(function(){ submitCommentForm($(this)); return false;});	
});

function hideShadows()
{
	$("div.dashboard-box, .data-box, .shadow").removeShadow();
}

function dropShadows()
{
	$("div.dashboard-box, .data-box, .shadow").removeShadow();
	$("div.dashboard-box, .data-box, .shadow").dropShadow({left: 1, top: 1, opacity: 0.3, blur: 2});
}

/**  END  **/


/** Comment functions **/
function submitCommentForm(frm)
{
	var comment = frm.children("textarea.comment-form-textbox").val();
	var type = frm.children(":input[name=type]").val();
	var id = frm.children(":input[name=id]").val();
	var withComments = frm.children(":input[name=withComments]").val();
	var reply = frm.children(":input[name=reply]").val();
	var vote = "";
	if(type == "vote" && withComments == "0")
	{
		vote = frm.children(":input[name=vote]").val();
		if(vote == "0" && comment == "")
		{
			alert("Please justify your vote against this proposal.");
			return false;
		}
	}
	else if(comment == "")
		return false;
	if(type == "")
	{
		alert("BRAINnet system error - Comment form error TYPE_01");
		return false;
	}
	if(id == "")
	{
		alert("BRAINnet system error - Comment form error IDENTIFIER_01");
		return false;
	}	
	if(!(type == "vote" && withComments == "0"))
	{
		frm.parent().parent().find(".comment-new").append(getCommentScript(comment));
		dropShadows();
	}
	
	$.post(GLOBAL_DIR + "/comment/addComment", { comment: comment, type: type, id : id, vote: vote, reply: reply }, function(xml){
		frm.children("textarea.comment-form-textbox").val("");
		if(type == "vote" && withComments == "0")
			location.reload(); 
	});	
	return false;
}

function getCommentScript(comment)
{
	var rtnText = '<div class="comment-display-new"><div class="comment-display-avitar">';
	rtnText += '<span class="comment-display-name">You</span><Br /><span class="comment-display-time">Just now</span></div>';
	rtnText += '<div class="comment-display-message">';
	rtnText += comment;
	rtnText += '</div><div class="clearMe"></div></div>';
	return rtnText;
}

function commentRemove(id, isSub)
{
	if(!confirm("Are you sure you want to remove this comment?"))
	{
		return;
	}
	$("#comment-" + id).css("background-color", "#F2BFBF");
	$("#comment-" + id + " .comment-display-delete").html("removing...");
	$.post(GLOBAL_DIR + "/comment/removeComment", {id : id }, function(xml){
		$("#comment-" + id).fadeTo("slow", 0.33);
		$("#comment-" + id + " .comment-display-delete").html("comment removed");
	});
}

function commentReply(id, id2, type)
{
	var rntText = '<form name="comment-form" class="comment-form comment-form-reply" onSubmit="return false"><textarea class="comment-form-textbox" name="comment"></textarea><input type="hidden" name="type" value="' + type + '"><input type="hidden" name="withComments" value="0"><input type="hidden" name="reply" value="' + id + '"><input type="hidden" name="id" value="'+ id2 + '"><br /><input type="submit" class="comment-form-submit" value="Post Reply" /></form>';
	$("#comment-" + id + " .comment-display-extra").html(rntText);
	$('.comment-form-reply').submit(function(){ submitCommentForm($(this)); return false;});
}

function validateThread(vv)
{
	if(vv.name.value == "")
	{
		alert("Please provide a name for this thread.");
		
	}
	if(vv.description.value == "")
	{
		alert("Please provide a description for this thread.");
	}
	return true;
}

/** Variable Free functions **/

function exitVariableTree()
{
	hideShadows();
	$("#dataRequestConfirmed").val("true");
	//variable-tree-iframe
	$("#variable-tree-iframe").slideUp("normal", function(){
		$("#varible-tree-box").html("Saving your data..");
		dropShadows();
		$("#dataRequestForm").submit();
	});
}

function addDataRequestFile()
{
	var currentNum = $("#numDataRequest").val();
	currentNum++;
	$("#subjectIdList").append('<input type=file name="data_request_' + currentNum + '" class="fileuploads" /><br />');
	$("#numDataRequest").val(currentNum);
}

function checkDataRequestForm()
{
	if($("#dataRequest-name").val() == "")
	{
		alert("Please add a title to your data request.");
		return false;
	}
	if($("#dataRequestConfirmed").val() != "true")
	{
		alert("Please finish your Variable Tree session before you save the form.");
		return false;
	}
	return true;
}

function xml_field( xml, fieldname ) {
	var sReturn = "";
	if ( xml.getElementsByTagName(fieldname) )
		if ( xml.getElementsByTagName(fieldname)[0] )
			if ( xml.getElementsByTagName(fieldname)[0].firstChild )
					sReturn = xml.getElementsByTagName(fieldname)[0].firstChild.nodeValue;
	return sReturn;
}