// JavaScript Document

var musicPlayerPlaying = true;
var musicPlayerPopup;
var a_variable = 'Hello world';
var musicPlayerPopUnder;

function openPlayer(URL,myW,myH) {
	
	musicPlayerPopUnder = window.open(URL,"mp3window","height="  + myH + ",width=" + myW + ",status=yes,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");
	if (!musicPlayerPopUnder.opener) musicPlayerPopUnder.opener = this.window;
	musicPlayerPopUnder.blur();
	window.focus();
}

// some variables to save
var currentPosition;
var currentVolume;
var currentItem;
var playstatus;
var windowstatus = false;

// these functions are caught by the JavascriptView object of the player.
function sendEvent(typ,prm) { thisMovie("flashMp3").sendEvent(typ,prm); };
function getUpdate(typ,pr1,pr2,pid) {
	if (typ == "time") {
		currentPosition = pr1;
	} else if (typ == "volume") {
		currentVolume = pr1;
	} else if(typ == "item") {
		currentItem = pr1;
		setTimeout("getItemData(currentItem)",100);
	}
//	alert(typ);
	//alert(document.getElementById(typ));

	// doesnt seem to work if after the id==object check... (set to something else by id.innerHTML?)
	if (typ == "state") playstatus = pr1;
	
	var id = document.getElementById(typ);
	//alert(id);
	if (id==object) id.innerHTML = typ+ ": "+Math.round(pr1);
	
	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	//pr2 == undefined ? null: playstatus = Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(received from the player with id <i>"+pid+"</i>)";
	}
	
};

// These functions are caught by the feeder object of the player.
function loadFile(obj) { thisMovie("flashMp3").loadFile(obj); };
function addItem(obj,idx) { thisMovie("flashMp3").addItem(obj,idx); }
function removeItem(idx) { thisMovie("flashMp3").removeItem(idx); }

/*
function getItemData(idx) {
	var obj = thisMovie("flashMp3").itemData(idx);
	var nodes = "";
	for(var i in obj) { 
		nodes += "<li>"+i+": "+obj[i]+"</li>"; 
	}
	if (document.getElementById("data")) {
		document.getElementById("data").innerHTML = nodes;
	}
};
*/

function getItemData(idx) {
	var obj = thisMovie("flashMp3").itemData(idx);
	var info = '<table><tr><td><img src="/imgs/mp3/currentlyplaying.png" alt="Currently playing:" width="121" height="18" /></td>';
	var musicinfo = '';
	
	for(var i in obj) { 
		if (i == "author" || i == 'title') musicinfo = '<td>'+(i=="author"?'<img src="/imgs/mp3/author.png" width="52" height="18" />':'')+"</td><td><span>"+obj[i]+"</span>&nbsp;" + musicinfo + '</td>'; 
		
	}
	info = info + musicinfo + '</tr></table>';
	if (window.opener.document.getElementById("currentlyplaying")) {
		window.opener.document.getElementById("currentlyplaying").style.backgroundColor = '#000000';
		window.opener.document.getElementById("currentlyplaying").innerHTML = info;
	}
	
};


// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};

function sendmp3command ( action, winWidth, winHeight, websiteColorid ) {
	try {
		if (!musicPlayerPopUnder) {
			openPlayer('player.php?wid='+websiteColorid, winWidth, winHeight );
		} else {
			if (musicPlayerPopUnder.document) {
				musicPlayerPopUnder.document.mp3controlform.mp3control.value = action;
			} else {
				openPlayer('player.php?wid='+websiteColorid, winWidth, winHeight );
			}
		}
	} catch (err) {
		try {
			location.reload(true);
		} catch (err1) {
			txt="There was an error on this page.\n\n"
			txt+="Error description: " + err.description + "\n\n"
			txt+="Click OK to continue.\n\n"
			alert(txt);
		}
	}
}

// this function checks what action needs to be taken
function checkControl () {

	//musicPlayerPopUnder = this.window;

	// if the player is not playing, then update the status in the main window
	if (playstatus == 0) {
		//alert(playstatus);
		window.opener.document.getElementById("currentlyplaying").innerHTML = '';
		window.opener.document.getElementById("playpause").src= '/imgs/mp3/play.gif';
	} else {
//	alert (playstatus);
		// otherwise, keep updating the STATUS
		setTimeout("getItemData(currentItem)",100);
		window.opener.document.getElementById("playpause").src= '/imgs/mp3/pause.gif';
	}

	if (document.mp3controlform.mp3control.value != "") {
		sendEvent(document.mp3controlform.mp3control.value);
		document.mp3controlform.mp3control.value = "";
	}
	//alert('5 seconds!');
	var t=setTimeout("checkControl()",500);
}


function checkMp3window () {
	
	// check to see if the window
	var status = document.getElementById("currentlyplaying").innerHTML;
	
	if (status == '&nbsp;') {
		windowstatus = false;
		document.getElementById("currentlyplaying").style.backgroundColor = 'transparent';
		//alert(playstatus);
	} else {
		//checkControl ();
		windowstatus = true;
		document.getElementById("currentlyplaying").style.backgroundColor = '#000000';
	}
	
	//document.getElementById("currentlyplaying").style.backgroundColor = '#cc00cc';
	
	//alert(document.getElementById("currentlyplaying").style.backgroundColor);
	
	//alert(windowstatus);
	
	var c=setTimeout("checkMp3window()",5000);
}



function screenResize () {
	
	var left = 190;
	var top = 10;
	
	var curWidth = $(window).width();
	var curHeight = $(window).height();
	
	if (curWidth > 1200) {
		left = ((curWidth-1000));
	}
	if (curHeight > 595) {
		top = ((curHeight-585));
	}
	if (top > 100) {
		top = 100;	
	}
	var $container = $("#content");
		$container.css("top",top+"px");
		$container.css("left",left+"px");
	
	
}