swfobject.registerObject("audioPlayer", "9.0.115");

function display_flashplayer(file, path, div){

  if (swfobject.hasFlashPlayerVersion("9.0.115")) {
  
    //alert("play in div: " + div + "\nfile: " + file);
	var fn = function() {
	
	  var att = { data:"" + path + "/docroot/mgb/flash/player.swf", width:"270", height:"24" };
	  var par = { flashvars:"file=" + file};
	  var id = div;
	  swfobject.createSWF(att, par, id);
	};
	
	swfobject.addDomLoadEvent(fn);
  }
}

jQuery.fn.alertElementId = function(fileUrl, optionValue, path){    
	return this.each(function() {
	
		var parent = this.parentNode;

		if(hasClass(parent, "synchronize"))
		{
			var selectId = this.id.replace("substituteForFlashPlayer-", "select");
			var myOpts = document.getElementById(selectId).options;
			
			for(var i = 0; i < myOpts.length; i++)
			{
				if(myOpts[i].value == optionValue) // Falls Auswahlliste das entsprechende Option hat
				{
					document.getElementById(selectId).selectedIndex = i;
					display_flashplayer(fileUrl, path, this.id);
					break;
				}
			}
		}
		// Innere Funktion, hat Zugriff auf die lokalen Variablen der Funktion, in die sie eingebettet ist.
		function hasClass(element, cls)
		{
			var r = new RegExp('\\b' + cls + '\\b');
			return r.test(element.className);
		}
	});
};

function playback(selectobj, optionValueList, fileUrlList, path, div)
{
	for(var i=0; i<optionValueList.length; i++)
	{
		if(optionValueList[i]==selectobj.value)
		{
			if($('#' + div).parent().hasClass("synchronize"))
			{
				// Erstelle ein Session-Cookie
				document.cookie = 'optionValue=' + selectobj.value + '; path=/'
				
				// Erzeuge Flashplayer für entsprechende Nodes auf aktueller Seite
				$('object[id|="substituteForFlashPlayer"]:not("#' + div + '")').alertElementId(fileUrlList[i], optionValueList[i], path);
			}

			// Erzeuge Flahsplayer für aktuellen Node
			display_flashplayer(fileUrlList[i], path, div);
		}
	}
}
