var CurrSongPos = -1;
var CurrSongState = "STOP";
var CurrSongObj = null;
var CurrSongCover = null;
var CurrSongCoverBW = null;
var blinkingTimeOut = null;
var firsSongURL = "";

var retryCount = 0;
function init()
{
   if(!mp3Player("FlashMP3Player"))
   {
      if (retryCount < 7)
         window.setTimeout("init();", 200);
      else
         document.getElementById("noFlashTable").style.display = "block";
      retryCount++;
      return;
   }
   playSong(document.getElementById("SongURL0").value, 0);
}

function endeMP3()
{
   var urlobj = document.getElementById("SongURL" + (CurrSongPos + 1));
   if (urlobj)
      playSong(urlobj.value, CurrSongPos+1);
   else
      stopSong();
}

function mp3Player(playername)
{
	if (navigator.appName.indexOf("Microsoft") != -1) 
	{
		return window[playername];
	} 
	else 
	{
		return document[playername];
	}
}

function stopSong()
{
   //if (songPos != CurrSongPos)
   //   return;   
   mp3Player("FlashMP3Player").stopMP3();
   if (blinkingTimeOut != null)
      window.clearTimeout(blinkingTimeOut);
   CurrSongObj.className = "";
   CurrSongCover.style.display = "none";
   CurrSongCoverBW.style.display = "block";
   CurrSongPos = -1;
   CurrSongState = "STOP";
   CurrSongObj = null;
   CurrSongCover = null;
   CurrSongCoverBW = null;
}

function playSong(url, songPos)
{
   if (songPos != CurrSongPos)
   {
      mp3Player("FlashMP3Player").setMP3(url);
      CurrSongState = "PLAY";
      CurrSongPos = songPos;
      if (CurrSongObj != null)
         CurrSongObj.className = "";
      if (CurrSongCover != null)
      {
         CurrSongCover.style.display = "none";
         CurrSongCoverBW.style.display = "block";
      }

      CurrSongObj = document.getElementById("Song" + songPos);
      CurrSongObj.className = "mtSongActive";
      
      CurrSongCover = document.getElementById("SongCover" + songPos);
      CurrSongCoverBW = document.getElementById("SongCoverBW" + songPos);
      CurrSongCover.style.display = "block";
      CurrSongCoverBW.style.display = "none";
      
      if (blinkingTimeOut != null)
         window.clearTimeout(blinkingTimeOut);
      blinkingTimeOut = null
   }
   else if(CurrSongState == "PAUSE")
   {
      mp3Player("FlashMP3Player").pauseMP3();
      CurrSongState = "PLAY";
      if (blinkingTimeOut != null)
         window.clearTimeout(blinkingTimeOut);
      blinkingTimeOut = null
      CurrSongObj.className = "mtSongActive";
   }
   else if(CurrSongState == "PLAY")
   {
      mp3Player("FlashMP3Player").pauseMP3();
      CurrSongState = "PAUSE";
      blinkingTimeOut = window.setTimeout("pausBlinking();", 100);
   }
}

function pausBlinking()
{
   CurrSongObj.className = CurrSongObj.className == "" ? "mtSongActive" : "";
   blinkingTimeOut = window.setTimeout("pausBlinking();", 333);
}

function closeDLBtn()
{
   CurrSongDLURL = "";
   dlBtn.style.display = "none";
   document.body.onclick = null;
   document.body.oncontextmenu = null;
}

function dlCurrentSong()
{
   if (CurrSongDLURL)
      window.location.href = CurrSongDLURL;
   closeDLBtn();
}

function startSong()
{
   playSong(CurrSongDLURL, cmSongPos);
}

function pauseSong()
{
   playSong(CurrSongDLURL, cmSongPos);
}

var lastTarget = null;
var CurrSongDLURL =  null;
var cmSongPos = -1;
var dlBtn = null;
function downloadSong(e, dlURL, songPos)
{
   document.getElementById("cmPause").innerHTML = CurrSongState == "PLAY" ? "Pause" : "Wiedergabe";
   
   if (!dlBtn)
      dlBtn = document.getElementById("dlBtn");
   if(window.event)
   {
      e = window.event;
      e.returnValue = false;
   }
   if (e.preventDefault)
      e.preventDefault();
   e.cancelBubble = true;
   
   if(e && e.target) //Workaround für Firefox, der feuert sonst zwei mal
   {
      if (e.target == lastTarget)
         return false;
      lastTarget = e.target;
      window.setTimeout("lastTarget = null;", 500);
   }

   var posX = 0;
   var posY = 0;
   dlBtn.style.display = "block";
   if (e.target)
   {
      posX = e.pageX;// - dlBtn.offsetWidth;
      posY = e.pageY;// - dlBtn.offsetHeight;
   }
   else
   {
      var scrollPosX = 0;
      var scrollPosY = 0;
      if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') 
      {
         scrollPosY = document.documentElement.scrollTop;
         scrollPosX = document.documentElement.scrollLeft;
      }
      else if (typeof document.body != 'undefined') 
      {
         scrollPosY = document.body.scrollTop;
         scrollPosX = document.body.scrollLeft;
      }
      posX = e.clientX + scrollPosX;// - dlBtn.offsetWidth;
      posY = e.clientY + scrollPosY;// - dlBtn.offsetHeight;
   }
   dlBtn.style.left = posX + "px";
   dlBtn.style.top = posY + "px";
   document.body.onclick = closeDLBtn;
   document.body.oncontextmenu = closeDLBtn;
   CurrSongDLURL = dlURL;
   cmSongPos = songPos;
   document.getElementById("cmPlay").style.display = songPos == CurrSongPos ? "none" : "block";
   document.getElementById("cmStop").style.display = songPos == CurrSongPos ? "block" : "none";
   document.getElementById("cmPause").style.display = songPos == CurrSongPos ? "block" : "none";
   return false;
}


