function swapImage (imgId, imgName) 
{
  window.document.getElementById(imgId).src = imgName;
}

var SUFFIX_MOUSEOVER = "-mo";
var SUFFIX_MOUSEDOWN = "-ac";
var CLASSNAME_MOUSEOVER = "imgover";
var CLASSNAME_MOUSEDOWN = "imgoverclick";
var CLASSNAME_IMGGRP = "imggrp";
var CLASSNAME_HOVER = "hover";
var CLASSNAME_TD_MO = "tab_act_mo";
var imggroups = new Array();
var rollover_spin = 0;

function rollover_spinlock() { /* while(rollover_spin != 0); rollover_spin = 1; */ }
function rollover_spinunlock() { rollover_spin = 0; }

/**
 * Initializes all images with a rollover or onmousedown image change.
 * Note: An image can only have either (a rollover image) OR (a rollover image
 *  AND a mousedown image), i.e. it cannot have a mousedown img w/o a rollover.
 */
function rollover_init() {
	if (!document.getElementById) { return; }
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className.indexOf(CLASSNAME_MOUSEOVER) != -1 || aImages[i].className.indexOf(CLASSNAME_MOUSEDOWN) != -1) {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, SUFFIX_MOUSEOVER+ftype);
			//var csrc = src.replace(ftype, SUFFIX_MOUSEDOWN+ftype);
			aImages[i].setAttribute('rsrc', src);
			aImages[i].setAttribute('hsrc', hsrc);
			//aImages[i].setAttribute('csrc', csrc);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			//aPreLoad[i].src = csrc;
			if (aImages[i].className.indexOf(CLASSNAME_IMGGRP) != -1) {
				/* Image is in an image group */
				/* Find out the image group */
				var idx = aImages[i].className.indexOf(CLASSNAME_IMGGRP) + CLASSNAME_IMGGRP.length;
				var num = parseInt(aImages[i].className.substr(idx, 2));
				if (!imggroups[num]) {
					imggroups[num] = new Array();
				}
				imggroups[num][imggroups[num].length] = aImages[i];
				aImages[i].imggrp = num;
				
			}
			aImages[i].highlight = function() { 
				this.setAttribute('src', this.getAttribute('hsrc')); 
			};
			aImages[i].onmouseover = function() {
				if (this.imggrp) {
					rollover_spinlock();
					var j = 0;
					for (j = 0; j < imggroups[this.imggrp].length; j++) {
						imggroups[this.imggrp][j].highlight();
					}
					rollover_spinunlock();
				}
				this.highlight();				
			}
			aImages[i].downlight = function() {
				this.setAttribute('src', this.getAttribute('rsrc'));
			};
			aImages[i].onmouseout = function() {
				if (this.imggrp) {
					rollover_spinlock();
					var j = 0;
					for (j = 0; j < imggroups[this.imggrp].length; j++) {
						imggroups[this.imggrp][j].downlight();
					}
					rollover_spinunlock();
				}
				this.downlight();	
			}
			if (aImages[i].className.indexOf(CLASSNAME_MOUSEDOWN) != -1) {
				aImages[i].onmousedown = function() {
					this.setAttribute('src', this.getAttribute('csrc'));
				}
				aImages[i].onmouseup = function() {
					this.setAttribute('src', this.getAttribute('hsrc'));
				}
			}
		} else if (aImages[i].className.indexOf(CLASSNAME_HOVER) != -1) {
			/* This is necessary because IE does not support :hover for images :-/ */
			aImages[i].onmouseover = function() {
				this.style.borderColor = '#ff0000';
			}
			aImages[i].onmouseout = function() {
				this.style.borderColor = '#999999';
			}
		}
	}

	/* So far for the images. Now to the table rows (needed for tabs) */
	var aTr = document.getElementsByTagName('tr');
	for (i = 0; i < aTr.length; i++) {
		if (aTr[i].className.indexOf(CLASSNAME_MOUSEOVER) != -1) {
			/* The table row is part of a mouseover effect */
			var idx = aTr[i].className.indexOf(CLASSNAME_IMGGRP) + CLASSNAME_IMGGRP.length;
			var num = parseInt(aTr[i].className.substr(idx, 2));
			if (!imggroups[num]) {
				imggroups[num] = new Array();
			}
			var aTds = aTr[i].childNodes;
			var j;
			for (j = 0; j < aTds.length; j++) {
				/* Next line is powered by: Mozilla Firefox */
				if (aTds[j].nodeName.toLowerCase() != "td") continue;
				/* See below.. aTds[j].setAttribute('oldClassName', aTds[j].className); */
				aTds[j].highlight = function() { 
					this.style.backgroundColor = "#ff0000";
					this.style.borderColor = "#ff0000";
				}
				aTds[j].downlight = function() {
/* Craziest bug ever seen: If you change the className attribute of a table
cell in one row, IE __SOMETIMES__ lets images in the __PREVIOUS__ line
disappear... That's why this is commented out
					this.setAttribute('className', this.getAttribute('oldClassName'));
*/

					this.style.backgroundColor = "#ffffff";
					this.style.borderColor = "#cccccc";

				}
				imggroups[num][imggroups[num].length] = aTds[j];
			}
				
		}
	}
}

function playsample(trackid, source, sampleUrl) {
	playsample(trackid, source, '', sampleUrl);
}

var oldID = " ";

function turnOnSong(trackid, source, sampleUrl, newImage)
{
  var url = baseUrl + "track.do?method=prelisten&dontAddToHistory=true&trackId=" + trackid;
  if (source != '') url = url + "&source=" + source;
  sampleUrl = sampleUrl + '';
  if (prelistenPlayer=="wmp") {
    document.getElementById('mediaplayer').url = url;
    var t=setTimeout(function(){swapImages(newImage)},32000);
    document.getElementById('mediaplayer').controls.play();
  } else {
	  var obj;
	  if (sampleUrl.endsWith('3gp'))
	  {
	      obj = {file:sampleUrl, type:'video'};
	  } else 
	  {
		  obj = {file:sampleUrl, type:'sound'};
	  }
	  player.sendEvent('LOAD', obj); 
	  var t=setTimeout(function(){swapImages(newImage)},32000);
	  player.sendEvent('PLAY');
	  
  }
  
}

function swapImages(image) {
	var newsrc = image.src;
	newsrc = newsrc.substring(0, newsrc.lastIndexOf('/list'));
    image.setAttribute('src', newsrc+'/list-listen.png');
}

function playsample(trackid, source, i, sampleUrl) 
{
  if (i == "")
  {
    if (oldID != " ")
    {
      // we got called without an ID, so stop the last played track and switch the item back
      document.getElementById(oldID).src = "list-listen.png";
  	  rollover_init();      
      oldID = " ";
    }
  }
  else
  {
  	if (oldID != " ")
  	{
  	  // a song's already playing -- turn off that song's icon
    	var oldImage = document.getElementById(oldID);
    	var oldsrc = document.getElementById(oldID).src;
    	oldsrc = oldsrc.substring(0, oldsrc.lastIndexOf('/list'));
    	oldImage.setAttribute('src', oldsrc+'/list-listen.png');
  	    rollover_init();
  	    
    	if (i != oldID)
    	{
	      	// since the old and new song are different, turn on the new song's icon
	      	var newImage = document.getElementById(i);
	      	var newsrc = document.getElementById(i).src;
	        newsrc = newsrc.substring(0, newsrc.lastIndexOf('/list'));
	        newImage.setAttribute('src', newsrc+'/listen-playing.gif');
	        newImage.onmouseover = null;
	        newImage.onmouseout = null;
	        oldID = i;
	        // and play the new song
	        if (prelistenPlayer=="flash") {
	        	player.sendEvent('STOP'); 
	        }
	        turnOnSong(trackid, source, sampleUrl, newImage);
	        
       	}
    	else
    	{
    	  // someone clicked on the icon of the currently playing song
	      	if (prelistenPlayer == "wmp") {
	    	  document.getElementById('mediaplayer').controls.stop();    	  
	      	}
	      	else {
	      	   player.sendEvent('STOP'); 
	      	}
    		oldID = " ";
    	}
  	}
   	else
   	{
   	  // no song's playing yet
     	var newImage = document.getElementById(i);
     	var newsrc = document.getElementById(i).src;
      newsrc = newsrc.substring(0, newsrc.lastIndexOf('/list'));
      newImage.setAttribute('src', newsrc+'/listen-playing.gif');
      newImage.onmouseover = null;
      newImage.onmouseout = null;

      oldID = i;
      // and play the new song
      turnOnSong(trackid, source, sampleUrl, newImage);
  	}
  }
}

String.prototype.endsWith = function(str)
{
	return (this.match(str+"$")==str);
}

// function to open separate flash-player-window
function openFlashplayerWindow(listType, listId, continueFlashPlayer) {
   var windowName = "FlashPlayerWindow";
   var windowOptions = "width=530,height=307,scrollbars=no,location=no";
   if (getIEVersion() == '7') {
     windowOptions = "width=530,height=335,scrollbars=no,location=no";
   }
   if (continueFlashPlayer == undefined) continueFlashPlayer = "no";
   playerUrl = "idj/player.do?method=show&continue=" + continueFlashPlayer + "&listType=" + listType + "&id=" + listId;
   
   var flashWindow = window.open(playerUrl, windowName, windowOptions);
   flashWindow.focus();

   // cgin, ADDED 28.03.2007 due to TD# 10543
   setSnsOpeners(flashWindow);
   setTimeout( function () { setSnsOpeners(flashWindow); }, 1000);
   setTimeout( function () { setSnsOpeners(flashWindow); }, 5000);
}

// function to open separate flash-player-window
function openRealFlashplayerWindow(listType, listId, continueFlashPlayer) {
   var windowName = "FlashPlayerWindow";
   var windowOptions = "width=530,height=307,scrollbars=no,location=no";
   if (getIEVersion() == '7') {
     windowOptions = "width=530,height=335,scrollbars=no,location=no";
    }
   if (continueFlashPlayer == "no")
   {
     playerUrl = "player.do?method=showReal&listType=" + listType + "&id=" + listId;
   }
   else
   {
     playerUrl = "player.do?method=showReal&continue=yes&listType=" + listType + "&id=" + listId;
   }

   if (getIEVersion() == '7') {
     this.window.resizeTo(500,335);
   } else {
     this.window.resizeTo(500,307);
   }
   
   // pwin, 21.06.2007 changed window.open to location.href   
   //setSnsOpeners(this);
   //setTimeout( function () { setSnsOpeners(this); }, 1000);
   //setTimeout( function () { setSnsOpeners(this); }, 5000);
   
   this.location.href = playerUrl;
   // var flashWindow = window.open(playerUrl, windowName, windowOptions);   
   
}

// cgin, ADDED 28.03.2007 due to TD# 10543
// assign needed opener-objects to newly opened/reopened window
// window.opener was to unrelyable 
function setSnsOpeners(popup) {
  try {
   popup.snsOpener = window.self;
   popup.snsOpenerParent = window.self.parent;
  } catch(e) {}
}

// function to open separate flash-player-window (for DEBUG license etc)
function openFlashplayerWindow_debug(listType, listId) {
  openFlashplayerWindow_debug(listType, listId, "no");
}

function openFlashplayerWindow_debug(listType, listId, continueFlashPlayer) {
   var windowName = "FlashPlayerWindow";
   var windowOptions = "width=500,height=500,scrollbars=no,location=no";
   if (continueFlashPlayer == undefined) continueFlashPlayer = "no";
   playerUrl = "idj/player.do?method=show&continue=" + continueFlashPlayer + "&listType=" + listType + "&id=" + listId;
   
   var flashWindow = window.open(playerUrl, windowName, windowOptions);

   // cgin, ADDED 28.03.2007 due to TD# 10543
   setSnsOpeners(flashWindow);
   setTimeout( function () { setSnsOpeners(flashWindow); }, 1000);
   setTimeout( function () { setSnsOpeners(flashWindow); }, 5000);
}

function getIEVersion()
{
	var agt = navigator.userAgent.toLowerCase();
	var ie  = (agt.indexOf("msie") != -1);
	if (ie)
	{
    temp=navigator.appVersion.split("MSIE");
    version=parseFloat(temp[1]);
    return version;
  }
	else
	{
		return 0;
	}
}