BT.ns(function() {with(BT) {
	Product = Class.create({
		initialize: function(options) {
			this.setOptions(options); //initialize the default options
			
			this.selects = {};
			this.container = $(this.options.container);
		},
		
		loadModels: function(models, modelMap) {
			this.models = models;
			this.modelMap = modelMap;
		},
		
		loadAttributes: function(attributes) {
			this.attributes = attributes;
		},
		
		loadAttributeValues: function(attributeValues) {
			this.attributeValues = attributeValues;
		},
		
		generateTypes: function() {
			var select = $('priceTypeSelect');
			var defaultTypeId = null;
			
			if(select != null){
				for(typeId in this.models) {
					var option = document.createElement('option');
					option.innerHTML = this.attributeValues[typeId].long_value;
					option.value = typeId;
					
					delete(option);
					
					if(defaultTypeId == null) {
						defaultTypeId = typeId;
					}
					
					if(document.location.hash.toLowerCase() == '#'+this.attributeValues[typeId].short_value) {
						defaultTypeId = typeId;
						option.setAttribute('selected', 'selected');
					}
					
					select.appendChild(option);
					
				}
	
				this.updateColors(defaultTypeId);
			}
			
			delete(select);
			delete(defaultTypeId);
		},
		
		updateColors: function(typeId) {
			var select = $('priceColorSelect');
			var defaultColorId = null;
			
			if(select != null){
				select.innerHTML = ''; // clear out the current prices
				
				for(colorId in this.models[typeId]) {
					option = document.createElement('option');
					option.innerHTML = this.attributeValues[colorId].long_value;
					option.value = colorId;
					select.appendChild(option);
					
					delete(option);
					
					if(defaultColorId == null) {
						defaultColorId = colorId;
					}
				}
				
				this.updateSizes(typeId, defaultColorId);
				
				// Update the size chart tabs
				sizeChartTabs.tabClick(null, $('tab_'+this.attributeValues[typeId].short_value+'_chart'));
			}
			delete(select);
			delete(defaultColor);
		},
		
		updateSizes: function(typeId, colorId) {
			var select = $('priceSizeSelect');
			var models = this.models[typeId][colorId];
		
			var defaultPriceModel = null;

			if(select){
				select.innerHTML = ''; // clear out the current prices
				
				var model = '';
				for(sizeId in models) {
					model = this.modelMap[models[sizeId]];
					option = document.createElement('option');
					option.innerHTML = this.attributeValues[sizeId].long_value;
					option.value = model.id;
					select.appendChild(option);
					
					delete(option);
					
					if(defaultPriceModel == null) {
						defaultPriceModel = model.id;
					}
				}
				
				this.updatePriceDisplay(defaultPriceModel);
				
				// Update the inventory chart
				inventoryTabs.tabClick(null, $('tab_'+typeId));
				eval("ColorTabs"+typeId).tabClick(null, $('tab_'+typeId+'_'+colorId));
				
				ProductImages.update(typeId, colorId); // update the images
			}

			delete(select);
			delete(models)
			delete(defaultPrice);
		},
		
		changeDropdown: function(type, value) {
			var select = $('price'+type+'Select');
			
			if(select){
				select = select.childElements();
				select.each(function(option) {
					var elm = $('price'+type+'Select');
					if(option.value == value && elm.value != value) {
						option.selected = true;
						elm.onchange();
					}
				});
			}
		},
		
		updatePriceDisplay: function(modelId) {
			var select = $('priceDisplayValue');
			if(select)
				select.update(this.modelMap[modelId].price);
		},
		
		setOptions: function (options) {
			this.options = {
				container: 'modelOptions'
			};
			Object.extend(this.options, options || {});
		}
	});
	
	
	
	ProductImages = {
		load: function(images, attributes, gallery) {
			this.attributes = attributes;
			this.images = images;
			this.gallery = gallery;
			
			this.imageGallery = new DynamicImageGallery();
		},
		
		update: function(typeId, colorId) { 
			function sort_array_map_numericly(a, b){
				return (b[1]-a[1]); 
			}

			var matches = new Hash();
			var counter, matched_images = [];
			var matched_images;
			
			for(nodeId in this.attributes) {
				counter = 0;
				for(attrId in this.attributes[nodeId]) {	
					if(this.attributes[nodeId][attrId] == typeId || this.attributes[nodeId][attrId] == colorId)
						counter++;
				}
				
				matches.set(nodeId, counter);
			}
			
			matches = matches.map().sort(sort_array_map_numericly);
			
			for(var x = 0, count = matches.length; x < count; x++) {
				matched_images[matches[x][0]] = this.gallery[matches[x][0]];
			}

			this.imageGallery.load(matched_images);
			
			var elm, images = this.images[typeId][colorId];
		
			for(frame in images) {
				if(frame != 'gallery') {
					elm = $('img_'+frame);

					if(elm) {
						elm.innerHTML = '';
						
						var img = document.createElement('img');
						
						img.src = eval('images.'+frame+'.url');
						img.width = eval('images.'+frame+'.width');
						img.height = eval('images.'+frame+'.height');
						Element.addClassName(img,'gallery_img');
						
						if(frame != 'featured') {
							var large = eval('images.'+frame+'.large');
							this.imageGallery.addMain(large.node_id, large);
							this.imageGallery.register(img, large.node_id);
						}
						
						elm.appendChild(img);
						
						if(frame == 'artwork') {
							this.imageGallery.showImage(null, large.node_id);
						}
						
						delete(large);
					}
				}
			}
		
			if(slideGallery)
				slideGallery.allLeft()
				
			delete(matches);
			delete(matched_images);
			delete(counter);
			delete(elm);
			delete(images);
		}
	};
	
	
	
	loadNotificationForm = 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});
		
	};
	
	loadEmailShareForm = function(product_id) {
		
		new Jument.ajax({
			div: 'email_share_form', 
			url: '/ajax/product/email_share_form', 
			options: {
				parameters: {product_id: product_id}
			}
		});


		new Effect.Appear('email_share_form', {duration:.5});
		if($('notification').style.display!='none'){
			new Effect.Fade('notification', {duration:.5});
		}
		
	};
	
	emailShare = function(frm) {
		new Jument.ajax({
			url: '/ajax/product/email_share',
			form: frm,
			options: {
				onFailure: function() { alert('An error occurred. Please try again.'); }
			}
		});
	};
	
}});