// JavaScript Document


function makeRating(rid, rating, stars)
{
              for (var j = 0; j < stars; j++)
              {
                  var star = document.createElement('img');
                  if (rating >= 1)
                  {
                      star.setAttribute('src', './images/stars/rating_on.gif');
                      star.className = 'on';
                      rating--;
                  }
                  else if(rating >= 0.5)
                  {
                      star.setAttribute('src', './images/stars/rating_half.gif');
                      star.className = 'half';
                      rating = 0;
                  }
                  else
                  {
                      star.setAttribute('src', './images/stars/rating_off.gif');
                      star.className = 'off';
                  }
                  var widgetId = rid;
                  var div = document.getElementById('rating_'+rid);
                  star.setAttribute('id', 'star_'+widgetId+'_'+j);
                  star.onmouseover = new Function("evt", "displayHover("+widgetId+", "+j+");");
                  star.onmouseout = new Function("evt", "displayNormal("+widgetId+", "+j+");");
                  star.onclick = new Function("evt", "rate("+widgetId+", "+j+");")
                  div.appendChild(star);
              }
}

      
      function rate(ratingId, star)
      {
          pars = 'eid='+ratingId+'&rate='+star;
          
          //Show we are doing something;
          thediv = document.getElementById('rating_'+ratingId);
          thediv.innerHTML = '<img src="/images/working.gif" alt="Loading" />';
               
          http.open('POST', '/?act=episodeguide&action=rate', true);
          		
          //Send the proper header information along with the request
          http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          http.setRequestHeader("Content-length", pars.length);
          http.setRequestHeader("Connection", "close");
          
        	http.onreadystatechange = function(){
            if(http.readyState == 4) rated(ratingId);
          }
          http.send(pars);
    }

    function rated(ratingId)
    {
      if (http.readyState == NORMAL_STATE) {
    	document.getElementById('rating_'+ratingId).innerHTML =  'Thanks for voting!';
     }	
    }  
      
      function displayHover(ratingId, star)
      {
          for (var i = 0; i <= star; i++)
          {
              document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_over.gif');
          }
      }
      
      function displayNormal(ratingId, star)
      {
          for (var i = 0; i <= star; i++)
          {
              var status = document.getElementById('star_'+ratingId+'_'+i).className;
              document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_'+status+'.gif');
          }
      }