// Functions related to the RFP Panel

var ie6_offset_props= 55;
var ie6_offset_list = 0;
var is_ie6			= ( window.external && typeof window.XMLHttpRequest == "undefined" );

// called to updated all the panel's items with the currently active RFP/List
function refreshPanelContents(property_id)
{
	// property_id is optional.  if passed, save it so this property can be shown after panel is refreshed
	if ( typeof property_id != "undefined" )
	{
		var oDiv = document.getElementById("panel_contents");
		if ( oDiv ) oDiv.show_property_id = property_id;
	}

	if (!xmlh_lock)
	{
		xmlh_lock = true;
		xmlh_queryPost("/ajax/createProgressPanel.php", "", refreshPanelContentsDone);
	}
}


function refreshPanelContentsDone(response)
{
	var data = eval('(' + response + ')');

	if ( data['success'] )
	{
		var oDiv = document.getElementById("panel_contents");
		if ( oDiv)
		{
			oDiv.innerHTML	= data["html"];
			oDiv.rfp_id		= data["rfp_id"];
			oDiv.num_items	= data["num_items"];
			oDiv.style.display = "block";		// compensates for IE 6 fix
			if ( typeof oDiv.show_property_id != "undefined" && oDiv.show_property_id ) animatePanelProp();

			// for ie6, if panel is closed, then hide properties to prevent infinite scrolling bug
			if ( is_ie6 )
			{
				var oPanel = document.getElementById("panel");
				if ( oPanel.state === undefined || oPanel.state == "close" )
				{
					var oProps = document.getElementById("panel_properties");
					oProps.style.display = "none";
				}
			}
		}
	}
	else alert(data["msg"]);

	xmlh_lock = false;
}


// call this to slide open the panel
function openPanel()
{
	var oDiv = document.getElementById("panel");
	if ( oDiv )
	{
		oDiv.state = "open";
		oDiv.dest = 0;
		if ( oDiv.timeout !== undefined ) clearTimeout(oDiv.timeout);
		oDiv.timeout = setTimeout("slidePanel()",10);
	}
}


// call this to slide close the panel
function closePanel()
{
	var oDiv = document.getElementById("panel");
	if ( oDiv )
	{
		oDiv.state = "close";
		oDiv.dest = -154;
		if ( oDiv.timeout !== undefined ) clearTimeout(oDiv.timeout);
		oDiv.timeout = setTimeout("slidePanel()",10);
	}
}


// call this to toggle the panel's open/close state
function togglePanel()
{
	var oDiv = document.getElementById("panel");
	if ( oDiv )
	{
		if ( oDiv.state === undefined || oDiv.state == "close" ) openPanel();
		else closePanel();
	}
}


// called by setInterval to animate the panel opening/closing
function slidePanel()
{
	var oDiv	= document.getElementById("panel");
	var oProps	= document.getElementById("panel_properties");
	var state	= oDiv.state;
	var cur		= parseInt(oDiv.style.bottom,10);
	var dest	= oDiv.dest;

	if ( state == "open" )
	{
		if ( document.all && is_ie6 )
		{
			ie6_offset_props = 211;
			oProps.style.display = "block";
		}
		else
		{
			var delta = Math.abs(Math.ceil((dest-cur)/2));		// How far to move?  Half of distance left over
			if ( delta > 50 ) delta = 50;						// Limit max distance to 50
			var new_b = cur + delta;
			if ( Math.abs(new_b-dest) <= 1) new_b = dest;
			oDiv.style.bottom = new_b+"px";
			if ( new_b != dest ) oDiv.timeout = setTimeout("slidePanel()",20);
		}
	}
	else if ( state == "close" )
	{
		if ( document.all && is_ie6 )
		{
			ie6_offset_props = 55;
			var oPanel = document.getElementById("panel_properties");
			oProps.style.display = "none";
		}
		else
		{
			var delta = Math.abs(Math.ceil((dest-cur)/2));		// How far to move?  Half of distance left over
			if ( delta > 50 ) delta = 50;						// Limit max distance to 50
			var new_b = cur - delta;
			if ( Math.abs(new_b-dest) <= 1) new_b = dest;
			oDiv.style.bottom = new_b+"px";
			if ( new_b != dest ) oDiv.timeout = setTimeout("slidePanel()",20);
		}
	}
}


function animatePanelProp()
{
	var oDiv = document.getElementById("panel_contents");
	if ( oDiv)
	{
		var oItem = document.getElementById("panel_prop_"+oDiv.show_property_id);
		if ( typeof oItem.offsetTop == "number" )
		{
			oItem.parentNode.scrollTop = oItem.offsetTop-60;
			oItem.style.backgroundColor = "rgb(255,255,255)"
			fadeColor("panel_prop_"+oDiv.show_property_id, 75, 36,50,59);
		}
	}
}


function fadeColor(id, step, r,g,b)
{
	var obj = document.getElementById(id);
	if ( step <= 0 )
	{
		obj.style.backgroundColor = "rgb("+r+","+g+","+b+")";
		obj.style.backgroundColor = "";
	}
	else
	{
		var color = new RGBColor(obj.style.backgroundColor);
		var new_r = Math.floor(color.r - (color.r-r)/step);
		var new_g = Math.floor(color.g - (color.g-g)/step);
		var new_b = Math.floor(color.b - (color.b-b)/step);
		obj.style.backgroundColor = "rgb("+new_r+","+new_g+","+new_b+")";

		step--;
		setTimeout("fadeColor('"+id+"', "+step+", "+r+","+g+","+b+")",20);
	}
}


////////////////
// Panel List
////////////////

// call this to open up the panel's list of rfps/lists
function refreshPanelList(action)
{
//	alert("refreshPanelList()");
	if ( typeof action == "undefined" ) action = "";
	var oDiv = document.getElementById("panel_list");
	oDiv.action = action;

	if (!xmlh_lock)
	{
		xmlh_lock = true;
		xmlh_queryPost("/ajax/createPanelList.php", "", refreshPanelListDone);
	}
} 


function refreshPanelListDone(response)
{
//	alert("refreshPanelListDone()");
	var data = eval('(' + response + ')');

	if ( data['success'] )
	{
		var oDiv = document.getElementById("panel_list");
		oDiv.innerHTML = data["html"];

		// panel is refreshed, perform any action
		if ( oDiv.action == "open" ) openPanelList();
		else if ( oDiv.action == "close" ) closePanelList();
		else if ( oDiv.action == "toggle" ) togglePanelList();
		oDiv.action = "";
	}
	else alert(data["msg"]);

	xmlh_lock = false;
}


function openPanelList()
{
//	alert("openPanelList()");
	var oDiv	= document.getElementById("panel_list_container");
	var oList	= document.getElementById("panel_list");

	// if panel is empty, refresh it then open it
	if ( !oList.innerHTML ) refreshPanelList("open");
	else if ( oDiv )
	{
		oDiv.state = "open";
		oDiv.dest = parseInt(getHeight(oList),10);
		if ( oDiv.timeout !== undefined ) clearTimeout(oDiv.timeout);
		oDiv.timeout = setTimeout("slidePanelList()",10);
	}
}

function closePanelList()
{
//	alert("closePanelList()");
	var oDiv	= document.getElementById("panel_list_container");
	var oList	= document.getElementById("panel_list");
	if ( oDiv )
	{
		oDiv.state = "close";
		oDiv.dest = 0;
		if ( oDiv.timeout !== undefined ) clearTimeout(oDiv.timeout);
		oDiv.timeout = setTimeout("slidePanelList()",10);
	}
}


// call this to toggle the panel's open/close state
function togglePanelList()
{
//	alert("togglePanelList()");
	var oDiv = document.getElementById("panel_list_container");
	if ( oDiv )
	{
		if ( typeof oDiv.state == "undefined" || oDiv.state == "close" ) openPanelList();
		else closePanelList();
	}
}


// called by setInterval to animate the panel opening/closing
function slidePanelList()
{
	var oDiv	= document.getElementById("panel_list_container");
	var state	= oDiv.state;
	var cur		= parseInt(oDiv.style.height,10);
	var dest	= oDiv.dest;

	if ( state == "open" )
	{
		if ( document.all && is_ie6 )
		{
			oDiv.style.display	= "block";
			oDiv.style.height	= "auto";
			ie6_offset_list		= oDiv.dest;
		}
		else
		{
			var delta = Math.abs(Math.ceil((dest-cur)/2));		// How far to move?  Half of distance left over
			if ( delta > 50 ) delta = 50;						// Limit max distance to 50
			var new_h = cur + delta;
			if ( Math.abs(new_h-dest) <= 1) new_h = dest;
			oDiv.style.height = new_h+"px";slidePanelList
			if ( new_h != dest ) oDiv.timeout = setTimeout("slidePanelList()",20);
		}
	}
	else if ( state == "close" )
	{
		if ( document.all && is_ie6 )
		{
			oDiv.style.display	= "none";
			ie6_offset_list		= 0;
		}
		else
		{
			var delta = Math.abs(Math.ceil((dest-cur)/2));		// How far to move?  Half of distance left over
			if ( delta > 50 ) delta = 50;						// Limit max distance to 50
			var new_h = cur - delta;
			if ( Math.abs(new_h-dest) <= 1) new_h = dest;
			oDiv.style.height = new_h+"px";
			if ( new_h != dest ) oDiv.timeout = setTimeout("slidePanelList()",20);
		}
	}
}


//Called when the user clicks an RFP in the panel list.  If rfp is new (not saved) and has items in it, then throw a warning.
function confirmActivateRfp(rfp_id)
{
	var oDiv = document.getElementById("panel_contents");
	if ( !oDiv.rfp_id && oDiv.num_items > 0 )
	{
		if ( confirm("You have not saved your new RFP.  Activating a different RFP will delete your new RFP.  Do you want to continue?") ) window.location.href="/planners/rfpSelect.php?rfp_id="+rfp_id;
		else if ( confirm("Would you like to save your new RFP?") ) window.location.href="/planners/rfpCreate.php";
	}
	else window.location.href="/planners/rfpSelect.php?rfp_id="+rfp_id;
}


///////////
// RFPS
///////////

// Called when user hits the 'x' next to a property in the progress panel.  Confirms the action and calls remFromRfp()
function confirmRemFromRfp(rfp_id, property_id, name)
{
	if ( confirm("Are you sure you want to remove "+name+" from the RFP") )
	{
		remFromRfp(rfp_id, property_id)
	}
}


// Uses ajax to remove a property from an rfp
function remFromRfp(rfp_id, property_id)
{
	if ( property_id )
	{
		if (!xmlh_lock)
		{
			xmlh_lock = true;
			xmlh_queryPost("/ajax/removePropertyFromRfp.php", "property_id="+property_id+"&rfp_id="+rfp_id, remFromRfpDone);
		}
	}
}


// callback from remFromRfp()
function remFromRfpDone(response)
{
	var data = eval('(' + response + ')');

	if ( data["success"] )
	{
		removePropFromPanel(data["property_id"], data["num_items"]);

		// toggle "Add to RFP" button on page (if available)
		var oDiv = document.getElementById("add_to_rfp_"+data["property_id"]);
		if ( oDiv ) oDiv.className = "add_to_rfp";
		
		// If there are no more properties in the tray, set the next button to
		// link to the search page.
		if (data["num_items"] == 0)
		{
			var pDiv = document.getElementById("panel_next_link");		
			if ( pDiv ) pDiv.href = "/search.php?error_msg=The+<i>Speed</i>RFP+engine+can\'t+run+without+gas+in+the+tank.+Please+select+at+least+one+property.";		
		}
	}
	else alert(data["msg"]);

	xmlh_lock = false;
}


///////////
// Lists
///////////

// Called when user hits the 'x' next to a property in the progress panel.  Confirms the action and calls remFromList()
function confirmRemFromList(list_id, property_id, name)
{
	if ( confirm("Are you sure you want to remove "+name+" from the List") )
	{
		remFromList(list_id, property_id)
	}
}


// Uses ajax to remove a property from a list
function remFromList(list_id, property_id)
{
	if ( property_id )
	{
		if (!xmlh_lock)
		{
			xmlh_lock = true;
			xmlh_queryPost("/ajax/removePropertyFromList.php", "property_id="+property_id+"&list_id="+list_id, remFromListDone);
		}
	}
}


// callback from remFromList()
function remFromListDone(response)
{
	var data = eval('(' + response + ')');

	if ( data["success"] )
	{
		/* lists are no longer shown in the panel
		removePropFromPanel(data["property_id"], data["num_items"]);

		// remove property from list (function only exists if on list page)
		if ( typeof removePropFromListTable == 'function') removePropFromListTable(data["property_id"]);

		// hide "In List/RFP" button on search page (if available)
		var oDiv = document.getElementById("in_rfp_"+data["property_id"]);
		if ( oDiv ) oDiv.className = "not_in_rfp";
		*/
	}
	else alert(data["msg"]);

	xmlh_lock = false;
}


// removed a property from the progress panel's list of items
function removePropFromPanel(property_id, num_items)
{
	// remove the item from the progress panel
	var oItem = document.getElementById("panel_prop_"+property_id);
	if ( oItem ) oItem.parentNode.removeChild(oItem);

	// update number of items in list in progress panel title
	var oDiv = document.getElementById("panel_num_items")
	if ( oDiv ) oDiv.innerHTML = num_items;

	// update number of items used in javascript
	var oDiv = document.getElementById("panel_contents");
	if ( oDiv ) oDiv.num_items = num_items;
}
