// Sets variables, showRegister defaults the "more registration info" items hidden; 
var showRegister = 0;
var openpid = '';
var opensid = '';

// Sets observations using prototype to track clicks on "show more info" on registration page
$(document).ready(function() {
	$('#providemoreinfo').click(showRegisterOptions);
	init();
});

// Changes innerhtml on div made for each post to display comment form to enter comments inline
function commentbox(pid,sid,subject,key){
	if(opensid != '' && openpid != ''){
		closebox(openpid,opensid);
	}
	

	var boxCode = "<div id='newForm"+pid+"-"+sid+"'><form action='modules.php?name=News&amp;file=comments' method='post'>"+
		"<font class='option'><b>Comment:</b></font><br>"+
	    "<textarea wrap='virtual' cols='50' rows='10' name='comment' id=\"commentbox\"></textarea><br />"+
			"<input type='hidden' name='pid' value='"+pid+"'>"+
            "<input type='hidden' name='sid' value='"+sid+"'>"+
            "<input type='hidden' name='subject' value=\""+subject+"\"><br />"+
            "<input type='hidden' name='key' value='"+key+"'>"+

            "<input type='submit' name='op' value='Ok!'> "+
            "<input type='button' name='blah' value='Cancel' onclick='javascript:closebox("+pid+", "+sid+"); return false;'>"+
			"</form></div>\n";


/*
		$('#closeCommentButton')
			.livequery('click', function(event) {
				$('#comment'+pid+'-'+sid).html('');
				openpid = '';
				opensid = '';
		});
*/
/*
		$('#comment'+pid+'-'+sid).html(boxCode);
	    document.getElementById('commentbox').select();         
*/

		$(".newcommentDiv"+pid+"-"+sid).append(boxCode).find("textarea").select();
		openpid = pid;
		opensid = sid;
}

// Closes open comment box and clears the open pid and sid
function closebox(pid,sid){
	$("#newForm"+pid+"-"+sid).remove();
/* 	$('#comment'+pid+'-'+sid).html(''); */
		openpid = '';
		opensid = '';
}

// Function to show more registration obtions.  Shows/hides div to say show more or show less content to enter when registering.
function showRegisterOptions(){ 
	if (showRegister == 0) {
        $('#hiddenoptions').slideDown("slow");
		showRegister = 1;
	} else {
		$('#hiddenoptions').slideUp("slow");
		showRegister = 0;
	}	
}

// Works like php's nl2br, used in the edit story in place, taken from php.js
function nl2br (str, is_xhtml) {
    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }
	return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

function init() {
	var sid = "770";
	var origText = $('#contentstory').html();

	$('#editButton').click(function() {
		$('#contentstory').attr("contentEditable", true);
		$('#contentstory').focus();
		$('textarea.expanding').autogrow();
		$('#buttons').append('  &nbsp;&nbsp; <input type="button" value="Submit Your Edits" id="submitButton" />');
		$(this).hide();

		$.post('/EditInPlace/checkFileExists.php', {'sid': sid, 'currentText': origText});
	});
	
	$('#submitButton')
		.livequery('click', function(event) { 

		var edits = $('#story').html();
		var username = $('#username').text();

		$.post('/EditInPlace/addUserFile.php', {'sid': sid, 'username': username, 'edits': edits}, function(data) { 
			alert('Your Edits Have Been Submitted');
			//window.location = "http://dev.jeremyhalvorsen.com/MI/text_diff.php?sid="+sid+"&userfile="+data;
			$('#contentstory').fadeIn(10);
			$('#editscontainer').remove;
    	}); 
	});

	$('#revertButton').click(function() {
		$('#contentstory').html(origText);
		$('#editButton').show();
		$('#submitButton').remove();
	});

}

