

//
// isound-api.js - iSound related Javascript functions
// Version 1.7.6
// Authors Pascal Becker, Michael Pelny, Oliver Lietz
// (c) Audiantis GmbH, 2005, 2006
// http://www.audiantis.com/
//

// ************************
// *** INITIAL SETTINGS ***
// ************************
var g_streamID = false;
var g_hostname = "" ;
var g_isound_url = "";
var g_isound_debug = "";
var g_control_cgi = "";
var g_stream_port = "";
var g_proxy_path = "";
var g_isoundreq = false;
var isoundUseXmlHttpReq = false;
var g_TargetObject = "";

// ************************
// *** HELPER FUNCTIONS ***
// ************************
function isound_xmlhttpreq(url)
{
	/*
	*** DESCRIPTION ***
		Create xmlhttp object and issue request
		
	*** USAGE ***
		e.g. ok = isound_xmlhttpreq(<http-address>);

	*** PARAMETERS ***
		url : Complete http-adress. E.g. complete_command from sendCommand()
			  REQUIRED
			  
	*** VARIABLES ***
		ok  : Status (true / false)
		req : Handle to XMLHTTPRequest Object / ActiveX Object
		
	*** RETURN VALUE ***
		ok : true - success
		     false - failure
	*/
	
	var ok = false;
	var req = false;
	
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest)
	{
    	try
		{
			req = new XMLHttpRequest();
        }
		catch(e)
		{
			if(g_isound_debug)
			{
				alert("no xmlhttp");
				req = false;
			}
        }
    }
	// branch for IE/Windows ActiveX version
	else if(window.ActiveXObject)
	{
       	try
		{
        	req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
        	try
			{
          		req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
          		req = false;
        	}
		}
    }
	
    if(req)
	{
		try
		{
			req.onreadystatechange = isound_processReqChange;
			req.open("GET", url, true);
			req.send("");
			ok = true;
		}
		catch (err)
		{
			if(g_isound_debug)
			{
				alert("xmlhttp error"); //+ err.name + " " + err.message);	
			}
			ok=false;
		}
    }
    g_isoundreq = req;
    return ok;
}

// send ax player command with UseCGI
//FIXME dependent from global object AXPlayer
function isound_sendCommand_ax(cmd)
{
	var r = -1;
	try {
		if(AXPlayer)
		{
			var axcmd = "/" + g_control_cgi + cmd;
			try {
				//alert("AXPlayer CGI: " + axcmd);
				r = AXPlayer.UseCGI(axcmd);
				//alert("Return of CGI: " + r);
				// Wenn timeout,dann 'success', weil sonst doppelt versendet wird
			        if (r==-7)
					r=0;

			} catch(e) {
				r=1;	// probably old AXPlayer without UseCGI
			}
			if(r<0)
			{
				//alert("AXPlayer CGI Error: " + axcmd);
			}
		}
	} catch(e) {
		r=2;	// no AXPlayer 
	}
	return r;
}

function sendCommand(cmd)
{
	//g_isound_debug=1;
	/*
	*** DESCRIPTION ***
		Send iSound command to server.
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:sendCommand('setsound&sid.mp3&bgm.pcm~setsound&sid.mp3&vox.pcm')">sendCommand()</a>
		
	*** PARAMETERS ***
		cmd : Command string. See iSound manual for further information
			  REQUIRED
			  
	*** VARIABLES ***
		target           : Retrieves the target object by calling isound_getTargetObject()
		complete_command : Assembles the complete command 
		ok               : Status for sending commands with xmlhttp-object
		
	*** RETURN VALUE ***
		none
	*/

	//alert(cmd);

	//FIXME: try to send AXPlayer command first
	if(!isound_sendCommand_ax(cmd))
		return;

	
	var target = isound_getTargetObject();
	var complete_command = "";
	var ok = false;
	
	if (g_proxy_path !="")
	{
		complete_command = "http://" + g_hostname + ":" + g_stream_port + "/" + g_proxy_path + "/" + g_control_cgi + cmd;
	}
	else
	{
		complete_command = "http://" + g_hostname + ":" + g_stream_port + "/" + g_control_cgi + cmd;
	}
	
	if(g_isound_debug<3)
	{	
	
	    // now send the command via http location to the server
	    // new method: xmlHttpRequest	   
	    if(isoundUseXmlHttpReq)
	    {
	        if(g_isound_debug)
		{
		    //complete_command = "http://" + g_hostname + ":" + g_stream_port + "/cgi-bin/control-reply.cgi?" + cmd;
		    alert("isound: Sending REQ " + complete_command);
		}

		ok = isound_xmlhttpreq(complete_command);
 	    }
 	    
 	    if(!ok)
	    {
			// old method: location.href
			if(g_isound_debug)
				alert("isound: Sending HREF " + complete_command);
			target.location.href = complete_command;
	    }
	}
	else
	{
		alert("isound Emulation: NOT Sending " + complete_command);
	}
}


// *********************
// *** DEBUG RELATED ***
// *********************
function isound_processReqChange()
{
	/*
	*** DESCRIPTION ***
		Simulates XMLHTTP communication when in debug mode
		
	*** USAGE ***
		e.g. req.onreadystatechange = isound_processReqChange;
		
	*** PARAMETERS ***
		none
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/

    // only if req shows "loaded"
    if (g_isoundreq.readyState == 4)
	{
		if(g_isound_debug)
		{
			alert("request status/answer: " + g_isoundreq.status + " " + g_isoundreq.statusText + " " + g_isoundreq.responseText);
			if (g_isoundreq.status == 200)
			{
				// only if "OK"
			}
			else
			{
			}
		}
    }
}

function isound_setTargetObject(obj)
{
	/*
	*** DESCRIPTION ***
		This function sets the target object for sendCommand
		
	*** USAGE ***
		e.g. var myTarget = isound_setTargetObject(parent.FrameName)
		
	*** PARAMETERS ***
		obj : Window object
			  REQUIRED
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		g_TargetObject
	*/
	
	g_TargetObject = obj;
	
	return g_TargetObject;
}

function isound_getTargetObject()
{
	/*
	*** DESCRIPTION ***
		This function returns the target object for sendCommand.
		Default 'this'
		
	*** USAGE ***
		e.g. var myTarget = isound_getTargetObject()
		
	*** PARAMETERS ***
		none		
		
	*** VARIABLES ***
		target : Target object or default (this)
		
	*** RETURN VALUE ***
		target : Target object or default (this)
	*/
	
	var target = "";
	
	if (!g_TargetObject)
	{
		target = this;
	}
	else
	{
		target = g_TargetObject;
	}
	
	return target;
}

function isound_debugmode(dbg)
{
	/*
	*** DESCRIPTION ***
		Set isound debug mode.
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:isound_debugmode(1);">isound_debugmode(1)</a>
		
	*** PARAMETERS ***
		dbg : Debug-Level
		      0 - Off
			  1 - On. WITHOUT reply messages from the server
			  2 - On. WITH reply messages from the server
			  3 - On. Emulation. No command is send to the server
			  REQUIRED
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/
	
	g_isound_debug = dbg;
	switch (dbg)
	{
		case 0:
			alert("Switching off iSound Debug Mode");
			g_control_cgi = "cgi-bin/control.cgi?";	
			break;
		case 1:
			alert("Switching on iSound Debug Mode without reply mode");
			g_control_cgi = "cgi-bin/control.cgi?";	
			break;
		case 2:
			alert("Switching on iSound Debug Mode and control reply mode");
			g_control_cgi = "cgi-bin/control-reply.cgi?";	
			break;
		case 3:
			alert("Switching on iSound Emulation mode (no server access)");
			break;
	}
}

function isound_debugmsg()
{
	/*
	*** DESCRIPTION ***
		Returns the hostname and streamid if debug mode is on (>1)
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:isound_debugmsg();">isound_debugmsg()</a>
		
	*** PARAMETERS ***
		none
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/
	
	if(g_isound_debug)
	{
		alert("iSound host: " + g_hostname + " id:" + g_streamID);
	}
}


// **********************
// *** STREAM RELATED ***
// **********************
function stream_init(hostname, customer_id, stream_port, control_cgi, streamID, proxy_path)	
{
	/*
	*** DESCRIPTION ***
		Initialize stream for customer and return streamID.
		
	*** USAGE ***
		e.g. <body onLoad="stream_init('<your_isound_server>','','','','sid.mp3','','')">
		
	*** PARAMETERS ***
		hostname	: Hostname of iSound server
					  REQUIRED
		customer_id : Not in use
		stream_port : Streaming port. Default 8081
					  OPTIONAL
		control_cgi : Path to CGI-Interface. Default 'cgi-bin/control.cgi'
					  OPTIONAL
		streamID	: Name of the stream id. Default generated unix time
					  OPTIONAL
		proxy_path  : Path of webservers proxy. Default ''
					  OPTIONAL
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		g_streamID : Global variable with generated or given stream id
	*/
	
	g_hostname     = hostname;
	g_proxy_path   = proxy_path;
	g_isound_debug = 0;
	
	if(stream_port!="")
	{
		g_stream_port = stream_port;
	}
	else
	{
		g_stream_port = 8081;
	}
	
	// set control.cgi path
	if(control_cgi!="")
	{
		g_control_cgi = control_cgi;
	}
	else
	{
		g_control_cgi = "cgi-bin/control.cgi?";	
	}
	if(streamID != "")
	{
		g_streamID = streamID;
	}
	else
	{
		var date = new Date();
		g_streamID = date.getTime() + ".mp3";
	}

	var autoOpen = "autoopen/";
	//var autoOpen = "";
	
	if (proxy_path != "")
	{
		g_isound_url = "http://" + g_hostname + ":" + g_stream_port + "/" + g_proxy_path + "/" + autoOpen + g_streamID;
	}
	else
	{
		g_isound_url = "http://" + g_hostname + ":" + g_stream_port + "/" + autoOpen + g_streamID;
	}
	
	return g_streamID;
}

function stream_getSID()
{
	/*
	*** DESCRIPTION ***
		Return current streamID.
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:alert(stream_getSID());">stream_getSID()</a>
		
	*** PARAMETERS ***
		none
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		g_streamID : Global variable
	*/
	return g_streamID;
}

function stream_getURL()
{
	/*
	*** DESCRIPTION ***
		Return current streaming URL.
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:alert(stream_getURL());">stream_getURL()</a>
		
	*** PARAMETERS ***
		none
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		g_isound_url : Global variable with complete http streaming address
	*/
	
	return g_isound_url;
}

function stream_openStream(streamID, bitrate, sound)	
{
	/*
	*** DESCRIPTION ***
		Calls the helper function 'sendCommand' to open a stream using passed streamid
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:stream_openStream('test_sid.mp3');">stream_openStream()</a>
		
	*** PARAMETERS ***
		streamID : The name of the stream to open
				   REQUIRED
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/

	if(!streamID)
		streamID = stream_getSID();
	
	var ostr = "openstream&" + streamID;
	var bit = "";
	if(bitrate)
	{
		bit = "&mp3&" + bitrate;
	}
	
	if(!sound)
	{
	    sendCommand(ostr + bit);
	}
	else
		sendCommand(ostr + bit + "~setsound" + "&" + streamID + "&" + sound);
}

function stream_closeStream(streamID)	
{
	if(!streamID)
		streamID = stream_getSID();

	/*
	*** DESCRIPTION ***
		Calls the helper function 'sendCommand' to close a stream using passed streamid
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:stream_closeStream('test_sid.mp3');">stream_closeStream()</a>
		
	*** PARAMETERS ***
		streamID : The name of the stream to close
				   REQUIRED
		
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/
	
	sendCommand("closestream&" + streamID);
}


// *********************
// *** SOUND RELATED ***
// *********************
function sound_start(streamID, fname, volume, fade_time, loop)
{
	/*
	*** DESCRIPTION ***
		Calls the helper function 'sendCommand' to start a sound on specified stream
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:sound_start('sid.mp3','vox.pcm',150,'0.05',1)">sound_start()</a>
		
	*** PARAMETERS ***
		streamID  : The name of the stream
					REQUIRED
		fname	  : Name of sound file
					REQUIRED
		volume	  : Volumelevel (0-255). Default 150
					OPTIONAL
		fade_time : Fadein time in seconds. Default 0.01
					OPTIONAL
		loop	  : Number of times this sound will be repeated 0 means infinity. Default 1
					OPTIONAL
				  
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/

	if(!streamID)
		streamID = stream_getSID();
	if(!volume)
		sendCommand("setsound&" + streamID + "&" + fname);
	else
	sendCommand("setsound&" + streamID + "&" + fname + "&" + volume + "&" + fade_time + "&" + loop);
}

function sound_stop(streamID,fname, volume, fade_time)	
{
	/*
	*** DESCRIPTION ***
		Calls the helper function 'sendCommand' to stop a sound on specified stream
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:sound_stop('sid.mp3','vox.pcm','','0.5')">sound_stop()</a>
		
	*** PARAMETERS ***
		streamID  : The name of the stream
					REQUIRED
		fname	  : Name of sound file
					REQUIRED
		volume	  : Not in use
		fade_time : Fadeout time in seconds. Default 0.01
					OPTIONAL
				  
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/
	
	if(!streamID)
		streamID = stream_getSID();
	
	if(!fade_time)
		sendCommand("stopsound&" + streamID + "&" + fname);
	else
	sendCommand("stopsound&" + streamID + "&" + fname + "&" + fade_time);
}

function sound_stopstart(streamID, curName, fname, volume, fade_time, loop)
{
        /*
        *** DESCRIPTION ***
                Calls the helper function 'sendCommand' to stop a sound and start another sound on specified stream
        */

    	if(!streamID)
	    	streamID = stream_getSID();

        var cmd = "stopsound&" + streamID + "&" + curName + "~" + "setsound&" + streamID + "&" + fname + "&" + volume + "&" + fade_time + "&" + loop;
        //alert(cmd);
        sendCommand(cmd);
}

function sound_pause(streamID, fname)	
{
	/*
	*** DESCRIPTION ***
		Calls the helper function 'sendCommand' to pause a sound on stream
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:sound_pause('sid.mp3','vox.pcm')">sound_pause()</a>
		
	*** PARAMETERS ***
		streamID  : The name of the stream
					REQUIRED
		fname	  : Name of sound file
					REQUIRED
				  
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/
	
	if(!streamID)
		streamID = stream_getSID();
	sendCommand("pausesound&" + streamID + "&" + fname);
}

function sound_resume(streamID, fname)	
{
	/*
	*** DESCRIPTION ***
		Calls the helper function 'sendCommand' to resume a sound on stream
		
	*** USAGE ***
		e.g. <a href="#" onclick="javascript:sound_resume('sid.mp3','vox.pcm')">sound_resume()</a>
		
	*** PARAMETERS ***
		streamID  : The name of the stream
					REQUIRED
		fname	  : Name of sound file
					REQUIRED
				  
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/
	
	if(!streamID)
		streamID = stream_getSID();
	sendCommand("resumesound&" + streamID + "&" + fname);
}

function sound_setVol(streamID, fname, volume)
{
	/*
	*** DESCRIPTION ***
		Calls the helper function 'sendCommand' to set the volume level of a playing sound
		
	*** USAGE ***
		e.g. <a href="javascript:sound_setVol('sid.mp3','vox.pcm',160)">sound_setVol()</a>
		
	*** PARAMETERS ***
		streamID  : The name of the stream
					REQUIRED
		fname	  : Name of sound file
					REQUIRED
		volume	  : Volume level (0-255)
				  
	*** VARIABLES ***
		none
		
	*** RETURN VALUE ***
		none
	*/
	
	if(!streamID)
		streamID = stream_getSID();
	sendCommand("setvolsound&" + streamID + "&" + fname + "&" + volume);
}


// ****************************************************** //
// *** Not Implemented in iSound-Server-Version 0.3.0 *** //
// ****************************************************** //

// Start playing specified stream.
function stream_start(streamID){}

// Stop playing specified stream.
function stream_stop(streamID){}

// Pause playing specified stream.
function stream_pause(streamID){}

// Resume playing specified stream.
function stream_resume(streamID){}

// Increase volume by fixed value.
function stream_increaseVol(streamID, delta){}
	
// Decrease volume by fixed value.
function stream_decreaseVol(streamID, delta){}

// Set volume to specified value.
function stream_setVol(streamID, volume){}

// Retrieve volume of specified stream.
function stream_getVol(streamID, ret_volume){}

// Fade in volume to specified value.
function stream_fadeIn(streamID, volume, fade_time){}

// Fade out volume to specified value.	
function stream_fadeOut(streamID, volume, fade_time){}

// Initialize a group for a specified stream and retrieve a groupID.
function group_init(streamID, ret_groupID){}

// Add sound to group on stream.
function group_add(streamID, groupID, fname){}

// Remove sound from group on stream.
function group_remove(streamID, groupID, fname){}

// Start playing specified group.
function group_start(groupID, volume, fade_time){}

// Stop playing specified group.
function group_stop(groupID){}

// Pause playing specified group.
function group_pause(groupID){}

// Resume playing specified group.
function group_resume(groupID){}

// Increase volume of group by fixed value.
function group_increaseVol(groupID, delta){}

// Decrease volume of group by fixed value.
function group_decreaseVol(groupID, delta){}

// Set volume of group to specified volume.
function group_setVol(groupID, volume){}

// Retrieve volume of group.	
function group_getVol(groupID, ret_volume){}

// Fade in volume of group to specified value.
function group_fadeIn(groupID, volume, fade_time){}

// Fade out volume of group to spcified value.
function groug_fadeOut(groupID, volume, fade_time){}

// Set loop flag for specified sound on stream.
function sound_setLoop(streamID, fname, loop){}

// Increase volume of specified sound by a given relative value.
function sound_increaseVol(streamID, fname, delta){}

// Decrease volume of specified sound by a given relative value.	
function sound_decreaseVol(streamID, fname, delta){}

// Retrieve volume of specified sound.
function sound_getVol(streamID, fname, ret_volume){}

// Fade in sound to specified value.	
function sound_fadeIn(streamID, fname, fade_time, volume){}

// Fade out sound to specified value.	
function sound_fadeOut(streamID, fname, fade_time, volume){}

// Set volume of specified sound; decrease volume of all other sounds.	
function sound_focus(streamID, fname, fade_time, fg_volume, bg_volume){}
