var xaudioelloc=false //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
	*/
	if(elid != '')
	{
		xaudioelloc=document.getElementById(elid)
	}

	createRequest()
	
	var url='/includes/popupaudio/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.replace(/\]/g,']\n'))
		if(resp=='')
		{
			alert('An error has occurred.')
		}
		else
		{
			resp=unescape(resp)
		}
		//alert(resp)
		if (request.status==200)
		{
			if(resp.indexOf('Error:')>-1)
			{
				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='', firstname='', lastname=''
	
	//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]=='first_name')
		{
			firstname=curfield[1]
		}
		else if(curfield[0]=='last_name')
		{
			lastname=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

	document.body.insertBefore(newaudiowrapper, document.body.firstChild)

	if(navigator.appName=='Microsoft Internet Explorer')
	{

		document.getElementById(curaudiowrapperid).className='bgeaaudiowrapper'
		document.getElementById(curaudiowrapperid).Id=curaudiowrapperid
		//alert(curaudiowrapperid)
	}

	createmask()


	var flashhtml=''
	var detailshtml=''
	var iconshtml=''

/*	flashhtml+='<div style="position:relative;width:400px;color:#878787;font-weight:bold;height:20px;margin-bottom:0px;">AUDIO\n'
	flashhtml+='	<div style="position:absolute;right:0px;top:0px;">\n'
	flashhtml+='		<div style="cursor:pointer;" onclick="javascript:closexaudio(\''+curaudiowrapperid+'\')">CLOSE <img src="/assets/images/buttons/GreyXButton.gif" style="margin-left:5px;vertical-align:text-top;"></div>'
	flashhtml+='	</div>\n'
	flashhtml+='</div>\n'*/
	flashhtml+='<div class="popuptextwrapper">'
	flashhtml+='<div style="position: relative; cursor: pointer;" onclick="closexaudio(\''+curaudiowrapperid+'\')">'
	flashhtml+='	<div style="position: absolute; right: 0px; height: 16px; color: rgb(135, 135, 135); font-weight: bold;">CLOSE <img src="/assets/images/buttons/GreyXButton.gif" style="margin-left: 5px; vertical-align: text-top;"></div>'
	flashhtml+='</div>'
	flashhtml+='<p class="Bluesubtitle">Audio</p>'
	flashhtml+='</div>'

	flashhtml+='<div style="float:left;width:265px;height:24px;" id="xflashaudio'+curtime+'" class="xflashaudio">&nbsp;</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;" class="Bluesubtitle">'+title+'</div>\n'
		if(speaker!='')
		{
			detailshtml+='<div style="font-style:italic;">'+firstname+' '+lastname+'</div>\n'
		}
		detailshtml+='<div style="">'+description+'</div>\n'
		//detailshtml+='</div>\n'
		document.getElementById('bgeaaudiodetails'+curtime).innerHTML=detailshtml
	}
	
	document.getElementById('bgeaaudiobox'+curtime).innerHTML=flashhtml

	//alert('/mediastorage/mp3/'+mp3path)

	if(swfpath.charAt(0)!='/')
	{
		swfpath = '/'+swfpath
	}

	var so = new SWFObject(swfpath,'jsaudio'+curtime,'265','24','8');
	so.addParam('allowfullscreen','false');
	so.addVariable('file','/assets/media/mp3/'+mp3path);
	so.addVariable('backcolor','0xffffff');
	so.addVariable('autostart','true');
	so.addVariable('width','265');
	so.addVariable('height','24');
	//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="/assets/images/section/FooterIconPray.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="/assets/images/section/FooterIconDonate.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="/assets/images/icons/freeoffer.gif" style="border:0px;"></a></div>'
					break;
				case 'contact us':
					iconshtml+='<div class="bgeaaudioicon" title="Contact us"><a href="/contact.asp" target="_blank"><img src="/assets/images/section/FooterIconEnvelope.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="/assets/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="/assets/images/section/FooterIconEnvelope.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 &raquo;</a></span></div>'
					iconshtml+='<input type="hidden" name="VIC_Question_00421" value="'+location.protocol+'//'+location.host+'/popupaudio.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;
				}
				
			}
		}
		document.getElementById('bgeaaudioicons'+curtime).innerHTML=iconshtml
	}

	
	
	var closeevt=function(evt){checkclosepop(evt,'closexaudio(\''+curaudiowrapperid+'\');')}
	//add esc listener
	if(!isie())
	{
		
		window.addEventListener('keydown',closeevt, false)
	}
	else
	{
		document.body.attachEvent('onkeyup',closeevt)
	}

	centerelement('bgeaaudioabswrapper'+curtime)
}


function closexaudio(idx)
{
	//alert('yay')
	if(gebid(idx))
	{
		var blah=idx.replace(/[^0-9]/g,'') //extract only the numeric part of the id
		//alert(blah)
		var parentnode=document.getElementById(idx).parentNode

		if(parentnode.nodeName=='BODY')
		{
			document.body.removeChild(document.getElementById(idx))
		}
		else
		{
			var remno = document.getElementById(parentnode.id).removeChild(document.getElementById(idx))
		}
	}
	
	removebox('bgeapagemask')
	/*childnode=document.getElementById("bgeapagemask")
	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'

	}	
}
