// JavaScript Document

// Flash Player 

function MainPlayer(DivName)
{
   this.name=DivName;
   this.InterfaceName = this.name+"int";
   this.flashvars = {};
   this.params = {wmode: "transparent"};
   this.attributes = { id: this.InterfaceName , name: this.InterfaceName };

   swfobject.embedSWF( "songPlayer.swf", this.name, "1","1","9", false, this.flashvars, this.params, this.attributes);


   this.oldStat=0;
   this.timer=null;
   this.stat;
   this.ref=null;
   var self=this;

   this.CallBackFunction=null;
   
   this.RegisterCallBack=function(func)
   {
//      alert("Set CallBack="+func);
      this.CallBackFunction=func;
   }

   this.GetMovie=function()
   {
     a= swfobject.getObjectById(this.InterfaceName);
//     alert(a.id);
     return a;
   }

   this.i=0;
   this.check=function()
   {
     if(self.CallBackFunction)
     {
       self.ref = self.GetMovie();
       self.stat=self.ref.GetPlayerStatus();
       self.timer =  window.setTimeout(self.check, 100); 
       if(self.stat != self.oldStat)
       {
           self.CallBackFunction(self.stat);
       }
       self.oldStat=self.stat;
       if(!self.stat)
       {
         if(self.i>10)
         {
           if(self.timer)
           {
             window.clearTimeout(self.timer); 
           }
           self.timer=null;
           self.i=0;
         }
         else
         {
           self.i++;
         }
       }
       else
       {
           self.i=0;
       }
     }
     else
     {
//       alert("No CallBack");
     }
   }

  this.Play=function(str) 
  {
    this.ref = this.GetMovie();
    if(this.ref)
    {
      this.timer =  window.setTimeout(self.check, 100); 
      this.ref.soundPlay(str);
    }
    else
    {
       alert("Can not find swfobject '" +this.InterfaceName+ "'");
    }
  }
  
  this.isPlay=function() 
  {
    this.ref = this.GetMovie();
    if(this.ref)
    {
      return this.ref.GetPlayerStatus();
    }
    else
    {
      alert("Can not find swfobject '" +this.InterfaceName+ "'");
      return 0;
    }
  }
  
  this.Stop=function() 
  {
    this.oldStat=0;
    this.ref = this.GetMovie();
    if(this.ref)
    {
      if(this.timer)
      {
        window.clearTimeout(this.timer); 
      }
      this.ref.soundStop();
    }
    else
    {
       alert("Can not find swfobject '" +this.InterfaceName+ "'");
    }
  }
}

// ===============================================================
// Logical Player
// ===============================================================
  
function Player(main_id)
{
  this.isPlaying = 0 ;
  this.ref=main_id;
  this.PlayIMG=null;

  this.PlayerIdleImg  = '';
  this.PlayerLoadImg  = '';
  this.PlayerPlayImg  = '';
  var self=this;

  this.SetPlayerImages=function(imgOn,imgLoad,imgOff)
  {
    this.PlayerIdleImg  = imgOn;
    this.PlayerLoadImg  = imgLoad;
    this.PlayerPlayImg  = imgOff;
  }

  this.stopMP3=function()
  {
    self.ref.RegisterCallBack(null);
    self.ref.Stop();

    if(self.PlayIMG)
    {
       self.PlayIMG.src=self.PlayerIdleImg;
    }
    self.PlayIMG=null;
    self.isPlaying = 0;
  }

  this.statLoading=function()
  {
    if(self.PlayIMG)
    {
       self.PlayIMG.src=self.PlayerLoadImg;
    }

  }
  this.statPlaying=function()
  {
    if(self.PlayIMG)
    {
       self.PlayIMG.src=self.PlayerPlayImg;
    }
  }

  this.playMP3=function (_this, fileName)
  {
    this.isPlaying = 1;

    if(_this)
    {
      tag=_this.tagName;
      if(tag=='A')
      {
        if(_this.getElementsByTagName('img'))
        {  
          this.PlayIMG=_this.getElementsByTagName('img')[0];
        }
      }
      else
      {
        if(tag=='IMG')
        {
          this.PlayIMG = _this;
        }
      }
    }
    if(this.PlayIMG)
    {
       this.PlayIMG.src=this.PlayerIdleImg;
    }
     this.ref.RegisterCallBack(self.StatusWasChahged);
     this.ref.Play(fileName);
  }
   
  this.stopAll=function()
  {
    self.stopMP3();
  }

  this.StatusWasChahged=function(stat)
  {
//    alert("New status="+stat);
    switch(stat)
    {
      case 0:
          self.stopMP3();
        break;
      case 1:
          self.statLoading();
        break;
      case 2:
          self.statPlaying();
        break;
    }
  }
}
// ===============================================================
// End Logical Player
// ===============================================================
   
function getVariant() 
{
 theGroup = document.getElementsByName("variant");

 for (i=0; i< theGroup.length; i++)
 {
     if (theGroup[i].checked) 
     {
        return theGroup[i].value;
        break;
     }
  }
  return 0;
}

