/**
 * @author alexlindsay
 */

/*
function categoryClick(id) {
	var categoryNum = id.slice(id.lastIndexOf('_')+1);
	document.location = '/category/' + categoryNum;
}

function productClick(id) {
	var productNum = id.slice(id.lastIndexOf('_')+1);
	jQuery('#container_1_1_2_1').load(
		'/module_functions.php',
		{
			is_ajax: true,
			method_key: '2f32533edba075e98064167eba2dd83c',
			method_name: 'product_details',
			product_id: productNum
		}
	);
}

function addProductToCart() {
	var customerId;
	if (null != jQuery.cookie('customer_id')) {
		customerId = jQuery.cookie('customer_id');
	} else {
		customerId = 100000000 + Math.floor(Math.random()*1000000000);
		jQuery.cookie('customer_id', customerId, { path: '/' });
	}
	var productId = jQuery('#product_id').val();
	var productQuantity = jQuery('#quantity').val();
	var productPrice = jQuery('#product_price').val();
	jQuery.ajax({
		url: '/module_functions.php',
		type: 'POST',
		data: {
			customer_id: customerId,
			product_id: productId,
			quantity: productQuantity,
			price: productPrice
		}
	});
}

jQuery(function() {
	jQuery('.category').hover(
		function() {
			addHover(jQuery(this));
		},
		function() {
			removeHover(jQuery(this));
		}
	).click(function() {
		var id = jQuery(this).attr('id');
		categoryClick(id);
	});
	jQuery('.product').hover(
		function() {
			addHover(jQuery(this));
		},
		function() {
			removeHover(jQuery(this));
		}
	).click(function() {
		var id = jQuery(this).attr('id');
		productClick(id);
	});
	jQuery('#add_to_cart').live('click', function(){
		addProductToCart();
	});
	// select either the designated product fromt the url or
	// the first product if the product list is shown:
	if (jQuery('.product').length > 0) {
		var productId = jQuery.url.segment(2);
		if ( productId != null) {
			jQuery('#product_' + productId).click();
		} else {
			jQuery('.product:first').click();
		}
	}
});
*/

function getPageProducts(selector) {
	var productKey = 'products_id='
	var rows = jQuery(selector);
	var products = new Array();
	jQuery(rows).each(function() {
		var product = new Object();
		product['href'] = jQuery(this).find('td:eq(0) a').attr('href');
		product['product_id'] = product['href'].substr(product['href'].indexOf(productKey)+productKey.length);
		product['image'] = jQuery(this).find('td:eq(0) img').attr('src');
		product['title'] = jQuery(this).find('td:eq(0) img').attr('alt');
		product['price'] = jQuery(this).find('td:eq(2)').text();
		product['buy_href'] = jQuery(this).find('td:eq(3) a').attr('href');
		product['buy_image'] = jQuery(this).find('td:eq(3) img').attr('src');
		product['buy_title'] = jQuery(this).find('td:eq(3) img').attr('alt');
		products.push(product);
	});
	return products;
}

function setupProducts() {
	jQuery('.product').click(function() {
		document.location = jQuery(this).find("a:first").attr('href');
	}).hover(function(){
		addHover(jQuery(this));
		highlightProduct(jQuery(this));
	}, function(){
		removeHover(jQuery(this));
	});
}

function highlightProduct(target) {
	jQuery('#container_1_1_2_1').html(
		"<div class=\"product_detail\">\n"
		+ target.html()
		+ "</div>"
	).find('.product_name').prependTo('.product_detail');
	jQuery('#container_1_1_2_1')
		.find('.product_detail')
			.append('<div class="product_back"></div>')
			.find('.product_back')
				.append('<a></a>')
				.find('a')
					.attr('href', '/catalog/categories.php')
					.attr('title', ' Back to: Categories ')
					.text('Back to: Categories');			
}

function reformatProducts() {
	products = getPageProducts('#container_1_1_2_2 table table tr')
	jQuery('#container_1_1_2_2').empty();
	for (var i=0; i < products.length; i++) {
		product = products[i];
//		jQuery('#container_1_1_2_2').append('<hr /><h3>Product #' + i + '</h3>' + "\n");
//		for (key in product) {
//			jQuery('#container_1_1_2_2').append('<p>' + key + ': ' + product[key] + '</p>' + "\n");
//		}
		jQuery('#container_1_1_2_2')
			.append('<div id="product_' + product.product_id + '" class="product"></div>' + "\n")
			.find('#product_' + product.product_id)
				.append('<div class="product_inner"></div>')
				.find('.product_inner')
					.append('<div class="product_image"></div>')
					.find('.product_image')
						.append('<img width="165" height="123"/>')
						.find('img')
							.attr('src', product.image)
							.attr('alt', product.title)
							.attr('title', ' ' + product.title + ' ').end().end()
					.append('<div class="product_name" ><a /></div>')
					.find('a')
						.text(product.title)
						.attr('href', product.href).end()
					.append('<div class="product_price" />')
					.find('.product_price')
						.text(product.price).end()
					.append('<div class="product_buy" />')
					.find('.product_buy')
						.append('<a><img /></a>')
						.find('a')
							.attr('href', product.buy_href)
							.attr('title', ' ' + product.buy_title + ' ')
							.find('img')
								.attr('src', product.buy_image)
								.attr('alt', product.buy_title)
								.attr('title', ' ' + product.buy_title + ' ')
								.attr('border', '0')
		;
	}
}

jQuery(function() {
	var productKey = 'products_id=';
	jQuery('#container_1_1_2_2')
		.find('table tr:first').remove().end()
		.find('table tr:first').remove().end()
		.find('table table tr:first').remove().end()
		.find('table table:last').remove().end();
	reformatProducts();
	setupProducts();
	highlightProduct(jQuery('.product:first'));
});

