/* --- component Photo Rate JS --- */

/**
 * Photo Rate javascript class
 */
function JSPhotoRating()
{
	
	/* -- Class constructor -- */
	this._construct = function()
	{
		// integer
		this.curr_photo_id = 0;
		
		// objects
		this.obj_container = $('photo_rating_container');
		
		this.obj_average_score_container = $('photo_rating_average_score_container');
		this.obj_average_score = $('photo_rating_average_score');
		this.obj_average_score_rates = $('photo_rating_average_score_rates');
		
		this.sex_pow_select = $('photo_rating_sex_filter');
		
		this.obj_points_container = $('photo_rating_points_container');
		
		// points nodes
		this.points = this.obj_points_container.getElementsByTagName('div');
		
		this.rate_photo(0);
	}
	
	
	this.rate_highlight = function( point )
	{
		var className = point ? 'rate_point_on' : 'rate_point_off';
		
		point--; // index begins at 0, points at 1
		
		for( var i = 0; i < this.points.length; i++ )
		{
			if( i == point )
			{
				this.points[i].className = 'rate_point_active';
				className = 'rate_point_off';
				continue;
			}
			
			this.points[i].className = className;
		}
		
		return;
	}
	
	
	this.rate_higlight_reset = function()
	{
		return this.rate_highlight(0);
	}
	
	
	this.rate_photo = function( score )
	{
		this.obj_container.className = 'photo_rating_inactive';
		
		xajax_ratePhoto( this.curr_photo_id, score, this.sex_pow_select.value );
	}
	
	
	this.loadPhoto = function( photo_id, profile_id, img_url, average_score, rates, profile_href, error_message )
	{
		this.curr_photo_id = photo_id;
		this.curr_profile_id = profile_id;
		
		this.new_img = new Image();
		
		this.new_img.src = img_url;
		
		if( error_message != '' )
			this.obj_average_score_container.innerHTML = error_message;
		else
		{
			this.obj_average_score.innerHTML = average_score;
			this.obj_average_score_rates.innerHTML = rates;
		}
		
		$('photo_rating_image_screen').innerHTML = '';
		
		$('photo_rating_profile_href').href = profile_href;
		
		var screen = $('photo_rating_image_screen');
		
		if( screen.getElementsByTagName('img').length )
			screen.removeChild( screen.getElementsByTagName('img')[0] );
		
		screen.appendChild(this.new_img);
		
		this.new_img.onload = function()
		{
			$('photo_rating_container').className = '';
		}
	}
	
	
	this.printError = function( text )
	{
		var screen = $('photo_rating_image_screen');
		
		if( screen.getElementsByTagName('img').length )
			screen.removeChild( screen.getElementsByTagName('img')[0] );
		
		screen.innerHTML = text;
		$('photo_rating_container').className = '';
		$('photo_rating_profile_href').href = '#';
	}
	
	
	this._construct();
}