/*
Page:           rating.js
Created:        Aug 2006
Last Mod:       Mar 11 2007
Handles actions and requests for rating bars.
------------------------------------------------------------------------------
ryan masuga, masugadesign.com
ryan@masugadesign.com
------------------------------------------------------------------------------
* adapted to work with jQuery and symfony templates by moritz kraft, pmg media
------------------------------------------------------------------------------ */

var tmp = null;

function sndReq(vote,instancename) {
  var theUL = document.getElementById('unit_ul'); // the UL

  // switch UL with a loading div
  tmp = theUL.innerHTML;
  theUL.innerHTML = '<div class="loading"></div>';

  $.ajax({
    url: '/rateCallback/' + instancename + '/rating/' + vote,
    type: "GET",
    timeout: 6000,
    dataType: "html",
    success: changeText
  });
}

function changeText( text )
{
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0;
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById("unit_long");
        if ( text == "0" ) viewer.innerHTML = tmp;
        else viewer.innerHTML = text;
    }  else if(IE) {
        if ( text == "0" ) document.all["unit_long"].innerHTML = tmp;
        else document.all["unit_long"].innerHTML = text;
    }
}

var ratingAction = {
    'a.rater' : function(element)
    {
      element.onclick = function()
      {
        var theVote = $(this).html();
        var instanceName = $("#instanceNameDiv").html();
        sndReq(theVote,instanceName); return false;
      }
      return false;
    }

  };
Behaviour.register(ratingAction);

