//setup the namespace
if (typeof crumbs == "undefined" || !crumbs) {
    var crumbs = {};
}

crumbs.Catalog = Class.create();
crumbs.Catalog.prototype = {
	initialize: function(element) {
		this.products = null;
		this.element = $(element);
		this.categoryDescription = $("category-description");
		this.categoryCount = $("number-products");
		this.initProducts();
		this.duration = 1;
	},
	
	initProducts: function() {
		if(!initialProducts) {
			$(this.categoryCount.parentNode).setStyle({display: 'block'});
			return;
		}
		this.addProducts(initialProducts);
	},

	updateProducts: function(newProducts) {
		this.clearProducts();
		this.addProducts(newProducts);
		this.hideLoadingStatus();
	},

	addProducts: function (productArray) {
		if(!this.products)  
			this.products = new Array();

		var template = new Template($('product_template').innerHTML);
		for(var i=0; i<productArray.length; i++) {
			this.products[i] = new crumbs.Product(productArray[i], template);
			this.products[i].setDisplayOrder(i);
		}
		this.renderProducts();
		$(this.categoryCount.parentNode).setStyle({display: 'block'});
		if (this.products.length) 
			this.categoryCount.innerHTML = this.products.length;
		else
			this.categoryCount.innerHTML = "0";

		if (!Object.isUndefined(screenManager))
			screenManager.adjustColumnHeights();
	},
	
	showLoadingStatus: function() {
		if(this.categoryDescription && !this.categoryDescription.hasClassName("filtered"))
			Effect.Fade(this.categoryDescription, { to: 0.1 });
		Effect.Fade(this.element, { to: 0.1 });
	},
	
	hideLoadingStatus: function() {
		//hide the category description
		this.hideCategoryDescription();
		Effect.Appear(this.element);
	},
	
	hideCategoryDescription: function() {
		if(this.categoryDescription && this.categoryDescription.getStyle('display')!='none') {
			Effect.Squish(this.categoryDescription, {duration: this.duration});
			setTimeout("screenManager.adjustColumnHeights()", parseFloat(this.duration) * 1000 + 100);  // do this after the height changes
		}
	},
	
	renderProducts: function() {
		for(var i=0; i<this.products.length; i++) {
			this.products[i].render(this.element);
		}
	},

	clearProducts: function () {
		if (this.products) { 
			for(var i=0; i<this.products.length; i++) {
				this.products[i].destruct();
			}
			this.products = new Array();		
		}
	},

	getProducts: function() {
		return this.products;
	},

	getAdjacentProduct: function(direction) {
		if(direction=="prev") {
			
		} else if(direction=="next") {
			
		}
	}
};