
var ratings       = new Object();
var ratings_track = {};

function starlight(o, light) {

  o = typeof(o) == 'object' ? o : document.getElementById(o);

  if( o && o.tagName == 'UL' ) {

    var li      = o.getElementsByTagName('li');
        light   = !light || typeof(light) != 'number' ? ratings[o.id] : light;

    if( li.length ) {

      for(count = 0; count < li.length; count++) {

        li[count].className = count < light ? 'light' : null;

        if( !li[count].id ) {
          li[count].setAttribute('id', o.id + '-star-' + (count + 1));
          listen(li[count], 'mouseover', starevent);
          listen(li[count], 'mouseout', starevent);
          listen(li[count], 'click', starevent);
        }

      }

    }

  }

} // ! starlight()



function starevent(e) {

  var o = source(e);

  if( o ) {

    while( o && o.tagName != 'LI' ) {
      o = o.parentNode;

    }

    if( o && o.tagName == 'LI' ) {

      var star;

      if( e.type == 'mouseover' || e.type == 'click' ) {

        star = o.id.split('-');
        star = Number(star[star.length - 1]);

        if( e.type == 'click' ) {

          if( ratings_track[episode_active] ) {
            return;

          }

          var rateframe = document.getElementById('rate-frame');

          if( rateframe ) {
            rateframe.setAttribute('src', '/rate.php?episode=' + episode_active + '&vote=' + star);
            rateframe.style.display = 'block';

          } else {

            rateframe = document.createElement('iframe');
            rateframe.setAttribute('id',          'rate-frame');
            rateframe.setAttribute('frameborder', 0);
            rateframe.setAttribute('scrolling',   'no');
            rateframe.setAttribute('src',         '/rate.php?episode=' + episode_active + '&vote=' + star);

            document.getElementById('rating-area').appendChild(rateframe);

          }

          ratings_track[episode_active] = star;

        }

      } else if( e.type == 'mouseout' ) {

        star = ratings[o.parentNode.id];

      }

      starlight(o.parentNode, star);

    }

  }

} // ! starevent()


