// JavaScript for the client-side products database

/*
A product must store:

- product name
- product page filename
- small image name
- large image name

These functions can generate:
- a gallery
*/

function Products()
{
	this.products = new Array();
	this.add = Products_add;
	this.find = Products_find;
	this.write = Products_write;
	this.writeLargeImage = Products_writeLargeImage;
	this.writeLargeLabel = Products_writeLargeLabel;
}

// Add a new product to the database
function Products_add(name,url,small_image,large_image,on_sale)
{
	var product = new Product(name,url,small_image,large_image,on_sale);
	
	// Add sizes
	for(var i = 5;i < arguments.length;i++) {
		product.sizes[i - 5] = arguments[i];
	}
	
	this.products[this.products.length] = product;
}

// Find a product
function Products_find(name)
{
	for (var i in this.products) {
		var p = this.products[i];
		if(p.name == name) {
			return p;
		}
	}
	return null;
}

// Write the product gallery HTML
function Products_write()
{
	var content = "";
	var cols = 3;
	
	content += '<table border="0" cellpadding="0" cellspacing="0" id="product_gallery">';
	
	var i = 0, n = this.products.length;
	var rows = Math.ceil(n / cols);
	var row = 0;
	
	while(i < n) {
		
		content += "<tr>";
		
		col = 0;
		while(col < cols) {
			content += '<td>';
			
			if(i < n) {
				var p = this.products[i];
				
				// Product image
				content += '<div class="pic">';
				content += '<a href="' + p.url + '">';
				content += '<img src="' + p.small_image + '">';
				content += '</a>';
				content += '</div>';
				
				// Maybe it's on sale?
				if(p.on_sale) {
					content += '<div class="sale"><img src="images/sale.gif"></div>';
				}
				
				// Label
				content += '<div class="dots"><img src="images/dots_196.gif"></div>';
				content += '<div class="label">' + '<a href="' + p.url + '">' + p.name + '</a>' + '</div>';
				content += '<div class="dots"><img src="images/dots_196.gif"></div>';
			}
			
			content += "</td>\n";
			
			i++;
			col++;
		}
		
		content += "</tr>\n";
		
		row ++;
	}
	
	content += '</table>';

	document.writeln(content);
}

function Products_writeLargeImage(name)
{
	var p = this.find(name);
	var content = "";
	
	if(p != null) {
		content += '<img src="' + p.large_image + '">';
	}
	
	document.writeln(content);
}

function Products_writeLargeLabel(name)
{
	var p = this.find(name);
	var content = "";
	
	if(p != null) {
		
		// Maybe write "on sale" too
		if(p.on_sale) {
			content += '<div class="sale"><img src="../products/images/on_sale.gif"></div>';
		}
		
		// Make the dots and the label
		content += '<div class="dots"><img src="../products/images/dots_230.gif"></div>';
		content += '<div class="label">' + p.name + '</div>';
		content += '<div class="dots"><img src="../products/images/dots_230.gif"></div>';
	}
	
	document.writeln(content);
}

function Product(name,url,small_image,large_image,on_sale)
{
	this.name = name;
	this.url = url;
	this.small_image = small_image;
	this.large_image = large_image;
	if(on_sale != null) {
		this.on_sale = true;
	}
	else {
		this.on_sale = false;
	}
	this.sizes = new Array();
}

var products = new Products();