BT.ns(function() {with(BT) {
	Product_page_controller = {
		options: {
			// product
			product_attributes_container: 'product_attributes',
			price_display_container: 'price_display_value',
			out_of_stock_container: 'out_of_stock',
			notification_container: 'notification',
			inventory_tab_selected: 'inventory_tab_selected',
			inventory_container_class: 'inventory_content',
			inventory_container_prefix: 'content_',
			inventory_bar_class: 'inventory_bar',
			inventory_backorder_class: 'inventory_bar_backorder',
			// images
			main_image_class: 'gallery_img',
			main_featured_container: 'img_featured',
			main_studio_container: 'img_studio',
			main_artwork_container: 'img_artwork',
			scroll_images_visible: 5,
			scroll_left_id: 'slide_left',
			scroll_right_id: 'slide_right',
			scroll_time: .3,
			scroll_disabled_class: 'slide_disabled',
			thumb_container: 'gallery_container',
			thumb_class: 'gallery_thumb',
			thumb_prefix: 'thumb_',
			thumb_selected_class: 'thumb_selected',
			gallery_image_container: 'image_gallery_container',
			gallery_image_class: 'gallery_image',
			gallery_image_prefix: 'image_',
			image_transition_time: .5,
			product_attributes_container: 'product_attributes',
			product_show_info: false,
			// shared
			inventory_tabs_container: 'inventory_tabs'
		},
		product: false,
		product_images: false,
		set_options: function(options) {
			Object.extend(this.options, options || {});
		},
		initialize_product: function() {
			this.product = new Product(this.options);
		},
		initialize_product_images: function() {
			this.product_images = new Product_images(this.options);
		},
		load_product_data: function(product_data) {
			if (this.product_images) {
				this.product_images.load_product_data(product_data);
			}
		},
		load_head_attribute: function(attribute_group) {
			if (this.product) {
				this.product.load_head_attribute(attribute_group);
			}
		},
		load_attributes_map: function(attributes_map) {
			if (this.product) {
				this.product.load_attributes_map(attributes_map);
			}
		},
		load_model_data: function(model_data) {
			if (this.product) {
				this.product.load_model_data(model_data);
			}
		},
		load_gallery_images: function(images) {
			if (this.product_images) {
				this.product_images.load_gallery_images(images);
			}
		},
		load_main_images: function(images) {
			if (this.product_images) {
				this.product_images.load_main_images(images);
			}
		},
		activate_selectors: function() {
			if (this.product) {
				this.product.activate_selectors();
			}
			if (this.product_images) {
				this.product_images.activate_selectors();
			}
		},
		activate_inventory_tabs: function() {
			if (this.product) {
				this.product.activate_inventory_tabs();
			}
		},
		activate_carousel: function() {
			if (this.product_images) {
				this.product_images.activate_carousel();
			}
		},
		activate_extra_actions: function() {
			if (this.product) {
				this.product.activate_extra_actions();
			}
		},
		set_default_selections: function() {
			if (this.product) {
				var head_attribute;
				hash_head_attribute = document.location.hash.toLowerCase();
				if (hash_head_attribute) {
					head_attribute = hash_head_attribute.substring(1);
				}
				if (!this.product.head_attribute[head_attribute]) {
					for (var first in this.product.head_attribute) break;
					if (first) {
						head_attribute = first;
					}
				}
				if (head_attribute) {
					this.product.switch_to_head_attribute(head_attribute);
				} else {
					this.product.update_attributes(false);
				}
			}
			if (this.product_images && this.product_images.gallery_thumb_container) {
				this.product_images.sort_gallery(true);
			}
		}
	};
	Product = Class.create({
		options: {},
		attribute_options: {},
		head_attribute: {},
		attributes_map: {},
		model_data: {},
		current_head_attribute: false,
		attribute_selects: false,
		price_display: false,
		stock_display: false,
		notification_display: false,
		current_attributes: [],
		inventory_containers: false,
		current_inventory_tab: false,
		initialize: function(options) {
			this.set_options(options);
		},
		set_options: function (options) {
			Object.extend(this.options, options || {});
		},
		load_head_attribute: function(attribute_group) {
			this.head_attribute = attribute_group;
		},
		load_attributes_map: function(attributes_map) {
			this.attributes_map = attributes_map;
		},
		load_model_data: function(model_data) {
			this.model_data = model_data;
		},
		activate_selectors: function() {
			this.attribute_selects = $$('div.' + this.options.product_attributes_container + ' select.attributes');
			this.price_display = $(this.options.price_display_container);
			this.stock_display = $(this.options.out_of_stock_container);
			this.notification_display = $(this.options.notification_container);
			for (var i = 0; i < this.attribute_selects.length; i++) {
				this.current_attributes[i] = this.attribute_selects[i].value;
				this.attribute_options[i] = {};
				for (var j = 0; j < this.attribute_selects[i].options.length; j++) {
					this.attribute_options[i][this.attribute_selects[i].options[j].value] = this.attribute_selects[i].options[j];
				}
			}
			var self = this;
			this.attribute_selects.invoke('observe', 'change', function() {
				self.update_attributes(true);
				if (self.inventory_containers) {
					for (attribute_label in self.head_attribute) {
						if (self.head_attribute[attribute_label] == self.attribute_selects[0].value) break;
					}
					self.switch_to_inventory_tab(attribute_label);
				}
			});
		},
		activate_inventory_tabs: function() {
			this.inventory_containers = $$('.' + this.options.inventory_container_class);
			if (this.inventory_containers) {
				var self = this;
				$$('div#' + this.options.inventory_tabs_container + ' a').invoke('observe', 'click', function() {
					var label = this.readAttribute('id').substring(4);
					self.switch_to_inventory_tab(label);
				});
				var inventory_bars = $$('.' + this.options.inventory_bar_class);
				if (inventory_bars) {
					inventory_bars.invoke('observe', 'click', function() {
						self.attribute_selects[2].value = this.readAttribute('id').substring(4);
						self.update_attributes(true);
					});
				}
				var inventory_backorders = $$('.' + this.options.inventory_backorder_class + ' a');
				if (inventory_backorders) {
					inventory_backorders.invoke('observe', 'click', function() {
						var model_id = self.attributes_map[self.current_attributes[0]][self.current_attributes[1]][this.readAttribute('id').substring(8)];
						if (model_id) {
							self.load_notification_form(model_id);
						}
					});
				}
			}
		},
		activate_extra_actions: function() {
			var self = this;
			var explanation = $$('.huh');
			if (explanation.length > 0) {
				explanation.invoke('observe', 'click', function() {
					new Effect.Appear('explanation');
				});
			}
			var sizing_and_fit = $('sizing_and_fit');
			if (sizing_and_fit) {
				Event.observe(sizing_and_fit, 'click', function() {
					new Effect.Appear('size_chart');
					if ($('email_share_form').style.display != 'none') {
						new Effect.Fade('email_share_form');
					}
				});
			}
			var out_of_stock_link = $('out_of_stock_link');
			if (out_of_stock_link) {
				Event.observe(out_of_stock_link, 'click', function() {
					self.load_attributes_notification_form('product_form');
				});
			}
			var share_email = $('share_email');
			if (share_email) {
				Event.observe(share_email, 'click', function() {
					new Effect.Appear('email_share_form', {duration: .5});
					if ($('notification').style.display!='none') {
						new Effect.Fade('notification', {duration: .5});
					}
				});
				Event.observe('email_share_submit', 'click', function(e) {
					e.stop();
					self.email_share('email_share');
				});
				Event.observe('emails', 'focus', function() {
					this.select();
				});
			}
			var close_buttons = $$('.hide_x');
			if (close_buttons) {
				close_buttons.invoke('observe', 'click', function() {
					new Effect.Fade(this.up(), {duration:.5});
				});
			}
		},
		switch_to_head_attribute: function(attribute_label) {
			if (this.head_attribute[attribute_label]) {
				this.current_attributes[0] = false;
				this.attribute_selects[0].value = this.head_attribute[attribute_label];
				this.update_attributes(true);
				if (this.current_head_attribute != attribute_label) {
					this.current_head_attribute = attribute_label;
					this.switch_to_inventory_tab(attribute_label);
				}
			}
		},
		update_attributes: function(update_images) {
			var cascade = false;
			var attribute_path = [];
			var model_path = false;
			for (var i = 0; i < this.attribute_selects.length; i++) {
				attribute_path[i] = this.attribute_selects[i].value;
				if (cascade || this.current_attributes[i] != attribute_path[i]) {
					this.set_attribute_options(attribute_path);
					this.current_attributes[i] = this.attribute_selects[i].value;
					cascade = true;
				}
				if (this.model_data) {
					if (i == 0) {
						model_path = this.attributes_map[this.attribute_selects[i].value];
					} else if (model_path) {
						if (model_path[this.attribute_selects[i].value]) {
							model_path = model_path[this.attribute_selects[i].value];
						} else {
							model_path = false;
						}
					}
				}
			}
			if (model_path) {
				if (this.price_display) {
					this.price_display.update(this.model_data[model_path].price);
				}
				if (this.stock_display) {
					if (this.model_data[model_path].floating_inventory == 0) {
						this.stock_display.show();
					} else {
						this.stock_display.hide();
					}
				}
				if (this.notification_display) {
					this.notification_display.hide();
				}
			}
			if (update_images) {
				if (Product_page_controller.product_images) {
					Product_page_controller.product_images.update_main_images();
				}
				if (Product_page_controller.product_images) {
					Product_page_controller.product_images.update_gallery();
				}
			}
		},
		set_attribute_options: function(attribute_path) {
			var current_selector = attribute_path.length;
			if (this.attribute_selects[current_selector]) {
				var attribute_node;
				for (var i = 0; i < attribute_path.length; i++) {
					if (i == 0) {
						attribute_node = this.attributes_map[attribute_path[i]];
					} else {
						attribute_node = attribute_node[attribute_path[i]];
					}
					if (!attribute_node) {
						return;
					}
				}
				this.attribute_selects[current_selector].update('');
				for (key in this.attribute_options[current_selector]) {
					if (attribute_node[key]) {
						this.attribute_selects[current_selector].appendChild(this.attribute_options[current_selector][key]);
					}
				}
			}
		},
		switch_to_inventory_tab: function(attribute_label) {
			var inventory_container = $(this.options.inventory_container_prefix + attribute_label);
			if (inventory_container) {
				if (this.inventory_containers) {
					this.inventory_containers.each(function(e) {
						e.hide();
					});
				}
				inventory_container.show();
				if (this.current_head_attribute) {
					$('tab_' + this.current_head_attribute).removeClassName(this.options.inventory_tab_selected);
				}
				$('tab_' + attribute_label).addClassName(this.options.inventory_tab_selected);
				if (this.current_head_attribute != attribute_label) {
					this.current_head_attribute = attribute_label;
					this.switch_to_head_attribute(attribute_label);
				}
			}
		},
		load_notification_form: function(model_id) {
			new Jument.ajax({
				div: 'notification', 
				url: '/ajax/product/stock_notification', 
				options: {
					parameters: {model_id: model_id}
				}
			});
			new Effect.Appear('notification', {duration: .5});
		},
		load_attributes_notification_form: function(frm) {
			new Jument.ajax({
				div: 'notification', 
				url: '/ajax/product/stock_notification', 
				form: frm,
				options: {
					onFailure: function() {
						alert('An error occurred. Please try again.');
					}
				}
			});
			new Effect.Appear('notification', {duration: .5});
		},
		email_share: function(frm) {
			new Jument.ajax({
				url: '/ajax/product/email_share',
				form: frm,
				options: {
					onFailure: function() {
						alert('An error occurred. Please try again.');
					}
				}
			});
		}
	});
	Product_images = Class.create({
		options: {},
		product_data: {},
		attribute_selects: false,
		selectors_active: false,
		group_attributes: false,
		group_attribute_count: 0,
		main_images: {},
		main_images_exist: false,
		main_featured_container: false,
		main_studio_container: false,
		main_artwork_container: false,
		gallery_all_images: {},
		gallery_images: {},
		gallery_index_map: [],
		gallery_image_count: 0,
		gallery_active_node: null,
		gallery_scroll_width: 0,
		gallery_scroll_offset: 0,
		gallery_allow_scroll: true,
		gallery_allow_switch: true,
		gallery_thumb_container: false,
		gallery_image_container: false,
		initialize: function(options) {
			this.set_options(options);
		},
		set_options: function (options) {
			Object.extend(this.options, options || {});
		},
		load_gallery_images: function(images) {
			var index = 0;
			if (!$(this.options.gallery_image_container)) {
				this.gallery_allow_switch = false;
			} else {
				var active_image_id = $$('img.' + this.options.gallery_image_class)[0].readAttribute('id');
				this.gallery_active_node = active_image_id.substring(active_image_id.lastIndexOf('_') + 1);
			}
			for (var key in images) {
				if (images[key].main) {
					images[key].main.image = new Image(images[key].main.width, images[key].main.height);
					images[key].main.image.src = images[key].main.url;
					images[key].main.image.id = this.options.gallery_image_prefix + key;
					images[key].main.image.style.display = 'none';
					images[key].main.image.style.opacity = 0;
					$(images[key].main.image).addClassName(this.options.gallery_image_class);
				}
				if (images[key].thumb) {
					images[key].thumb.image = new Image(images[key].thumb.width, images[key].thumb.height);
					images[key].thumb.image.src = images[key].thumb.url;
					images[key].thumb.image.id = this.options.thumb_prefix + key;
					$(images[key].thumb.image).addClassName(this.options.thumb_class);
				}
				images[key].index = index;
				this.gallery_index_map[index] = key;
				++index;
			}
			this.gallery_all_images = images;
			this.gallery_images = images;
			this.gallery_image_count = index;
		},
		load_main_images: function(images) {
			this.main_images = this.pre_load_main_images(images);
			this.main_images_exist = true;
		},
		pre_load_main_images: function(images) {
			if (images['artwork'] || images['featured'] || images['studio']) {
				for (var key in images) {
					images[key].image = new Image(images[key].width, images[key].height);
					images[key].image.src = images[key].url;
					if (this.product_data['name_long']) {
						if (key == 'featured') {
							images[key].image.alt = this.product_data['name_long'] + ' - model';
						} else if (key == 'artwork') {
							images[key].image.alt = this.product_data['name_long'] + ' - product';
						}
					}
					$(images[key].image).addClassName(this.options.main_image_class);
				}
			} else {
				for (var key in images) {
					images[key] = this.pre_load_main_images(images[key]);
				}
			}
			return images;
		},
		load_product_data: function(product_data) {
			this.product_data = product_data;
		},
		activate_carousel: function() {
			var self = this;
			var li = $$('ul#' + this.options.thumb_container + ' li')[0];
			this.gallery_image_container = $(this.options.gallery_image_container);
			this.gallery_thumb_container = $(this.options.thumb_container);
			this.gallery_scroll_width = li.getDimensions().width + parseFloat(li.getStyle('margin-left')) + parseFloat(li.getStyle('margin-right'));
			Event.observe(this.options.scroll_right_id, 'click', function() {
				self.scroll('left');
			});
			Event.observe(this.options.scroll_left_id, 'click', function() {
				self.scroll('right');
			});
		},
		activate_selectors: function() {
			var self = this;
			this.attribute_selects = $$('div.' + this.options.product_attributes_container + ' select.attributes');
			this.group_attribute_count = this.attribute_selects.length - 1;
			this.main_featured_container = $(this.options.main_featured_container);
			this.main_studio_container = $(this.options.main_studio_container);
			this.main_artwork_container = $(this.options.main_artwork_container);
			this.attribute_selects.invoke('observe', 'change', function() {
				if (!Product_page_controller.product.attribute_selects) {
					self.update_gallery();
				}
			});
			this.selectors_active = true;
		},
		update_main_images: function() {
			if (this.main_images_exist && this.selectors_active) {
				var image_path = false;
				for (var i = 0; i < this.attribute_selects.length - 1; i++) {
					if (image_path) {
						image_path = image_path[this.attribute_selects[i].value];
					} else {
						image_path = this.main_images[this.attribute_selects[i].value];
					}
				}
				this.main_featured_container.update('');
				this.main_featured_container.appendChild(image_path['featured'].image);
				this.main_studio_container.update('');
				this.main_studio_container.appendChild(image_path['studio'].image);
				this.main_artwork_container.update('');
				this.main_artwork_container.appendChild(image_path['artwork'].image);
			}
		},
		update_gallery: function() {
			if (this.selectors_active) {
				if (this.group_attribute_count > 0) {
					// sort / update gallery
					this.sort_gallery();
				} else {
					// just switch gallery image
					var attributes = this.get_attributes();
					var matched = this.get_image_matches(attributes, true);
					if (matched[0]) {
						this.switch_image(matched[0]);
					}
				}
			}
		},
		get_attributes: function() {
			var attributes = [];
			var index = 0;
			this.attribute_selects.each(function(e) {
				attributes[index] = e.value;
				++index;
			});
			return attributes;
		},
		get_image_matches: function(attributes, prioritize_exact_matches) {
			var priority = [];
			var priority_index = 0;
			var images = [];
			var images_index = 0;
			var i;
			var match = true;
			var attribute;
			for (var node in this.gallery_all_images) {
				for (i = 0; i < attributes.length; i++) {
					if (!this.gallery_all_images[node]['attributes'][attributes[i]]) {
						match = false
					}
				}
				if (match) {
					if (prioritize_exact_matches && this.gallery_all_images[node]['attribute_count'] == 1) {
						priority[priority_index] = node;
						++priority_index;
					} else {
						images[images_index] = node;
						++images_index;
					}
				}
				match = true;
			}
			return priority.concat(images);
		},
		sort_gallery: function(set_default) {
			var update = true;
			var group_attributes = [];
			var prioritize_exact_matches = true;
			if (set_default) {
				prioritize_exact_matches = false;
			}
			if (this.group_attribute_count > 0) {
				update = false;
				for (var i = 0; i < this.group_attribute_count; i++) {
					group_attributes[i] = this.attribute_selects[i].value;
					if (group_attributes[i] != this.group_attributes[i]) {
						update = true;
					}
				}
				if (update) {
					var matches = this.get_image_matches(group_attributes, prioritize_exact_matches);
					this.gallery_index_map = [];
					var images = {};
					for (i = 0; i < matches.length; i++) {
						images[matches[i]] = this.gallery_all_images[matches[i]];
						images[matches[i]].index = i;
						this.gallery_index_map[i] = matches[i];
					}
					this.gallery_images = images;
					this.gallery_image_count = matches.length;
				}
			}
			if (update) {
				this.gallery_scroll_offset = 0;
				this.gallery_thumb_container.style.left = 0;
				this.gallery_thumb_container.update('');
				for (var key in this.gallery_images) {
					if (this.gallery_allow_switch) {
						this.gallery_image_container.appendChild(this.gallery_images[key].main.image);
					}
					li = document.createElement('li');
					if (this.options.product_show_info) {
						var a = document.createElement('a');
						a.setAttribute('href', '/' + this.gallery_images[key].product_url);
						a.appendChild(this.gallery_images[key].thumb.image);
						a.appendChild(document.createElement('br'));
						a.appendChild(document.createTextNode(this.gallery_images[key].product_name));
						li.appendChild(a);
					} else {
						li.appendChild(this.gallery_images[key].thumb.image);
					}
					this.gallery_thumb_container.appendChild(li);
				}
				if (this.group_attribute_count > 0) {
					this.switch_image(this.gallery_index_map[0]);
				} else if (this.attribute_selects) {
					var attributes = this.get_attributes();
					var matched = this.get_image_matches(attributes, prioritize_exact_matches);
					if (matched[0]) {
						this.switch_image(matched[0]);
					}
				}
				$(this.options.scroll_left_id).addClassName(this.options.scroll_disabled_class);
				if (this.gallery_image_count > this.options.scroll_images_visible) {
					$(this.options.scroll_right_id).removeClassName(this.options.scroll_disabled_class);
				} else {
					$(this.options.scroll_right_id).addClassName(this.options.scroll_disabled_class);
				}
				this.group_attributes = group_attributes;
				var self = this;
				$$('img.' + this.options.thumb_class).invoke('observe', 'click', function() {
					var id = this.readAttribute('id');
					var node = id.substring(id.lastIndexOf('_') + 1);
					self.switch_image(node);
				});
			}
		},
		switch_image: function(node) {
			if ((this.gallery_allow_switch && node != this.gallery_active_node) || !this.group_attributes) {
				var self = this;
				var old_image = $(this.options.gallery_image_prefix + this.gallery_active_node);
				if (old_image) {
					new Effect.Opacity(old_image, {duration: this.options.image_transition_time, from: 1, to: 0, afterFinish: function() {
						old_image.hide();
					}});
				}
				var new_image = $(this.options.gallery_image_prefix + node);
				if (new_image) {
					new_image.show();
					new Effect.Opacity(new_image, {duration: this.options.image_transition_time, from: 0, to: 1, afterFinish: function() {
						new_image.show();
					}});
				}
				$$('img.' + this.options.thumb_selected_class).each(function(e) {
					e.removeClassName(self.options.thumb_selected_class);
				});
				if (node) {
					$(this.options.thumb_prefix + node).addClassName(this.options.thumb_selected_class);
				}
				this.gallery_active_node = node;
			}
		},
		scroll: function(direction) {
			if (this.gallery_allow_scroll) {
				var self = this;
				if (direction == 'left') {
					if (this.options.scroll_images_visible + this.gallery_scroll_offset < this.gallery_image_count) {
						this.gallery_allow_scroll = false;
						new Effect.MoveBy(this.options.thumb_container, 0, -this.gallery_scroll_width, {duration: this.options.scroll_time, afterFinish: function() {self.gallery_allow_scroll = true}});
						++this.gallery_scroll_offset;
						if (this.options.scroll_images_visible + this.gallery_scroll_offset >= this.gallery_image_count) {
							$(this.options.scroll_right_id).addClassName(this.options.scroll_disabled_class);
						}
						$(this.options.scroll_left_id).removeClassName(this.options.scroll_disabled_class);
					}
					if (this.gallery_allow_switch && this.gallery_images[this.gallery_active_node].index + 1 < this.gallery_image_count) {
						this.switch_image(this.gallery_index_map[this.gallery_images[this.gallery_active_node].index + 1]);
					}
				} else if (direction == 'right') {
					if (this.gallery_scroll_offset > 0) {
						this.gallery_allow_scroll = false;
						new Effect.MoveBy(this.options.thumb_container, 0, this.gallery_scroll_width, {duration: this.options.scroll_time, afterFinish: function() {self.gallery_allow_scroll = true}});
						--this.gallery_scroll_offset;
						if (this.gallery_scroll_offset <= 0) {
							$(this.options.scroll_left_id).addClassName(this.options.scroll_disabled_class);
						}
						$(this.options.scroll_right_id).removeClassName(this.options.scroll_disabled_class);
					}
					if (this.gallery_allow_switch && this.gallery_images[this.gallery_active_node].index > 0) {
						this.switch_image(this.gallery_index_map[this.gallery_images[this.gallery_active_node].index - 1]);
					}
				}
			}
		}
	});
}});