
//var Rating = Class.create();
//Rating.prototype = {
var Rating = Class.create({
  initialize: function(element, bid, path) {
    this.element = $(element);
	this.bid = bid;
    this.path = path;
	this.backend = path + 'backend/';
	this.visible = false;
	this.myrating = 0;
	this.rating = 0;
	this.votes = 0;
	this.sfull_path = path + 'img/star_full.gif';
	this.shalf_path = path + 'img/star_half.gif';
	this.sempty_path = path + 'img/star_empty.gif';
	this.sfull_hl_path = path + 'img/star_full_hl.gif';
	this.sfull_img = new Image();
	this.sfull_hl_img = new Image();
	this.sempty_img = new Image();
	this.sempty_img.src = this.sempty_path;
	this.sfull_img.src = this.sfull_path;
	this.sfull_hl_img.src = this.sfull_hl_path;
	this.lastupdated = 0;

    this.eventMouseOver = this.showRatebox.bindAsEventListener(this);
    this.eventMouseOut = this.hideRatebox.bindAsEventListener(this);
	this.registerEvents();
  },

  destroy: function() {
    Event.stopObserving(this.element, "mouseover", this.eventMouseOver);
    Event.stopObserving(this.element, "mouseout", this.eventMouseOut);
  },

  registerEvents: function() {
    Event.observe(this.element, "mouseover", this.eventMouseOver);
    Event.observe(this.element, "mouseout", this.eventMouseOut);
  },

  showRatebox: function(event) {
	Event.stop(event);
	if (!checkMouseEnter(this.element, event))
		return;

	var dat = new Date();
	if (this.lastupdated == Math.floor((dat.getTime() / 1000)))
		return;

	var obj = this;
	var handler = function(req, stat) {
		if (stat.result == false)
			return;
		var data = JSON.parse(req.responseText); 
		obj.myrating = data.rating;
		obj.renderRateBox();
		obj.lastupdated = Math.floor(dat.getTime() / 1000);
	};

	var pars = 'bid=' + this.bid + '&p=' + this.path;
	var req = new Ajax.Request(this.backend + 'getMyRating.php',
		{ method: 'get', parameters: pars, onComplete: handler});

  },

  hideRatebox: function(event) {
	if (!checkMouseLeave(this.element, event))
		return;
	this.refresh();
	//this.render();
  },

  rate_submit: function() {
	var obj = this;
	var handler = function(req) {
		obj.myrating = rating;
		obj.refresh();
	};

	var pars = 'bid=' + this.bid + '&r=' + this.myrating;
	var req = new Ajax.Request(this.backend + 'setRating.php',
		{ method: 'get', parameters: pars, onComplete: handler});
  },

  refresh: function() {
	var obj = this;
	var handler = function(req) {
		var data = JSON.parse(req.responseText); 
		obj.rating = data.rating;
		obj.votes = data.votes;
		obj.render();
	};
	var pars = 'bid=' + this.bid;
	var req = new Ajax.Request(this.backend + 'getRating.php',
		{ method: 'get', parameters: pars, onComplete: handler});

  },

  render: function() {
	this.element.innerHTML = '';
	var j = 5;
	for (i = 0; i < Math.floor(this.rating); i++) {
		var c = document.createElement('img');
		c.src = this.sfull_path; i.border = '0';
		this.element.appendChild(c);
	}
	j -= i;
	frac = this.rating - Math.floor(this.rating);
	if (frac >= 0.5) {
		var c = document.createElement('img');
		c.src = this.shalf_path; c.border = '0';
		this.element.appendChild(c);
		j--;
	}
	for (i = 0; i < j; i++) {
		var c = document.createElement('img');
		c.src = this.sempty_path; c.border = '0';
		this.element.appendChild(c);
	}

    var text = document.createElement('span');
	text.innerHTML = this.votes + ' votes';
	this.element.appendChild(text);
  },

  renderRateBox: function() {
	this.element.innerHTML = '';
	for (i = 0; i < this.myrating; i++) {
		var c = document.createElement('img');
		c.osrc = c.src = this.sfull_path; c.border = '0';
		c.id = 's_'+i;
		c.number = i;
		c.fullsrc = this.sfull_hl_img.src; 
		c.emptysrc = this.sempty_img.src; 
		c.onmouseover = rate_on;
		c.onmouseout = rate_off;
		c.backref = this;
		c.onclick = rate_submit_wrapper;
		this.element.appendChild(c);
	}
	for (;i < 5; i++) {
		var c = document.createElement('img');
		c.osrc = c.src = this.sempty_path; c.border = '0';
		c.id = 's_'+i;
		c.number = i;
		c.fullsrc = this.sfull_hl_img.src; 
		c.emptysrc = this.sempty_img.src; 
		c.onmouseover = rate_on;
		c.onmouseout = rate_off;
		c.backref = this;
		c.onclick = rate_submit_wrapper;
		this.element.appendChild(c);
	}
  }
});

function rate_submit_wrapper() {
	var obj = this.backref;
	obj.myrating = this.number + 1;
	obj.rate_submit();
}

function rate_on() {
	for (i = 0; i <= this.number; i++) {
        var r = $('s_'+i); 
		r.src = this.fullsrc;
	}
	for (; i < 5; i++) {
        var r = $('s_'+i); 
		r.src = this.emptysrc;
	}
}

function rate_off() {
	for (i = 0; i <= 5; i++) {
		this.src = this.osrc; 
	}
}

