// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

////////////// Edit comment js //////////////
var count = 60;
var timer = 0;
var time_paused = 0;
var commentEditor;
function decrement_time() {
	if (time_paused > 0){
		count=time_paused;
		time_paused=0;
	}
	else {
		count=count-1;
	}
	$('comment_time').innerHTML=count;
	if(count == 0) { // But stop updating after 60 seconds;
		new Effect.Fade('edit_box');
		clearInterval(timer);
	}
}
function no_edit() {
	new Effect.Fade('edit_box');
	//new Insertion.After('edit_box');
	clearInterval(timer);
}
function start_timer() {
	timer = setInterval("decrement_time()",1000);
}
function start_from_pause(){
	clearInterval(timer);
	timer = setInterval("decrement_time()",1000);
}
function set_pause(){
	time_paused = count;
	clearInterval(timer);
}

function toggleEditors(name, post_holder, old) {
	if (old) { Element.remove(old); }
}


// effects
function completed_comment_edit() {
	//this does all of the effects and queues when the edit comment ajax completes
	new Effect.BlindUp('in_place_edit', {queue: 'first', scope: 'form'});
	new Effect.Appear('comment_text', {queue: 'end', scope: 'form'});
	new Effect.Fade('saving_comment', {queue: 'first', scope: 'saving'});
	new Effect.Appear('edit_box');

	return start_from_pause();
}

function cancel_comment_edit() {
	// this loads all of the effects when you hit cancel editing
	new Effect.BlindUp('in_place_edit', {queue: 'first'});
	new Effect.Appear('comment_text', {queue: 'end'});
	new Effect.Appear('edit_box');

	start_from_pause();
}

function show_edit_box() {
	callback = editorState ? null : function(){ this.toggleSourceView('on'); }
	toggleEditors('comment_content', 'comment_edit');
	new Effect.BlindDown('in_place_edit');
	Element.toggle($('edit_box'));
	Element.toggle($('comment_text'));
	if(commentEditor) {
		commentEditor.removeEditor();
		commentEditor = new Editor('edit_comment', false, callback);
	} else { commentEditor = new Editor('edit_comment'	, false, callback); }
	return set_pause();
}

// end effects

////////// end edit comment js //////////////


function show_new_message_form(elementID)
{
	displayPart = document.getElementById(elementID);
    displayPart.style.display = "";

}

function checkInputs()
{
    comment_area = document.getElementById('content');
    if (trimAll(comment_area.value) == '')
    {
        alert('The comment area cannot be empty.');
        return false;
    }

    tag_field = document.getElementById('tag_list');
    blocked_user = document.getElementById('blocked_user');
    if (trimAll(tag_field.value) == '')
    {
        alert('You have to tag the post when you are commenting.');
        return false;
    }
    if (trimAll(tag_field.value).split(',').length > 5)
    {
        alert('You cannot tag more than 5 tags one time.');
        return false;
    }

}

function trimAll(sString)
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function toggle_forms(selectedValue)
{
	new_message_form = document.getElementById('new_message_form')
	new_post_form = document.getElementById('new_post_form')
    if (selectedValue == "blogs")
	{
		new_post_form.style.display = "";
		new_message_form.style.display = "none";
		//var editor = new Editor('post_form', true, null);
		//var editorState = true;
	}
	else if (selectedValue == "messages")
	{
		new_message_form.style.display = "";
		new_post_form.style.display = "none";
		//var editor = new Editor('message_form', true, null);
		//var editorState = true;
	}
}
