var ProductViewer = Class.create();
ProductViewer.prototype = {
	initialize: function()
	{
		this.el = new Element('div').addClassName('product_viewer lightbox_content text_smaller');
		
		this.header_el = new Element('div').addClassName('').update('&nbsp;');
		this.message_el = new Element('div').addClassName('').update('');
		this.long_description_el = new Element('div').addClassName('').update('');
		this.detail_table_el = new TableElement({num_cols: 2, num_rows: 6}).addClassName('table');
		this.actions_el = new Element('div').addClassName('view text_align_right').update('&nbsp;');
		
		this.detail_table_el.getCell(0,0).update('Code').addClassName('table_field width_30 text_align_right');
		this.detail_table_el.getCell(1,0).update('Manufacturer').addClassName('table_field width_30 text_align_right');
		this.detail_table_el.getCell(2,0).update('Product').addClassName('table_field width_30 text_align_right');
		this.detail_table_el.getCell(3,0).update('Warranty').addClassName('table_field width_30 text_align_right');
		this.detail_table_el.getCell(4,0).update('Last Update').addClassName('table_field width_30 text_align_right');
		this.detail_table_el.getCell(5,0).update('Price').addClassName('table_field width_30 text_align_right');
		this.detail_table_el.getCell(0,1).update('&nbsp;').addClassName('table_field text_bold');
		this.detail_table_el.getCell(1,1).update('&nbsp;').addClassName('table_field text_bold');
		this.detail_table_el.getCell(2,1).update('&nbsp;').addClassName('table_field text_bold');
		this.detail_table_el.getCell(3,1).update('&nbsp;').addClassName('table_field text_bold');
		this.detail_table_el.getCell(4,1).update('&nbsp;').addClassName('table_field text_bold');
		this.detail_table_el.getCell(5,1).update('&nbsp;').addClassName('table_field text_bold');
		
		this.el.insert(this.header_el).insert(this.message_el).insert(this.long_description_el).insert(this.detail_table_el).insert(this.actions_el);
		
		// Lightbox
		this.lightbox = root.lightbox;
		
		// Add itself into lightbox
		if ( this.lightbox != false )
		{
			this.lightbox.addContent('product_viewer', this.el);
		}
	},
	display: function(product_record)
	{
		if ( this.lightbox != false )
		{
			this.header_el.update('<h1>'+product_record.Product.description+'</h1><div class="text_smaller">'+product_record.Product.category_code+'/'+product_record.Product.product_code+'</div>');
			
			this.long_description_el.addClassName('long_description').update(product_record.Product.long_description);
			
			this.detail_table_el.getCell(0,1).update(product_record.Product.category_code+'/'+product_record.Product.product_code);
			this.detail_table_el.getCell(1,1).update('<a href="http://'+product_record.Manufacturer.url+'" target="_blank">'+product_record.Manufacturer.name+'</a>');
			this.detail_table_el.getCell(2,1).update(product_record.Product.description);
			this.detail_table_el.getCell(3,1).update(product_record.Product.warranty);
			this.detail_table_el.getCell(4,1).update(convertDateUSToAU(product_record.Product.last_update));
			if ( product_record.Price.length > 0 )
			{
				this.detail_table_el.getCell(5,1).update('$'+( root.display_gst ? number_format(product_record.Price[0][root.current_use_price] * 1.1, 2)+' RRP InGST' : number_format(product_record.Price[0][root.current_use_price], 2)+' ExGST' ));
			}
			
			var add_to_cart_button_el = new Element('input', {type:'button', value:'Add To Cart'}).addClassName('input_button');
			var view_cart_button_el = new Element('input', {type:'button', value:'View Cart'}).addClassName('input_button');
			this.actions_el.update('').insert(add_to_cart_button_el).insert(view_cart_button_el);
			
			// Is in Cart?
			this.message_el.update('');
			if ( root.cart != false )
			{
				if ( !Object.isUndefined(root.cart.getProduct(product_record.Product.id)) )
				{
					this.message_el.update('<div class="form"><div class="info">This product has been added into shopping cart.</div></div>');
					add_to_cart_button_el.hide();
				}
			}
			
			// Event
			add_to_cart_button_el.observe('click', function(event)
			{
				if ( confirm('Add this product into cart?') ) 
				{
					// Global shopping cart
					if (root.cart != false) 
					{
						root.cart.addProduct(product_record);
					}
					if (root.cart_display != false) 
					{
						this.lightbox.display('cart_display');
					}
				}
			}.bindAsEventListener(this));
			view_cart_button_el.observe('click', function(event)
			{
				// Global shopping cart
				if ( root.cart != false && root.cart_display != false )
				{
					this.lightbox.display('cart_display');
				}
			}.bindAsEventListener(this));
			
			this.lightbox.display('product_viewer');
		}
	}
};