var gbl_selectedClip;
//
// Function: clip
// Parameters: the many properties for a clip
// Returns: Nothing
// Purpose: Constructor for clips
//
function clip (clipId,pvdrEventId,marketId,exchangeId,providerId,applyDrm,showMarketData,showMarketDropdown,applyGeoBlock,clipSportId,leadInMinutes,durationMinutes,clipStartTimeAsGmtString,location,availableLocales,arrayMarketIds,arrayMarketDescs,arrayBBVMarketIds,clipDescription,subId,subDate,marketEventId,arrayMarketEventIds) {
  this.clipId = clipId;
  this.pvdrEventId = pvdrEventId;
  this.marketId = marketId;
  this.exchangeId = exchangeId;
  this.providerId = providerId;
  this.applyDrm = applyDrm;
  this.showMarketData = showMarketData;
  this.showMarketDropdown = showMarketDropdown;
  this.applyGeoBlock = applyGeoBlock;
  this.clipSportId = clipSportId;
  this.leadInMinutes = leadInMinutes;
  this.durationMinutes = durationMinutes;
  this.clipStartTime = new Date();
  this.clipStartTime.setTime(Date.parse(clipStartTimeAsGmtString));
  this.location = location;
  this.availableLocales = availableLocales;
  this.arrayMarketIds = arrayMarketIds;
  this.arrayMarketDescs = arrayMarketDescs;
  this.arrayBBVMarketIds = arrayBBVMarketIds;
  this.clipDescription = clipDescription;
  this.subId=subId;
  this.subDate=subDate;
  this.marketEventId=marketEventId;
  this.arrayMarketEventIds=arrayMarketEventIds;
}

//
// method to tell us if the stream is actually live now
//
clip.prototype.isLiveNow = function() {
  var result = false;
  var now = new Date();
  var streamStartTime = new Date(this.clipStartTime.valueOf());
  streamStartTime.setMinutes(streamStartTime.getMinutes() - this.leadInMinutes);
  var streamEndTime = new Date(this.clipStartTime.valueOf());
  streamEndTime.setMinutes(streamEndTime.getMinutes() + this.durationMinutes);
  if (streamStartTime < now && streamEndTime > now) {
    result = true;
  }
  return result;
}

//
// Method to tell us if the stream is an archive one
// i.e. one that occured in the past
//
clip.prototype.isArchiveClip = function() {
  var result = false;
  var now = new Date();
  var streamEndTime = new Date(this.clipStartTime.valueOf());
  streamEndTime.setMinutes(streamEndTime.getMinutes() + this.durationMinutes);
  if (streamEndTime < now) {
    result = true;
  }
  return result;
}
//
// Method to write out the clip as an html table row
//
clip.prototype.writeAsTableRow = function() {
  var extraStyle;
  if (this.showMarketData != "Y" && !this.isLiveNow()) {
    extraStyle = 'style="text-decoration:none;"';
  } else {
    extraStyle = 'style="cursor:pointer;"';
  }
  var outHtml ="<tr id='clipId_" + this.clipId + "_sportId_" + this.clipSportId + "' onClick='selectClip(\"" + this.clipId + "\")' class='" + ((this.rowNumber %2 ==0)?'TblRowLight':'TblRowDark') + "'>";
  outHtml +="  <td width='13'>";
  outHtml +='<img src="./images/liveVideo/icon_tv' + ((this.isLiveNow()) ? '_on' : '') + '.gif" width="13" height="13"/>';
  outHtml +="  </td>"
  outHtml +="  <td>";
  outHtml +="    <div class='TblBodyBlu' " + extraStyle + ">" + this.clipDescription + "</span><br/><span class='TblBodyGrey'>" + this.clipStartTime.toString().substring(0,(this.clipStartTime.toString().lastIndexOf(':'))) + "</div>";
  outHtml +="  </td>";
  outHtml +="</tr>";
  return outHtml;
}
//
// Function: isGeoblocked
// Parameters: none but users gbl_userCountry
// Returns: Nothing
// Purpose: Determine if clip is valid for user from country X
//
clip.prototype.isGeoblocked = function() {
  var geoBlockRow = false;
  //
  // If the event has inclusive type geoblocking enabled and
  // if the cookie value for the users country has been set then,
  // if the clip is not allowed for the country the user is in,
  // hide the clip
  //
  if (this.applyGeoBlock == "Y" && gbl_userCountry != "undefined" && gbl_userCountry != null && gbl_userCountry != "" ) {
    var foundUserCountryInList = false;
    for (var jj=0; jj < this.availableLocales.length ; jj++ ) {
      if(this.availableLocales[jj].toUpperCase() == gbl_userCountry.toUpperCase()) {
        foundUserCountryInList = true;
      }
    }
    geoBlockRow = !foundUserCountryInList;
  }
  //
  // If the event has exclusive type geoblocking enabled and
  // if the cookie value for the users country has been set then,
  // if the users country is in the exclude list,
  // hide the clip
  //
  if (!geoBlockRow) {
    if (this.applyGeoBlock == "X" && gbl_userCountry != "undefined" && gbl_userCountry != null && gbl_userCountry != "" ) {
      var foundUserCountryInExcludeList = false;
      for (var jj=0; jj < this.availableLocales.length ; jj++ ) {
        if(this.availableLocales[jj].toUpperCase() == gbl_userCountry.toUpperCase()) {
          foundUserCountryInExcludeList = true;
        }
      }
      geoBlockRow = foundUserCountryInExcludeList;
    }
  }
  return geoBlockRow;
}

clip.prototype.getMarketId = function() {
    return this.marketId;
};
clip.prototype.getExchangeId = function() {
    return this.exchangeId;
};
clip.prototype.getClipId = function() {
    return this.clipId;
};
clip.prototype.getPvdrEventId = function() {
    return this.pvdrEventId;
};
clip.prototype.getProviderId = function() {
    return this.providerId;
};
clip.prototype.getApplyDrm = function() {
    return this.applyDrm;
};
clip.prototype.getApplyGeoBlock = function() {
    return this.applyGeoBlock;
};
clip.prototype.getShowMarketData = function() {
    return this.showMarketData;
};
clip.prototype.getShowMarketDropdown = function() {
    return this.showMarketDropdown;
};
clip.prototype.getClipSportId = function() {
    return this.clipSportId;
};
clip.prototype.getLeadInMinutes = function() {
    return this.leadInMinutes;
};
clip.prototype.getDurationMinutes = function() {
    return this.durationMinutes;
};
clip.prototype.getClipStartTime = function() {
    return this.clipStartTime;
};
clip.prototype.getClipStartTimeFormatted = function() {
    // miilis since jan01 1970
    return this.clipStartTime.getTime();
};
clip.prototype.getAvailableLocales = function() {
    return this.availableLocales;
};
clip.prototype.getArrayMarketIds = function() {
    return this.arrayMarketIds;
};
clip.prototype.getArrayMarketDescs = function() {
    return this.arrayMarketDescs;
};
clip.prototype.getArrayBBVMarketIds = function() {
    return this.arrayBBVMarketIds;
};
clip.prototype.getBBVMarketIdsFormatted = function() {
    var result = "";
    for (var ii=0; ii < this.arrayBBVMarketIds.length ; ii++ ) {
      result += this.arrayBBVMarketIds[ii] + "||";
    }
    if (result.length >=2) {
      result = result.substring(0,result.length - 2)
    }
    return result;
};
clip.prototype.getClipDescription = function() {
    return this.clipDescription;
};
clip.prototype.toString = function() {
    return this.getClipDescription() + this.getMarketId();
};
clip.prototype.getLocation = function() {
    return this.location;
};
clip.prototype.getSubId = function() {
    return this.subId;
};
clip.prototype.getSubDate = function() {
    return this.subDate;
};
clip.prototype.getMarketEventId = function() {
    return this.marketEventId;
};
clip.prototype.getArrayMarketEventIds = function() {
    return this.arrayMarketEventIds;
};

