function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

var per_page = 16;
var orig_per_page = per_page;
var page_num = 1;
var cid_click = '';
var orig_cids = '';
var cats = {};
	
function getProducts(cids,page_num) {
	
	cat_name = load_from_url();
	
	var offset = 0;
	var limit = 10000;
	
	if (cids.length > 0) {
		cids = cids.replace(/,/g,'+or+category_ids:');
		cid_click = cids;
	} else {
		cids = cid_click;
	}
	
	if (page_num > 0) {
		offset = (page_num-1) * per_page;
		limit = per_page;
	}
	
	var search_query = "/aca_search/?q=category_ids:" + cids + "&s=" + offset + "&r=" + limit + "&sort=product_id+desc"
	
	//alert(search_query);
	
	$j.getJSON(search_query,
		function(data) {
			var item_code = '';
			var cnt = 1;
			if (data.response.numFound > 0) {
				
				$j.each(
					data.response.docs,
					function(i, item) {
						item_code += get_product_node_html(item);
						if (cnt == 4) {
							item_code += '<div class="clear"><!-- --></div>';
							cnt = 0;
						}
						cnt++;
					});
				
				/*
				var total_pages = Math.ceil(data.response.numFound
						/ per_page);
						
				if (total_pages > 1) {
					$j('#page_next_image').show();
				}
				*/
				
				page_links(per_page, data.response.numFound)
				$j('#product_list').html(item_code);
				$j('.viewall').show();
				$j('.cat_zoom_img').mouseout(function() {
					$j(this).hide();
				});
				$j('.prod_zoom').mouseover(function() {
					var new_id = $j(this).attr('id') + '_z';
					var pos = $j(this).offset();
					var width = $j(this).width();
					if ($j.browser.msie && $j.browser.version < 8) {
						$j('#' + new_id).css({ 
							"left": (pos.left + width - 218) + "px",
							"top": (pos.top - 48) + "px"
						});
					} else {
						$j('#' + new_id).css({ 
							"left": (pos.left + width - 218) + "px",
							"top": (pos.top - 218) + "px"
						});
					}
					$j('#' + new_id).show();				
				});
				//$j("#collection option[value='" + load_category_id + "']").attr('selected', 'selected');
			} else {
				$j('#product_list').html('<h3>No Products Found</h3>');
				$j('.pages').html('');
				$j('.viewall').hide();
				/*
				$j('#page_of').html('0');								
				$j('#page_prev_image').hide();
				$j('#page_next_image').hide();
				*/
			}
		});
}

function go_to_page(to_page_num) {
	page_num = to_page_num;
	getProducts('',to_page_num);
}

function filter(cids,to_page_num) {
	per_page = orig_per_page;
	page_num = to_page_num;
	getProducts(cids,page_num);
}

function view_all() {
	per_page = 10000;
	getProducts('',1);
}

function isdefined (variable) {
    return (typeof(variable) !== "undefined") ? true : false;
}


function get_product_node_html(item) {	
	var html_code = '';
	html_code += '<div class="item"><div class="visual">';
	if (isdefined(item.small_cat_image_170x170_stored_text_attribute)) {
		html_code += '<a href="' + item.product_url_stored_text_attribute + '" class="prod_zoom" id="img_' + item.product_id + '"><img src="' + item.small_cat_image_170x170_stored_text_attribute + '" alt="' + item.name + '" /></a>';
		html_code += '<div class="invisible cat_zoom_img" id="img_' + item.product_id + '_z"><a href="' + item.product_url_stored_text_attribute + '"><img src="' + item.big_cat_image_250x250_stored_text_attribute + '" alt="' + item.name + '" /></a></div>';
	} else {
		html_code += '<a href="' + item.product_url_stored_text_attribute + '"><img src="/skin/frontend/default/afaze/images/no_image.jpg" /></a>';
	}
	html_code += '</a>';
	html_code += '</div><!--/.visual-->';
	html_code += '<div class="description">';
	html_code += '<h3><a href="' + item.product_url_stored_text_attribute + '">' + item.name + '</a></h3>';
	if (isdefined(item.short_description)) {
		html_code += '<p>' + item.short_description + '</p>';
	}
	html_code += '<p class="price">$' + addCommas(item.price.toFixed(2)) + '</p>';
	html_code += '</div><!--/.description-->';
	html_code += '</div><!--/.item-->';
	return html_code;
}

function page_links(results_per_page, num_results) {
	var total_pages = Math.ceil(num_results / results_per_page);
	var page_code = '';
	
	var count_start = 1, count_end = 5, print_end = true;
	
	//if ((total_pages > 1) && (page_num  total_pages) {
	if (total_pages > 1 && page_num > 1) {
		page_code += '<span><a href="#" onclick="go_to_page(' + (page_num-1) + '); return false;">&laquo; Previous</a></span> ';
	}
	
	if (page_num > 5) {
		page_code += '<span><a href="#" onclick="go_to_page(1); return false;">1</a></span> <span class=\"dots\">...</span> ';
		count_start = page_num - 3;
		count_end = page_num + 1;
	}
	
	for (var i = count_start; i <= count_end; i++) {
		if (i == page_num) {
			page_code += '<span class="current">' + i + '</span> ';
		} else if (i <= total_pages) {
			page_code += '<span><a href="#" onclick="go_to_page(' + i + '); return false;">' + i + '</a></span> ';
		}
	}
	
	if (count_end == total_pages - 2) {
		count_end++;
		print_end = false;
	}
	
	if (total_pages - page_num > 2 && print_end && total_pages > 5) {
		page_code += ' <span class="dots">...</span> <span><a href="#" onclick="go_to_page(' + total_pages + '); return false;">' + total_pages + '</a></span> ';
	}

	if (page_num < total_pages) {
		page_code += '<span><a href="#" onclick="go_to_page(' + (page_num+1) + '); return false;">Next &raquo;</a></span> ';
	}
	
	$j('.pages').html(page_code);
}

function load_from_url() {
	var cat_name = window.location.toString();
	cat_name = cat_name.substring(cat_name.indexOf('#')+1,cat_name.length);
	
	return cat_name;
}