
// -- Global Variables --
debug=1;

// ---------------------------------------------------------
// Writing into a DIV
// ---------------------------------------------------------
function MyGetElementById(my_element,id)
{
	if (document.getElementById) x= my_element.getElementById(id);
	else if (document.all)	     x= my_element.all[id];
	else if (document.layers)    x= my_element.document.layers[id];
	
	return(x);
}


// ----------------------------------------------------------------
// -- 
// ----------------------------------------------------------------
function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></script>'); 
}
IncludeJavaScript('goodies.js');





// --------------------------------------------------------
// -- Station Object --
// -------------------------------------------------------
function Station(frequency, home, name, genre, graphic, link, index) {
  this.frequency = frequency;
  this.home      = home;
  this.name      = name;
  this.genre     = genre;
  this.graphic   = graphic;
  this.link      = link;
  this.index     = index;
}


// --------------------------------------------------------
// -- Station List Object --
// --------------------------------------------------------
var sl              = new Object;
sl.num_stations = 0;
sl.station      = new Object;
// -- Add Station Method --
sl.add  = add;
sl.next = next;
sl.prev = prev;
function add(frequency, home, name, genre, graphic, link){
  sl.station[sl.num_stations] = new Station(frequency, home, name, genre, graphic,link,sl.num_stations);
  sl.num_stations++;
}
function next(){
  return(sl.station[(curr_station+1)%sl.num_stations]);
}
function prev(){
  
  return(sl.station[(curr_station+sl.num_stations-1)%sl.num_stations]);
}


// --------------------------------------------------------
// -- Add Stations --
// --------------------------------------------------------
// Got from: http://www.ontheradio.net/metro/San_Jose_CA.aspx


curr_station = Math.floor(Math.random()*sl.num_stations);
curr_station = 0;

//alert("sl.num_stations = " + sl.num_stations);
//alert("current station = " + curr_station);
  
  
// --------------- Navigation Buttons--------------------
// Responses from pressing buttons
// ------------------------------------------------------

// -- Button Play --
function ButtonPlay(){
  preset_set_off();
  StopScan();

  play_station(curr_station);  
  
}

// -- Button Stop --
function ButtonStop(){
  preset_set_off();
  StopScan();

  stop_station();
}

// -- Next Button --
function ButtonNext(){
  preset_set_off();
  StopScan();

  // Turn off scan first
  if(scan_status == "on"){
    window.clearTimeout(ID);
    scan_status = "off";
  }
  station = sl.next();
  play_station(station.index);
}

// -- Previous Button --
function ButtonPrev(){
  preset_set_off();
  StopScan();
      
  // Turn off scan first
  if(scan_status == "on"){
    window.clearTimeout(ID);
    scan_status = "off";
  }
  station = sl.prev();
  play_station(station.index);
}

// -- Toggle Scan --
function ToggleScan(){
  preset_set_off();
  
  if(scan_status == "off"){
    scan_status = "on";
    //alert("scan => on");
    counter=0;          // no delay
    StationScan();
  }else if(scan_status == "on"){
    StopScan();
  }
}

// -- Stop Scan --
function StopScan(){
    if(scan_status == "on"){
    	//alert("scan => off");
	    scan_status = "off";    
   	    status_flash("Scanning stopped");
	    //window.clearTimeout(scan_ID);
     }
}


// --------------- Scan Function ------------------------
// Recursive every 1000 million second (1 second).
// On every recursion, update counter.
// When counter becomes 0, load the next station on list.
// ------------------------------------------------------
scan_status = "off";
var counter=0;
// -- Station Scan --
function StationScan(){
   
   if(scan_status == "off"){ 
     return; 
   }
   

   // -- If Delay=0, Change Station -- //
   //alert("counter="+counter);
   if(counter==0){
      play_station((curr_station+1)%sl.num_stations);
      counter=30; // reset delay to 30 seconds.
   }

   // -- Update Counter --
   status_show("Scanning: Next station in " + counter + " seconds");      
   counter--;
   scan_ID=window.setTimeout("StationScan();", 1000);
 }


// --------------- Player Object ------------------------
// Handling different radio station streaming formats.
// Not a object yet.  Just a place holder.
// Might use this for reloading.
// ------------------------------------------------------
function play_station(station_to_play){

	UpdateAdv();

	var StationList = frames["StationList"];    // If using Inline Frame
    
	//-- Unhighlight Prev Color --	
	row_select = "row"+curr_station;
	//alert("old row="+row_select);
	element=MyGetElementById(document, row_select);
	  
	//alert("here1");
	// ^ breaks function somehow
	element.style.background="#333333";
	
	// -- Highlight new --
	row_select = "row"+station_to_play;
	//alert("new row="+row_select);
	element=MyGetElementById(document, row_select);

	// ^ breaks function somehow
	element.style.background="#887700";
	//alert("here");

        // -- Set Cookie --
        createCookie("station_num",row_select,365);


	curr_station = station_to_play;
        curr_station = station_to_play;
  
  // -- Find Frame --
  
  var PlayerFrame = frames["Player"];    // If using Inline Frame
  //var PlayerFrame = parent["Player"];  // If using regular frame
 
 
  // -- REPLACE Method 1: Replace content --  
  PlayerFrame.location.replace("audio_stream.php?cmd=play&station_list="+list_name+"&station_num="+curr_station);

  //PlayerFrame.width          = '500px';
  //PlayerFrame.document.width = '400px'; //getter only      // IE: PErmission denied
  //PlayerFrame.document.style.width = '400px';            // FF: no property

    
  //PlayerFrame.style.width = '400px'; //no property         // IE: style is null or no object
  //PlayerFrame.style.height = '400px'; //no property

  currentfr=document.getElementById('iplayer');
  //currentfr.width        = '500px';
  
  //iplayer.width        = '400px';
  //iplayer.style.width  = '500px';                          // IE: null or not object
  
  //iplayer.document.body.scrollWidth  = '400px';              // IE: Permission denied
  //iplayer.document.body.offsetWidth  = '400px';
  //iplayer.document.body.clientWidth  = '400px';

  //iplayer.document.body.scrollHeight = '400px';              //IE: does not support this action 
  
  
  
  // -- REPLACE Method 2: Replace whole frame --  
  // Issues: Entire IE window flashss
  /*  
  write_it(
	     ' <iFRAME name="Player" src="audio_stream.php?cmd=play" '                  
	   + '  width=495 height=235 frameborder=0 style="padding:0;" scrolling=auto> ' +
	   + ' </iframe>' ,
	   'haha_player'
	   );
  */	   

  //debug&&alert("curr_station="+ curr_station);
    
  // -- forward Frame --
  //PlayerFrame.MyScroll(curr_station*35);
  //PlayerFrame.document.refresh();
  
  write_it(
      "Stream source: " + sl.station[curr_station].name + " "
      	+ '<a class="title" style="font-weight:normal;" href="javascript:visit_station();">'
      	+ '  (Visit station home)'
      	+ '</a>',
      'title'
  );
  
  
  // -- Modify Title --
  write_it(
      "Streaming source: " + sl.station[curr_station].name + " "
      	+ '<a class="title" style="font-weight:normal; color:#4444ff;" href="javascript:visit_station();">'
      	+ '  (Visit station home)'
      	+ '</a>',
      'title'
  );
  
  // -- Modify Sub-title --
  status_flash(
    "Now playing " + sl.station[curr_station].name + " "
  );

  // -- Change Button --
  write_it(
          '<input TYPE="image"                                      '
        + 'onClick     = " ButtonStop();"                           '
	+ 'src="images/button_stop_norm.jpg" alt="STOP"             '
	+ "onMouseOver = \"this.src='images/button_stop_over.jpg'\" "
	+ "onMouseOut  = \"this.src='images/button_stop_norm.jpg'\" "
	+ "onMouseDown = \"this.src='images/button_stop_down.jpg'\" "
	+ "onMouseUp   = \"this.src='images/button_stop_over.jpg'\" "
	+ '>                                                      ',
	'play');


  return(0); 
 
}
          
// -------------------------------------------------------------
// STOP PLAY
// -------------------------------------------------------------
function stop_station(){

  status_erase();
  
  PlayerFrame =parent["Player"]
  PlayerFrame.location.replace("audio_stream.php?station_list=New York&cmd=stop");

  // PlayerFrame.document.open();
  // PlayerFrame.document.write("<H1>Player Stopped.</H1>");
  // PlayerFrame.document.close();
  
  
  // Remove station link
  //  write_it(
  //  'Streaming Player:',
  //  'station_home_link');
  
  // 
  write_it(  
          '<input TYPE="image"                                      '
        + 'onClick     = " ButtonPlay();"                           '
	+ 'src="images/button_play_norm.jpg" alt="PLAY"             '
	+ "onMouseOver = \"this.src='images/button_play_over.jpg'\" "
	+ "onMouseOut  = \"this.src='images/button_play_norm.jpg'\" "
	+ "onMouseDown = \"this.src='images/button_play_down.jpg'\" "
	+ "onMouseUp   = \"this.src='images/button_play_over.jpg'\" "
	+ '>                                                      ',
	'play');

}

function visit_station(){
   window.open(sl.station[curr_station].home,'',
              'width =700,height =700,'+
              'status=1  ,toolbar=1  ,location=1,menubar=1,resizable=1,scrollbars=1,directories=1');
}





// -----------------------------------------------------------
// Status Display
// -----------------------------------------------------------
function status_flash(status_msg)
{
  status_show(status_msg);
  status_ID=setTimeout("status_erase();",5*1000);
}

function status_show(status_msg)
{
    tmp_txt = '<div class="status" id="status_background" 	        ' 
	    + '		style="         opacity: 0.5;                   '
	    + '                background-color: #000000;	        '
	    + '                          filter: alpha(opacity=50);"    ' 
	    + '>                                                        '
	    + '</div>							' 
	    + '<div class="status" id="status_foreground"		'
	    + '		style="opacity:1.0;">                           ' 
	    + 		status_msg					  
	    + '</div>							' ;

  write_it(tmp_txt, "status");
}

function status_erase()
{
  
  write_it("", "status");
}


// -----------------------------------------------------------
// Preset
// -----------------------------------------------------------
set_flag = 0;
function preset_set()
{    
    // -- Set Button --
    if(set_flag=='0'){
      set_flag = 1;
      status_show("Now select a preset number");
    }else{
      preset_set_off();
    }
}
function preset_set_off()
{
      set_flag = 0;
      status_erase();
}

function preset_num(button)
{
  // -- Set Station --
  if(set_flag=='1'){
    tmp_name = readCookie('city')+button;
    //alert('station =' + curr_station);
    createCookie(tmp_name, curr_station, 365);
    set_flag='0';  // Reset flag
    status_flash();

  }

  // -- Play Station --
  else{
    tmp_name = readCookie('city')+button;
    tmp_station = readCookie(tmp_name);
    //alert('station = ' + tmp_station);
    play_station( tmp_station );
  }
}


// -----------------------------------------------------------
// Info
// -----------------------------------------------------------
function tab_action(button)
{
	if(button=='main'){
		write_it('','info');
	}else{
		write_it(
			'<!-- <div style="background-color:black; opacity:0.5; padding:0; width:540; height:300;"> </div> -->'
			+'<iFRAME name="Player" src="tab_info.php?tab='+button+'"'
                        +'  class ="info_frame" '
                        +'  marginwidth ="0" '
                        +'  marginheight="0" '
                        +'  style       ="border: 1px #888888 solid;"'
			+'      scrolling=auto> '
			+'	   </iframe> '
		      ,'info'
		);
	}
	// ALLOWTRANSPARENCY="true" causes Real Player to be on top.
}


// -----------------------------------------------------------
// Info
// -----------------------------------------------------------
function UpdateAdv(){

   if(1){
     PlayerFrame =parent["Advertise"]
     PlayerFrame.location.replace("advertise.php");
   }

   if(0){
   write_it(
   '<script type="text/javascript"><!--'
   +'google_ad_client = "pub-8672795852420698";'
   +'/* 120x240, created 2/2/08 */'
   +'google_ad_slot = "8242011725";'
   +'google_ad_width = 120;'
   +'google_ad_height = 240;'
   +'//-->'
   +'</script>'
   +'<script type="text/javascript"'
   +'src="http://pagead2.googlesyndication.com/pagead/show_ads.js">'
   +'</script>'
   ,'advertise_pane');
   }   

   if(0){

   write_it(
       '<iFRAME name ="Player"  '
       +'        src  ="advertise.php"  '
       +'        id   =iplayer  '
       +'        style="background-color:#ffffff; border: 0px red solid;"  '
	+'           width=120  '
	+'           height=600  '
	+'           frameborder=0  '
	+'           style="padding:0;"  '
	+'           scrolling=auto>  '
	+'   </iframe>  '
       ,'advertise_pane');
       
    }


}