/**
 * 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_scene_id, in_item_rated_id, in_rating_type, in_initial_rating, in_class_prefix, in_score_div, in_obj_name) 
{
	//alert(in_class_prefix);
	this.scene_id = in_scene_id;
	this.item_rated_id = in_item_rated_id;
	this.rating_type = in_rating_type;
	this.class_prefix =  in_class_prefix;  
	this.initial_rating = 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)
{
	//alert('check');
	this.clearTimer();	//stop timer
	
	
	var i;
	for (i = 1; i <= 10; i++)
	{                                
		if (i <= in_number)
		{
			//alert(this.class_prefix + i + " " + this.class_prefix + i + "_show");                                 
			document.getElementById(this.class_prefix + this.scene_id + "_" + i).className = this.class_prefix + i + " " + this.class_prefix + i + "_show";
		}
		else
			document.getElementById(this.class_prefix + this.scene_id + "_" + i).className = this.class_prefix + i;
	}
}

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)
{   
	//alert('RATE');
	              
	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
		
		//var sURL = "controllers/ajax/tour_rate.php?item_rated_id=" + me.item_rated_id + 
		var sURL = "?action=ajax/tour_rate&guimode=ajax&item_rated_id=" + me.item_rated_id + 
				   "&scene_id=" + me.scene_id + 
				   "&rating_type=" + me.rating_type + 
				   "&rating=" + rating + 
				   "&ip=" + ip;
		//alert(sURL);
		// show thanks you before running php script
		me.hideRating();
		
		//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;
					
					me.score_text = me.request.responseText;
					//me.hideRating();
					me.resetRating();
				}
			}   
		};
		
		me.request.send(null);
		
	}
	
}

/*TourRater.prototype.incXSTrailerHits = function(in_scene_id)
{
	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
		
		var sURL = "controllers/ajax/inc_xs_trailer_hit.php?scene_id=" + in_scene_id;
		me.request.open("get", sURL , true);
		me.request.onreadystatechange = function () {
			if (me.request.readyState == 4) 
			{
				//alert('status' + me.request.status);	
				if (me.request.status == 200)
				{
					//do something? nah				
				}
			}   
		};
		
		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 = "<span>Thank You!</span>";
	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;
}

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

}