// resolves movie using supplied id
function thisMovie(movieName) 
{
	if (navigator.appName.indexOf("Microsoft") != -1) 
	{
		return window[movieName]
	}
	else 
	{
		return document[movieName]
	}
}

// standard interface for JavaScript calls on Flash
function flashInterface(jObj)
{
	return thisMovie("flashContent").flashInterface(jObj);
}

function externalInterfaceTest()
{
	var command = document.getElementById("command").value;
	var params = document.getElementById("params").value.split(",");
	
	// remove leading and trailing whitespace from params
	for (var i = 0; i < params.length; i ++) 
	{
		params[i] = stripWhitespace(params[i]);
	}
	
	var jObj = {"command": command, "returnRequired": false, "params": params};
	flashInterface(jObj);
}

// standard interface for Flash calls on JavaScript
function javascriptInterface(jObj)
{
	//window.alert("Call made by Flash, command: " + jObj.command + ", returnRequired: " + jObj.returnRequired + ", parameters: " + jObj.params + ", webtrends: " + jObj.analytics);

	
	// pass any analytics parameters to dcsTrk method
	
	/*
	if (jObj.analytics.length>0) 
	{ 
		output("WebTrends: " + jObj.analytics[0]); 
	}
	else
	{
		output("Call made by Flash, command: " + jObj.command + ", returnRequired: " + jObj.returnRequired + ", parameters: " + jObj.params + ", webtrends: " + jObj.analytics);
	}
	*/
		
	//output("Call made by Flash, command: " + jObj.command + ", returnRequired: " + jObj.returnRequired + ", parameters: " + jObj.params + ", webtrends: " + jObj.analytics);
	
	switch(jObj.command)
	{	
		case "javascript_available":
		try {
			configureOverlay();
		} catch (err)
  		{}

		return true;
		break;
		
		case "flash_available":
		// no return required here
		// this is the hook for any applications which 
		// receive initial state via external interface
		break;
		
		case "redirect":
		redirect(jObj);
		break;
		
		default:
		window.alert("Command name " + jObj.command + " not recognised");
	}
}

function redirect(jObj)
{
	var url = jObj.params[0];
	var type = jObj.params[1];
	
	switch(type)
	{
		case "thickbox":
		url = url + "&KeepThis=true&TB_iframe=true&width=912&height=600";
		//tb_show("Thickbox demo", url);		
		//url = "flip.html?KeepThis=true&TB_iframe=true&width=600&height=300";
		tb_show("", url);
		break;
		
		case "overlay":
		launchOverlay(url);
		break;

		
		default:
		window.alert("Window type " + type + " not recognised");
	}
}

// for debug only, outputs supplied message through output field
function output(msg)
{
	document.getElementById("output").value = (msg + "\n" + document.getElementById("output").value);
}

// strips leading and trailing whitespace
// http://bytes.com/forum/thread165013.html
function stripWhitespace(str)
{
	return str.replace(/^\s*|\s*$/g, "");
}


/* Trace Your Coke */
// VARIABLES REQUIRED FOR IMPLEMENTATION OF OVERLAY

var flashId = "#flashContent";
var overlayId = "#overlay-container";
var closeButtonId = "#overlay-close";

var overlayAjax = "#overlay-ajax";

function configureOverlay()
{
	var flashWidth = $(flashId).width(); // (NUMBER)
	var flashHeight = $(flashId).height(); // (NUMBER)
	var containerPaddingX = 16; // (NUMBER)
	var containerPaddingY = 6; // (NUMBER)
	// Prepare overlay...
	$(overlayId).css({"height" : String(flashHeight - (2 * containerPaddingY)) + "px", "width" : String(flashWidth - (2 * containerPaddingX)) + "px", "left" : String(containerPaddingX) + "px", "top" : String(containerPaddingY) + "px"});
	$(overlayAjax).css("height", String(flashHeight - (2 * containerPaddingY)) + "px");
	// ONLY REQUIRED IF WE NEED TO CLOSE THE WINDOW FROM OUSIDE OF THE AJAX.
	bindClose();
}

function launchOverlay(url) {
	
	$(overlayId).fadeIn("slow");
	
	// ONLY REQUIRED IF WE NEED TO CLOSE THE WINDOW FROM INSIDE THE AJAX.
	//overlayAjax.ajaxComplete(function(evt) { bindClose() });
	$(overlayAjax).load(url);
	$(overlayAjax).ajaxComplete(function(evt){ attachPaginationListeners(); });
}

function attachPaginationListeners()
{
	
$("ul.pagination li a").each(function(evt)
	{
		$(this).bind("click", function(evt){
			
			var thisPaginator = $(this);
			
			evt.preventDefault();
			$(overlayAjax).fadeOut("slow", function(evt){ 
				//alert($(thisPaginator).attr("href"));
				$(overlayAjax).load($(thisPaginator).attr("href"));
				$(overlayAjax).ajaxComplete(function(evt){ 
					//attachPaginationListeners();
					$(overlayAjax).fadeIn("slow"); 
				
				});
				
			});
		});
	});
}

function bindClose()
{
	// Assign Control
	$(closeButtonId).bind("click", function(evt)
	{
		evt.preventDefault();
		$(overlayId).fadeOut("slow", function(evt) 
		{ 
			$(overlayAjax).text(""); 
			// tell flash to resume whatever it was doing before the overlay was opened
			var jObj = {"command": "resume", "returnRequired": false};
			flashInterface(jObj);
		});
	});	
}

