// Copyright 2007 Google Inc. All Rights Reserved.

/**
 * Util methods for gadget ratings and comments in
 * Google Desktop gadget gallery.
 *
 * @author Vishwajith HK
 */

// Preload images
var imgRateStarOn = new Image();
imgRateStarOn.src = '/images/rateStarOn.gif';
var imgRateStarOff = new Image();
imgRateStarOff.src = '/images/rateStarOff.gif';

/**
 * @param elementId element to be fetched.
 * @return the element in the DOM corresponding to elementId. 
 */
function gel(elementId) {
  return document.getElementById(elementId);
}

/**
 * Displays write-comments form where the user can rate a gadget and
 * write comments.
 */
function showWriteCommentsForm() {
  var commentsSpan = gel('commentsSpan');
  var comments = gel('comments');
  var writeComment = gel('writeComment');
  commentsSpan.className = 'linkon';
  commentsSpan.onclick = showComments;
  if (comments) {
    comments.style.display = 'none';
  }
  if (writeComment) {
    writeComment.style.display = 'inline';
  }

  var abuseLinkDiv = gel('abuseLinkDiv');
  if (abuseLinkDiv) {
    abuseLinkDiv.style.display = 'none';
  }

  var rating = gel('rating').value;
  setStars(parseInt(rating));
}

/**
 * Display the comments section.
 */
function showComments() {
  var commentsSpan = gel('commentsSpan');
  var comments = gel('comments');
  var writeComment = gel('writeComment');
  commentsSpan.className = 'linkoff';
  commentsSpan.onclick = null;
  if (comments) {
    comments.style.display = 'inline';
  }
  if (writeComment) {
    writeComment.style.display = 'none';
  }

  var abuseLinkDiv = gel('abuseLinkDiv');
  if (abuseLinkDiv) {
    abuseLinkDiv.style.display = 'inline';
  }
}

/**
 * Delete the user's comment.
 */
function deleteReview() {
  var submitForm = gel('commentForm');
  submitForm.action = '/deletecomment';
  submitForm.submit();
}

/**
 * This sets the number of stars for the given comment
 * @param numStars The number of stars to be set.
 */
function setStars(numStars) {
  gel('rating').value = '' + numStars;
  showStars(numStars);
}

/*
 * This shows the given number of stars. It is intended to
 * be used for either a temporary display (such as from a
 * mouseover) or for a more permanent display, such as the
 * number of stars the gadget rating is set to.
 *
 * @param numStars The number of stars to be displayed.
 */
function showStars(numStars) {
  if (!numStars) {
    numStars = parseInt(gel('rating').value);
  }
  for (var i = 1; i <= 5; ++i) {
    var starElement = gel('star' + i);
    if (!starElement) {
      continue;
    }
    if (numStars >= i) {
      starElement.src = imgRateStarOn.src;
    } else {
      starElement.src = imgRateStarOff.src;
    }
  }
}

/**
 * Toggle abuse-links.
 * @param showAbuseLinks If set, displays abuse-links, hide-abuse-link, 
 *                       hides show-abuse-link and vice versa.
 */
function changeReportAbuseLinks(showAbuseLinks) {
  var abuseLinkStyle = showAbuseLinks ? 'inline' : 'none';
  // If abuse links are shown, the show-abuse link should be hidden.
  var showAbuseLinkStyle = showAbuseLinks ? 'none' : 'inline';
  var comments = gel('comments');
  var allElements = comments.getElementsByTagName ? 
      comments.getElementsByTagName('div') : new Array();
  for (var i = 0; i < allElements.length; ++i) {
    if (allElements[i].id.search('abuse') == 0) {
      allElements[i].style.display = abuseLinkStyle;
    }
  }

  var showAbuseLinkSpan = gel('showAbuseLink');
  if (showAbuseLinkSpan) {
    showAbuseLinkSpan.style.display = showAbuseLinkStyle;
  }

  var hideAbuseLinkSpan = gel('hideAbuseLink');
  if (hideAbuseLinkSpan) {
    // if abuse-links are shown, hide-abuse links should also be shown.
    hideAbuseLinkSpan.style.display = abuseLinkStyle;
  }
}
