var currenttime=0;
var buffershow=1;
var imageflag=1;

var otvobject="OTVCTRL.otvctrlCtrl.1";

var otvobjectflag=CheckActiveXObject(otvobject);

$("otvctrldiv").innerHTML=GetOTVCtrl(moviewidth,movieheight);

if(otvobjectflag)
{
  setTimeout('Start()',1000);
}
else
{
   $("otvctrldiv").innerHTML=GetOTVDown(moviewidth,movieheight);
}

function GetOTVCtrl(w,h){
	var str='<div id="otvplayerdiv" width="'+w+'" height="'+h+'" style="position:relative; border:1px solid #B6C4D1;" >'
	+'<object classid="clsid:F2C5567C-3FE1-4B0B-8765-3B85E6295A81" width="'+w+'" height="'+h+'" id="otvplayer" align="middle">'
	+'<embed></embed></object></div>';					
	return str;
}
function GetOTVDown(w,h){
	var str='<div id=myOTVDownload style="width:'+w+'px;height:'+h+'px;overflow:hidden;BACKGROUND: #000000;" align=center>'
        +'<div style="margin-top:120px;"></div><div style="color:#FFFFFF;font-size:18px;"><b style="margin:20px;">'
        +'缺少OTV播放软件,请立即<a href="http://www.ywnt.net/otv/客户端/otv_installer.exe" target=_blank style="color:#FF0000;">下载</a>安装</b></div></div>';					
	return str;
}

function $(s)
{   
	return document.getElementById(s);
}

function CheckActiveXObject(sName)
{
    try{
		  var player = new ActiveXObject(sName);
		  return true;
	   }
	catch(e){
		return false;
	   }
}

function FormatTime(lTime)
{
  ts=parseInt(lTime/1000%60);
  tm=parseInt(lTime/1000/60%60);
  th=parseInt(lTime/1000/60/60%60);
  
  if(ts<10)strts="0"+ts;
  else strts=ts;
  if(tm<10)strtm="0"+tm;
  else strtm=tm;
  if(th<10)strth="0"+th;
  else strth=th;
  
  return  strth+":"+strtm+":"+strts;
}

var isstart=0,PlayState=0,IdTime=0,IdTime1=0;

function Start()
{
   flashflag=1;
   
   if(isstart==0)
   {
     nCount=0;
       
     SetAutoPlay(1);

     isstart=OTVStart();

     mediabuffestatus.innerText="正在搜索视频资源...";
     
     if(IdTime>0)clearInterval(IdTime);
     
     IdTime=setInterval(OnPlay,100);
     
     if(IdTime1>0)clearInterval(IdTime1);
     
     IdTime1=setInterval(OnPlay1,100);
     
     isstart=1;

     SetSize(moviewidth,movieheight);

     SetSoundBarPos();

   }
   else
   {
      //OAFilterState: 0 State_Stopped， 1 State_Paused ，2 State_Running
      
      iState=GetPlayState();
      
      if(iState==0 ||iState==1 )
      {
          MediaPlay();
      }
      else if(iState==2)
      {
          MediaPause();
      }
   }
   
}


function Stop()
{
  clearInterval(IdTime);
  clearInterval(IdTime1);
  OrtvActiveX1.Stop();
  
  isstart=0;
}


var PlayState=0;
var nCount=0;
var per99=0;
var ichange=0;
var strchage="...  ";
var seektime=0;
function OnPlay()
{
   ichange++;

   if(Math.round(ichange/10)%4==0)
  {
    strchage="     ";
  }
  else if((ichange/10)%4==1)
  {
    strchage=".     ";
  } 
   else if((ichange/10)%4==2)
  {
    strchage="..   ";
  } 
   else if((ichange/10)%4==3)
  {
    strchage="...  ";
  }
/*
  try
  {
      nCount++;
  
      if(nCount<100)
      {
         var waittime=Math.round((100-nCount)/10);
        
         if(waittime>0)
        {
           mediabuffestatus.innerText="请稍等"+Math.round((100-nCount)/10)+"秒,广告后精彩马上开始"+strchage;
         }
        else
       {
          mediabuffestatus.innerText="正在准备,马上开始"+strchage;
       }
         
         return;
      }
      else if(nCount==40)
      {
         MediaPlay();

         return;
      }
      else if(nCount==100)
      {
         //ShowActiveX(1);

         MediaPlay();
       
         buffershow=1;
      }

      
      
  }
  catch(err)
  {
  
  }
*/
  PlayState=GetPlayState();
  
  mediatimestatus.innerText=FormatTime(GetCurrentTime())+"/"+FormatTime(GetTotalTime());

  if(PlayState==2)  
  {
        if(imageflag==0)
        {     
           mediactrls.src="Play/zstop1.jpg";  

           imageflag=1;
         }
     
  }
  else
  {
      if(imageflag==1)
      { 
        imageflag=0;
    
        mediactrls.src="Play/play_03.jpg";
      }
  }
   
   if(currenttime>=0 && PlayState==2)
   {          
       if(currenttime==GetCurrentTime() && GetCurrentTime()+2*1000<GetTotalTime())
       {
          mediabuffestatus.innerText="正在缓冲"+GetBufferPercent()+"%"+strchage;
          buffershow=0;
          seektime=0;
       }
       else
       {
         seektime=1000;

            if(buffershow==0)
          {
             

             buffershow=1;
             
             //ShowActiveX(1);
             
             mediabuffestatus.innerText="缓冲100%";
             
          }
          else
          {
            bitrate=GetBitrate();
            
            if(bitrate>0)
            {
                mediabuffestatus.innerText="当前节目《"+movietitle+"》"+",比特率"+bitrate+"Kbps";
            }
            else
            {
             mediabuffestatus.innerText="当前节目《"+movietitle+"》";
             }
          }
          
       }
       
   }
   else
   {
  
         if(currenttime==0 && GetCurrentTime()+2*1000<GetTotalTime())
         {
              var percent=GetBufferPercent();

             if(percent==99 && per99>30)
             {
                 per99++;

                 mediabuffestatus.innerText="正在缓冲"+percent+"%,如果不能观看,请刷新";
             }
             else
            {   
                 per99=0;

                 mediabuffestatus.innerText="正在缓冲"+percent+"%";
             }

             //ShowActiveX(0);
            
             //buffershow=0;
         }
         else 
         {
             //ShowActiveX(1);
             
             buffershow=1;
         }

   }
      
   currenttime=GetCurrentTime();
   
}

function OnPlay1()
{
     seektime++;

    if(dragMe==false)SetBarPos();
   
   // if(dragMe1==false)SetSoundBarPos();

}


var sPosition;
var showPerc=100;
document.onmousedown=dragLayer
document.onmouseup=dragLayerUp;


function SeekBar(allwidth,pos)
{
  
  if(isstart==1)
  {     
     seekpos=(100*pos/(allwidth));
    
     if(seekpos>0 && seekpos<100)
     {
        SeekPosition(seekpos);
     }
  }
}
function SeekBar1(allwidth,pos)
{
  if(isstart==1)
  {
     seekpos1=(100*pos/(allwidth-8));
     
     if(seekpos1>0 )
     {
        if(seekpos1>100)seekpos1=100;
        
        SetSoundPos(seekpos1);
     }
  }
}

var dragMe=false, kObj, xPos,direction,seekwidth,seekpos;
var dragMe1=false, kObj1, xPos1,direction1,seekwidth1,seekpos1;
var soundwidth=130,temp=20;

function moveLayer()
{
   if (event.button==1 && dragMe)
  {
     frameWidth=moviewidth-30;
     
     oldX = kObj.style.pixelLeft; 

     kObj.style.pixelLeft=temp+event.clientX-xPos; 
    
     if (kObj.style.pixelLeft > oldX)
    {
         direction="right";
    }
    else 
    {
      direction="left";
    }  

     if (kObj.style.pixelLeft < 1 && direction=="left")
     {
         kObj.style.pixelLeft=1; direction="right";
     }

    if (kObj.style.pixelLeft > frameWidth&& direction=="right") 
    {
        kObj.style.pixelLeft=frameWidth; 
        
        direction="left";
    }

        otvinnerLayer.style.width=kObj.style.pixelLeft;
        
        sPosition=kObj.style.pixelLeft; 
        
        showPerc =sPosition-2;  
        
        seekwidth=frameWidth+30;
        
        seekpos=sPosition-otvouterLayer.style.left;

       return false;
    }
    
   if (event.button==1 && dragMe1)
  {
     frameWidth1=soundwidth;//parseInt(otvsoundouterLayer.style.width);sound
     
     oldX1 = kObj1.style.pixelLeft; 

     kObj1.style.pixelLeft=temp1+event.clientX-xPos1; 
    
     if (kObj1.style.pixelLeft > oldX1)
    {
         direction1="right";
    }
    else 
    {
        direction1="left";
    }  

     if (kObj1.style.pixelLeft < 0 && direction1=="left")
     {
         kObj1.style.pixelLeft=0; direction1="right";
     }

    if (kObj1.style.pixelLeft > frameWidth1-8 && direction1=="right") 
    {
        kObj1.style.pixelLeft=frameWidth1-8; 
        
        direction1="left";
    }

        
        sPosition1=kObj1.style.pixelLeft; 
        
        showPerc1 =sPosition1-2;  
        
        seekwidth1=frameWidth1;
        
        seekpos1=sPosition1-otvsoundouterLayer.style.left;
  
       return false;
    }
}

function dragLayer()
{
    if (!document.all) return;

    if (event.srcElement.className=="otvdraglayer")
    {
        if(seektime<8*10)return;

        seektime=0;

        dragMe=true;
        
        kObj=event.srcElement;
       
        temp=kObj.style.pixelLeft; 
        
        xPos=event.clientX; 
 
        document.onmousemove=moveLayer; 
    }
    
     if (event.srcElement.className=="otvsounddraglayer")
    {
        dragMe1=true;
        
        kObj1=event.srcElement;
       
        temp1=kObj1.style.pixelLeft; 
        
        xPos1=event.clientX; 
 
        document.onmousemove=moveLayer; 
    }
}

function dragLayerUp()
{
   if(dragMe)
   {
     SeekBar(seekwidth,seekpos);
    
     dragMe=false;
    }
    
    if(dragMe1)
    {
     SeekBar1(seekwidth1,seekpos1);
    
     dragMe1=false;
    }
   
  
}

function SetBarPos()
{
  if(isstart==1)
  {
    frameWidth=moviewidth;
   
    otvdrag.style.pixelLeft=(frameWidth)*GetPosition()/100;
   
    otvinnerLayer.style.width=otvdrag.style.pixelLeft;
   }
      
}


function SetSoundBarPos()
{
  if(isstart==1)
  {
     frameWidth1=soundwidth;
   
     otvsounddrag.style.pixelLeft=(frameWidth1-8)*GetSoundPos()/100;
     
     if(otvsoundouterLayer.style.pixelLeft+16>otvsounddrag.style.pixelLeft)otvsounddrag.style.pixelLeft=otvsoundouterLayer.style.pixelLeft+16;
   }
 
}

