/* --- Rate Block JS --- */

/**
 * Profile album javascript class
 */
function JSProfileAlbum( ids_array, display_points, display_score )
{
	/* -- Class constructor -- */
	// boolean
	this.display_points = ( display_points == undefined ) ? true : display_points;
	this.display_score = ( display_score == undefined ) ? true : display_score;
	
	// integer
	this.curr_photo_id = 0;
	
	// arrays
	this.user_rate_scores = [];
	this.average_rate_scores = [];
	this.rates_number = [];
	
	// objects
	this.obj_container = $('profile_photo_album');
	
	this.obj_average_score_container = $('profile_photo_album_average_score_container');
	this.obj_average_score = $('profile_photo_album_average_score');
	this.obj_average_score_rates = $('profile_photo_album_average_score_rates');
	
	this.obj_points_container = $('profile_photo_album_rate_block_points');
	
	this.obj_screen = $('profile_photo_album_screen');
	
	this.obj_thumbs_container = $('profile_photo_album_thumb_list');
	
	
	// points nodes
	this.points = $('profile_photo_album_rate_block_points').getElementsByTagName('div');
	
	// thumbnails nodes
	this.thumb_nodes = this.obj_thumbs_container.getElementsByTagName('img');
	
	// thumbnails photo_id and index comparing
	for( var i = 0; i < ids_array.length; i++ )
	{
		this.thumb_nodes[i].photo_id = ids_array[i];
		this.thumb_nodes[i].thumb_index = i;
	}
	
	// here var `i` contains number of album photos
	if( i == 1 )
		this.obj_thumbs_container.style.display = 'none';
	
	// ids_array[0] == 0 means no photos
	if( ids_array[0] == 0 || !this.display_score )
	{
		this.obj_average_score_container.style.display = 'none';
		this.obj_container.className = '';
	}
	
	// hide points if need
	if( !this.display_points )
		this.obj_points_container.style.display = 'none';
	
	
	/* -- Class methods -- */
	// Displays photo in main album screen
	this.openPhoto = function( thumb_index )
	{
		var img = this.thumb_nodes[thumb_index];
		
		this.curr_photo_id = img.photo_id;
		
		if( this.user_rate_scores[img.photo_id] == undefined )
		{
			xajax_getPhotoScore( img.photo_id );
		}
		else
		{
			this.obj_average_score.innerHTML = this.average_rate_scores[img.photo_id];
			this.obj_average_score_rates.innerHTML = this.rates_number[img.photo_id];
		}
		
		this.obj_screen.style.backgroundImage = "url('"+img.src+"')";
		
		return this.rate_higlight_reset();
	}
	
	
	// Fixes photo rating score data
	this.fix_rate_scores = function( user_score, average_score, rates_num, photo_id )
	{
		if( !photo_id ) photo_id = this.curr_photo_id;
		
		this.user_rate_scores[photo_id] = user_score;
		this.average_rate_scores[photo_id] = average_score;
		this.rates_number[photo_id] = rates_num;
		
		this.obj_average_score.innerHTML = average_score;
		this.obj_average_score_rates.innerHTML = rates_num;
		
		this.obj_container.className = '';
		
		return this.rate_higlight_reset();
	}
	
	
	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( this.user_rate_scores[this.curr_photo_id] );
	}
	
	
	this.rate_photo = function( score )
	{
		$('profile_photo_album').className = 'profile_photo_album_inactive';
		
		xajax_ratePhoto( this.curr_photo_id, score );
	}
	
	
	this.openPhoto(0);
	setTimeout( "$('profile_photo_album').className = ''", 150 );
}
