/*
	allows a comment to be a reply to another comment
*/

/**
 * @todo Remove the cid argument, the comment id is being passed twice.
 */
function reply_to(id, type_id, username, cid)
{
	// grab fields
	targetForm = gebi('comments-form');
	t_id = targetForm.target_object_id;
	t_type = targetForm.target_content_type;
	// keep original values
	original_id = t_id.value;
	original_type = t_type.value;
	// set new values
	t_id.value = id;
	t_type.value = type_id;

	// create reply info
	reply_block = gebi('reply_information');
	reply_block.innerHTML = build_reply_info(username, id, original_id, original_type);
	reply_block.style.display = 'block';
	
	// debug
	// d_id = gebi('debug_id');
	// d_type = gebi('debug_type');
	// d_id.innerHTML = id;
	// d_type.innerHTML = type_id;
	//alert('Set to: ' + targetForm.target_object_id.value + ', ' + targetForm.target_content_type.value);
}

/*
	builds the reply DOM stuff
*/

function build_reply_info(name, cid, o_id, o_type)
{
	t = ''
		+ "<b>You're replying</b> to <a href=\"c" + cid + "\" title=\"View comment\">" + name + "'s comment</a>."
		+ '<span class="no_reply">'
		+ '(<a href="#post" '
		+ 'onclick="clear_reply_to(\'' + o_id + '\', \'' + o_type + '\');" '
		+ 'title="Don\'t post this comment as a reply">'
		+ 'Don\'t post as reply?</a>)'
		// Debug.
		//+ '(<a href="#post" '
		//+ 'onclick="alert(\'' + o_id + ', ' + o_type + '\');">'
		//+ 'Debug Original</a>)'
		//+ '(<a href="#post" '
		//+ 'onclick="alert(\'' + gebi('target_object_id').value + ', ' + gebi('target_content_type').value + '\');">'
		//+ 'Debug Current</a>)'
		//+ '</span>'
		+ '';
	return t;
}

/*
	clears the reply connection
*/

function clear_reply_to(id, type)
{
	targetForm = gebi('comments-form');

	reply_block = gebi('reply_information');
	reply_block.innerHTML = '';
	reply_block.style.display = 'none';

	// reset hidden values
	targetForm.target_object_id.value = id;
	targetForm.target_content_type.value = type;

	// debug
	// d_id = gebi('debug_id');
	// d_type = gebi('debug_type');
	// d_id.innerHTML = o_id;
	// d_type.innerHTML = o_type;
	// alert('Set to: ' + gebi('target_object_id').value + ', ' + gebi('target_content_type').value);
}

/*
	removes "Search" from search field if necessary
*/

function prep_search(e)
{
	if (e.value == 'Search')
	{
		e.value = '';
	}
}

/*
	removes "Email address" from email field if necessary
*/

function prep_email(e)
{
	if (e.value == 'Email address')
	{
		e.value = '';
	}
}

/*
	changes the selected tab in the sidebar
	utilizes switch_block(), switch_tab()
*/
function change_tumble(type)
{
	recent_tab = gebi('t_tab_recent');
	recent_block = gebi('tumble_recent');
	events_tab = gebi('t_tab_events');
	events_block = gebi('tumble_events');
	diaries_tab = gebi('t_tab_diaries');
	diaries_block = gebi('tumble_diaries');
	
	if (type == 'recent')
	{
		switch_tab(recent_tab, true, '');
		switch_tab(events_tab, false, '');
		switch_tab(diaries_tab, false, 'y');
		switch_block(recent_block, true);
		switch_block(events_block, false);
		switch_block(diaries_block, false);
	}
	else if (type == 'events')
	{
		switch_tab(recent_tab, false, '');
		switch_tab(events_tab, true, '');
		switch_tab(diaries_tab, false, 'y');
		switch_block(recent_block, false);
		switch_block(events_block, true);
		switch_block(diaries_block, false);
	}
	else
	{
		switch_tab(recent_tab, false, '');
		switch_tab(events_tab, false, '');
		switch_tab(diaries_tab, true, 'y');
		switch_block(recent_block, false);
		switch_block(events_block, false);
		switch_block(diaries_block, true);
	}
}

/*
	utility function to switch the active block in the sidebar
*/
function switch_block(b, a)
{
	if (a == true)
	{
		s = 'block';
	}
	else
	{
		s = 'none';
	}
	b.style.display = s;
}

/*
	utility function to swtich the active tab in the sidebar
*/
function switch_tab(t, a, l)
{
	if (a == true)
	{
		c = 'active';
	}
	else
	{
		c = '';
	}
	if (l != '')
	{
		c = c + ' last';
	}
	t.className = c;
}

/*
	selects a state and forwards to the right page
*/

function state_select(s)
{
	if (s != '')
	window.location.href = '/states/' + escape(s) + '/';
}

/*
	shortcut method for getting an element by ID
*/
function gebi(e)
{
	return document.getElementById(e);
}
