var HgStockroom = new Class({
	
	/**************************************************
		Constructor
	**************************************************/
	initialize: function(element, index, modalContactForm)
	{
		this.$_element		= $(element);
		this.$_index		= index;
		this.$_overlay		= this.$_element.getElement('.Overlay');
		this.$_enquiry		= this.$_element.getElement('.Enquiry');
		this.$_fullImage	= this.$_element.getElement('.FullImage').href;
		this.$_contactForm	= modalContactForm;
		
		this.$_element
			.addEvent('mouseenter', this._OnMouseEnter.bind(this))
			.addEvent('mouseleave', this._OnMouseLeave.bind(this))
		;
		
		this.$_effects = {
			'ShowOverlay'	: new Fx.Styles(this.$_overlay, {
				transition	: Fx.Transitions.Quad.easeOut,
				duration	: 125
			}),
			
			'HideOverlay'	: new Fx.Styles(this.$_overlay, {
				transition	: Fx.Transitions.Quad.easeIn,
				duration	: 200
			}),
			
			'ShowLink'		: new Fx.Styles(this.$_enquiry, {
				transition	: Fx.Transitions.Expo.easeOut,
				duration	: 150
			}),
			
			'HideLink'		: new Fx.Styles(this.$_enquiry, {
				transition	: Fx.Transitions.Expo.easeIn,
				duration	: 200
			})
		};
		
		this.$_overlay
			.setOpacity(0)
			.addEvent('click', this._OnEnlarge.bindWithEvent(this))
		;
		
		this.$_enquiryStyleBottom = this.$_enquiry.getStyle('bottom').toInt();
		
		this.$_enquiry
			.setStyle('bottom', -10)
			.setProperty('href', '')
			.addEvent('click', this._OnEnquire.bindWithEvent(this))
		;
	},
	
	
	/**************************************************
		Properties
	**************************************************/
	GetIndex: function()
	{
		return this.$_index;
	},
	
	GetFullImageUrl: function()
	{
		return this.$_fullImage;
	},
	
	GetArtist: function()
	{
		return this.$_overlay.getElement('.Artist').getText();
	},
	
	GetTitle: function()
	{
		return this.$_overlay.getElement('.Title').getText();
	},
	
	GetYear: function()
	{
		return this.$_overlay.getElement('.Year').getText();
	},
	
	GetMaterial: function()
	{
		return this.$_overlay.getElement('.Description').getText();
	},
	
	GetDimensions: function()
	{
		return this.$_overlay.getElement('.Dimensions').getText();
	},
	
	GetIsSold: function()
	{
		return this.$_overlay.getElement('.Sold').getText();
	},
	
	
	/**************************************************
		Public Methods
	**************************************************/
	ShowOverlay: function()
	{
		this.$_effects['HideOverlay'].Stop();
		this.$_effects['HideLink'].Stop();
		
		this.$_effects['ShowOverlay'].start({
			'opacity'	: .999
		});
		
		this.$_effects['ShowLink'].StartDelayed(100, {
			'opacity'	: .999,
			'bottom'	: this.$_enquiryStyleBottom
		});
		
		this.$_open = true;
		
		return this;
	},
	
	HideOverlay: function()
	{
		this.$_effects['ShowOverlay'].Stop();
		this.$_effects['ShowLink'].Stop();
		
		this.$_effects['HideLink'].start({
			'opacity'	: 0,
			'bottom'	: -10
		});
		
		this.$_effects['HideOverlay'].StartDelayed(100, {
			'opacity'	: 0
		});
		
		this.$_open = false;
		
		return this;
	},
	
	
	/**************************************************
		Private Methods
	**************************************************/
	_LoadImage: function()
	{
		if (!this.$img)
			this.$img = new Asset.image(this.$_fullImage, {onload: this._OnImageLoad.bind(this)});
		
		return this;
	},
	
	_ShowEnquiryForm: function()
	{
		if (this.$_contactForm)
		{
			this.$_contactForm.Show();
			
			HgContactForm.SetSubject(
				'Enquiry: '
				+ this.GetArtist()
				+ ' - '
				+ this.GetTitle()
			);
		}
		
		return this;
	},
	
	_ShowLightBox: function()
	{
		HgLightBox.LoadStockItem(this);
		
		return this;
	},
	
	
	/**************************************************
		Event Handling
	**************************************************/	
	_OnMouseEnter: function()
	{
		return this.ShowOverlay();
	},
	
	_OnMouseLeave: function()
	{
		return this.HideOverlay();
	},
	
	_OnEnlarge: function(e)
	{
		e.stop();
		return this._ShowLightBox();
	},
	
	_OnEnquire: function(e)
	{
		e.stop();
		return this._ShowEnquiryForm();
	}
});

HgStockroom._Initialize = function(modalContactForm)
{
	if (HgStockroom._Init)
		return;
	
	var
		elements = $$('.Stockroom .Item'),
		items = new Array();
	
	for (var i = 0, len = elements.length; i < len; i++)
	{
		items.push(new HgStockroom(elements[i], i, modalContactForm));
	}
	
	HgLightBox.SetItemCollection(items);
	
	HgStockroom._Init = true;
};
