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 = 20;
var orig_per_page = per_page;
var page_num = 1;
var cid_click = '';
var orig_cids = '';
var cats = {};
var total_pages = 0;
var dd_options = '';
	
function getProducts(cids,page_num,reset_filters) {
	
	cat_name = load_from_url();
	
	var offset = 0;
	var limit = 10000;
	
	if (typeof(custom) !== "undefined") {
		cids = cq;
	} else {
		if (cids.length > 0) {
			cids = '(category_ids:' + cids.replace(/,/g,'+or+category_ids:') + ")";	
			cid_click = cids;
		} else {
			cids = cid_click;
		}
	}
	
	$j('.dd_filter').each(function() {
		var id = $j(this).attr('id');
		$j('#' + id + ' option:selected').each(function() {
			var val = $j(this).val();
			if (val != '0') {
				switch (id) {
					case 'size':
						cids += "+AND+size_facet_attribute:" + val;
						break;
					case 'price':
						var split_val = val.split(':');
						cids += '+AND+price:[' + split_val[0] + '+TO+' + split_val[1] + ']';
						break;
					default:
						break;
				}
			}
		});
	});
	
	if (page_num > 0) {
		offset = (page_num-1) * per_page;
		limit = per_page;
	}
	
	var search_query = "/aca_search/?q=" + cids + "&s=" + offset + "&r=" + limit + "&f=size_facet_attribute";
		
	//var search_query = "/search/products/select/?" + Math.floor(Math.random()*1001).toString() + "&wt=json&q=" + cids + "&version=2.2&start=" + offset + "&rows=" + limit + "&indent=on";
	//search_query += '&facet.field=size_varchar_attribute&facet=true';
	
	//alert(search_query);
	
	$j.getJSON(search_query,
		function(data) {
			var item_code = '';
			var cnt = 1;
			if (data.response.numFound > 0) {
				
				if (reset_filters == 1) {
					if (!populate_size_dd(data.facet_counts)) {
						$j('#size').hide();
					}
				}
				
				$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++;
					});
				
				total_pages = Math.ceil(data.response.numFound / per_page);
						
				if (total_pages > 1) {
					$j('#page_next_image').show();
				}
				
				$j('.cat_nav').show();
				page_links(per_page, data.response.numFound);
				$j('#product_list').html(item_code);
			} else {
				if (reset_filters == 1) {
					$j('.cat_nav').hide();
				}
				
				page_links(per_page, 0);
				$j('#product_list').html('<p>No Products Found</p>');
			}
		});
}

function populate_size_dd(facets) {
	var fields = facets.facet_fields;
	
	if (window.location.href.indexOf("men.html") == -1) {
		if (isdefined(fields.size_facet_attribute)) {
			if (fields.size_facet_attribute.length > 0) {
				var length = fields.size_facet_attribute.length;
				$j('#size').html('<option value="0">Shop by Size</option>');
				for (var i = 0; i < length; i++) {
					if (fields.size_facet_attribute[i+1] > 0) { //check count of facet
						$j('#size').append('<option value="' + fields.size_facet_attribute[i] + '">' + fields.size_facet_attribute[i] + '</option>');
					}
					i++; // needed to skip over the "count" of sizes in collection
				}
				return true;
			}
		}
	}
	return false;
}

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

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

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

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

function dd_filter() {
	page_num = 1;
	getProducts(orig_cids,page_num,0);
}

function get_product_node_html(item) {	
	var html_code = '';	
	html_code += '<li>'
	html_code += '<h3><a href="' + item.product_url_stored_text_attribute + '">' + item.name + '</a></h3>'
	html_code += '<p><span class="price">$' + addCommas(item.price.toFixed(2)) + '</span> <a href="' + item.product_url_stored_text_attribute + '" class="more">View Details</a></p>';
	html_code += '<a href="' + item.product_url_stored_text_attribute + '">';
	if (isdefined(item.small_image_stored_text_attribute)) {
		if(base_bucket_url.length>0){
			item.small_image_stored_text_attribute=new String(item.small_image_stored_text_attribute).replace('http://lrg-dev.acacloud.com',base_bucket_url);
		}
		html_code += '<img src="' + item.small_image_stored_text_attribute + '" alt="' + item.name + '" />';
	} else {
		html_code += '<img src="/skin/frontend/default/lrg/images/no_image_cat.jpg" />';
	}
	html_code += '</a>';
	html_code += '</li>';
	return html_code;
}

function page_links(results_per_page, num_results) {
	var page_code = '';
	
	if (num_results > 0) {

		/*
		if (page_num > 1) {		
			page_code += '<span class="prev_next_nav"><a href="#" onclick="go_to_page(';
			page_code += (page_num-1).toString();
			page_code += '); return false;"><img src="/skin/frontend/default/lrg/images/arrow_sm_left.gif" id="prevpage" alt="Previous Page" /></a></span>';
		} else {
			page_code += '<span class="prev_next_nav">&nbsp;</span>';
		}
		
		page_code += '<span>' + page_num.toString() + ' of <a href="#" onclick="go_to_page(' + total_pages + '); return false;">' + total_pages + '</a></span> ';
		
		if (page_num < total_pages) {
			page_code += '<span class="prev_next_nav"><a href="#" onclick="go_to_page(';
			page_code += (page_num+1).toString();
			page_code += '); return false;"><img src="/skin/frontend/default/lrg/images/arrow_sm_right.gif" id="nextpage" alt="Next Page" /></a></span>';
		} else {
			page_code += '<span class="prev_next_nav">&nbsp;</span>';
		}
		
		if (total_pages > 1) {
			page_code += '<span><a href="#" onclick="view_all()">View All</a></span>';
		}
		*/
		
		var count_start = 1, count_end = 5, print_end = true;
	
		if (total_pages > 1 && page_num > 1) {
			page_code += '<span class="prev_next_nav"><a href="#" onclick="go_to_page(';
			page_code += (page_num-1).toString();
			page_code += '); return false;"><img src="/skin/frontend/default/lrg/images/arrow_sm_left.gif" id="prevpage" alt="Previous Page" /></a></span>';
		} else {
			page_code += '<span class="prev_next_nav">&nbsp;</span>';
		}
		
		if (page_num > 5) {
			page_code += '<span class="pl"><a href="#" onclick="go_to_page(1); return false;">1</a></span> <span class=\"dots\">...</span> ';
			count_start = page_num - 2;
			count_end = page_num + 2;
		}
		
		for (var i = count_start; i <= count_end; i++) {
			if (i == page_num) {
				page_code += '<span class="pl"><a class="current">' + i + '</a></span> ';
			} else if (i <= total_pages) {
				page_code += '<span class="pl"><a href="#" onclick="go_to_page(' + i + '); return false;">' + i + '</a></span> ';
			}
		}
		
		if (total_pages - page_num > 3 && 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 class="prev_next_nav"><a href="#" onclick="go_to_page(';
			page_code += (page_num+1).toString();
			page_code += '); return false;"><img src="/skin/frontend/default/lrg/images/arrow_sm_right.gif" id="nextpage" alt="Next Page" /></a></span>';
		} else {
			page_code += '<span class="prev_next_nav">&nbsp;</span>';
		}
		
		if (total_pages > 1) {
			page_code += '<span><a href="#" onclick="view_all()">View All</a></span>';
		}
		
	} else {
		page_code += ' 0';
	}
	
	$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;
}