function row_is_visible(row)
{
    if (row.length == 0)
        return false;
    else if (row.is(":visible") == false)
        return false;
    else if (row.is(":visible") == true && row.height() == 0) // For IE6
        return false;
    else
        return true;
}

function update_emo_photos_submits()
{
    if (row_is_visible($("#emoticons_row")))
    {
        if (row_is_visible($("tr#embed_videos_row")))
            $("#embed_videos_hr_row").show();
        else
            $("#photos_hr_row").show();
    }
    
    if (row_is_visible($("tr#embed_videos_row")))
        $("#photos_hr_row").show();
    
    if (row_is_visible($("tr#photos_row1")) || row_is_visible($("tr#photos_row2")))
        $("#photos_hr_row").show();
    
    if (row_is_visible($("tr#emoticons_row")) == false &&
        row_is_visible($("tr#embed_videos_row")) == false &&
        row_is_visible($("tr#photos_row1")) == false &&
        row_is_visible($("tr#photos_row2")) == false)
    {
        $("#photos_hr_row").hide();
    }
    
    if ($("#submit_up").length > 0 && $("#submit_down").length > 0) {
        if (row_is_visible($("tr#emoticons_row")) || row_is_visible($("tr#embed_videos_row")) ||
            row_is_visible($("tr#photos_row")))
        {
            $("#submit_up").hide();
            $("#submit_down").show();
        }
        else
        {
            $("#submit_up").show();
            $("#submit_down").hide();
        }
    }
}

function insert_emo(emo_text)
{
	area = document.getElementById('textarea');
	
	emo_text = ' ' + emo_text + ' ';
	
	//IE support
	if (document.selection)
	{
		area.focus();

		//in effect we are creating a text range with zero
		//length at the cursor location and replacing it
		//with emo_text
		sel = document.selection.createRange();
		sel.text = emo_text;
	}
	
	//Mozilla/Firefox/Netscape 7+ support
	else if (area.selectionStart || area.selectionStart == '0')
	{

		//Here we get the start and end points of the
		//selection. Then we create substrings up to the
		//start of the selection and from the end point
		//of the selection to the end of the field value.
		//Then we concatenate the first substring, emo_text,
		//and the second substring to get the new value.
		var startPos = area.selectionStart;
		var endPos = area.selectionEnd;
		area.value = area.value.substring(0, startPos)+ emo_text+ area.value.substring(endPos, area.value.length);
		area.selectionStart = startPos + emo_text.length;
		area.selectionEnd	= area.selectionStart;
	}
	else
	{
		area.value += emo_text;
	}
	
	return retFalse();
}

function toggle_emoticons_row()
{
	var before		= document.getElementById('emoticons_hr_row');
	var row			= document.getElementById('emoticons_row');
	var after		= document.getElementById('photos_hr_row');
	var photos_row	= document.getElementById('photos_row');
	var photos_row1	= document.getElementById('photos_row1');
	
	var submit_up		= document.getElementById('submit_up');
	var submit_down		= document.getElementById('submit_down');
	
	if (row.style.display == 'none')
	{
		showRow('emoticons_row');
		showRow('emoticons_hr_row');
		showRow('photos_hr_row');
	}
	else
	{
		hideRow('emoticons_row');
		hideRow('emoticons_hr_row');
		
		if ((photos_row1 && photos_row1.style.display == 'none') ||
			(photos_row && photos_row.style.display == 'none'))
		{
			hideRow('photos_hr_row');
		}
	}
    
    update_emo_photos_submits();
	
	return retFalse();
}

function toggle_photos_row()
{
	var row1			= document.getElementById('photos_row1');
	
	if (row1.style.display == 'none')
	{
		show_photos_row();
	}
	else
	{
		hide_photos_row();
	}
	
	return retFalse();
}

function show_photos_row()
{
	var submit_up		= document.getElementById('submit_up');
	var submit_down		= document.getElementById('submit_down');
	
	showRow('photos_row1');
	showRow('photos_row2');
	showRow('photos_hr_row');
	showRow('photos_after_hr_row');
	
	update_emo_photos_submits();
}

function hide_photos_row()
{
	var emoticons_row	= document.getElementById('emoticons_row');
	var submit_up		= document.getElementById('submit_up');
	var submit_down		= document.getElementById('submit_down');
	
	hideRow('photos_row1');
	hideRow('photos_row2');
	hideRow('photos_after_hr_row');
	
	if (emoticons_row.style.display == 'none' && 
        ($("tr#embed_videos_row").length == 0 || $("tr#embed_videos_row").height() == 0)) // $("tr#embed_videos_row").is(":visible") == false
	{
		hideRow('photos_hr_row');
	}
    
    update_emo_photos_submits();
}

function hide_emo_photos()
{
	hideRow('emoticons_hr_row');
	hideRow('emoticons_row');
	hideRow('photos_hr_row');
	hideRow('photos_row1');
	hideRow('photos_row2');
	hideRow('photos_after_hr_row');
}
