
var ttt,ttt2;
var cvol=100;

var adju7=400;

var sta=0;

var vc=0; var vn=1; var ava;

function setCookie2(c_name,value)
{
  var expiredays=999;
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie2(c_name)
    {
      try {
      if (document.cookie.length>0)
      {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
          c_start=c_start + c_name.length+1;
          c_end=document.cookie.indexOf(";",c_start);
          if (c_end==-1) c_end=document.cookie.length;
          cook=unescape(document.cookie.substring(c_start,c_end));
          return cook;
        }
      }
      } catch (ex) {}
      return ''; //100
    }


  var vidp='';
  var vids=0;
  var vidstop=0;
  //var vidstart=0;
  var mini=false;
        // Create the event tracking object
        var eventLabel;
	var nowPlaying;



function setMini() {
  mini=true;
}


function setMiniOff() {
  mini=false;
}


        function updateHTML(elmId, value) {
try {
          ///document.getElementById(elmId).innerHTML = value;
if (mini==false) adju((value-vids)*adju7/getDuration2()); //700
} catch (ex) {}
        }


        function setytplayerState(newState) {
          ///updateHTML("playerstate", newState);
	  ///if (translateYTPState(newState) == 'ended' && parseInt(getCurrentTime()) > 0) { 
            ///recordEnd(eventLabel,parseInt(getCurrentTime()));
          ///}
        }


        function onYouTubePlayerReady(playerId) {

          ytplayer = document.getElementById("myytplayer");

          setInterval(updateytplayerInfo, 2500); //1000
          ///updateytplayerInfo();
          ytplayer.addEventListener("onStateChange", "onytplayerStateChange");

//alert(playlist);

if (mini==false) {

vids=0; vidstop=0; rstart=0;

  ava=playlist.split('@');
vn=ava.length-1;


vc=0;

for (jj=0;jj<ava.length;jj++) {
  if (ava[jj]==vidp) {vc=jj;}
}

}

//vn=1;
for (j=vc;j<vc+1;j++) {

if (mini==false) {
  vidp=ava[j]; vidp=vidp.replace("&amp;","&"); vido=vidp;
  cc=vidp.indexOf("&start=");
  cc88=999;

  if (cc>0) {
    cc2=vidp.indexOf("&stop=");
    if (cc2>0) {
      cc88=cc2;
    }
    try {
      vids=parseInt(vido.substring(cc+7,cc88));
    } catch (ex5566) {}
    rstart=vids;
  }

  cc2=vidp.indexOf("&stop=");
  if (cc2>0) {
    vidstop=vido.substring(cc2+6,999);
  }

  if (cc>0) {vidp=vidp.substring(0,cc);}

}

//alert(vidp+" "+vids+" "+vidstop);

          setVolume(0);
          startNewVideo(vidp,vids); //('fPuUHtoRBUE');
          setVolume(0);
}



//if (CURID==1338) alert(vidstop);

updateytplayerInfo();
	}


function playNext() {
//for (j=0;j<1;j++) 
{

try {
  stop();
} catch (ex7987) {}
  vc++

vids=0; vidstop=0;

  vidp=ava[vc]; vidp=vidp.replace("&amp;","&"); vido=vidp; vido2=vidp;
  cc=vidp.indexOf("&start=");
  cc88=999;

  if (cc>0) {
    cc2=vidp.indexOf("&stop=");
    if (cc2>0) {
      cc88=cc2;
    }
    try {
      vids=parseInt(vido.substring(cc+7,cc88));
    } catch (ex5566) {}
    //rstart=vids;
  }

  cc2=vidp.indexOf("&stop=");
  if (cc2>0) {
    vidstop=vido.substring(cc2+6,999);
  }

  if (cc>0) {vidp=vidp.substring(0,cc);}

//alert(vidp+" "+vids+" "+vidstop);

          setVolume(0);
          startNewVideo(vidp,vids); //('fPuUHtoRBUE');
          setVolume(0);
//updateytplayerInfo();
t2=getTitle(CURID,vidp);
setTitle(t2);
}
}


function goVideo() {
  setVolume(0);
  startNewVideo(vidp,vids); //('fPuUHtoRBUE');
  setVolume(0);
}


        function onytplayerStateChange(newState) {
          setytplayerState(newState);

if (newState==1) {
  setVolume(0);
  fadeVolUp();
  try {
    //showCont();
    if (mini==false) document.getElementById('cont').style.visibility='';
  } catch (exx) {}
}

//alert(newState);
if (newState==0) {
  //alert("end");
  try { //// was out, first 2 lines
    fadeVolDown();
    if (mini==false) setTimeout('closeP()',500); //700
if (mini) {stopv1(idd,idd1,aid);}
  } catch (exx) {}
}
        }


        function translateYTPState(state) {

          switch (state) {
            case -1: return "unstarted";
            case 0 : return "ended";
            case 1 : return "playing";
            case 2 : return "paused";
            case 3 : return "buffering";
            case 5 : return "video cued";
          }
          return;
        }


function abc() {
  //sept 2011  if (mini==false) ytplayer.setPlaybackQuality("large");
  //alert('abc');
}


        function onPlayerError(errorCode) {
          alert("An error occurred: "+ errorCode);
        }


var zzz=0;


        function updateytplayerInfo() {
try {
if (vidstop==0) vidstop=getDuration();
if (vidstop==undefined) vidstop=getDuration();
///if ((vidstop>0) && (getCurrentTime()>=vidstop-2)) {  ///-3

if (
(getPlayerState()==0) && // ||
( 
(getDuration2()>10) && (getCurrentTime()>=vids+getDuration2()-2)) 
)
{
  fadeVolDown();
  /////////////////////////////////////////////////////////////////////////
setTimeout('closeP()',600); //1000
}

//if (getCurrentTime()>vids+getDuration2()-3) {
//  fadeVolDown();
//}
//////////////

//////////////
if ((zzz==0) || (zzz>=5))
{
/////////////
zzz=0;
          //updateHTML("bytesloaded", getBytesLoaded());
          //updateHTML("bytestotal", getBytesTotal());
          //updateHTML("videoduration", getDuration());
          if (mini==false) updateHTML("videotime", getCurrentTime());
          //updateHTML("startbytes", getStartBytes());
          //updateHTML("volume", getVolume());
//////////////
}
zzz++;
} catch (ex) {}
        }


        // functions for the api calls
        function loadNewVideo(id, startSeconds) { //alert("load "+id);
          if (ytplayer) {
            setVolume(0);
//alert(ytplayer.getAvailableQualityLevels()[1]);

if (mini) {
  ytplayer.loadVideoById(id, startSeconds, 'small');
} else 
{



if (id=='j31Q-iaDuxI&start=8') {
  ytplayer.loadVideoById(id, startSeconds,'large');
  return;
}

if (id=='j31Q-iaDuxI&start=9') {
  ytplayer.loadVideoById(id, startSeconds,'medium');
  return;
}

if ((getCookie2('vq')=="HQ") || (id=='j31Q-iaDuxI&start=8')) {
  ytplayer.loadVideoById(id, startSeconds,'large');
  document.getElementById("hq").style.color="white";
  document.getElementById("lq").style.color="#aaaaaa";
} else {

{
    ytplayer.loadVideoById(id, startSeconds);//,'hd720');
  }
  document.getElementById("lq").style.color="white";
  document.getElementById("hq").style.color="#aaaaaa";
}

}

	  }
        }


        function cueNewVideo(id, startSeconds) { //alert("cue "+id);
          if (ytplayer) {
zzz=0;

if (id=='j31Q-iaDuxI&start=8') {
  ytplayer.cueVideoById(id, startSeconds,'hd720');
  return;
}

if (id=='j31Q-iaDuxI&start=9') {
  ytplayer.cueVideoById(id, startSeconds,'medium');
  return;
}
            
//alert(ytplayer.getAvailableQualityLevels()[1]);

if ((getCookie2('vq')=="HQ")) { /// || ((id=='j31Q-iaDuxI&start=8'))) {
ytplayer.cueVideoById(id, startSeconds,'large');
document.getElementById("hq").style.color="white";
document.getElementById("lq").style.color="#aaaaaa";
} else {
/***
if (id=='j31Q-iaDuxI&start=8') {
ytplayer.cueVideoById(id, startSeconds,'small');//,'
} else
***/
/// {
ytplayer.cueVideoById(id, startSeconds);//,'hd720');
/// }
document.getElementById("lq").style.color="white";
document.getElementById("hq").style.color="#aaaaaa";
}
          }
        }


	function startNewVideo (a,b) {
/*
	  // if there is a current video playing, record the end
	  var oldTime = parseInt(getCurrentTime());
          var oldEventLabel = eventLabel;

          if (oldTime > 0) { 
	    recordEnd(oldEventLabel, oldTime);
	  }	
*/
	  ///eventLabel = document.getElementById('loadvideoid').options[document.getElementById('loadvideoid').selectedIndex].text;

zzz=0;

	  loadNewVideo(a, b);
	}


        function recordEnd(l,t) {

        }


        function play() {
          if (ytplayer) {
            ytplayer.playVideo();
          }
        }


        function playvid() {
          if (ytplayer) {
            ytplayer.playVideo();
          }
        }


        function pause() {
          if (ytplayer) {
            ytplayer.setVolume(0);
            ytplayer.pauseVideo();
	    }
        }



        function stop() {
          if (ytplayer) {
            ytplayer.stopVideo();
          }
        }


        function getPlayerState() {
          if (ytplayer) {
            return ytplayer.getPlayerState();
          }
        }


function getDuration2() {
  try {
    if (vidstop==0) vidstop=getDuration();
    if (vidstop==undefined) vidstop=getDuration();
    if (vids==undefined) vids=0;
  } catch (ex) {}
  try {
    return vidstop-vids;
  } catch (ex2) {}
  return parseInt(vidstop)-parseInt(vids);
}


function seek(value) {
  try {
    if (value<0) value=0;
    x=value*(0+getDuration2())/adju7; //700
    nx=Math.round(x); //parseInt(x)
    if (x<0) x=0;

    np=nx+vids; //parseInt(vids)

    mute();
    seekTo(np);
    if (mini==false) updateHTML("videotime", getCurrentTime());
    updateytplayerInfo();
    setVolume(1);
    unMute();
    setVolume(1);
    fadeVolUp();
  } catch (ex) {}
}


function seekout(value) {
  try {
    if (value<0) value=0;
    x=value*(0+getDuration2())/adju7; //700
    nx=Math.round(x); //parseInt(x)
    if (x<0) x=0;

    np=nx+vids; //parseInt(vids)

    mute();
    seekTo(np);
    if (mini==false) updateHTML("videotime", getCurrentTime());

    if (ytplayer) ytplayer.seekTo(seconds, false);

  } catch (ex) {}
}


        function seekTo(seconds) {
try {
          if (ytplayer) {
            ytplayer.seekTo(seconds, true);
          }
 } catch (ex) {}
        }


        function getBytesLoaded() {
          if (ytplayer) {
            return ytplayer.getVideoBytesLoaded();
          }
        }


        function getBytesTotal() {
          if (ytplayer) {
            return ytplayer.getVideoBytesTotal();
          }
        }


        function getCurrentTime() {
          if (ytplayer) {
if (getPlayerState()!=1) return 0;
            return ytplayer.getCurrentTime();
          } else return 0;
        }


        function getDuration() {
          if (ytplayer) {
            return ytplayer.getDuration();
          } 
        }


        function getStartBytes() {
          if (ytplayer) {
            return ytplayer.getVideoStartBytes();
          }
        }

        function mute() {
          if (ytplayer) {
            ytplayer.mute();
/*            ytpEventTracker._trackEvent("Mute",eventLabel);
*/
          }
        }

        function unMute() {
          if (ytplayer) {
            ytplayer.unMute();
/*            ytpEventTracker._trackEvent("Unmute",eventLabel);
*/
          }
        }
        
        function getEmbedCode() {
          alert(ytplayer.getVideoEmbedCode());
/*	  ytpEventTracker._trackEvent("Get Embed Code", eventLabel); 
*/
        }

        function getVideoUrl() {
          alert(ytplayer.getVideoUrl());
/*          ytpEventTracker._trackEvent("Get Video URL", eventLabel); 
*/
	}
        
        function setVolume(newVolume) {
          if (ytplayer) {
            ytplayer.setVolume(newVolume);
          }
        }

function fadeVolUp() {  try {clearTimeout(ttt2);} catch (exxx) {}
  vol=getVolume();
  if (vol<cvol) {  //100) {
    vol=vol+6; if (vol>cvol) vol=cvol; //100;
    setVolume(vol);
    if (vol<100) ttt=setTimeout('fadeVolUp()',120);
  }
}

function fadeVolDown() {  try {clearTimeout(ttt);} catch (exxx) {}
  vol=getVolume();
  if (vol>0) 
  {
    ///vol=vol-8; 
    vol=3*vol/4;
    if (vol<=2) vol=0;
    setVolume(vol);
    if (vol>0) ttt2=setTimeout('fadeVolDown()',75);
  }
}

        function getVolume() {
          if (ytplayer) {
            return ytplayer.getVolume();
          }
        }

        function clearVideo() {
          if (ytplayer) {
            ytplayer.clearVideo();
          }
        }
        

function closePlayer() {
  try {
    stop();
    ytplayer=null;
  } catch (exx) {}
}

	function lq() {
if (mini==false) {
fadeVolDown();
setTimeout('lq2()',900);
}
	}

	function lq2() {
sta=getCurrentTime();
  /////ytplayer.setPlaybackQuality('medium');
   //alert('LQ');
      document.getElementById("lq").style.color="white";
document.getElementById("hq").style.color="#aaaaaa";
setCookie2('vq','LQ');    closeIt(); ///play8z(vido,NT,sta,vidstop); //
play123(vido);
//if (ytplayer) 
{
///  setTimeout("seekit()",4000);
}
}

	function hq() {
if (mini==false) {
fadeVolDown();
setTimeout('hq2()',900);
}
	}

function hq2() {
sta=getCurrentTime();
  /////ytplayer.setPlaybackQuality('large');
    document.getElementById("hq").style.color="white";
document.getElementById("lq").style.color="#aaaaaa";
setCookie2('vq','HQ');   closeIt(); ///play8z(vido,NT,sta,vidstop); //
play123(vido);
//if (ytplayer) 
{
///  setTimeout("seekit()",4000);
}
}


function seekit() {  //alert("seekit");
  //if (ytplayer) 
  {
    ytplayer.seekTo(sta, false);
    updateytplayerInfo();
  }
}

    
