// --------------------------------------------------------
// 
// --------------------------------------------------------
$(document).ready(
	function ()
	{
		/*
		try
		{
			var args = document.location.search.replace(/\?/, "").split("&");
			
			for (var i = 0; i < args.length; i++)
			{
				var fields = args[i].split("=");
				
				if (fields[0] == "c")
				{
					updateCompanyData(fields[1]);
					break;
				};
			};
		}
		catch (e)
		{

		};
		*/
			
		$("#media-company-list").change(handleCompanySelection);
	}
);

// --------------------------------------------------------
// 
// --------------------------------------------------------
function updateCompanyData (companyId)
{
	$("#company-data-loading").show();
		
	$.ajax(
		{
			url: "company_data/" + companyId + ".html", 
			dataType: "text",
			error: function ()
			{
			
			},
			complete: function (a)
			{

			},
			success: function (d)
			{
				// console.log(d);

				$("#company-data-loading").hide();
				
				$("#company-data").html(d);
				
				// document.title = companyId + " : " + document.title;
				
				var menuEl = $("#media-company-list");
				
				for (var i = 0; i < menuEl[0].options.length; i++)
				{
					if (menuEl[0].options[i].value == companyId)
					{
						// console.log(menuEl[0].options[i]);
						
						document.title = menuEl[0].options[i].text + " : " + document.title;
					}
				}
			}
		}
	);
	
}

// --------------------------------------------------------
// 
// --------------------------------------------------------
function handleCompanySelection (e)
{
	// clear the old content
	$("#company-data").html("");
	
	// send the request for the new company
	var menuEl = $("#media-company-list");
	
	// updateCompanyData(menuEl.val());
	
	// console.log(self.location);
	
	self.location = self.location.pathname + "?c=" + escape(menuEl.val());
};

// --------------------------------------------------------
// toggle the holdings / timeline data
// --------------------------------------------------------
function selectCompanyView (view)
{
	$("#nav-who-owns-what")[0].className = view;

	(view == "timeline") ? $("#media-company-timeline").show() : $("#media-company-timeline").hide();

	(view == "holdings") ? $("#media-company-holdings").show() : $("#media-company-holdings").hide();
};
