/**
 *  enable bg-color toggling and init rating onload
 */
window.addEvent('domready', function ()
  {
  var comment = $('comment');
  if (comment)
    {
    comment.addEvent('focus', function () {this.setStyle('backgroundColor', '#1F1D1D');});  
    comment.addEvent('blur', function () {this.setStyle('backgroundColor', '#090909');});  
    }
  
  init_rating();
  });
  
  
  
/* -- COMMENTS -- */
  

/**
 *  add a comment to a video
 *  
 *  @return boolean FALSE to prevent default behavior
 */
function addComment ()
  {
  // get elems
  submit_button = $('submit');
  comment_box = $('comment');
  
  // check comment length
  if (comment_box.value.length < 2)
    {
    alert('I\'m sure you have a bit more to say than that..');
    return false;
    }
  
  // submit request
  submit_button.set('value', 'posting..');  
  var commentRequest = new Request.JSON({
    url: '/uc/portal/media/comment/' + window.media_key, 
    onComplete: function (response) 
      {      
      // set response handler
      comment_response(response.message);
      }}).post({'comment': comment_box.value});
    
  return false;
  }
  
  
/**
 *  handle comment server-response
 */
function comment_response (response)
  {
  // no errors -> reload comments
  if (response == '')
    {
    this.value = 'posting..';
    window.setTimeout(function ()
      {      
      reloadComments(window.media_key);
      }, 500);  
    window.setTimeout(function ()
      {
      comment_box.value = '';
      submit_button.set('value', "Let them know");        
      }, 1000);
    }
  // show errors
  else
    alert(response);
  }


/**
 *  reload comment list after submitting new comments
 */
function reloadComments (media_key)
  {
  var commentRequest = new Request.HTML({
      'evalScripts': false,
      'update': $('vid_comments')}).get('/uc/portal/media/reloadComments/' + media_key);
  }
  
  
  
  
  

/* -- RATING -- */

// rating categories
a_values = ['No votes yet', 'Terrible', 'Needs improvement', 'Pretty cool', 'Awesome', 'Outstanding'];
star_full = '/uc/portal/img/rating/star1.gif';
star_empty = '/uc/portal/img/rating/star1.1.gif';


/**
 *  init rating
 */
function init_rating ()
  {  
  // get stars
  stars = $$('li.star img');
  // add required events
  stars.each(function (star)
    {
    star.onmouseover = function () {star_over(star);};
    star.onmouseout = function () {star_out(star);};
    star.addEvent('click', function () {star_click(star);});
    });    
  // set current rating
  set_rating();
  }


/**
 *  star hover event
 */
function star_over (star)
  {
  display(a_values[star.id]);  
  stars.each(function (c_star) {c_star.src = star_empty;});  
  for (var i = 0; i < star.id; i++)
    stars[i].src = star_full;
  }


/**
 *  star mouseout event
 */
function star_out (star)
  {
  display('');
  for (var i = 0; i < star.id; i++)
    stars[i].src = star_empty;
  set_rating();
  }


/**
 *  start click event
 */
function star_click (star)
  {
  display('sending..');
  
  // submit rating to server
  var ratingRequest = new Request.JSON({
    url: '/uc/portal/media/rateMedia/'+window.media_key+'/'+star.id, 
    // set response handler
    onComplete: function (response) 
      {
      rating_response(response.message);
      }
    }).get();
  }
  

/**
 *  handle rating server-response
 */
function rating_response (response)
  {
  // no errors
  if (response == '')
    {
    // remove rating events
    stars.each(function (c_star)
      {
      c_star.onmouseout = null;
      c_star.onmouseover = null;
      });      
    display('Your vote was counted.');
    }
  // show errors
  else
    {
    alert(response);
    set_rating();  // reset rating
    }
  }


/**
 *  helper - display a text for the rating gadget
 */
function display (text)
  {
  $('rating_display').set('html', text);
  }


/**
 *  set the current rating
 */
function set_rating ()
  {
  // rating is saved as decimal
  // we round up after *.7, otherwise down
  var rating_parts = window.rating.split('.');
  var rating = (rating_parts[1][0] >= 7) ? Math.round(window.rating) : Math.floor(window.rating);
  
  for (var i = 0; i < rating; i++)
    stars[i].src = star_full;
  display(a_values[rating]);
  }
