var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var etherplayerObj;

function movieCue(cueName) {
	var command = cueName.split("_");

	if (command[2]=='off') {
		fadeBG(command[0], "rgb(255,255,255)", 7);	
	} else {
		fadeBG(command[0], "rgb(204,255,153)", 7);
	}
	
}

function stopMovie() {
	etherplayerObj.SetVariable("/:JSMessage", "stopMovie");
}

function fadeBG(className, tColour, fadeSpeed) {
	var stC = getStyleClass(className);
	if (stC) {
		var currentColour = parseColourText(stC.style.backgroundColor);
		var targetColour = parseColourText(tColour);

		var diffColour = new Array(
			Math.ceil((targetColour[0]-currentColour[0])/fadeSpeed),
			Math.ceil((targetColour[1]-currentColour[1])/fadeSpeed),
			Math.ceil((targetColour[2]-currentColour[2])/fadeSpeed)
		);

		
		var newColour = new Array(
			currentColour[0]+diffColour[0],
			currentColour[1]+diffColour[1],
			currentColour[2]+diffColour[2]
		);
		
		
		stC.style.backgroundColor = 'rgb('+newColour+')';
	}
	
	if ((Math.abs(diffColour[0])+Math.abs(diffColour[1])+Math.abs(diffColour[2]))>1) {
		setTimeout("fadeBG('"+className+"','"+tColour+"',"+fadeSpeed+")", 40);
	} else {
	}
}

function parseColourText(input_color) {

  var color = input_color.toString();
  var thecolor;
  
  var rgb_regex = /(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})/;
  var hex_regex = /#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\b/;
 
  //alert("here's the color (inside getcolor): " + color);
  if (color.match(hex_regex))
  {
    thecolor = new Array(parseInt(color.match(hex_regex)[1],16),parseInt(color.match(hex_regex)[2],16),parseInt(color.match(hex_regex)[3],16));
  }
  else if (color.match(rgb_regex))
  {
    thecolor = new Array(parseInt(color.match(rgb_regex)[1]),parseInt(color.match(rgb_regex)[2]),parseInt(color.match(rgb_regex)[3]));
  }
  else
  {
    //alert("holy error Batman!  color \""+ color +"\" didn't match rgb or hex regex")
    thecolor = new Array("0","0","0");
  }
  
  return thecolor;
}

function getStyleClass (className) {
	for (var s = 0; s < document.styleSheets.length; s++)
	{
		if(document.styleSheets[s].rules)
		{
			for (var r = 0; r < document.styleSheets[s].rules.length; r++)
			{
				if (document.styleSheets[s].rules[r].selectorText == '.' + className)
				{
					return document.styleSheets[s].rules[r];
				}
			}
		}
		else if(document.styleSheets[s].cssRules)
		{
			for (var r = 0; r < document.styleSheets[s].cssRules.length; r++)
			{
				if (document.styleSheets[s].cssRules[r].selectorText == '.' + className)
					return document.styleSheets[s].cssRules[r];
			}
		}
	}
	
	return null;
}

function enterFullScreen(theURL) {
	window.open(theURL, 'fsMovie', 'fullscreen=yes, scrollbars=no, dependent=yes, resizable=yes, status=no');
}

// Handle all the FSCommand messages in a Flash movie. Flash 7 method used for compatibility.
function etherplayer_DoFSCommand(command, args) {		
	if (command=="init") {
		etherplayerObj = isInternetExplorer ? document.all.etherplayer : document.etherplayer; //Can only do this once flash has loaded
		etherplayerObj.SetVariable("/:inBrowser", "true");
	}
	if (command=="fullscreen") {
		if (args="true") {
			stopMovie();
			enterFullScreen("fullscreen.htm");
		}
	}
	
	if (command=="cue") {
		movieCue(args);
	}
}

// Hook for Internet Explorer.
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub etherplayer_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call etherplayer_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}