﻿var dps_link_node_to_hide = null;
var dps_link_node_to_show = null;

function DPS_ShowControl()
{
	DPS_RestoreInitialState();

	var control_node = document.getElementById("dps_submit_control");
	var place_node = document.getElementById("dps_submit_post_place");
	
	if( control_node.parentNode != place_node )
	{
		place_node.parentNode.appendChild( control_node );
	}
	
	document.getElementById("dps_start_link").style.display = "none";
	document.getElementById("dps_close_link").style.display = "inline-block";
	
	dps_link_node_to_hide = document.getElementById("dps_close_link");
	dps_link_node_to_show = document.getElementById("dps_start_link");
	
	control_node.style.display = "block";
	
	DPS_ScrollToControl();
}

function DPS_HideControl()
{
	DPS_RestoreInitialState();
}

function DPS_ReplyToItem( post_id )
{
	DPS_RestoreInitialState();

	var control_node = document.getElementById("dps_submit_control");
	var item_node = document.getElementById("dps_pi_" + post_id);
	if( control_node.previousSibling != item_node )
	{
		var item_sibling_node = item_node.nextSibling;
		
		if( item_sibling_node == null )
			item_node.parentNode.appendChild( control_node );
		else
			item_node.parentNode.insertBefore( control_node, item_sibling_node );
	}

	document.getElementById( document.getElementById("dps_replypost_field").value ).value = post_id;
	control_node.style.display = "block";

	DPS_ScrollToControl();
}

function DPS_ReplyToLogin( post_id )
{
	document.getElementById("dps_pr_" + post_id).style.display = "none";
	document.getElementById("dps_prl_" + post_id).style.display = "block";
}

function DPS_NewsToItem( news_id )
{
	DPS_RestoreInitialState();

	var control_node = document.getElementById("dps_submit_control");
	var item_node = document.getElementById("dps_ni_" + news_id);
	if( control_node.previousSibling != item_node )
		item_node.parentNode.insertBefore( control_node, item_node.nextSibling );

	document.getElementById( document.getElementById("dps_news_field").value ).value = news_id;
	
	dps_link_node_to_hide = document.getElementById("dps_ncl_" + news_id);
	dps_link_node_to_hide.style.display = "inline-block";
	
	dps_link_node_to_show = document.getElementById("dps_nsdl_" + news_id);
	dps_link_node_to_show.style.display = "none";
	
	control_node.style.display = "block";

	DPS_ScrollToControl();
}

function DPS_NewsToLogin( news_id )
{
	document.getElementById("dps_np_" + news_id).style.display = "none";
	document.getElementById("dps_npl_" + news_id).style.display = "block";
}

function DPS_EditPost( post_id, submit_form )
{
	if( submit_form )
	{
		document.getElementById( document.getElementById("dps_editpost_field").value ).value = post_id;
		document.getElementById( document.getElementById("dps_loaddata_field").value ).value = "1";
		
		document.forms[0].submit();
	}
	else
	{
		var control_node = document.getElementById("dps_submit_control");
		var item_node = document.getElementById("dps_pi_" + post_id);
		if( control_node.previousSibling != item_node )
		{
			var item_sibling_node = item_node.nextSibling;
			
			if( item_sibling_node == null )
				item_node.parentNode.appendChild( control_node );
			else
				item_node.parentNode.insertBefore( control_node, item_sibling_node );
		}

		item_node.style.display = "none";
		control_node.style.display = "block";

		document.getElementById("dps_close_link").style.display = "none";
		document.getElementById("dps_start_link").style.display = "inline-block";

		DPS_ScrollToControl();
	}
}

function DPS_RestoreInitialState()
{
	document.getElementById("dps_submit_control").style.display = "none";
	if( dps_link_node_to_hide != null )
		dps_link_node_to_hide.style.display = "none";
		
	if( dps_link_node_to_show != null )
		dps_link_node_to_show.style.display = "inline-block"
	
	dps_link_node_to_hide = null;
	dps_link_node_to_show = null;
	
	document.getElementById( document.getElementById("dps_replypost_field").value ).value = "";
	document.getElementById( document.getElementById("dps_news_field").value ).value = "";
	
	var edit_post_field = document.getElementById( document.getElementById("dps_editpost_field").value );
	if( edit_post_field.value != "" )
	{
		document.getElementById( "dps_pi_" + edit_post_field.value ).style.display = "block";
		edit_post_field.value = "";
	}	
}

function DPS_ShowFullRepliedPost( post_id )
{
	document.getElementById( "dps_rtpf_" + post_id ).style.display = "block";
	document.getElementById( "dps_rtps_" + post_id ).style.display = "none";
}

function DPS_ScrollToControl()
{
	window.scroll(0, document.getElementById("dps_submit_control").offsetTop);
}
