var nextClipId = "0";
var adPlayed = false;

function Ad( pPrerollFormat, pAfterFunction )
{
	this.PrerollVideo = null; // a Video object
	this.BigBox = ""; // the URL of the ad javascript that will serve a Big Box ad unit
	this.StartImageTracker = ""; // the URL of an image to serve when the preroll video starts
	this.EndImageTracker = ""; // the URL of an image to serve when the preroll video ends
	this.PrerollFormat = pPrerollFormat; // the format of the PrerollVideo
	
	this.AfterFunction = pAfterFunction; // the function to call after an ad is loaded
	this.IsLoaded = false;
	this.IsDestroyed = false;
	
	this.LoaderTag = null;
	
	//this.AdIndex = Ad.SetInstance( this );
	return this.AdIndex;
	
}
Ad.DARTSite = "video.ctv";

Ad.DARTVideoTag = function( pVideo )
{
    return "default;permalink=;clip=" + pVideo.ClipId;    
}


Ad.GetPreroll = function()
{
	var AdCall = "http://ad.doubleclick.net/pfadx/" + Ad.DARTSite + "/";
	
	//var UpcomingVideo = Playlist.GetInstance().FindNext();
	
    AdCall += Ad.DARTVideoTag( "test" );
    
    AdCall += ";sz=1x1";
    
    AdCall += ";ord=" + Math.round( 10000000 * Math.random() );
       
	var AdScript = document.createElement("script");
	AdScript.src = AdCall + ";format=flv";
	
	AdScript.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild( AdScript );
	
}
Ad.Load = function( pAdInfo )
{
    //alert(pAdInfo.Preroll);
    var flashMovie=getFlashMovieObject("mymovie");
    flashMovie.SetAd(pAdInfo.Preroll);
    
    //flashMovie.PlayUrl(pAdInfo.Preroll);
    //pAdInfo.StartPlayImage
    //pAdInfo.EndPlayImage
  
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

function Video( pVideoInfo )
{
	this.Title = "";
	this.Description = "";
	this.Thumbnail = "";
	this.EpisodeThumbnail = "";
	this.Format = null;	
	this.IsAd = false;  // Is this Video an Ad?
	this.Url = null;  // URL of the Video stream or file (for progressive downloading)
	this.BugUrl = "";  // URL of the video bug (image in the bottom-right corner)
	this.ClipId = null;  // The unique PIPE Clip Id of the video
	this.Duration = -1;  // The duration of the video
	this.Permalink = null;
	this.EpisodePermalink = null;
	this.IsCanadaOnly = false;

	this.SiteMap = null;
	this.SetInfo( pVideoInfo );
	this.OnStart = null;  // Function that is called when the Video is played
    this.OnEnd = null;  // Function that is called when the Video is stopped
	this.AfterRemoteLoad = null;  // Function that is called when the Video has finished loading remotely
	this.IsInErrorState = false;  // Is this Video object in an error state?
}


Video.Load = function( pVideoLoadInfo )
{
    
    //alert(pVideoLoadInfo.url);
    //PlayUrl
    
    
    var flashMovie=getFlashMovieObject("mymovie");
    flashMovie.PlayUrl(pVideoLoadInfo.url);
    //alert("LOAD STAGE");    
}

function playVideoFLV()
{
    //alert(clip_id);    
    //pUrlToPassClipId = "http://esi.ctv.ca/datafeed/flv/urlgenjs.aspx?vid=";

    //var VideoRemoteUrlScript = document.createElement("script");
    //VideoRemoteUrlScript.src = pUrlToPassClipId + nextClipId;// + "&random=" + Math.round( 10000000 * Math.random() );
    //VideoRemoteUrlScript.type = "text/javascript";

    //document.getElementsByTagName("head")[0].appendChild( VideoRemoteUrlScript );
    if (nextClipId != "-1")
    {        
        playVideoClipID(nextClipId);
        //clicked on the image
        if (currentPos == -1)
        {            
            findNewSelection(1);
        }
        else
        {
            nextClipId = -1;
        }
    }
    else
    {
        //for next video
        controlVideo(4);
    }
}

function playVideoClipID(clip_id_new)
{
    if (adPlayed == false)
    {
        nextClipId = clip_id_new;
        Ad.GetPreroll();
        adPlayed = true;
    }
    else
    {
        pUrlToPassClipId = "http://esi.ctv.ca/datafeed/flv/urlgenjs.aspx?vid=";
        
        var VideoRemoteUrlScript = document.createElement("script");
        VideoRemoteUrlScript.src = pUrlToPassClipId + clip_id_new;// + "&random=" + Math.round( 10000000 * Math.random() );
        VideoRemoteUrlScript.type = "text/javascript";

        document.getElementsByTagName("head")[0].appendChild( VideoRemoteUrlScript );
    }
}

