/**
 * Constants to identify the different rating 
 * values we can retrieve from the server.
 */
 var ratingAverageValue = 0;
 var ratingNumberOfTopMarksValue = 1;
 var ratingPercentageOfTopMarksValue = 2;
 var ratingNumberOfBottomMarksValue = 3;
 var ratingPercentageOfBottomMarksValue = 4;
 var ratingNumberOfUsersRatedValue = 5;
 
 /**
  * Through this array, we can tell the rating functionality what
  * node inner html needs to be replaced with each of the different
  * rating values.
  *
  *  Example:
  *
  *      ratingValueNodes[ratingAverageValue] = document.getElementById("currentRatingDiv");
  *      ratingValueNodes[ratingNumberOfTopMarksValue] = document.getElementById("numberOfPapsDiv");
  *      ratingValueNodes[ratingPercentageOfTopMarksValue] = document.getElementById("numberOfSlapsDiv");
  *      ratingValueNodes[ratingNumberOfBottomMarksValue] = document.getElementById("percentageOfPapsDiv");
  *      ratingValueNodes[ratingPercentageOfBottomMarksValue] = document.getElementById("percentageOfSlapsDiv");
  *      ratingValueNodes[ratingNumberOfUsersRatedValue] = document.getElementById("numberOfUsersRatedDiv");
  */
  var ratingValueNodes = new Array();


/*
 *  Function:   submitRating
 *
 *  Purpose:    Submits a rating to the database for the given detail type and id
 *
 *  Parameters: rating - the integer value that you are giving to the rating
 *              averageRatingDiv - the div that the average rating is to be displayed in
 *              ratingLevelPicId - the start of the id of the image tag
 *              onImage - the path to the on image
 *              offImage - the path to the off image
 *              detailTypeIdToRate - the detail type id to rate (optional, will take the page's detail type id if not specified)
 *              detailIdToRate - the detail id to rate (optional, will take the page's detail id if not specified).
 *            
 *  Returns:    Nothing
 */
function submitRating(rating, averageRatingDiv, ratingLevelPicId, onImage, offImage, detailTypeIdToRate, detailIdToRate) {
  
  //
  // If the detailTypeIdToRate has not been specified, take the global one for the page.
  // 
  if (!detailTypeIdToRate && window.GLOBAL_DETAIL_TYPE_ID && window.GLOBAL_DETAIL_TYPE_ID > 0) {
    detailTypeIdToRate = window.GLOBAL_DETAIL_TYPE_ID;
  }
  
  //
  // If the detailIdToRate has not been specified, take the global one for the page.
  //
  if (!detailIdToRate && window.GLOBAL_DETAIL_ID && window.GLOBAL_DETAIL_ID > 0) {
    detailIdToRate = window.GLOBAL_DETAIL_ID;
  }  
  
  if (rating >= 0 &&
	   detailTypeIdToRate && 
	   detailIdToRate &&
	   window.trackingSiteId &&
	   detailTypeIdToRate > 0 && 
	   detailIdToRate > 0 &&
	   window.trackingSiteId > 0) {
    
    setJustRated(rating, ratingLevelPicId, onImage, offImage);
    
    var parameters = "ratingGiven=" + rating + "&detailTypeId=" + detailTypeIdToRate + "&detailId=" + detailIdToRate + "&siteId=" + trackingSiteId;
  
    $.ajax({
      type: "POST",
      url: '/page/ugc/xmlHttpRequest/rateTheContent.xml',
      cache: false,
      data: parameters,
      success: function(msg){
        var avgRating = msg.getElementsByTagName("rating").item(0).getAttribute("average");       
        setAverageRating(msg, averageRatingDiv);
        if(window.onRatingSubmitted) {
          onRatingSubmitted(rating, avgRating);
        }
      }
    });
  }
  
  return;
}


/*
 *  Function:   retrieveRating
 *
 *  Purpose:    Gets the rating from the database for the given detail type and id
 *
 *  Parameters: averageRatingDiv - the div that the average rating is to be displayed in
 *              detailTypeIdToRate - the detail type id to rate (optional, will take the page's detail type id if not specified)
 *              detailIdToRate - the detail id to rate (optional, will take the page's detail id if not specified).
 *
 *  Returns:    Nothing
 */
function retrieveRating(averageRatingDiv, detailTypeIdToRate, detailIdToRate) {
  
  //
  // If the detailTypeIdToRate has not been specified, take the global one for the page.
  // 
  if (!detailTypeIdToRate && window.GLOBAL_DETAIL_TYPE_ID && window.GLOBAL_DETAIL_TYPE_ID > 0) {
    detailTypeIdToRate = window.GLOBAL_DETAIL_TYPE_ID;
  }
  
  //
  // If the detailIdToRate has not been specified, take the global one for the page.
  // 
  if (!detailIdToRate && window.GLOBAL_DETAIL_ID && window.GLOBAL_DETAIL_ID > 0) {
    detailIdToRate = window.GLOBAL_DETAIL_ID;
  }  
  if(detailTypeIdToRate && 
     detailIdToRate &&
     detailTypeIdToRate > 0 && 
     detailIdToRate > 0) {
  
    $.ajax({
      type: "GET",
      url: '/page/ugc/xmlHttpRequest/getAssetRating.xml?detailId=' + detailIdToRate + '&detailTypeId=' + detailTypeIdToRate,
      cache: false,
      success: function(msg){
        var avgRating = msg.getElementsByTagName("rating").item(0).getAttribute("average");
        setAverageRating(msg, averageRatingDiv);
        if(window.onRatingRetrieved) {
          onRatingRetrieved(avgRating);
        }
      }
    });
  }
  
  return;
}


/*
 *  Function:   validateRatingAndSubmit
 *
 *  Purpose:    Makes sure the user has not already rated this bit of content and then submits it
 *
 *  Parameters: rating - the integer value that you are giving to the rating
 *              averageRatingDiv - the div that the average rating is to be displayed in
 *              ratingLevelPicId - the start of the id of the image tag
 *              onImage - the path to the on image
 *              offImage - the path to the off image
 *
 *  Returns:    Nothing
 */
function validateRatingAndSubmit(rating, averageRatingDiv, ratingLevelPicId, onImage, offImage, detailTypeIdToRate, detailIdToRate) {
  
  if(validateRating(detailTypeIdToRate, detailIdToRate)) {
    submitRating(rating, averageRatingDiv, ratingLevelPicId, onImage, offImage, detailTypeIdToRate, detailIdToRate);
  }
  
}


/*
 *  Function:   validateRating
 *
 *  Purpose:    Works out whether a user has already rated this content
 *
 *  Parameters: detailTypeIdToRate - the detail type id to rate (optional, will take the page's detail type id if not specified)
 *              detailIdToRate - the detail id to rate (optional, will take the page's detail id if not specified).
 *
 *  Returns:    true if the user has not yet rated it, false otherwise
 */
function validateRating(detailTypeIdToRate, detailIdToRate) {

  var isValid = false;
  
  //
  // If the detailTypeIdToRate has not been specified, take the global one for the page.
  // 
  if (!detailTypeIdToRate && window.GLOBAL_DETAIL_TYPE_ID && window.GLOBAL_DETAIL_TYPE_ID > 0) {
    detailTypeIdToRate = window.GLOBAL_DETAIL_TYPE_ID;
  }
  
  //
  // If the detailIdToRate has not been specified, take the global one for the page.
  // 
  if (!detailIdToRate && window.GLOBAL_DETAIL_ID && window.GLOBAL_DETAIL_ID > 0) {
    detailIdToRate = window.GLOBAL_DETAIL_ID;
  }  
  
  if(detailTypeIdToRate && 
     detailIdToRate &&
     detailTypeIdToRate > 0 && 
     detailIdToRate > 0) {
    
    var contentIdentification = 'rating_' + detailIdToRate + '_' + detailTypeIdToRate;

    isValid = (GetCookie(contentIdentification) ==  null);

    if(isValid) {
      SetCookie(contentIdentification , "Y");
    } else {
      if(window.onAlreadyRated) {
        onAlreadyRated();
      }
    }
  }

  return isValid; 
}


/*
 *  Function:   setAverageRating
 *
 *  Purpose:    Sets the average rating in the div supplied
 *
 *  Parameters: None
 *
 *  Returns:    Nothing
 */
function setAverageRating(msg, averageRatingDiv) {
  var avgRating = msg.getElementsByTagName("rating").item(0).getAttribute("average");
  //trim value down to 1 decimal point i.e: 3.3
  //ignore 0.0 values
  if (avgRating.length == 4){
  	avgRating = avgRating.substring(0,3);
  }
  if(averageRatingDiv && document.getElementById(averageRatingDiv)) {
    document.getElementById(averageRatingDiv).innerHTML = "" + avgRating;
  }
  
  //
  // If the ratingValueNodes array have been populated with the nodes where each
  // rating value should be inserted, go on an insert every value in the appropiate 
  // place.
  //
  if (window.ratingValueNodes) {
  
    var numberOfTopRatings = msg.getElementsByTagName("rating").item(0).getAttribute("numberOfTopRatings");
    var numberOfBottomRatings = msg.getElementsByTagName("rating").item(0).getAttribute("numberOfBottomRatings");
    var percentageOfTopRatings = msg.getElementsByTagName("rating").item(0).getAttribute("percentageOfTopRatings");
    var percentageOfBottomRatings = msg.getElementsByTagName("rating").item(0).getAttribute("percentageOfBottomRatings");
    var numberOfUsersRated = msg.getElementsByTagName("rating").item(0).getAttribute("numberOfUsersRated"); 
    if (window.ratingValueNodes[ratingAverageValue]) { window.ratingValueNodes[ratingAverageValue].innerHTML = avgRating; }
    if (window.ratingValueNodes[ratingNumberOfTopMarksValue]) { window.ratingValueNodes[ratingNumberOfTopMarksValue].innerHTML = numberOfTopRatings; }
    if (window.ratingValueNodes[ratingPercentageOfTopMarksValue]) { window.ratingValueNodes[ratingPercentageOfTopMarksValue].innerHTML = percentageOfTopRatings; }
    if (window.ratingValueNodes[ratingNumberOfBottomMarksValue]) { window.ratingValueNodes[ratingNumberOfBottomMarksValue].innerHTML = numberOfBottomRatings; }
    if (window.ratingValueNodes[ratingPercentageOfBottomMarksValue]) { window.ratingValueNodes[ratingPercentageOfBottomMarksValue].innerHTML = percentageOfBottomRatings; } 
    if (window.ratingValueNodes[ratingNumberOfUsersRatedValue]) { window.ratingValueNodes[ratingNumberOfUsersRatedValue].innerHTML = numberOfUsersRated; }
    
  }    
}
     


/*
 *  Function:   setJustRated
 *
 *  Purpose:    Sets the images to their correct state, on or off, after the rating
 *
 *  Parameters: rating - the integer value that you are giving to the rating
 *              ratingLevelPicId - the start of the id of the image tag
 *              onImage - the path to the on image
 *              offImage - the path to the on image
 *
 *  Returns:    Nothing
 */
function setJustRated(rating, ratingLevelPicId, onImage, offImage) {
  
  if(rating >= 0 && ratingLevelPicId && onImage) {
    
    var i=0;

    while(true) {
      i++;
      if(document.getElementById(ratingLevelPicId + i)) {
        if(rating >= i) {
          document.getElementById(ratingLevelPicId + i).src = onImage;
        } else {
          document.getElementById(ratingLevelPicId + i).src = offImage;
        }
      } else {
        break;
      }
    }
  
  }
  
  return;
}

