// Folio Control v1.2
// Andrew Pettican (December 2009)
//
// Changelog:
// v1.0 - 23/11/2009 - Initial build
// v1.1 - 25/11/2009 - folio_stats box now never goes off the bottom of the screen (providing there is space for it)
// v1.2 - 02/12/2009 - Securities statistic has been added
//////////////////////////////////////////////////////////////////

// ---------------------------------------------------------------
// Parameters
// ---------------------------------------------------------------

// Classnames of the folio_stats items we will attempt to retrieve
var fc_stats_element_classes = new Array("fs_company", "fs_sctr", "fs_securities", "fs_investment", "fs_stus", "fs_invt", "fs_exit");

// The names of the related folio_stats items
var fc_stats_element_names = new Array("Portfolio Company", "Sector", "Securities", "Investment", "Status", "Investment Year", "Exit Year");

// ---------------------------------------------------------------
// Parameters End
// ---------------------------------------------------------------



// ---------------------------------------------------------------
// Global Vars, Do not change anything below this line!
// ---------------------------------------------------------------

var fc_folio_y_offset = 0;				// Keep track of the y-offset for the folio_list element
var fc_folio_stats_height_offset = 3;	// Required to ensure bottom alignment of the folio_stats box

// ---------------------------------------------------------------
// Global Vars End, Do not change anything above this line!
// ---------------------------------------------------------------



// ---------------------------------------------------------------
// JS Events
// ---------------------------------------------------------------

$(document).ready(function()
{
	if($('ul#folio_list').length > 0)
	{
		// Determine the position of the list
		fc_folio_y_offset = Math.round($('ul#folio_list').offset().top);
		
		// When the mouse enters a list element, do the following:
		// 1. Repopulate data in the folio_stats box
		// 2. Adjust the position of the folio_stats box
		// 3. Display the folio_stats box
		$('ul#folio_list li').mouseenter(function()
		{
			// Do we have an id?
			if(this.id !== "")
			{
				// 1. Repopulate data in the folio_stats box
				$("#folio_stats").html(populate_folio_stats(this.id));
				
				// 2. Adjust the position of the folio_stats box
				var list_height = $("#folio_list").height();
				var window_height = $(window).height();
				var window_scroll = $(window).scrollTop();
				
				// Determine the height of the header, folio_stats boxes will not be allowed above this point
				var header_height = Math.max(0, $("ul#folio_list li.top").offset().top - fc_folio_y_offset);
				
				// Get the height required for the folio_stats box
				var folio_stats_height = $("#folio_stats").height()+fc_folio_stats_height_offset;
				
				// Determine how much space we have to display the folio_stats box.
				// This must fit within the window area, and cannot be pushed beyond the end of the folio_list.
				var available_space = Math.min(list_height, (window_height+window_scroll) - fc_folio_y_offset);
				
				// Determine the maximum top margin we can use to fit within the available_space
				var max_available_top_margin = Math.max(header_height, available_space - folio_stats_height);
				
				// Determine the ideal margin required to line up the folio_stats box next to its related list element
				var list_y_offset = Math.round($(this).offset().top);
				var ideal_top_margin = Math.max(0, list_y_offset - fc_folio_y_offset);
				
				// Determine which margin we will use
				var final_top_margin = Math.min(ideal_top_margin, max_available_top_margin);
				$("#folio_stats").css('margin-top', final_top_margin+'px');
				
				// 3. Display the folio_stats box
				$("#folio_stats").css('display','block');
			}
			else
			{
				// Hide the folio_stats box
				$("#folio_stats").css('display','none');
			}
			
			// Debug
			/*
			var debug = "";
			debug += "fc_folio_y_offset: "+fc_folio_y_offset+"<br />";
			debug += "list_height: "+list_height+"<br />";
			debug += "window_height: "+window_height+"<br />";
			debug += "window_scroll: "+window_scroll+"<br />";
			debug += "header_height: "+header_height+"<br />";
			debug += "folio_stats_height: "+folio_stats_height+"<br />";
			debug += "available_space: "+available_space+"<br />";
			debug += "max_available_top_margin: "+max_available_top_margin+"<br />";
			debug += "list_y_offset: "+list_y_offset+"<br />";
			debug += "ideal_top_margin: "+ideal_top_margin+"<br />";
			debug += "final_top_margin: "+final_top_margin+"<br />";
			debug += "<br />";
			$("#debug").html(debug);
			*/
		});
		
		// When the mouse leaves the folio_wrapper, hide the folio_stats box
		$('#folio_wrapper').mouseleave(function(e)
		{
			// Hide the folio_stats box
			$("#folio_stats").css('display','none');
		});
	}
});

// ---------------------------------------------------------------
// JS Events End
// ---------------------------------------------------------------



// ---------------------------------------------------------------
// Functions
// ---------------------------------------------------------------

/**
 * Generates the html for a folio_list li element
 */
function populate_folio_stats(list_id)
{
	var stats_html = "";
	
	// Check that list_id exists and that a folio_stats_item can be found
	if($('ul#folio_list li#'+list_id+' .folio_stats_item').length > 0)
	{
		// Look for all stats element classes and build the html from their values
		for(i=0; i<fc_stats_element_classes.length; i++)
		{
			var classname = fc_stats_element_classes[i];
			var retreived_content = $('ul#folio_list li#'+list_id+' .folio_stats_item .'+classname);
			if(retreived_content.length > 0)
			{
				stats_html += '<h3>'+fc_stats_element_names[i]+'</h3>\n';
				stats_html += '<p>'+$(retreived_content).html()+'</p>\n';
			}
		}
	}
	
	// Insert a class="last" on the last <p> element in stats_html
	if(stats_html != "")
	{
		var last_p = stats_html.lastIndexOf("<p");
		stats_html = stats_html.substr(0, last_p)+'<p class="last"'+stats_html.substr(last_p+2);
	}
	
	return stats_html;
}

// ---------------------------------------------------------------
// Functions End
// ---------------------------------------------------------------
