var currentItem = -1; 
var previousItem = -1; 

var player = null;
function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	addListeners();
}


function addListeners() {
	if (player) { 
		player.addControllerListener("ITEM", "itemListener");
	} else {
		setTimeout("addListeners()",100);
	}
}


function itemListener(obj) { 
	if (obj.index != currentItem) {
 		previousItem = currentItem;
		currentItem = obj.index;

		var tmp = document.getElementById("itm");
		if (tmp) { 
			tmp.innerHTML = "current item: " + currentItem +
				"<br>previous item: " + previousItem;
		}
		getPlaylistData(currentItem);
	}
}


function getPlaylistData(theIndex) {
	var plst = null;
	plst = player.getPlaylist();

	if (plst) {
	
	var songid = plst[theIndex].title;

	$.get('fileinfo.php?id='+songid,function(data){
		$('#popup_content').html(data);
		$('#webmp3player_info').fadeIn(500);
		var tt=setTimeout("$('#webmp3player_info').fadeOut(1500)", 5000);
	$('.queryitem').animate( { borderLeftWidth:"1px" }, 200);
	$('#'+songid).animate( { borderLeftWidth:"25px" }, 200);
	});
	var songselement = document.getElementById(songid);
	ScrollToElement(songselement);
	
	}
}

function ScrollToElement(theElement){

  var selectedPosY = -150;
              
  while(theElement != null){
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
  $('html').animate({scrollTop: selectedPosY}, 1000);

}
