/*
    (c) 2007 Anne Jan Brouwer
            GNU GPL
*/

// variables
var baseVolume=80;
var fadeTime=20;
var currentPlayer="plL";
var activePlayer="plL";
var fadeState=false;
var fadeOffset=0;
var timeLeft=0;
var currentLoad=0;
var leftState=0;
var rightState=0;

// loaders
function loadFilter( filter ) {
  loadList( 'playlist.php?filter='+filter+'&html=list' );
  return false;
}

function loadStartWith( idx ) {
  loadList( 'playlist.php?html=list&idx='+idx );
}

function loadList( url ){
  var page_request = false;
  if (window.XMLHttpRequest){ // if Mozilla, Safari etc
    page_request = new XMLHttpRequest();
  } else if (window.ActiveXObject){ // if IE
    try {
	page_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
	try{
	    page_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e){}
    }
  }
  page_request.onreadystatechange=function(){
    placeList(page_request);
  }
  page_request.open('GET', url, true);
  page_request.send(null);
//  checkLists();
} 

function placeList(page_request){
  if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
    Sortable.destroy("thelist");
    Sortable.destroy("liL");
    Sortable.destroy("liR");
    document.getElementById("listcont").innerHTML=page_request.responseText;
    Sortable.create("thelist",{containment:["thelist","liL","liR"],constraint:false,onUpdate:checkLists});
    Sortable.create("liL",{dropOnEmpty:true,containment:["thelist","liL","liR"],constraint:false,onUpdate:checkLists});
    Sortable.create("liR",{dropOnEmpty:true,containment:["thelist","liL","liR"],constraint:false,onUpdate:checkLists});
    checkLists();
  }
}

// filling
function checkLists(){
    checkList("L",2);
    checkList("R",2);
}

function checkList(which,min){
    var list = document.getElementById("li"+which);
    var cnt = 0;
    for (i=0;i<list.childNodes.length;i++){
      if ( list.childNodes[i].nodeName == "LI" ){
        cnt++;
      }
    }
    if ( cnt < min ) {
	var thelist = document.getElementById("thelist");
	for (j=0;j<thelist.childNodes.length;j++){
	  if ( thelist.childNodes[j].nodeName == "LI" ){
	    document.getElementById("status").innerHTML="Auto added to player "+which+": "+thelist.childNodes[j].innerHTML;
	    list.appendChild(thelist.childNodes[j]);
	    j=thelist.childNodes.length;
	  }
	}
	checkList(which,min);
    }
}

function addTune(which){
    var list = document.getElementById("li"+which);
    var thelist = document.getElementById("thelist");
    for (j=0;j<thelist.childNodes.length;j++){
      if ( thelist.childNodes[j].nodeName == "LI" ){
        list.appendChild(thelist.childNodes[j]);
        j=thelist.childNodes.length;
      }
    }
}

function loadTunes(){
    loadTune("L");
    loadTune("R");
}

function loadTune(which){
    var list = document.getElementById("li"+which);
    for (i=0;i<list.childNodes.length;i++){
      if ( list.childNodes[i].nodeName == "LI" ){
      thisPlayer("pl"+which).loadFile({file:list.childNodes[i].id,title:list.childNodes[i].innerHTML});
	document.getElementById("status").innerHTML="Loaded in player "+which+": "+list.childNodes[i].innerHTML;
	list.removeChild(list.childNodes[i]);
	checkList(which,2);
	i=list.childNodes.length;
      }
    }
}

function startJukebox(){
    loadTunes();
    loadTunes();
    startPlayer("L");
}

// player stuff
function startPlayer(which){
    window.parent.document.title = thisPlayer("pl"+which).itemData(0)["title"]+" | JinXed Jukebox";
    document.getElementById("status").innerHTML="Now playing: "+thisPlayer("pl"+which).itemData(0)["title"];
    if ( activePlayer == "plL" ){
        document.getElementById("lnkL").title="Skipping disabled during playback.";
	document.getElementById("lnkR").title="Skip tune in the right player.";
    } else {
        document.getElementById("lnkR").title="Skipping disabled during playback.";
	document.getElementById("lnkL").title="Skip tune in the left player.";
    }    
    thisPlayer("pl"+which).sendEvent("playitem",0);
}

function fadetime(v) {
    fadeTime=v;
    document.getElementById("status").innerHTML="Fadetime is now: "+Math.round(v)+" seconds.";    
}

function volume( v ) {
    baseVolume=v;
    if ( !fadeState ){ 
	thisPlayer(activePlayer).sendEvent("volume", v); 
    }
    document.getElementById("status").innerHTML="Volume is now: "+Math.round(v)+"%";    
}

function fadeNow(){
    if ( !fadeState ) {
        fadeOffset=timeLeft;
	fadeState=true;
        document.getElementById("status").innerHTML="Fade forced at "+Math.round(fadeOffset)+" seconds remaining.";    
    } else {
	document.getElementById("status").innerHTML="Already crossfading..";    
    }
}

function doFade ( inTime ) {
    if ( fadeOffset != 0 ){
	theTime=fadeTime+(inTime-fadeOffset);
    } else { 
	theTime=inTime; 
    }

    if ( theTime < 0 ) {
	fadeState=false;
	fadeOffset=0;
	if ( activePlayer == "plL" ){ 
	    loadTune("R");
	} else {
	    loadTune("L");
	}
    } else {
     	
	if ( Math.round(theTime) <= 0 ){
	    document.getElementById("status").innerHTML="Crossfade done.";
        } else {
	    document.getElementById("status").innerHTML="Crossfading for another "+Math.round(theTime)+" seconds.";
	}

	if ( activePlayer == "plL" ){ 
            thisPlayer("plR").sendEvent("volume", 1+(baseVolume*(theTime/fadeTime)));
	    thisPlayer("plL").sendEvent("volume", 1+(baseVolume*(1-(theTime/fadeTime))));
	} else {
    	    thisPlayer("plL").sendEvent("volume", 1+(baseVolume*(theTime/fadeTime)));
	    thisPlayer("plR").sendEvent("volume", 1+(baseVolume*(1-(theTime/fadeTime))));
	}
    }
}

function crossFade ( theTime ) {
    if ( currentPlayer == "plL" ){ 
	    if ( rightState != 2 ) {
	    	activePlayer="plR";
    	        startPlayer("R");
		fadeState=true;
    	    } else if ( currentPlayer != activePlayer ) {
	    	doFade(theTime);
	    }
    } else {
    	    if ( leftState != 2 ) {
	    	activePlayer="plL";
        	startPlayer("L");
		fadeState=true;
	    } else if ( currentPlayer != activePlayer ) { 
		doFade(theTime);
	    }
    }
} 

function display() {
    if ( !fadeState ) {
        document.getElementById("status").innerHTML="Playing: "+thisPlayer(activePlayer).itemData(0)["title"];
    }
    setTimeout("display()",5000);
}
setTimeout("display()",10000);

function getUpdate(typ,pr1,pr2,pid) {
    if(pid != "null") {
	currentPlayer = pid;

    }
    if(typ == "time") { 
	if ( ( currentLoad == 100 || fadeState ) && ( pr2 <= fadeTime || ( fadeOffset != 0 && pr2 < fadeOffset ) ) ) { crossFade(pr2); }
	
	if ( currentPlayer == activePlayer ) {
	    timeLeft=pr2;
	    if ( pr1 == fadeTime ) {
		if ( currentPlayer == "plL" ) {
	    	    thisPlayer("plR").sendEvent("volume", 1);
		    thisPlayer("plL").sendEvent("volume", baseVolume);
		} else {
	    	    thisPlayer("plL").sendEvent("volume", 1);
		    thisPlayer("plR").sendEvent("volume", baseVolume);
		}
	    } 
	}
	
    } else if(typ == "state") {		
	if ( currentPlayer == "plR" ){
	    rightState = pr1;
	} else {
	    leftState = pr1;
	}
	if ((currentPlayer != activePlayer) && ( pr1 == 2 ) && ( !fadeState )) {
	    thisPlayer(currentPlayer).sendEvent("stop");
	}
	if (pr1 == 0 ){
	    if ( currentPlayer != activePlayer ) {
	        if ( currentPlayer == "plR" ) {
		    thisPlayer("plR").sendEvent("volume", 1);
		    thisPlayer("plL").sendEvent("volume", baseVolume);
		    loadTune("R");
		    fadeState=false;				
		    fadeOffset=0;
		} else {
		    thisPlayer("plL").sendEvent("volume", 1);
		    thisPlayer("plR").sendEvent("volume", baseVolume);
		    loadTune("L");
		    fadeState=false;				
		    fadeOffset=0;
		}
	    }
	}
    } else if(typ == "load") { 
	currentLoad = pr1; 
    }
}

function thisPlayer(playerName) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[playerName];
  } else {
    return document[playerName];
  }
}

// players
var s1 = new SWFObject('mediaplayer.swf','plL','320','40','9');
s1.addVariable("file","");
s1.addVariable("enablejs","true");
s1.addVariable("displayheight","0");
s1.addVariable("javascriptid","plL");
s1.addVariable("enablejs","true");
s1.addVariable("volume",baseVolume);
s1.addVariable("showvolume","false");
s1.addVariable('frontcolor','0xCCCCCC');
s1.addVariable('lightcolor','0xFF0000');
s1.addVariable('backcolor','0x000000');
s1.addVariable("callback","statistics.php");
var s2 = new SWFObject('mediaplayer.swf','plR','320','40','9');
s2.addVariable("file","");
s2.addVariable("enablejs","true");
s2.addVariable("displayheight","0");
s2.addVariable("javascriptid","plR");
s2.addVariable("enablejs","true");
s2.addVariable("volume","1");
s2.addVariable("showvolume","false");
s2.addVariable('frontcolor','0xCCCCCC');
s2.addVariable('lightcolor','0xFF0000');
s2.addVariable('backcolor','0x000000');
s2.addVariable("callback","statistics.php");
