var bcCDNURL = "http://admin.brightcove.com";

var browserVersion = 0;
if (navigator.appVersion.indexOf("MSIE") != -1) {
  var temp = navigator.appVersion.split("MSIE")
  browserVersion = parseFloat(temp[1])
}

var bcIsDMCompatable = false;
if (browserVersion >= 5.5) {
  bcIsDMCompatable = true;
}
    
function downloadVideo(token, assetId, titleId, playerId, licenseType, entitlementId) {
  var queryString = "token="+encodeURIComponent(token)+"&assetId="+assetId+"&titleId="+titleId+"&playerId="+playerId+"&licenseType="+licenseType+"&entitlementId="+entitlementId;
  var problemLoading = loadHiddenURL(bcCDNURL+"/viewer/download/crossDomainTalker.html?" + queryString);
  
  if (problemLoading) {
    alert("Unable to download using this browser version.  Please upgrade your browser and try again.");
  }
}

var IFrameObj = null; // our IFrame object
function loadHiddenURL(url) {
  if (!document.createElement) {return true};
  var IFrameDoc;
  if (IFrameObj == null) {
    // create the IFrame and assign a reference to the
    // object to our global variable IFrameObj.
    // this will only happen the first time 
    // loadHiddenURL() is called
   try {
      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id','BCDMFrame');
      tempIFrame.style.border='0px';
      tempIFrame.style.width='0px';
      tempIFrame.style.height='0px';
      IFrameObj = document.body.appendChild(tempIFrame);
      
      if (document.frames) {
        // this is for IE5 Mac, because it will only
        // allow access to the document object
        // of the IFrame if we access it through
        // the document.frames array
        IFrameObj = document.frames['BCDMFrame'];
      }
    } catch(exception) {
      // This is for IE5 PC, which does not allow dynamic creation
      // and manipulation of an iframe object. Instead, we'll fake
      // it up by creating our own objects.
      iframeHTML='\<iframe id="BCDMFrame" style="';
      iframeHTML+='border:0px;';
      iframeHTML+='width:0px;';
      iframeHTML+='height:0px;';
      iframeHTML+='"><\/iframe>';
      document.body.innerHTML+=iframeHTML;
      IFrameObj = new Object();
      IFrameObj.document = new Object();
      IFrameObj.document.location = new Object();
      IFrameObj.document.location.iframe = document.getElementById('BCDMFrame');
      IFrameObj.document.location.replace = function(location) {
        this.iframe.src = location;
      }
    }
    if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
      // we have to give NS6 a fraction of a second
      // to recognize the new IFrame
      setTimeout("loadHiddenURL('"+url+"')",10);
      return false;
    }
  }
  
  try {
    var IFrameDoc;
    if (IFrameObj.contentDocument) {
      IFrameDoc = IFrameObj.contentDocument;
    } else if (IFrameObj.contentWindow) {
      IFrameDoc = IFrameObj.contentWindow.document;
    } else if (IFrameObj.document) {
      // For IE5
      IFrameDoc = IFrameObj.document;
    } else {
      return true;
    }
    
    IFrameDoc.location.replace(url);
    
  } catch (e) {
    try {
      document.getElementById('BCDMFrame').src = url;
    } catch(e) {
      return true;
    }
  }
  
  return false;
}

    
/*  
function isVideoDownloaded(callback, username, assetId) {
  
  if (bcIsDMCompatable && DownloadControl) {
    var jobStr = DownloadControl.GetAllJobs(username);
    var jobs = eval(jobStr);
    for (var i = 0; i < jobs.length; i++) {
      if (jobs[i].assetId == assetId) {
        callback(true);
        return;
      }
    }
  }
    
  callback(false);
}

function areVideosDownloaded(callback, username, assetArry) {
  var downloaded = new Array();
  for (var i = 0; i < assetArry.length; i++) {
    downloaded[i] = false;
  }

  if (bcIsDMCompatable && DownloadControl) {

    var jobStr = DownloadControl.GetAllJobs(username);
    var jobs = eval(jobStr);
    
    for (var i = 0; i < jobs.length; i++) {
      for (var j = 0; j < assetArry.length; j++) {
        if (jobs[i].assetId == assetArry[j]) {
          downloaded[j] = true;
        }
      }
    }

  } else {
    for (var i = 0; i < assetArry.length; i++) {
      assetArry[i] = false;
    }
  }

  callback(downloaded);
}
*/