/*
Copyright (C) 2008 MpgTune.com
*/
function MtRating(url1, id1, type1, idValue1, rating1, outOf1)
{
  this.url = url1;
  this.id = id1;
  this.type = type1;
  this.idValue = idValue1;
  this.rating = rating1;
  this.outOf = outOf1;
}

MtRating.prototype.setRating = function(value)
{
    if (value) {
    	this.rating = value;
    }
	this.over(this.rating);
}

MtRating.prototype.setN = function(n) {
  this.getObject(this.id + '_n').innerHTML = n;
}

MtRating.prototype.out = function()
{
	this.over(this.rating);
}

MtRating.prototype.over = function (j)
{
  for (var i = 1; i <= this.outOf; i++)
  {
    this.getObject(this.id+'_i_'+i).src = "images/star" + (i <= j ? "On" : "") + ".gif";
  }
}

MtRating.prototype.disableRating = function ()
{
  for (var i = 1; i <= this.outOf; i++)
  {
    var obj = this.getObject(this.id+'_i_'+i);
    obj.onclick = this.pleaseLogin;
  }
}

MtRating.prototype.pleaseLogin = function()
{ 
	alert('Please login to rate!');
}

MtRating.prototype.getObject = function(id) 
{ 
  if (document.getElementById) 
  { 
    return document.getElementById(id); 
  } 
  else if (document.all) 
  { 
    return document.all[id]; 
  } 
  else if (document.layers) 
  { 
    return document.layers[id]; 
  } 
  else 
  { 
    return null; 
  } 
} 

MtRating.prototype.rate = function (j)
{

	var a = this;

	var fn = function (s)
	{

		if (s.length) 
		{
			var array = s.match(/([0-9\.]+):([0-9\.]+)/);
			a.setRating(array[1]);
			a.setN(array[2]);
		}
	}

	this.sendRequest(this.url + 'rate.php?type=' + this.type + 
		'&value=' + this.idValue +
		'&rating=' + j, fn);
}

MtRating.prototype.sendRequest = function (url, fn)
{
    var XMLHttpRequestObject = createHttpRequest();

    if( !XMLHttpRequestObject )
        return false;
    
    var aUrl = url;
    XMLHttpRequestObject.open( "GET", aUrl);
    XMLHttpRequestObject.onreadystatechange = function()
    {   
        if ( XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200 )
        {   
            var s = XMLHttpRequestObject.responseText;
            delete XMLHttpRequestObject;
            XMLHttpRequestObject = null;
            
            fn(s);
        }
    }
    XMLHttpRequestObject.send( null );
}

function createHttpRequest() {
   httpRequest = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      httpRequest = new XMLHttpRequest();
      if (httpRequest.overrideMimeType) {
         // set type accordingly to anticipated content type
         //httpRequest.overrideMimeType('text/xml');
         httpRequest.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   return httpRequest;
}


