/**
 * An ajax tour rater
 * @class
 * @scope public
 */
 
 /**
  *	@brief Constructor
  *
  *	@param in_update_id the update_id of the scene we want to rate
  *	@param in_rating_type the type of the component to be rated (EB (episode box), TR (trailer), etc...)
  *	@param in_initial_rating the initial rating of the user 
  *	@param in_image_prefix the prefix of the image name
  *	@param in_image_folder the folder where the images for rating are stored
  *	@param in_score_div the div where the score goes in 
  *	@param in_obj_name the name of the object
  */
function TourRater(in_update_id, in_rating_type, in_rating_file, in_initial_rating, in_image_prefix, in_image_folder, in_score_div, in_obj_name) 
{
	this.update_id = in_update_id;
	this.rating_type = in_rating_type;
	this.in_rating_file = in_rating_file;
	this.image_prefix =  in_image_prefix;
	this.image_folder = in_image_folder;
	this.initial_rating = Math.round(in_initial_rating);
	this.score_div = in_score_div;
	this.obj_name = in_obj_name;
	this.score_text = "";
	
	this.t = null;	//timer
	
	this.request = null;
	this.mutex = 0;		//semaphore for ajax
	
	//make our request object
    if (typeof XMLHttpRequest != "undefined") {
        this.request = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        this.request = new ActiveXObject("MSXML2.XmlHttp");
    } else {
        alert("No XMLHttpRequest object available. This functionality will not work.");
    }
}

TourRater.prototype.showRating = function(in_number)
{
	this.clearTimer();	//stop timer
	
	var i;
	for (i = 1; i <= 10; i++)
	{
		if (i <= in_number)
			document.getElementById(this.image_prefix + i).src = this.image_folder + "/a_" + i + ".gif";
		else
			document.getElementById(this.image_prefix + i).src = this.image_folder + "/" + i + ".gif";
	}
}

TourRater.prototype.clearRating = function()
{
	this.t = setTimeout(this.obj_name + ".resetRating()", 150);
}

TourRater.prototype.resetRating = function()
{
	this.clearTimer();
	this.showRating(this.initial_rating);	//clear the rating 
}

TourRater.prototype.rate = function(rating, ip)
{
	var me = this;	//for some reason I can't use this on the 'onreadystatechange' function, it has to be a local variable
	
	if (me.mutex == 0)
	{
		me.mutex = 1;	//lock the control

		// id=2352&file=???&type=EB&score=5&ip=64.111.193.69&dummy=1186589638934
		
		var sURL = "/ajax/tour_rate.php?id=" + me.update_id + 
				   "&type=" + me.rating_type + 
				   "&file=" + me.in_rating_file + 
				   "&score=" + rating + 
				   "&ip=" + ip +
				   "&dummy=" + new Date().getTime();
		
		//open connection to the gallery getter
		me.request.open("get", sURL , true);

		me.request.onreadystatechange = function () {
			if (me.request.readyState == 4) 
			{
				if (me.request.status == 200)
				{
					//show confirm message
					me.initial_rating = rating;
	
					//alert(me.request.responseText);
					//alert(me.request.responseText);
					me.score_text = me.request.responseText;

					me.hideRating();
					
					me.resetRating();
					
				}
			}   
		};
		
		me.request.send(null);
	}
}

TourRater.prototype.hideRating = function()
{
	var fx = new Fx.Styles(this.score_div, {
					duration: 200, 
					wait: true
	});
	
	fx.start({
		'filter':			"alpha(opacity=0)",
		'-moz-opacity': 	0,
		'opacity':			0
	});
	
	setTimeout(this.obj_name + ".showThankYou()",201);
}

TourRater.prototype.showThankYou = function()
{
	this.score_div.innerHTML = "<div align='center'>Thank You!</div>";
	var fx = new Fx.Styles(this.score_div, {
					duration: 400, 
					wait: true
	});
	
	fx.start({
		'filter':			"alpha(opacity=1)",
		'-moz-opacity': 	1,
		'opacity':			1
	});
	
	setTimeout(this.obj_name + ".hideThankYou()",601);
}

TourRater.prototype.hideThankYou = function()
{

	var fx = new Fx.Styles(this.score_div, {
					duration: 200, 
					wait: true
	});
	
	fx.start({
		'filter':			"alpha(opacity=0)",
		'-moz-opacity': 	0,
		'opacity':			0
	});

	setTimeout(this.obj_name + ".appearRating()",201);
}

TourRater.prototype.appearRating = function()
{
	this.score_div.innerHTML = this.score_text;
	var fx = new Fx.Styles(this.score_div, {
					duration: 400, 
					wait: true
	});
	
	fx.start({
		'filter':			"alpha(opacity=1)",
		'-moz-opacity': 	1,
		'opacity':			1
	});
	
	this.mutex = 0;
	window.location = window.location;
}

TourRater.prototype.clearTimer = function()
{
	if (this.t) 
	{
		clearTimeout(this.t);
		this.t = null;
	}

}

