var CatalogueInfo = function(){
	return {
		init : function(){
			this.repopulate();
		},
		
		repopulate: function() {

			if (SolidCMS.catalogue)  {
				if (SolidCMS.catalogue.list) {
					//Fill out the category combo
					var categoryEl = document.getElementById('infoCategory');
					categoryEl.options.length = 0;
					categoryEl.options.add(new Option('Please select a category', ''));

					var productEl = document.getElementById('infoProduct');
					productEl.options.length = 0;
					productEl.options.add(new Option('', ''));

					var imageEl = document.getElementById('infoImage');
					imageEl.options.length = 0;
					imageEl.options.add(new Option('', ''));

					for(var categoryIndex=0, categoryCount = SolidCMS.catalogue.list.length; categoryIndex<categoryCount; categoryIndex++) {
						var category = SolidCMS.catalogue.list[categoryIndex];

						categoryEl.options.add(new Option(category.title, category.id));
						
						if (category.id == SolidCMS.catalogue.selected.category) {
							categoryEl.selectedIndex = categoryIndex + 1;
							
							//Fill out the products with the selected item
							if (category.products) {
								for(var productIndex=0, productCount = category.products.length; productIndex<productCount; productIndex++) {
									var product = category.products[productIndex];
	
									productEl.options.add(new Option(product.title, product.id));
									
									if (product.id == SolidCMS.catalogue.selected.product) {
										productEl.selectedIndex = productIndex + 1;
										
										//Fill out the images with the selected item
										if (product.images) {
											for(var imageIndex=0, imageCount = product.images.length; imageIndex<imageCount; imageIndex++) {
												var image = product.images[imageIndex];
		
												imageEl.options.add(new Option(image.title, image.id));
												
												if (image.id == SolidCMS.catalogue.selected.image) {
													imageEl.selectedIndex = imageIndex + 1;
												}
												
											}
										}
									}
								}
							}
						}
						
					}
					
				}
			}
			
		},
		
		onChange: function(cmb) {

			//Determine which item has changed
			switch (cmb.id) {
			case 'infoCategory':
				SolidCMS.catalogue.selected.category = cmb.options[cmb.selectedIndex].value;
				break;
			case 'infoProduct':
				SolidCMS.catalogue.selected.product = cmb.options[cmb.selectedIndex].value;
				break;
			case 'infoImage':
				SolidCMS.catalogue.selected.image = cmb.options[cmb.selectedIndex].value;
				break;
			}
			
			//Repopulate the combos
			this.repopulate();
			
		return true;
		}
	};
}();

window.onload = function() { CatalogueInfo.init(); }