
// The preferred mode for showing the flash player where needed 
// is to move the containing DOM node. Because moving the node requires
// removing from and adding it back to the DOM, the flash player is 
// stopped and restarted accordingly. Some browsers to do not support
// a proper restart: for those absolute positioning is used as a workaround. 
var useOverlays = dojo.isIE || dojo.isOpera ||
	navigator.userAgent.indexOf("Linux")>=0;

// in case a platform makes too much trouble
var disableEmbedding = dojo.isIE<=6 || 
	!swfobject.hasFlashPlayerVersion("10");


var onPlayerEvent = function(oldState, newState)
{
	mikes.debug("state change "+oldState+" -> "+newState);
	audio.onStateChange(oldState, newState);
}

var onLogEvent = function(event)
{
	if (console.debug)
	{
		console.debug("flash log: "+event);
	}
}

var audio = {
	getFlashObject: function() {
		audio._flashObject = audio._flashObject || dojo.byId("fileAudio");
		return audio._flashObject;
	},
	isAvailable: function() {
		return (_flashObject = audio.getFlashObject())
			 && _flashObject.mk_play;
	},
	play: function(url, delayed) {
		mikes.debug("playing "+delayed);		
		var _play = function() {
			audio.getFlashObject().mk_setUrl(url);
			audio.getFlashObject().mk_play();
		};
		if (delayed) {
			audio._nextAction = _play;
		}
		else
		{
			_play();
		}
	},
	pause: function() {
		if (audio.isAvailable())
		{
			audio.getFlashObject().mk_pause();
		}
	},
	stop: function() {
		audio._nextAction = null;
		if (audio.isAvailable())
		{
			audio.getFlashObject().mk_stop();
		}
	},
	onStateChange: function(oldState, newState) {
		audio._state = newState;
		if (!oldState) // initializing
		{
			audio._init();
		}
		for (var i=0; i<audio._listeners.length; ++i)
		{
			audio._listeners[i](oldState, newState);
		}
	},
	addListener: function(listener) {
		audio._listeners.push(listener);
	},
	_init: function() {
		mikes.debug("flash object available");
		if (audio._nextAction) { 
			var action = audio._nextAction; 
			audio._nextAction = null;
			action(); 
		}
	},
	_listeners: [],
	_state: null
}

var current = {
	_node: null,
	_entryId: null,
	setNode: function(newNode) {
		if (newNode==current._node) {
			return;
		}
		if (current._node) {
			current.hide();
		}
		current._node = newNode;
		current._entryId = newNode ? dojo.attr(newNode, "id").substring(2) : null;
		if (current._node) {
			current.show();
		}
	},
	getNode: function() {
		return current._node; 
	},
	getEntryId: function() {
		return current._entryId;
	},
	hide: function() {
		if (disableEmbedding)
		{
			return;
		}
		var entryId = current.getEntryId();
		mikes.debug("hiding flash player....");
		dojo.removeClass("pl"+entryId, "a11y"); // play menu
		//mikes.show("dl"+entryId); // download menu
		mikes.hide("fl"+entryId); // proxy node for flash player 
		if (useOverlays)
		{
			dojo.style(audio.getFlashObject(), "left", "-1000px");			
		}
	},
	show: function() {
		if (disableEmbedding)
		{
			return;
		}
		var entryId = current.getEntryId();
		mikes.debug("showing flash player....");
		dojo.addClass("pl"+entryId, "a11y"); // play menu
		// mikes.hide(dojo.query("#pl"+entryId+" a")); // only hide link
		//mikes.hide("dl"+entryId); // download menu
		mikes.show("fl"+entryId); // proxy node for flash player
		if (useOverlays)
		{
			current.updatePosition();
		}
		else
		{
			dojo.style(audio.getFlashObject(), "position", "relative");
			dojo.style(audio.getFlashObject(), "left", "0");
			dojo.place(audio.getFlashObject(), "fl"+entryId, "last");
		}
	},
	updatePosition: function() {
		if (useOverlays && current._node)
		{
			var backgroundNode = dojo.byId("fl"+current.getEntryId()); 
			dojo.style(backgroundNode, "display", "block");
			dojo.marginBox(audio.getFlashObject(), getAbsolutePos(backgroundNode)); 
			var box = dojo.marginBox(audio.getFlashObject())
			dojo.marginBox(backgroundNode, {w: box.w, h: box.h});	
		}
	}
}

var players = {
	show: function()
	{
		dojo.query(".archivePlayer").style("display", "");
	},
	hide: function()
	{
		dojo.query(".archivePlayer").style("display", "none");
	},
	get: function() 
	{	
		return dojo.query(".archivePlayer .control");
	}
}

var getAbsolutePos = function(node) 
{
	var curleft = curtop = 0;
	if (node.offsetParent) {
		do {
			curleft += node.offsetLeft;
			curtop += node.offsetTop;
		} 
		while (node = node.offsetParent);
	}
	return {l:curleft,t:curtop};
}

var getIcon = function(state)
{
	var path = mikes.resourceUrl+"/icons/gifs/";
	switch (state)
	{
	case "playing":
		return path+"control_stop_blue.gif";
	case "connecting":
		return path+"control_load_blue.gif";
	default: 		
		return path+"control_play_blue.gif";
	}
}

var onFilePlayerClick = function(parent, url)
{
	var node = parent.getElementsByTagName("img")[0];
	if (node==current.getNode())
	{
		switch (audio._state)
		{
		case "connecting":
			return;
		case "playing":
			audio.stop();
			return;
		default:
			audio.play(url);
			return;
		}
	}
	else
	{	
		audio.stop();
		current.setNode(node);
		audio.play(url, !disableEmbedding && (!useOverlays || !audio.isAvailable()));
	}
}

var init = function()
{
	var _players = players.get();
	
	if (_players.length==0)
	{
		return;
	}

	if (!swfobject.hasFlashPlayerVersion("9"))
	{
		players.hide();
		return;
	}
	
	var flashvars = { 
		javascriptCallbackFunction: "onPlayerEvent",
		logCallbackFunction: "onLogEvent",
		leftButton: true
	};
	var params = { bgcolor: "#ffffff", allowscriptaccess: "always" };
	var attributes = {};

	swfobject.embedSWF(
		mikes.resourceUrl+"/flash/filePlayer.swf"+
			"?version="+mikes.version, 
		"fileAudio", 
		"270",
		"20",
		"9.0.0",
		"", 
		flashvars,
		params, 
		attributes
	);
	
	_players.attr("src", getIcon());
	
	audio.addListener(
		function(oldState, newState) {
			// update icon
			var node = current.getNode();
			if (node) {
				node.src = getIcon(newState);
			}
		}
	);

	audio.addListener(
		function(oldState, newState) {
			// hide flash player when stopped manually
			if (oldState && newState=="noUrl") {
				current.setNode(null);
			}
		}
	);
	
	if (useOverlays)
	{
		mikes.resizeListeners.push(current.updatePosition);
	}
}

dojo.addOnLoad(init);
