/**
 * @author John Schneider
 * @copyright 2008 Reservoir Creative Group, LLC
 */

//this variable is for loading the stream from the radio station.
var radioUrl = 'http://radio.affordablesound.com:8000/affordable'; //the Url on the internets.

//whether or not the radio is muted.
var isMuted = true;
//keeps track if whether or not the mouse is down.
var isMouseDown = false;
var pollInterval = 10000; //ten seconds.
//if this is turned on, it breaks on pages with margin or padding.
soundManager.useHighPerformance = false;
//soundManager.url = '/AffordableSound/'; // directory where SM2 .SWFs live
//soundManager.url = '/clients/affordab/'; // directory where SM2 .SWFs live
soundManager.url = '/'; // directory where SM2 .SWFs live
soundManager.debugMode = false;
soundManager.onload = function() {
	var mySound = soundManager.createSound({
	    id: 'radio',
	    url: radioUrl,
		volume: 20,
		onplay:function() {
			TestPosition();
		}
  	});
    mySound.play();
	mySound.mute();
	setInterval('PollNowPlaying()', pollInterval);
};

soundManager.onerror = function() {
	document.getElementById('miniplayercontent').innerHTML = "AffordableSound.fm is unavailable.";
}

/**
 * Plays the radio station if stopped, and stops it otherwise.
 */
function PlayRadio() {
	if (isMuted) {
		
		// STOP PLAYING SONGS IF THEY ARE PLAYING (requires songpreviewplayer.js)
		if(timerRunning)
		{
			clearTimeout(myTimer);
			timerRunning = false;
		}
		
		currentPlayingIDTR = '';
		
		for (var i=0; i<soundManager.soundIDs.length; i++)
		{
			var thisSongID = soundManager.soundIDs[i];
			var thisButtonID = thisSongID.replace("song", "playbutton");
			
			if(thisSongID != "radio")
			{
				soundManager.getSoundById(thisSongID).stop();
				document.getElementById( thisButtonID ).innerHTML = "Listen";
			}
		}
		// END songpreviewplayer.js PREREQUISITE
		
		document.getElementById('playbutton').innerHTML = "<img src=\"Images/Site/pausebutton.gif\" height=\"20px\" width=\"20px\" alt=\"Play\" />";
		soundManager.getSoundById('radio').unmute();
		isMuted = false;
		/*
		var jx = new Ajax();
		CallbackFunction = Dummy;
		jx.Get('Code/login.php?q=playradio');
		*/
		ResAjax.GetText('Code/login.php?q=playradio');
	}
	else {
		document.getElementById('playbutton').innerHTML = "<img src=\"Images/Site/playbutton.gif\" height=\"20px\" width=\"20px\" alt=\"Muted\" />";
		soundManager.getSoundById('radio').mute();
		isMuted = true;
		/*
		var jx = new Ajax();
		CallbackFunction = Dummy;
		jx.Get('Code/login.php?q=stopradio');
		*/
		ResAjax.GetText('Code/login.php?q=stopradio');
	}
}

/**
 * Mutes the radio station
 */
function MuteRadio() {
		document.getElementById('playbutton').innerHTML = "<img src=\"Images/Site/playbutton.gif\" height=\"20px\" width=\"20px\" alt=\"Muted\" />";
		soundManager.getSoundById('radio').mute();
		isMuted = true;
		//ResAjax.GetText('Code/login.php?q=stopradio');
}

/**
 * Polls the position of the stream every 5 milliseconds and checks to see if it started.
 */
function TestPosition() {
	var pos = soundManager.getSoundById('radio').position;
	if (pos != null) {
		SetContentHtml();
		return;
	}
	else {
		setTimeout('TestPosition()', 5);
	}
}

/**
 * Shows the player in the content div.
 */
function SetContentHtml() {
	/*
	var jx = new Ajax();
	//set up the now playing.
	CallbackFunction = function(text) {
	 	//set up the player and volume slider.
		var html = "<table cellspacing=\"0px\" id=\"miniplayertable\"><tr valign=\"center\">";
		// play/pause button
		html += "<td><a id=\"playbutton\" onclick=\"javascript:PlayRadio();\" style=\"border-width:0px;\"><img src=\"Images/Site/playbutton.gif\" alt=\"Muted\" height=\"20px\" width=\"20px\" /></a></td>";
		//volume control
		html += "<td><div id=\"minivolumecontrol\" onclick=\"SetVolume(event);\" onmousedown=\"AdjustVolumeWithDrag(event);\" onmousemove=\"AdjustVolumeWithMouseMove(event);\" onmouseup=\"ResetMouseDown();\" style=\"background-color:#000000;width:100px;height:10px;text-align:left;\"><div id=\"minivolbackcolor\" onmouseout=\"HandleMiniPlayerMouseOut(event);\" style=\"background-color:#50D01E;width:20%;height:10px;\"></div></div></td>";
		var songInfo = text.split(";");
		var bandAndSong = songInfo[0].replace("content=", "").split(" - ");
		var band = bandAndSong[0];
		var song = bandAndSong[1];
		var album = songInfo[1].replace("album = ", "");
		html += "<td id=\"miniplayermarquee\" style=\"font-size:12px;\">" + song + " - " + band + "</td>";
		html += "</tr></table>";
		document.getElementById('miniplayercontent').innerHTML = html;
		
		var jx = new Ajax();
		CallbackFunction = function(text) {
			if (text == "play") {
				soundManager.getSoundById('radio').unmute();
				isMuted = false;
				document.getElementById('playbutton').innerHTML = "<img src=\"Images/Site/pausebutton.gif\" height=\"20px\" width=\"20px\" alt=\"Play\" />";
			}
			
			//checks the volume.
			var jx = new Ajax();
			CallbackFunction = function(text) {
				document.getElementById('minivolbackcolor').style.width = text + "%";
				soundManager.setVolume('radio', text);
			}
			jx.Get('Code/login.php?q=getvolume');
		}
		jx.Get('Code/login.php?q=checkradio');
	}
	jx.Get('nowplaying.php');
	*/
	var text = ResAjax.GetText('nowplaying.php');
	//set up the player and volume slider.
	var html = "<table cellspacing=\"0px\" id=\"miniplayertable\"><tr valign=\"center\">";
	// play/pause button
	html += "<td><a id=\"playbutton\" onclick=\"javascript:PlayRadio();\" style=\"border-width:0px;\"><img src=\"Images/Site/playbutton.gif\" alt=\"Muted\" height=\"20px\" width=\"20px\" /></a></td>";
	//volume control
	html += "<td style=\"font-size: 10px;font-style:italic; text-align: center; color: #008900;\"><div id=\"minivolumecontrol\" onclick=\"SetVolume(event);\" onmousedown=\"AdjustVolumeWithDrag(event);\" onmousemove=\"AdjustVolumeWithMouseMove(event);\" onmouseup=\"ResetMouseDown();\" style=\"background-color:#000000;width:100px;height:10px;text-align:left;margin-top: 14px;\"><div id=\"minivolbackcolor\" onmouseout=\"HandleMiniPlayerMouseOut(event);\" style=\"background-color:#50D01E;width:20%;height:10px;\"></div></div>volume</td>";
	var songInfo = text.split(";");
	var bandAndSong = songInfo[0].replace("content=", "").split(" - ");
	var band = bandAndSong[0];
	var song = bandAndSong[1];
	var album = '';
	try {
		album = songInfo[1].replace("album = ", "");
	}
	catch (e) {
		album = '';
	}
	html += "<td id=\"miniplayermarquee\" style=\"font-size:12px;\">" + song + " - " + band + "</td>";
	html += "</tr></table>";
	try {
		document.getElementById('miniplayercontent').innerHTML = html;
	}
	catch (e) {
	}
	
	text = ResAjax.GetText('Code/login.php?q=checkradio');
	if (text == "play") {
		soundManager.getSoundById('radio').unmute();
		isMuted = false;
		document.getElementById('playbutton').innerHTML = "<img src=\"Images/Site/pausebutton.gif\" height=\"20px\" width=\"20px\" alt=\"Play\" />";
	}
	text = ResAjax.GetText('Code/login.php?q=getvolume');
	try {
		document.getElementById('minivolbackcolor').style.width = text + "%";
	}
	catch (e) {
	}
	soundManager.setVolume('radio', text);
	/*
	var jx = new Ajax();
	CallbackFunction = function(text) {
		
		//checks the volume.
		var jx = new Ajax();
		CallbackFunction = function(text) {
			document.getElementById('minivolbackcolor').style.width = text + "%";
			soundManager.setVolume('radio', text);
		}
		jx.Get('Code/login.php?q=getvolume');
	}
	//jx.Get('Code/login.php?q=checkradio');
	*/
}

/**
 * Adjusts the volume using on drag.
 * @param {Object} event
 */
function AdjustVolumeWithDrag(event) {
	isMouseDown = true;
	SetVolume(event);
}

/**
 * Moves the volume control if the mouse is down.
 * @param {Object} event
 */
function AdjustVolumeWithMouseMove(event) {
	if (isMouseDown) {
		SetVolume(event);
	}
}

/**
 * Handles the mouseOut event for the mini player.
 * @param {Object} event
 */
function HandleMiniPlayerMouseOut(event) {
	var pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("minivolumecontrol").offsetLeft;
	var pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("minivolumecontrol").offsetTop;
	var volWidth = document.getElementById('minivolumecontrol').offsetWidth;
	if (BrowserIsFirefox()) {
		pos_x = pos_x - document.getElementById('playbutton').offsetWidth - document.getElementById('playbutton').offsetLeft - document.getElementById('minivolumecontrol').offsetLeft - document.getElementById('miniplayercontent').offsetLeft;
		pos_y = pos_y - document.getElementById('playbutton').offsetHeight - document.getElementById('playbutton').offsetTop - document.getElementById('minivolumecontrol').offsetTop - document.getElementById('miniplayercontent').offsetTop;
	}
	if (pos_x < 0 && isMouseDown == true) {
		document.getElementById('minivolbackcolor').style.width = "0%";
		soundManager.setVolume('radio', 0);
	}
	if (pos_y < 0 && isMouseDown == true) {
		document.getElementById('minivolbackcolor').style.width = pos_x*100 / volWidth;
	}
}

/**
 * Sets the volume.  
 * @param {Object} event The event that fires this function call.
 */
function SetVolume(event) {
	var pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("minivolumecontrol").offsetLeft;
	if (BrowserIsFirefox()) {
		pos_x = pos_x - document.getElementById('playbutton').offsetWidth - document.getElementById('playbutton').offsetLeft - document.getElementById('miniplayertable').offsetLeft;
	}
	var volWidth = document.getElementById('minivolumecontrol').offsetWidth;
	var newVol = Math.round(pos_x*100/volWidth);
	soundManager.setVolume('radio', newVol);
	try {
		document.getElementById('minivolbackcolor').style.width = newVol + "%";
	}
	catch (e) {
		document.getElementById('minivolbackcolor').style.width = "0%";
	}
	//post back to the login page to set the volume.
	/*
	var jx = new Ajax();
	CallbackFunction = Dummy;
	jx.Get('Code/login.php?q=setvolume&v=' + newVol);
	*/
	ResAjax.GetText('Code/login.php?q=setvolume&v=' + newVol);
}

/**
 * Resets if the mouse is down.
 */
function ResetMouseDown(){
	isMouseDown = false;
}

/**
 * Returns true if the browser is firefox and false otherwise.
 */
function BrowserIsFirefox() {
	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
		return true;
	}
	else {
		return false;
	}
}

/**
 * Polls the now playing list for new songs.
 */
function PollNowPlaying() {
	/*
	var jx = new Ajax();
	CallbackFunction = function(text) {
		var songInfo = text.split(";");
		var bandAndSong = songInfo[0].replace("content=", "").split(" - ");
		var band = bandAndSong[0];
		var song = bandAndSong[1];
		var album = songInfo[1].replace("album = ", "");
		document.getElementById('miniplayermarquee').innerHTML = song + " - " + band;
	}
	jx.Get('nowplaying.php');
	*/
	var text = ResAjax.GetText('nowplaying.php');
	var songInfo = text.split(";");
	var bandAndSong = songInfo[0].replace("content=", "").split(" - ");
	var band = bandAndSong[0];
	var song = bandAndSong[1];
	var album = '';
	try {
		album = songInfo[1].replace("album = ", "");
	}
	catch (e) {
		album = '';
	}
	try {
		document.getElementById('miniplayermarquee').innerHTML = song + " - " + band;
	}
	catch (e) {
		
	}
}

/**
 * Sets the session play status.
 */
function SetSessionPlayStatus(playStatus) {
	/*
	var jx = new Ajax();
	CallbackFunction = Dummy;
	if (playStatus) {
		jx.Get('login.php?q=playradio');
	}
	else {
		jx.Get('login.php?q=stopradio');
	}
	*/
	if (playStatus) {
		ResAjax.GetText('login.php?q=playradio');
	}
	else {
		ResAjax.GetText('login.php?q=stopradio');
	}
}

function MakeThumb(src) {
	/*
	var jx = new Ajax();
	CallbackFunction = Dummy;
	jx.Get('thumbnailer.php?url=covers/' + src + '&method=maxpixels&h=100&w=100');
	*/
	ResAjax.GetText('thumbnailer.php?url=covers/' + src + '&method=maxpixels&h=100&w=100');
}

