var ap_instances = new Array();

function ap_stopAll(playerID) {
	var updatedCount = false;
	for(var i = 0;i<ap_instances.length;i++) {
		try {
			if(ap_instances[i] != playerID)
			{
				document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 1);
				//alert('closing player: '+playerID);
			}
			else
			{
				document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 0);
				if(updatedCount == false)
				{
					playTrack(playerID);
					updatedCount = true;
				}
			}
		} catch( errorObject ) {
			// stop any errors
			//alert(errorObject);
		}
	}
}

function ap_registerPlayers() {
	var objectID;
	var objectTags = document.getElementsByTagName("object");
	for(var i=0;i<objectTags.length;i++) {
		objectID = objectTags[i].id;
		if(objectID.indexOf("audioplayer") == 0) {
			ap_instances[i] = objectID.substring(11, objectID.length);
		}
	}
}

var ap_clearID = setInterval( ap_registerPlayers, 100 );

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function alertContents(httpRequest) {

	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			alert(httpRequest.responseText);
		} else {
			alert('There was a problem with the request.');
		}
	}
}


function updatePlayCount(httpRequest, playerID) {

	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			span = document.getElementById('playCount'+playerID);
			if(span)
			{
				span.innerHTML = addCommas(httpRequest.responseText);
			}
			
			span = document.getElementById('playCount'+playerID+'_popup');
			if(span)
			{
				span.innerHTML = addCommas(httpRequest.responseText);
			}			
		} else {
			//alert('There was a problem with the request.');
		}
	}
}
	
function updateDownloadCounters(httpRequest, trackID)
{
	if (httpRequest && httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			span = document.getElementById('downloadCount_'+trackID);
			if(span)
			{
				span.innerHTML = addCommas(httpRequest.responseText);
			}
			span = document.getElementById('downloadCount2_'+trackID);
			if(span)
			{
				span.innerHTML = addCommas(httpRequest.responseText);
			}
			span = document.getElementById('downloadCount'+trackID+'_popup');
			if(span)
			{
				span.innerHTML = addCommas(httpRequest.responseText);
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}
	

//Called direct by onclick
function updateDownloadCounter(trackID) {
	var httpRequest;
	var url = '/downloadCount/'+trackID;
	alert(url);
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				
			}
		}
	}

	if (!httpRequest) {
		//alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	httpRequest.onreadystatechange = function() {
		if (httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
				span = document.getElementById('downloadCount_'+trackID);
				if(span)
				{
					span.innerHTML = addCommas(httpRequest.responseText)+" hi";
					span.style = 'background-color: red;';
				}
				
			} else {
				alert('There was a problem with the request.');
			}
		}
	};
	//Wait a seond, let the download start first
	//setTimeout('doNothing()', 5000);
	httpRequest.open('GET', url, false);
	httpRequest.send('');
	return true;
}

function doNothing()
{
	
}

function makeRequest(url, playerID) {
	var httpRequest;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				
			}
		}
	}

	if (!httpRequest) {
		//alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	httpRequest.onreadystatechange = function() { updatePlayCount(httpRequest, playerID); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}