	// Larry:
	// I've marked the changes in this file with [UPDATE-2009-07-02]
	// Will

    //##============================================================================
    //## INITIALIZATION
    //##============================================================================

    var isPageLoaded = false;
	  var idAbort = null;
    var idBuff = null;
    var doRotateBanners = false;
    var bannerRotatorURL = 'http://sa-m.streamads.com/view?t=b&zid=2&ct=1&o=innerHTML&bsr=468,60,768,90&did=syncBanner&pid=' + pubID ;
    var coverRotatorURL = 'http://sa-m.streamads.com/view?t=b&zid=2&ct=1&o=innerHTML&bs=300,250&did=coverArtSync&pid=' + pubID ;
    
    
    //##  END INIT
    //########################################################
    //## Once the DOM is loaded we can begin
    //########################################################
    function doWidgetConfigure()
    {
     
    
     //do this only onetime
     if (isPageLoaded) {
       return;
     } else {
         isPageLoaded = true;  //set the page loaded flag
     }

     // setup the station links
     var oMyLink ;
     var oMyText  ;
     var strLink ;
     var strText ;

     for(s=0; s<4 ; s++) {
       strLink = 'stLink'+ s ;
       strText = 'stText' + s;
       oMyText = document.getElementById(strText);
       oMyText.innerHTML = sLinkText[s];
       oMyLink = document.getElementById(strLink);
       oMyLink.href = sLink[s];
     }
     //create the station player
     createStationPlayer();
    }

    //########################################################
    //## Creates the player that will hold the LIVE stream
    //## Starts hidden and muted
    //########################################################
    function createStationPlayer() {
      Lint.debugMode = true;
      if (getBrowser() == 'FF' && !TSLPlayer.isInstalled())
      window.location.href = '/streamload.html';
      
      doitall.stationID = staID; //AudioRealm station ID
	    doitall.publisherID = pubID; //StreamAds publisher ID [Only required for 1-to-1 targeting]
      doitall.timeoutPadding = 0;
	    doitall.loadStationInfo = false; //Load station information from AudioRealm
	    doitall.preloadHistory = true; //Load song history from AudioRealm
	    doitall.useDataPoller = false; //Check for track changes using "polling" method (Data provided by AudioRealm)
	    doitall.useLocalMedia = false; //Louis: Turn localmedia off - just until gateways are done!
	    doitall.RegisterVideoArea("videoPlayer"); //If you wish to display targeted video content you must register video area
	    doitall.RegisterSyncIDs("coverArtSync", "syncBanner", null); //Register TextAd area (As DOM element)
      doitall.RegisterPlayerSpace('player');

	    //Subscribe to events
      doitall.onConfigure.subscribe(slightConfigure);
	    doitall.onMedia.subscribe(cbDisplayMedia);
	    doitall.onAds.subscribe(cbDisplayAds);
	    doitall.onHistoryLoaded.subscribe(cbHistoryLoaded); //Song history data from AudioRealm loaded
	    doitall.player.onLoaded.subscribe(cbPlayerLoaded); //Player is loaded & ready to be controlled
      doitall.player.onPlayerStatusChange.subscribe(cbPlayerStatusChange); // [UPDATE-2009-07-02] Player send a status change
      doitall.player.onBuffering.subscribe(cbIsBuffering); // [UPDATE-2009-07-02] Player send a status change
	    doitall.onVideoBegin.subscribe(cbVideoBegin); //Video playback is about to start
	    doitall.onVideoEnd.subscribe(cbVideoEnd); //Video playback ended
	    //doitall.onRestore.subscribe(cbRestore); //Advertisement time-out triggered
      Gateway.onNoGateway.subscribe(cbGatewayDone); //no gateway the enable the banner rotation
      Gateway.onGatewayDone.subscribe(cbGatewayDone); // when gateway ends reenabel banner ads

	    //Create player
	    doitall.player.mediaurl = liveURL;
	    doitall.player.showcontrols = true;
	    doitall.player.width=300;
	    doitall.player.height=45;

	    //Start all basic systems
	    doitall.Start();
    }

    //##============================================================================
    //## EVENT HANDLERS
    //##============================================================================
    function cbPlayerLoaded(type, args, me)
    {
	    //Show SilverLight warning
	    if (!doitall.player.isControllable()) setVisibility('idSilverLight', true);
    }
    
    function cbGatewayDone(type,args,me)
    {
      doRotateBanners = true;
      idAbort = setTimeout("cbRestore()", (2000));
    }
    
    function cbDisplayMedia(type, args, me)
    {
      //if the gateway flag is running do not rotate banners.
      if (! doRotateBanners) return ;
	    var media = args[0];
	    DisplayNowPlaying(media);
	    DisplayHistory();

      // [UPDATE 2009-07-02] If this is a new song, rotate the banners
      if (media['mediatype'] == 'S') cbRestore();
    }

    function cbDisplayAds(type, args, me)
    {
	    var ad = args[0];
	    DisplayNowPlaying(ad);

	    //Make the advertisement areas visible
	    setVisibility("coverArtSync", (ad['coverID']>0)&&(ad['hasvideo']!='yes')); //Louis: Check for video!
	    setVisibility("syncBanner", (ad['bannerID']>0));

	    //Abort banner timeout, as cbRestore will be called
	    if (idAbort) idAbort.cancel();
    }

    function cbHistoryLoaded(type, args, me)
    {
	    DisplayHistory();
    }

    function cbUpdateStatus()
    {
	    var code = doitall.player.getPlayerStatus();
	    var msg  = doitall.player.getStatusMessage();
	    logStatus("debug", 'Status: ('+code+') '+msg);
    }

	// [UPDATE-2009-07-02]
	// -------------------------------------------------------------------------
	function cbPlayerStatusChange(type, args, me)
	{
		if (args[0] == 4)
		{
			document.getElementById('playerStatus').innerHTML = 'Stream Status: Buffering...';
		}
		else if (args[0] == 1)
		{ 
      if (idBuff) clearTimeout(idBuff);
			document.getElementById('playerStatus').innerHTML = 'Stream Status: Playing';
		}
		else if (args[0] == 2)
		{ 
      if (idBuff) clearTimeout(idBuff);
			document.getElementById('playerStatus').innerHTML = 'Stream Status: Paused';
		}
		else if (args[0] == 3)
		{ 
      if (idBuff) clearTimeout(idBuff);
			document.getElementById('playerStatus').innerHTML = 'Stream Status: Stopped';
		}
		else {
      if (idBuff) clearTimeout(idBuff);
			document.getElementById('playerStatus').innerHTML = 'Stream Status: ERROR!';
		}
		
	}
  
  function slightConfigure()
{
    if (getBrowser() == 'FF' && !TSLPlayer.isInstalled())
      window.location.href = '/streamload.html';
}



	// -------------------------------------------------------------------------

		// [UPDATE-2009-07-02]
	// -------------------------------------------------------------------------
	function cbIsBuffering(type, args, me)
	{
	  document.getElementById('playerStatus').innerHTML = 'Stream Status: Buffering...' + args[0] + '%';	
    idBuff = setTimeout("cbChkBuffer()", 250);
	}
  
  function cbChkBuffer()
  {
    var prog = doitall.player.getBufferProgress();
    if (prog == 100) {
      document.getElementById('playerStatus').innerHTML = 'Stream Status: Playing';
      clearTimeout(idBuff);
      idBuff = null ;
      }
    else {  
      document.getElementById('playerStatus').innerHTML = 'Stream Status: Buffering...' + prog + '%';
      idBuff = setTimeout("cbChkBuffer()", 250);
      }
  }
	// -------------------------------------------------------------------------


	
    function logStatus(divid, msg) {
      var myid = document.getElementById(divid);
      myid.innerHTML = msg;
    }


    function cbVideoBegin(type, args, me)
    {
	    //Video about to start...
    }

    function cbVideoEnd(type, args, me)
    {
	    //Video about to ended
		//reset CoverArt to visible LPD 2009-07-06
		setVisibility("coverArtSync", true);
		
    }

    function cbRestore(type, args, me)
    {
    if (! doRotateBanners) return ;
    //We only rotate ads when we are in music
		JSLoader.LoadJS(bannerRotatorURL, 'bannercontent');
		JSLoader.LoadJS(coverRotatorURL, 'covercontent');
		setVisibility("coverArtSync", true);
		setVisibility("syncBanner", true); 

		if (idAbort) idAbort.cancel(); //Abort banner timeout
		//idAbort = setTimeout("cbRestore()", (5*60*1000)); //5min banner timeout, if no ads!
		//idAbort = YAHOO.lang.later(5 * 60 * 1000, null, cbRestore);
    }

    //##============================================================================
    //## WORKER FUNCTIONS
    //##============================================================================

    function DisplayItem(item)
    {
	    var idMetadata = document.getElementById('debug');
	    DeleteAllChildren(idMetadata);

	    for (var key in item) //Iterate through all the values the item exposes
	    {
		    var display = key+": "+item[key];

		    var objDiv = document.createElement('div');
		    var objText = document.createTextNode(display);
		    objDiv.appendChild(objText);
		    idMetadata.appendChild(objDiv);
	    }
    }

    function DisplayRecent(item,pos)
    {
		var strArtist = '' ;
		var strTitle = '' ;
		var display = '' ;

	    for (var key in item) //Iterate through all the values the item exposes
	     {
	       if (key == "artist") { strArtist= item[key]; }
	       if (key == "title") { strTitle= item[key]; }
	     }
		display = strArtist + " - " + strTitle;

        if (pos == 1) {
          var idSongData = document.getElementById('rp_1');
        } else if (pos==2) {
          var idSongData = document.getElementById('rp_2');
        } else if (pos==3) {
          var idSongData = document.getElementById('rp_3');
        } else if (pos==4) {
          var idSongData = document.getElementById('rp_4');
        } else if (pos==5) {
          var idSongData = document.getElementById('rp_5');
        }
		idSongData.innerHTML = display ;
    }

    function DisplayNowPlaying(item)
	{
      	var idMetaArtist = document.getElementById('artistTxt');
      	var idMetaTitle = document.getElementById('titleTxt');

	    //Run the debugger
	    DisplayItem(item);
	    var display;
	    var strArtist = "Support our streaming sponsors" ;
	    var strTitle;
	    var strSongType;

		for (var key in item) //Iterate through all the values the item exposes
		{
			if(key == 'artist')
			{
			 	strArtist = item[key];
			}
			else if (key == 'title')
			{
			 	strTitle = item[key];
			} else if (key == 'songtype')
			{
			 	strSongType = item[key] ;
			}
		}
      idMetaArtist.innerHTML=strArtist;
      idMetaTitle.innerHTML=strTitle;
    }

    function DisplayHistory()
    {
	    var list = doitall.mediadata.getMediaHistory(6,"S"); //Return 6 items with songtype S (song)
		//clear the history
		document.getElementById('rp_1').innerHTML = '';
		document.getElementById('rp_2').innerHTML = '';
		document.getElementById('rp_3').innerHTML = '';
		document.getElementById('rp_4').innerHTML = '';
		document.getElementById('rp_5').innerHTML = '';

	    for(var i=0; i<list.length; i++)
	    {
			DisplayItem(list[i]);
		    if (i==0)
			{
				DisplayNowPlaying(list[i]); //item 0 goes into currently playing
		    }
			else
			{
				DisplayRecent(list[i],i);
		    }
	    }
    }
	
    //This function deletes all the nodes in a DOM tree starting at specified node
    function DeleteAllChildren(domObj)
    {
	    var child = domObj.firstChild;
	    while(child != null)
	    {
		    domObj.removeChild(child);
		    child = domObj.firstChild;
	    }
    }
    
