var xaudioelloc //element ID of the tag that calls the grabaudio function
var curaudiowrapperid

function grabaudio(elid,idx)
{
	/*the elid can come from either the tag's ID attribute (for href) or the this. (for onclick)
	idx=the clip id
	xaudioelloc is the document element that the audio player will be positioned next to. If it doesn't exist, the audio player will be centered on the screen
	*/
	
	xaudioelloc=document.getElementById(elid)

	createRequest()
	
	var url='/includes/audioplayer/audiofunctions.asp'
	var sel=''
	sel='idx='+escape(idx)

	request.open("POST",url,true)

	request.onreadystatechange = updateAudio
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
	request.send(sel)
}

function updateAudio()
{
	//alert('frmname='+frmname+'\nrequest.readyState='+request.readyState+'\nrequest.responseText='+request.responseText)
	
	if(request.readyState==4)
	{
		var resp=request.responseText
		//alert(resp)
		//alert(resp.replace(/\]/g,']\n'))
		if(resp=='')
		{
			alert('An error has occurred.')
		}
		else
		{
			resp=unescape(resp)
		}
		
		if (request.status==200)
		{
			if(resp.substr(1,6)=='Error:')
			{
				alert(resp)
			}
			else
			{
				resp=unescape(resp)
				//resp=eval(resp)
				showxaudio(resp)

			}

		}
		else
		{
			alert("An error has occurred. The server said: "+request.status)
		}
	}
}

function showxaudio(ds)
{
	var fields=ds.split("|")
	var curfield

	var idx='', title='', description='', mp3path='', swfpath='', flvpath='', speaker='', active='', mt='', starton='', stopon='', icons='', subtype='', duration=''
	
	//we only want to show the title/text if it's not generic 
	var showtxt=false
	var showicons=false
	for(var g=0;g<fields.length;g++)
	{
		curfield=fields[g].split('~')
		//alert(curfield[0])
		if(curfield[0]=='type')
		{
			if(curfield[1].toLowerCase() != 'generic')
			{
				showtxt=true
			}
		}
		else if(curfield[0]=='id')
		{
			idx=curfield[1]
		}

		else if(curfield[0]=='title')
		{
			title=curfield[1]
		}
		else if(curfield[0]=='description')
		{
			description=curfield[1]
		}
		else if(curfield[0]=='mp3_path')
		{
			mp3path=curfield[1]
		}
		else if(curfield[0]=='swf_path')
		{
			swfpath=curfield[1]
		}
		else if(curfield[0]=='flvpath')
		{
			flvpath=curfield[1]
		}
		else if(curfield[0]=='speaker')
		{
			speaker=curfield[1]
		}
		else if(curfield[0]=='active')
		{
			active=curfield[1]
		}
		else if(curfield[0]=='minor_topics')
		{
			mt=curfield[1]
		}
		else if(curfield[0]=='start_showing_on')
		{
			starton=curfield[1]
		}
		else if(curfield[0]=='stop_showing_on')
		{
			stopon=curfield[1]
		}
		else if(curfield[0]=='icons')
		{
			icons=curfield[1]
			
			if(icons.length>0)
			{
				showicons=true
			}

		}
		else if(curfield[0]=='subtype')
		{
			subtype=curfield[1]
		}
		else if(curfield[0]=='length_in_seconds')
		{
			duration=curfield[1]
		}

	}
	
	
	var newaudiowrapper=document.createElement("div")
	var mydate=new Date()
	var curtime=mydate.getTime()
	curaudiowrapperid='bgeaaudiowrapper'+curtime
	//alert(curaudiowrapperid)

	newaudiowrapper.setAttribute("id", curaudiowrapperid)
	newaudiowrapper.setAttribute("class", "bgeaaudiowrapper")

	var newaudiowrapperHTML=''
	newaudiowrapperHTML+='<div id="bgeaaudioabswrapper'+curtime+'" class="bgeaaudioabswrapper">\n'
	newaudiowrapperHTML+='<div id="bgeaaudiobox'+curtime+'" class="bgeaaudiobox">&nbsp;</div>\n'
	if(showtxt==true)
	{
		newaudiowrapperHTML+='<div id="bgeaaudiodetails'+curtime+'" class="bgeaaudiodetails">&nbsp;</div>\n'
	}
	if(showicons>0)
	{
		newaudiowrapperHTML+='<div id="bgeaaudioicons'+curtime+'" class="bgeaaudioicons">&nbsp;</div>\n'	
	}
	newaudiowrapperHTML+='</div>\n'
	newaudiowrapper.innerHTML=newaudiowrapperHTML


	//alert(xaudioelloc+'\n'+newaudiowrapper)
	//xaudioelloc=document.getElementById('audioid6')
	if(xaudioelloc==null)
	{
		//center on screen
		document.body.insertBefore(newaudiowrapper, document.body.firstChild)
		centerbgeaaudio(curaudiowrapperid)
	}
	else
	{
		xaudioelloc.insertBefore(newaudiowrapper, xaudioelloc.firstChild)
	}
	/*if(xaudioelloc && navigator.appName!='Microsoft Internet Explorer')
	{
		//position the audio player at the specified id (xaudioelloc)
		xaudioelloc.insertBefore(newaudiowrapper, xaudioelloc.firstChild)
	}
	else
	{
		//just center it on the screen
		//document.body.insertBefore(newaudiowrapper, document.body.firstChild)
		//centerbgeaaudio(curaudiowrapperid)
		xaudioelloc.insertBefore(newaudiowrapper, xaudioelloc.firstChild)
	}*/

	if(navigator.appName=='Microsoft Internet Explorer')
	{

		document.getElementById(curaudiowrapperid).className='bgeaaudiowrapper'
		document.getElementById(curaudiowrapperid).Id=curaudiowrapperid
		//alert(curaudiowrapperid)
	}


	var flashhtml=''
	var detailshtml=''
	var iconshtml=''

	flashhtml+='<div style="float:left;width:265px;" id="xflashaudio'+curtime+'" class="xflashaudio">&nbsp;</div>\n'
	flashhtml+='<div style="float:left;margin-left:10px;width:25px;"><a href="javascript:closexaudio(\''+curaudiowrapperid+'\')"><img src="/images/bgeaslideshow/close.png" style="border:0px;" valign="absmiddle"></a></div>\n'
	//alert(flashhtml)

	if(showtxt==true)
	{
		detailshtml+='<div id="audiodescription'+curtime+'" class="audiodescription">\n'
		detailshtml+='<div style="padding:0px 10px 10px 10px;">\n'
		detailshtml+='<div style="font-weight:bold;">'+title+'</div>\n'
		if(speaker!='')
		{
			detailshtml+='<div style="font-style:italic;">'+speaker+'</div>\n'
		}
		detailshtml+='<div style="overflow:auto;height:100px;">'+description+'</div>\n'
		detailshtml+='</div>\n'
		document.getElementById('bgeaaudiodetails'+curtime).innerHTML=detailshtml
	}
	
	document.getElementById('bgeaaudiobox'+curtime).innerHTML=flashhtml

	//alert('/mediastorage/mp3/'+mp3path)

	var so = new SWFObject(swfpath,'jsaudio'+curtime,'265','20','8');
	so.addParam('allowfullscreen','false');
	if(mp3path.indexOf('http')>-1)
	{
		//don't prefix anything - use as is
		so.addVariable('file',mp3path);
		so.addVariable('type','http');
		so.addVariable('streamer','http://istream.bgea.org/');
		so.addVariable('start','1');
	}
	else
	{
		so.addVariable('file','/mediastorage/mp3/'+mp3path);
	}
	if(duration!=0)
	{
		so.addVariable('duration',duration);
	}
	so.addVariable('backcolor','0xffffff');
	so.addVariable('autostart','true');
	so.addVariable('width','265');
	so.addVariable('height','20');
	//so.addParam("wmode", "transparent");
	so.addVariable('enablejs','true');
	so.addVariable('shownavigation','true');
	so.write('xflashaudio'+curtime);

	if(showicons==true)
	{
		iconlist=icons.split('@@')
		{
			for(var y=0;y<iconlist.length;y++)
			{
				//alert(iconlist[y].toLowerCase())
				switch(iconlist[y].toLowerCase())
				{
				case 'commitment':
					iconshtml+='<div class="bgeaaudioicon" title="Make a commitment to Christ"><a href="/commitment.asp" target="_blank"><img src="/images/icons/commitment.gif" style="border:0px;"></a></div>'
					break;
				case 'donate':
					iconshtml+='<div class="bgeaaudioicon" title="Donate to BGEA"><a href="/donate.asp" target="_blank"><img src="/images/icons/donate.gif" align="absmiddle" style="border:0px;"></a></div>'
					break;
				case 'free offer':
					iconshtml+='<div class="bgeaaudioicon" title="Free Offer"><a href="/freeoffer.asp" target="_blank"><img src="/images/icons/freeoffer.gif" style="border:0px;"></a></div>'
					break;
				case 'contact us':
					iconshtml+='<div class="bgeaaudioicon" title="Contact us"><a href="/Contact_BGEA.asp" target="_blank"><img src="/images/icons/contact.gif" style="border:0px;"></a></div>'
					break;
				case 'steps to peace':
					iconshtml+='<div class="bgeaaudioicon" title="Steps To Peace"><a href="/SH_StepsToPeace.asp" target="_blank"><img src="/images/icons/stepstopeace.gif" style="border:0px;"></a></div>'
					break;
				case 'email to friend':
					iconshtml+='<div class="bgeaaudioicon" title="Email this clip to someone"><a onclick="showhideonclick(\'bgeaaudioemailtofriend\');document.forms.bgeaaudioemailtofriend.VIC_Question_00026.focus()" style="cursor:pointer;"><div id="bgeaaudioemailtofriendlink" style="font-size:9px;margin-right:5px;width:33px;text-align:left;display:table;"><img src="/images/icons/email.gif" align="absmiddle" style="border:0px;"></div></a></div>'
					iconshtml+='<!--ETF popup-->'
					iconshtml+='<div style="position:relative;visibility:hidden;right:250px;" id="bgeaaudioemailtofriend">'
					iconshtml+='<div style="position:absolute;z-index:10;border:1px solid black;background-color:#ffffff;padding:20px 20px 20px 20px;width:260px;">'
					iconshtml+='<div style="position:absolute;right:3px;top:3px;z-index:11;cursor:pointer;color:#cc0000;" onclick="showhideonclick(\'bgeaaudioemailtofriend\')">X close</div>'
					iconshtml+='<form name="bgeaaudioemailtofriend">'
					iconshtml+='<div class="etfFormLineWrapper"><div class="etfTitleWrapper"><b>Friend\'s e-mail</b></div><div class="etfElementWrapper"><input type="text" name="VIC_Question_00026" size=20 maxlength="100"></div></div>'
					iconshtml+='<div class="etfFormLineWrapper"><div class="etfTitleWrapper">Friend\'s name</div><div class="etfElementWrapper"><input type="text" name="VIC_Question_00025" size=20 maxlength="100"></div></div>'
					iconshtml+='<div class="etfFormLineWrapper"><div class="etfTitleWrapper"><b>Your e-mail</b></div><div class="etfElementWrapper"><input type="text" name="VIC_Question_00418" size=20 maxlength="100"></div></div>'

					iconshtml+='<div class="etfFormLineWrapper"><div class="etfTitleWrapper"><b>Your name</b></div><div class="etfElementWrapper"><input type="text" name="VIC_Question_00419" size=20 maxlength="100"></div></div>'
					iconshtml+='<div class="etfFormLineWrapper"><span id="bgeaaudioemailtofriendlinkbutton"><a href="javascript:VF_ETFForm(\'bgeaaudioemailtofriend\',\'/rpc/mailtofriend/\',\'mailtofriend.asp\');">Submit<img src="/images/buttons_arrow.gif" align="absmiddle" style="border:0px;margin-left:8px;"></a></span></div>'
					iconshtml+='<input type="hidden" name="VIC_Question_00421" value="'+location.protocol+'//'+location.host+'/audioplayer.asp?id='+idx+'">'
					iconshtml+='<input type="hidden" name="VIC_ID" value="106">'
					iconshtml+='<input type="hidden" name="ETF_Form_Action" value="2">'
					iconshtml+='</form>'
					iconshtml+='<i>Separate multiple addresses with a comma</i>'
					iconshtml+='</div>'
					iconshtml+='</div>'
					iconshtml+='<!--end ETF popup-->'

					break;
				case 'download':
					iconshtml+='<div class="bgeaaudioicon" title="Download"><a href="/mediastorage/mp3/'+mp3path+'" target="_blank"><img src="/images/icons/download.gif" align="absmiddle" style="border:0px;"></a></div>'
					break;

				}
				
			}
		}
		document.getElementById('bgeaaudioicons'+curtime).innerHTML=iconshtml
	}

}


function closexaudio(idx)
{
	//alert(idx)
	var blah=idx.replace(/[^0-9]/g,'') //extract only the numeric part of the id
	//alert('jsaudio'+blah)
	//alert(('jsaudio'+blah))
	sendEvent('jsaudio'+blah,'stop')
	var parentnode=document.getElementById(idx).parentNode

	//alert(parentnode+' '+parentnode.id)
	//alert(document.getElementById(parentnode.id).firstChild.id+' '+idx)
	//alert(document.getElementById(idx).id)
	//alert(parentnode.nodeName)
	if(parentnode.nodeName=='BODY')
	{
		document.body.removeChild(document.getElementById(idx))
	}
	else
	{
		var remno = document.getElementById(parentnode.id).removeChild(document.getElementById(idx))
	}
	/*	if(document.getElementById(parentnode.Id))
		{
			var remno = document.getElementById(parentnode.Id).removeChild(document.getElementById(idx))
		}
		else
		{
	
			document.body.removeChild(document.getElementById(idx))
		}*/

//	alert(parentnode+' '+parentnode.nodeName)
//	var removednode=parentnode.removeChild(idx)
/*
	if(xaudioelloc)
	{
	}
	else
	{
		var childnode=document.getElementById(idx)
		//alert(childnode)
		var removednode=document.body.removeChild(childnode)
	}*/
}

function sendEvent(swf,typ,prm)
{ 
	thisMovie(swf).sendEvent(typ,prm); 
}

function getUpdate(typ,pr1,pr2,swf)
{
}

function thisMovie(swf)
{
	if(navigator.appName.indexOf("Microsoft") != -1)
	{
		return window[swf];
	}
	else
	{
    	return document[swf];
	}
}

function centerbgeaaudio(idx)
{
	if((navigator.appName=='Microsoft Internet Explorer') && (navigator.appVersion.indexOf('MSIE 6.0')>0))
	{
		//ie6
		document.getElementById(idx).style.position='absolute'
		document.getElementById(idx).style.top=document.documentElement.scrollTop+120+'px'
		document.getElementById(idx).style.left=(document.body.clientWidth/2)-(document.getElementById(idx).clientWidth/2)+'px'
	}
	else if(navigator.appName=='Microsoft Internet Explorer')
	{
		//alert(document.getElementById('bgeaaudiowrapper').clientWidth)
		document.getElementById(idx).style.width=document.getElementById(idx).clientWidth+'px'
		/*document.getElementById(idx).style.left="50%"
		document.getElementById(idx).style.marginLeft='-'+(document.getElementById(idx).clientWidth/2)+'px'*/
		document.getElementById(idx).style.position='fixed'
		document.getElementById(idx).style.top="50%"
		document.getElementById(idx).style.marginTop='-'+((document.getElementById(idx).clientHeight/2)+document.getElementById(idx).firstChild.clientHeight)+'px'
		document.getElementById(idx).style.left="50%"
		document.getElementById(idx).style.marginLeft='-'+(document.getElementById(idx).firstChild.clientWidth/2)+'px'

	}
	else
	{
		//document.getElementById(idx).style.width=document.getElementById(idx).style.width
		//alert(idx)
		document.getElementById(idx).style.position='fixed'
		//document.getElementById(idx).style.zIndex='45000'
		document.getElementById(idx).style.top="50%"
		document.getElementById(idx).style.marginTop='-'+((document.getElementById(idx).clientHeight/2)+document.getElementById(idx).firstChild.clientHeight)+'px'
		//document.getElementById(idx).style.top=document.scrollTop+200+'px'
		document.getElementById(idx).style.left="50%"
		document.getElementById(idx).style.marginLeft='-'+(document.getElementById(idx).clientWidth/2)+'px'

	}	
}