function addRenditionsToBasket(selectedItemData)
{
  if (selectedItemData.length > 0)
  {
    var basketItemName;
    
    for (var i=0; i<selectedItemData.length; i++)
    {
      basketItemName = "cb_" + selectedItemData[i]["dDocName"] + "_" + selectedItemData[i]["dRevLabel"] + "_" + selectedItemData[i]["renLabel"];      
      addItemToBasket(basketItemName, selectedItemData[i]["dDocName"], selectedItemData[i]["dID"], selectedItemData[i]["dDocTitle"], selectedItemData[i]["dRevLabel"], selectedItemData[i]["docUrl"], selectedItemData[i]["renFlag"], selectedItemData[i]["renLabel"], selectedItemData[i]["dRendition1"], selectedItemData[i]["dOriginalName"], selectedItemData[i]["contentCategory"], selectedItemData[i]["renUrl"]);
    }    
    displayBasket();
  }
  
  else
    alert(lc("wwNothingSelected"));
}


function deleteRenditions(selectedItemData)
{
  if (selectedItemData.length > 0)
  {
    var deleteForm = document.forms["AddRenditions_" + selectedItemData[0]["dID"]];
    var selectItemCount = 0;
    var renditionKeysArray = new Array();
    var basketDeleteList = new Array();
    var basketDeleteListIndex = 0; 
       
    // Loop through selected items and create delete form fields for individual renditions to be
    // deleted. Also put a corresponding entry for each field in the renditionKeys value string.
    for (var i=0; i<selectedItemData.length; i++)
    {
      if (selectedItemData[i]["renLabel"] != "Native File")
      {
        selectItemCount++;
        var coreFieldName = "addRendition" + selectItemCount.toString();
        createHiddenInputElement(deleteForm, coreFieldName + ".name", selectedItemData[i]["renLabel"]);
        renditionKeysArray[i] = coreFieldName;

        // Check to see if rendition being deleted is present in Basket; if so add to delete list
        var basketTestId = "cb_" + encodeForJavaScript(selectedItemData[i]["dDocName"]) + "_" + selectedItemData[i]["dRevLabel"] + "_" + selectedItemData[i]["renLabel"];        
        if (contentBasket[basketTestId])
        {
          delete contentBasket[basketTestId];  
          basketDeleteList[basketDeleteListIndex] = basketTestId;
          basketDeleteListIndex++
        }        
      }
      else
        alert(lc("wwRenInfoCantDeleteNative"));
    }
    
    // If the native file was the only file selected, it's possible for renditionKeysArray.length
    // to be zero even though selectedItemData.length is greater than zero (since deleting the native
    // file is not allowed). This conditional prevents a Content Server error from being thrown in
    // this situation.
    if (renditionKeysArray.length > 0)
    {
      // Set form value for renditionKeys
      deleteForm.renditionKeys.value = renditionKeysArray.join(",");
      
      // Set form value used to delete corresponding items from Basket if necessary
      //if (basketDeleteListIndex > 0)
        //deleteForm.topicString1.value = "deleteRows:content_basket:BasketItems:" + basketDeleteList.join(",");
            
      deleteForm.submit();
    }
  }
  
  else
    alert(lc("wwNothingSelected"));
}


var currentParentImage;

function initActionImages()
{
  currentParentImage = document.getElementById(currentParentId);
  
  if (typeof actionImage == "undefined")
  {
    actionImage = new Image();
    actionImageOver = new Image();
  }
  
  if (currentParentImage.src.indexOf("ActionsIcon") != -1 && actionImage.src != currentParentImage.src)
  {
    actionImage.src = httpSkinRoot + "ActionsIcon.gif";
    actionImageOver.src = httpSkinRoot + "ActionsIcon_over.gif";
  }
  
  // Use include 'custom_action_image_init_code' to insert custom image definition code
  // into this hook function.
  if (typeof customActionImageInit != "undefined")
    customActionImageInit();
}


function initDamImages()
{
  if (currentParentImage.src.indexOf("DetailsArrow") != -1 && actionImage.src != currentParentImage.src)
  { 
    actionImage.src = httpSkinRoot + "dam/DetailsArrow.gif";
    actionImageOver.src = httpSkinRoot + "dam/DetailsArrow_active.gif";
  }
  
  if (currentParentImage.src.indexOf("DescriptionIcon") != -1 && actionImage.src != currentParentImage.src)
  { 
    actionImage.src = httpSkinRoot + "dam/DescriptionIcon.gif";
    actionImageOver.src = httpSkinRoot + "dam/DescriptionIcon_active.gif";
  }  
}


// This code adds the DAM image init code to the existing initActionImages function while
// retaining the original function contents (essentially 'supering' the function)
var originalImageInit = initActionImages;
initActionImages = function() { originalImageInit(); initDamImages(); };


// 'id' is the id property of the popup menu's top-level container element
// 'parentId' is the id of icon image element whose onclick event displays the popup
// 'event' is the window's current event object (should always be set as event)
// 'parameterString' is an optional comma-delimited string of display parameters in the form:
// 'name1=value1,name2=value2'; the following optional display parameters are available:
//    'position' - allowable values: 'horizontal' (default), 'vertical', or 'overlay'
//    'closeMode' - allowable values: 'auto' (default) or 'manual'
function showPopup(id, parentId, event, parameterString)
{
  // Define parameter object and default values for all optional parameters
  var parameters = new Object();
  parameters.position = "horizontal";
  parameters.closeMode = "auto";
   
  // Set parameter values based upon contents of optional parameterString argument
  if (parameterString)
  {
    var parameterArray = parameterString.split(",");
    for (var i=0; i<parameterArray.length; i++)
      parameters[parameterArray[i].split("=")[0]] = parameterArray[i].split("=")[1];
  }   
  
  closePopups();
  
  currentParentId = parentId;  
  initActionImages(); // Moved initActionImages() call out of closePopups() function and placed it here instead
  var parentImage = document.getElementById(parentId);
  currentPopup = document.getElementById(id);

  displayMenu(parentImage, currentPopup, parameters.position) // Used parameter value instead of hard-coding 'horizontal'

  //*** change Actions image src if applicable
  if (parentImage.src)
  {
    if(parentImage.id=='targetedQuickSearchIcon')
    {
      actionImage.src = httpSkinRoot + "targetedQS.gif";
      actionImageOver.src = httpSkinRoot + "targetedQS_over.gif";
    }
    else
    {
      initDamImages();
    }           
    parentImage.src = actionImageOver.src;
  }

  //*** register the mouseout handlers used to close the popup
  if (parameters.closeMode == "auto")
  {
    if (document.addEventListener)
    { // DOM Level 2 Event Model
      currentPopup.addEventListener("mouseout", popupMouseOut, true);
      currentPopup.addEventListener("mouseover", popupMouseOver, true);
      parentImage.addEventListener("mouseout", parentMouseOut, true);
    }
    else if (document.attachEvent)
    { // Older event model
      currentPopup.onmouseout = popupMouseOut;
      currentPopup.onmouseover = popupMouseOver;
      parentImage.onmouseout = parentMouseOut;
    }
  }
}


function closePopups()
{ 
  if (currentPopup != null)
  {
    hideMenu(currentPopup);
  }
  if (currentParentId)
  {
    var parent = document.getElementById(currentParentId);
    parent.src = actionImage.src;
  }
}


  // This event handler closes an open popup when the user clicks anywhere on the page
  if (document.addEventListener) // DOM Event Model
    document.addEventListener("click", clearPopups, false);
  else if (document.attachEvent) // IE 5+ Event Model
    document.attachEvent("onclick", clearPopups);
    
  function clearPopups(e)
  {
    if (currentPopup != null && currentPopup.style.display == "block")
    {
      if (!e) e = event;

      if (e.target != null)
        var targetElmt = e.target;
      else
        var targetElmt = e.srcElement;
        
      var skipAction = false;
      
      if (targetElmt.onclick != null && targetElmt.onclick.toString().indexOf("showPopup") > -1)
        skipAction = true;
        
      if (!skipAction)
        closePopups();
    }
  }   


function formatFileSize(kilobyteValue)
{
  var sizeValue = kilobyteValue;
  var unitSign = " KB";
  
  if (sizeValue > 1024)
  {
    sizeValue = (Math.round((sizeValue / 1024) * 10)) / 10;  
    unitSign = " MB";
  }
  else if (sizeValue == 0)
    sizeValue = 1;
  
  
  var displayString = sizeValue + unitSign;
  return displayString;
}


function padLeft_2digit(x) {return ((x>9)?"":"0")+x}
function padLeft_3digit(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}

function unitizeMsTimeValue(millisecondValue, displayMilliseconds)
{
  if (displayMilliseconds)
  {
    var seconds = Math.floor(millisecondValue/1000);
    millisecondValue = millisecondValue % 1000;
    timeUnitString = padLeft_3digit(millisecondValue);
  }
  else    
    var seconds = Math.round(millisecondValue/1000);

  var minutes = Math.floor(seconds/60);
  seconds = seconds % 60;
  if (displayMilliseconds)
    var timeUnitString = padLeft_2digit(seconds) + "." + timeUnitString;
  else
    var timeUnitString = padLeft_2digit(seconds);
  
  var hours = Math.floor(minutes/60);
  minutes = minutes % 60;
  timeUnitString = padLeft_2digit(minutes) + ":" + timeUnitString;

  timeUnitString = hours + ":" + timeUnitString;
  
  return timeUnitString;
}


function trimLeft_2digit(x) {return ((parseInt(x.charAt(0))>0)?x:x.charAt(1))}

function timeUnitDisplay(millisecondValue)
{
  var timeUnitSting = unitizeMsTimeValue(millisecondValue, false);
  var timeUnits = timeUnitSting.split(":");
  var displayString = "";
  
  if (parseInt(timeUnits[0]) > 0)
    displayString += trimLeft_2digit(timeUnits[0]) + lc("wwHoursUnitLabel") + " ";
    
  if (parseInt(timeUnits[1]) > 0 || parseInt(timeUnits[0]) > 0)
    displayString += trimLeft_2digit(timeUnits[1]) + lc("wwMinutesUnitLabel") + " ";

  displayString += trimLeft_2digit(timeUnits[2]) + lc("wwSecondsUnitLabel");
  
  return displayString;  
}


function computeVideoFileSize(rendition)
{
  var bitrate = rendition.mediaRendBitrate;
  var duration = rendition.mediaRendDuration/1000; // Convert milliseconds to seconds
  var fileSize = Math.round(((bitrate * duration)/8)/1024); // Kilobyte integer value
  fileSize = formatFileSize(fileSize);
  
  return fileSize;
}


function changeTabs(clickedTabName)
{ 
  var i=1;
  var tabName = "tab_" + i;
  var frontTab = document.getElementById(tabName + "_front");
  var rearTab = document.getElementById(tabName + "_rear");
  var tabFrame = document.getElementById(tabName + "_frame");
  
  while (frontTab != null)
  {
    if (tabName == clickedTabName )
    {
      rearTab.style.display = "none";
      frontTab.style.display = "block";
      tabFrame.style.display = "block";
    }
    else if (frontTab.style.display == "block")
    {
      frontTab.style.display = "none";
      rearTab.style.display = "block";
      tabFrame.style.display = "none";   
    }
    
    i++;
    tabName = "tab_" + i;
    frontTab = document.getElementById(tabName + "_front");
    rearTab = document.getElementById(tabName + "_rear");
    tabFrame = document.getElementById(tabName + "_frame");    
  }
  
  // Possible fix idea for a quirky problem in Firefox where the player disappears when you do
  // any DHTML manipulation of page content (such as changing the tabs below).
  //window.scrollBy(0, 1);
  //setTimeout("window.scrollBy(0, -1);", 100);
}


function setFrameHeight(heightValue, windowObj)
{
  var frameElement = document.getElementById(windowObj.name);  
  frameElement.style.height = heightValue;
}


// This function is a fix for the "active content" issues with later versions of IE. If the write()
// statement is called from within the document itself, the video player is loaded in an inactive state,
// and requires an initial click to activate prior to resoponding to user input. If the write() 
// statement comes from an external file, then the player functions normally.
function insertPlayerMarkup()
{
  document.write(playerObj.getHTML());
}

