/*
 * stoneImageRotator
 */
function stoneImageRotator(wrapper_elem_id, image_elem_id, image_dir, stone_types, reverse_stone_types,
		stone_type_titles)
{
	this._wrapper_elem = $(wrapper_elem_id);
	this._image_elem_id = image_elem_id;
	this._image_elem = $(this._image_elem_id);
	if(this._image_elem != null)
		this._image_elem.observe('load', this._load_image.bindAsEventListener(this));

	this._image_dir = image_dir != null ? image_dir : '/res/images';
	this._cur_stone_type = null;
	this._cur_finish_type = null;

	this._viewing_angles = new Array('orth', 'oblique');
	this._cur_view_angle = this._viewing_angles[0];

	//create or clone arrays/hashes from these
	this._stone_types = $H(stone_types);
	this._reverse_stone_types = $H(reverse_stone_types);
	this._stone_type_titles = $H(stone_type_titles);

	this._cached_image_dict = new Hash();
	this._changed_type = false;

//	cur_stone_type = 'flc';
//	cur_finish_type = 'natural';

	if(this._wrapper_elem)
	{
		this._wrapper_elem.appendChild(new Element('div', {id: 'stone-view-orth'}));
		this._wrapper_elem.appendChild(new Element('div', {id: 'stone-view-oblique'}));
		$('stone-view-orth').observe('mousemove', this._move_view.bindAsEventListener(this));
		$('stone-view-oblique').observe('mousemove', this._move_view.bindAsEventListener(this));
	}
}
stoneImageRotator.prototype = new Object();
stoneImageRotator.prototype._wrapper_elem;
stoneImageRotator.prototype._image_elem_id;
stoneImageRotator.prototype._image_elem;
stoneImageRotator.prototype._image_dir;
stoneImageRotator.prototype._cur_stone_type;
stoneImageRotator.prototype._cur_finish_type;
stoneImageRotator.prototype._viewing_angles;
stoneImageRotator.prototype._cur_view_angle;
stoneImageRotator.prototype._stone_types;
stoneImageRotator.prototype._reverse_stone_types;
stoneImageRotator.prototype._stone_type_titles;
stoneImageRotator.prototype._cached_image_dict;
stoneImageRotator.prototype._changed_type;

//Private functions
stoneImageRotator.prototype._update_image =
	function()
	{
		if(this._image_elem == null)
		{
			new_elem = new Element('img', {id: this._image_elem_id, src: '', alt: ''});
			this._wrapper_elem.appendChild(new_elem);
			this._image_elem = $(this._image_elem_id);
			this._image_elem.observe('load', this._load_image.bindAsEventListener(this));
		}

		size = this._wrapper_elem.getDimensions();
		this._wrapper_elem.setStyle({width: size.width + 'px', height: size.height + 'px',
			background: 'white url("' + G_BASE_DIR + '/res/images/progress-indicator.gif") 50% 50% no-repeat'});

		this._image_elem.src = this._image_dir + '/stone-' + this._cur_stone_type + '-' + this._cur_finish_type + '-' + this._cur_view_angle
			+ '.jpg';
		this._image_elem.writeAttribute({alt: this._stone_type_titles.get(this._cur_stone_type) + ' - ' + this._cur_finish_type});

		if(this._changed_type)
		{
			this._image_elem.hide();
			this._changed_type = false;
		}
	};
	
stoneImageRotator.prototype._load_image =
	function(e)
	{
		this._image_elem.show();
		this._wrapper_elem.setStyle({width: 'auto', height: 'auto', background: 'none'});
	};

stoneImageRotator.prototype._move_view =
	function(e)
	{
		if(this._cur_stone_type == null || this._cur_finish_type == null)
			return;

		if(e.element().id == 'stone-view-oblique')
		{
			if(this._cur_view_angle == this._viewing_angles[1])
				return;
			this._cur_view_angle = this._viewing_angles[1];
		}
		else
		{
			if(this._cur_view_angle == this._viewing_angles[0])
				return;
			else
				this._cur_view_angle = this._viewing_angles[0];
		}
		
		//if there is a current stone type and finish type, and the viewing angle changed then we will get here
		this._update_image();
	};

//Public Functions
stoneImageRotator.prototype.click_stone_menu_item =
	function(menu_elem)
	{
		var menu_re = /^([a-zA-Z\-_]*)-([0-9]+)-([0-9]+)$/;
		var groups = menu_elem.id.match(menu_re);
		if(groups.length >= 4)
		{
			this._cur_stone_type = this._reverse_stone_types.get(groups[2]);
			this._cur_finish_type = this._stone_types.get(this._cur_stone_type)[groups[3]];
			this._cur_view_angle = this._viewing_angles[0];
//			alert(stone_type + ', ' + stone_types.get(stone_type)[groups[3]]);

			//cache the oblique angle if not already
			var cached = this._cached_image_dict.get(this._cur_stone_type + '-' + this._cur_finish_type + '-' + this._cur_view_angle);
			if(cached == undefined || !cached)
			{
				this._cached_image_dict.set(this._cur_stone_type + '-' + this._cur_finish_type + '-' + this._viewing_angles[0], true);
				this._cached_image_dict.set(this._cur_stone_type + '-' + this._cur_finish_type + '-' + this._viewing_angles[1], true);
				cache_image(this._image_dir + '/stone-' + this._cur_stone_type + '-' + this._cur_finish_type + '-' + this._viewing_angles[1]
                          + '.jpg');
			}

			this._changed_type = true;
			//now update the image, this will cache the 
			this._update_image();
		}
	};
