<!--
/* For this script to work we need:
1. audioStream directory with following files: StreamFrame207x95.gif, Blank.54x75.jpg, Template.54x75.jpg
2. auSrc directory inside audioStream directory that contains xxxxxxx.m3u or xxxxxx.mp3 files. 
   Each .m3u file points to the .wma file at OverDrive.com
4. streamAudio.js file must be in the js directory
5. In the .php file we need function printAudioStream() with properly adjusted positions 
   and dimentions for all div's
6. Remove <FORM generation. Stream audio doesn't work with forms

To add a new book to the show:
1. In the audioStream/auSrc directory create 0312963815_Birds_of_Prey-by_Wilbur_Smith.m3u file with a link to the audio stream
2. Add ASID to the imPool Array here
3. Add the new .m3u file name to the auPool directory
*/
timerID=new Array(null,null,null,null);
var curPool=0;
var amlink="<a href='http://www.amazon.com/gp/redirect.html?link_code=ur2&tag=booksalley-20&camp=1789&creative=9325&location=/gp/search%3F%26index=books%26keywords=xxxxxxxxxx%26_encoding=UTF8'> More...</a>";
imPool=new Array('blank','blank','0451220714','0312963815','0316848115','0345471008','0446531456','0765306786','1416514961','159887022X','0449207986');
auPool=new Array('','','0451220714_Kill_Me-by_Stephen_White.m3u','0312963815_Birds_of_Prey-by_Wilbur_Smith.m3u','0316848115_The_Lion`s_Game-by_Nelson_Demille.m3u','0345471008_The_Virgin_of_Small_Plains-by_Nancy_Pickard.m3u','0446531456_Exact_Revenge-by_Tim_Green.m3u','0765306786_Hart`s_Hope-by_Chris_Howard.m3u','1416514961_The_Two_Minutes_Rule-by_Robert_Crais.m3u','159887022X_The_Time_Traveler`s_Wife-by_Chris_Howard.m3u','0449207986_Sins_of_the_Fathers-by_Susan_Howatch.m3u');
var astrdir="../audioStream";
var ausrc=astrdir+"/auSrc/"+auPool[4];
var pluginMess=detectPlugin('np-mswmp');

// InsertNextAudio creates Windows Media Player plugin dynamically. Currently works in IE and FireFox
function InsertNextAudio()
 {
  var nAgt = navigator.userAgent;
  var aobj="";
  if ((verOffset=nAgt.indexOf("MSIE"))!=-1)
   {
	aobj+='<object id="wmP" width="200" height="45" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject">\n';	 
   }
  else
   {
    aobj+='<object id="wmP" type="application/x-mplayer2" width="200" height="45">\n';
   } 
  aobj+='<PARAM NAME="URL" VALUE="3pigs.m3u">\n';
  aobj+='<PARAM NAME="SendPlayStateChangeEvents" VALUE="True">\n';
  aobj+='<PARAM NAME="AutoStart" VALUE="True">\n';
//  aobj+='<PARAM name="uiMode" value="mini">\n';
  aobj+='<PARAM name="ShowControls" value="true">\n';
  aobj+='<PARAM name="enabled" value="true">\n';
  aobj+='<PARAM name="EnableTracker" value="true">\n';
  aobj+='<PARAM name="ShowTracker" value="true">\n';
  aobj+='<PARAM name="SendErrorEvents" value="true">\n';
  aobj+='<PARAM name="PlayCount" value="9999">\n';
  aobj+='</object>\n';
  return(aobj);
 }
 
function cuhand(a) {a.style.cursor="pointer";}

// Displays message me inside div with id='nowPlay'
function infMes(m)
 {
  if(pluginMess.length>10) m=pluginMess+"<br><b>Now Playing: </b> "+m;
  else m="<b>Now Playing: </b> "+m;
  var nb=" <input id='NextBook' type='submit' name='Next book' value='Next book >' style='width: 200px; height: 20px; text-align: right; font-size: 11px; font-weight: bold; border: 0px; background: white;' onclick='javascript: moveAll();' onmouseover='javascript: cuhand(this);'>";
  document.getElementById('nowPlay').innerHTML=m+"<br>"+nb+"<br>";
 }

// Moves all book cover images one step left
function moveAll()
 {
  for(var i=0;i<timerID.length;i++) clearInterval(timerID[i]);
  timerID[0]=setInterval("moveOneStep('b01',4,-44)",50);
  timerID[1]=setInterval("moveOneStep('b02',3,10)",50);
  timerID[2]=setInterval("moveOneStep('b03',2,68)",50);
  timerID[3]=setInterval("moveOneStep('b04',1,126)",30);
 }
 
// Creates and returns array of current images, sets global variable ausrc 
// Sets new audio stream source in the plugin
function getFromImArray(direction)
 {
  var curmess="",amid="",lindex=0;
  if(direction=='next') curPool++;
  if(direction=='privious') curPool--;
  if(direction=='start') curPool=0;
  
//  alert("curPool="+curPool+", imPool.length="+imPool.length);
  
  if(curPool<0) curPool=0;
  if(curPool>(imPool.length-3)) curPool=0;
  
  curArray=new Array(imPool[curPool],imPool[curPool+1],imPool[curPool+2],imPool[curPool+3]);
  curmess=auPool[curPool+2].replace(/_/gi," ");
  amid=curmess.match(/\S*/).toString();
  amid=amid.toString();
  lindex=amid.length;
  meslen=curmess.length-lindex-4;
  curmess=curmess.substr(lindex,meslen)+"&nbsp;&nbsp;"+amlink.replace(/xxxxxxxxxx/,amid); 
  infMes(curmess); 
 
  ausrc=astrdir+"/auSrc/"+auPool[curPool+2];
  document.getElementById('wmP').URL=ausrc;
//  alert(ausrc);
  return(curArray);
 } 
 
// imArray contains ASID numbers for images b01,b01 and b03 respectedly
function resetImages(direction)
 {
  var imSource='';
  
  imArray=getFromImArray(direction);
   
//D  alert("curPool="+curPool+", length="+imArray.length);
  
  imx=new Array(10,68,126,184);
  imIDs=new Array('b01','b02','b03','b04');
  
  for(var i=0; i<4; i++)
   {
	if(imArray[i] == 'blank') {imSource=astrdir+'/Blank.54x75.jpg';}
	else if(imArray[i] == 'template') {imSource=astrdir+'/Template.54x75.jpg';}
	else {imSource='http://images.amazon.com/images/P/'+imArray[i]+'.01._SCTHUMBZZZ_.jpg';}
	document.getElementById(imIDs[i]).src=imSource; 
	document.getElementById(imIDs[i]).style.left=imx[i];
//	getObjAttribs(imIDs[i]);
   }
 } 
 
function moveOneStep(imID,dx,xlimit)
 {
  var x=document.getElementById(imID).style.left;
  var re=/.?\d*/;
  var fx=x.match(re);
  
//D  infMes("x="+x+",fx="+fx);
  
  x=String(parseInt(fx)-parseInt(dx));
  
  if(x<=xlimit) 
   {
	switch(imID)
	 {
	  case 'b01': clearInterval(timerID[0]);break;
	  case 'b02': clearInterval(timerID[1]);break;
	  case 'b03': clearInterval(timerID[2]);break;
	  case 'b04': {clearInterval(timerID[3]);resetImages('next');break;}
     }
    return(0); 
   }
  else {document.getElementById(imID).style.left=x;}
 }

// This function is for debugging purposes. It reads all available object attributes.
// This function is not used on the live site
function getObjAttribs(ObjID)
 {
  window.el=document.getElementById(ObjID);
  var nats=el.attributes.length      // The number of attributes inside "el"
  
  var a=document.getElementById(ObjID).innerHTML+"\n\n";
  for (i=0;i<nats;i++)
   {
    n=el.attributes[i].nodeName  //name of the first attribute of "myimage"
    v=el.attributes[i].nodeValue //value of the first attribute of "myimage"
    if(v) a+="n="+n+", v="+v+"\n";
   }
  alert(a); 
 } 
 
function detectPlugin() 
 {
  var nAgt = navigator.userAgent;
  var mess="";
  var plName='np-mswmp';

  if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {return("");} // We are ok with IE browser
  else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {plName='np-mswmp'; mess="Please load <a href='http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx'> Windows Media Player plugin</a> to see this page properly.";}
  else if ((verOffset=nAgt.indexOf("Apple"))!=-1) {plName='windows media'; mess="Please load <a href='http://www.microsoft.com/mac/otherproducts/otherproducts.aspx?pid=windowsmedia'> Microsoft Windows Player for Mac</a> to see this page properly.";}
  else {plName='';mess="";} // Unknown browser, just display as is
  
// alert("navigator.plugins="+navigator.plugins+", navigator.plugins.length="+navigator.plugins.length);
  if (navigator.plugins && navigator.plugins.length > 0) // if plugins array is there and not fake
   {
    var pluginsArrayLength = navigator.plugins.length; // loop through all desired names and check each against the current plugin name
    for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) 
     {
      var numFound = 0;
      pname=navigator.plugins[pluginsArrayCounter].name;
      pdesc=navigator.plugins[pluginsArrayCounter].description;
//    alert("pname="+pname+", pdesc="+pdesc);
	  if((navigator.plugins[pluginsArrayCounter].name.indexOf(plName) >= 0) || (navigator.plugins[pluginsArrayCounter].description.indexOf(plName) >= 0)) {return("");} // Plugin is here, we are OK
	 }
	}
 return(mess);
} // end of detectPlugin()

//-----------------------------------------------------------------------------------------
// hand cursor and class bover when over a button
function mover(a){a.style.cursor='pointer';a.className='bover';}
// regular cursor and calls regular when out of button
function mout(a){a.className='submit';}

// hand cursor and class bover when over a Next/Previous
function movernext(a){a.style.cursor='pointer'; a.className='bovernext';}
// regular cursor and calls regular when out of button
function moutnext(a){a.className='submitnext';}

// Open a new window
function OpenWindow(PageSrc,Top,Left,Width,Height)
 {
  Features="top="+Top+",left="+Left+",width="+Width+",height="+Height+",toolbar=yes,menubar=no,location=no,directories=no,scrollbars=yes,resizable=yes";
  NewWin=window.open(PageSrc,"NewWindow",Features);
  NewWin.focus();
 }
// Play samples from audio.com
function popUpJSP(a,b,c)
 {
  if(b == null)	b = '';
  if(c == null)	winnew = window.open('http://www.audible.com/adbl'+a+'?BV_SessionID=@@@@1314122838.1155230225@@@@&BV_EngineID=cccjaddiiefhgigcefecekjdffidffg.0' + b,'help','resizable=yes,toolbar=yes,location=no,menubar=yes,scrollbars=yes,status=yes,width=500,height=400');
  else	winnew = window.open('http://www.audible.com/adbl'+a+'?BV_SessionID=@@@@1314122838.1155230225@@@@&BV_EngineID=cccjaddiiefhgigcefecekjdffidffg.0' + b,'help',c);
  if (window.focus) {winnew.focus()}
 }
 
//-->