﻿/* 
General site stuff
*/

if (!jQuery)
	throw "Needs jQuery to be installed";

$(function ()
{
	initialiseFooterPanel();

	initialiseImagePopup();

	initialisePopoutHeader();
});

function initialiseFooterPanel()
{
	// "Extra info" panel in footer.
	var $panel = $("#readMorePanel");
	var $link = $("#readMoreLink");
	var linkText = $link.text();

	$panel.hide();

	$link.click(function (e)
	{
		e.preventDefault();

		$panel.toggle();

		if ($panel.is(":visible") === true)
		{
			$link.text("Hide");
		}
		else
		{
			$link.text(linkText);
		}
	});
}

function initialiseImagePopup()
{
	$(".popupLink").colorbox({ photo: true });

	$(".popupVideo").colorbox({ inline: true, href: "#supagardVideo" });

}

function initialisePopoutHeader()
{
	if (!FlashDetect.installed)
	{
		// Hide flash panel, use non-flash instead
		$(".mainHeaderFlash").hide();
		$("#noflash").show();
	}
	else
	{
		var flashMovieFile = $("#FlashPath").val();

//		$("#flashDataObject").attr("data", flashMovieFile);
//		$("#FlashID").find("param[name=movie]").attr("value", flashMovieFile);
//		swfobject.registerObject("FlashID");

		loadSWF(flashMovieFile, "FlashID");

		$(".headerPopoutContainer [rel=trigger] a").click(function (e)
		{
			$(".headerPopout").show(200);
			$(this).parent().addClass("trigger-hover");

			e.preventDefault();
		});

		$(".headerPopout").mouseleave(function (e)
		{
			$(this).hide(200);
			$(this).parent().find(".trigger-hover").removeClass("trigger-hover");
		});
	}

}

function loadSWF(url, targetID)
{
	//Check for existing SWF
	if (isObject(targetID))
	{
		//replace object/element with a new div
		replaceSwfWithEmptyDiv(targetID);
	}

	//Embed SWF
	if (swfobject.hasFlashPlayerVersion("6"))
	{
		var attributes = { data: url, width: "741", height: "170" };
		var params = { wmode: "transparent" };
		var obj = swfobject.createSWF(attributes, params, targetID);
	}
}

//Support function: checks to see if target
//element is an object or embed element
function isObject(targetID)
{
	var isFound = false;
	var el = document.getElementById(targetID);
	if (el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED"))
	{
		isFound = true;
	}
	return isFound;
}

//Support function: creates an empty
//element to replace embedded SWF object
function replaceSwfWithEmptyDiv(targetID)
{
	var el = document.getElementById(targetID);
	if (el)
	{
		var div = document.createElement("div");
		el.parentNode.insertBefore(div, el);

		//Remove the SWF
		swfobject.removeSWF(targetID);

		//Give the new DIV the old element's ID
		div.setAttribute("id", targetID);
	}
}
