$(function(){
	var data;
	var attributes = new Object();
	
	sorts = {
	new2old:function(a,b){
			if (b.id == null || a.id == null || isNaN(b.id) || isNaN(a.id)) return false;
			return parseInt(b.id) - parseInt(a.id) ;  
			// return b.livedate > a.livedate ? 1 : -1; 
			
		},
	old2new:function(a,b){
			if (b.id == null || a.id == null || isNaN(b.id) || isNaN(a.id)) return false;
			// return parseInt(a.id) - parseInt(b.id) ;  
			return a.livedate > b.livedate ? 1 : -1;  
		},
	name:function(a,b){
			if (b.name == null || a.name == null) return false;
			return a.name > b.name ? 1 : -1;  
		},
	des:function(a,b){
			if (b.manufacturer == null || a.manufacturer == null) return false;
			return a.manufacturer > b.manufacturer ? 1 : -1;  
		},
	pricel2h:function(a,b){
			if (b.price == null || a.price == null) return false;
			return parseFloat(a.price.replace('$','')) > parseFloat(b.price.replace('$','')) ? 1 : -1;  
		},
	priceh2l:function(a,b){
			if (b.price == null || a.price == null) return false;
			return parseFloat(b.price.replace('$','')) > parseFloat(a.price.replace('$','')) ? 1 : -1;  
		}
	};

	function filter(){
		$("#cat_content").children().remove();
		$.each(data, function(i, v){
			if (v.id != null){
				var cart = "<div id=\"category_item\">"
				cart += "<div class=\"title\" style=\"border:2px solid #B4BD1E; padding:6px 10px;\">";
				cart += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\"><tr>";
				cart += "<td align=\"center\">";
				cart += "<a href=\"/category/"+v.id+"\" style=\"font-size:16px;\">"+v.name+"</a>";
				cart += "</td><td>";
				cart += "<a href=\"/category/"+v.id+"\" style=\"font-size:16px;\"><img src=\"/images/category_arrow.gif\" alt=\"&rsaquo;\" border=\"0\" align=\"left\" style=\"padding-left:4px;padding-top:1px\"></a>";
				cart += "</td></tr>";
				cart += "</table></div></div><div class=\"clear\"></div>";
				$("#cat_content").append($(cart));			
			};
			if (v.products != null){
				products = v.products.sort(sorts[$("#sort_by").val()]);
				
				var products2 = $.grep(products, function(value){
					for (var it in attributes){
						if ($("#shop_by_"+it+" :selected").val() > 0){
							if (value.attributes != null){
								for(var i = 0; i < value.attributes.length; i++){
									var attr = value.attributes[i];
									if (attr.name != null && attr.name == it){
										if (attr.options != null){
											for(var o = 0; o < attr.options.length; o++){
												var opt = attr.options[o];
												if (opt.name != null && $("#shop_by_"+attr.name+" :selected").val() == opt.id){
													return true;
												};
											};
											return false;
										};
									};
								};
							};
						return false;
						};
					};
					return true;
				});
				
				$.each(products2, function(j, value){
					
					if (value.id != null){
						var icon = "<div class=\"category-product\"><div class=\"category-product-inner\"><div style=\"min-height:150px\">";
						if (value.exclusive == 1) {
							icon += '<div id="exclusive_banner"><img src="/2010/images/exclusive_banner.png" alt="Exclusive" width="107" height="68" border="0" /></div>';
						}
						icon += "<a href=\"" + value.url + "\" title=\""+ value.title+"\">";
						icon += "<img src=\""+value.small+"\" " + (value.height != null ? "height="+value.height : "") +" \""+value.alt+"\" border=\"0\"></a>";
						icon += "</div>";
						icon += "<a href=\"" + value.url + "\" title=\""+ value.title+"\">";
						icon += "<b>"+value.name+"</b></a>" + (value.manufacturer != null ? "<br>by "+value.manufacturer : "");
						icon += "<br/>";
						if (value.available != null){
							icon += "<span class=\"detailsbold\">";
							if (value.price_discount != null){
								icon += "<span class=\"sale\">Sale Price:";
							} else {
								icon += "Price:";
							}
							icon += value.price;
							if (value.price_discount != null){
								icon += "</span>";
							}
							icon += "</span>";
						} else {
							icon += "<span class=\"sold_out\">Sold Out</span>";
						};
						icon += "</div></div>";
						
						$("#cat_content").append($(icon));
					};
				});
				$("#cat_content").append($("<div class=\"clear\"></div>"));
			}	;		
		});
		
	};
	if (($.browser.msie && $.browser.version > 6) || $.browser.mozilla || $.browser.opera || $.browser.safari || $.browser.webkit){
		$("#cat_content").css('opacity', 0.5)
			.click(function(event){event.preventDefault();})
			.append($('<div style=\"position:absolute; left:50%; margin-left:-20px; top:190px;\"><img src="/2008/images/ajax_loader.gif" border="0" /></div>'));
		
		if (preview_shop) {
			cat_url =  '/previewshop/preview_category_json/'+category_id;
		} else {
			cat_url =  '/category/category_json/'+category_id;		
		}
		$.getJSON( cat_url, 
			function(json) {
				data = json;
				$.each(data, function(i, v){
					if (v.products != null){
						$.each(v.products, function(j, value){
							if (value.attributes != null){
								$.each(value.attributes, function(j, attr){
									if (attr.name != null && (attr.id == 1)){	// Only allow size
//											if (attr.name != null && (attr.id == 1 || attr.id == 3)){	// Only allow size and color drop-down filters
										if (attributes[attr.name] == null) {
											attributes[attr.name] = new Object();
											attributes[attr.name].options = new Object();
										};										
										if (attr.options != null){
											$.each(attr.options, function(j, opt){
												if (opt.name != null){
													if (attributes[attr.name].options[opt.name] == null) attributes[attr.name].options[opt.name] = new Object();
													attributes[attr.name].options[opt.name].name = opt.name;
													attributes[attr.name].options[opt.name].id = opt.id;
												}
											});
										};											
										attributes[attr.name].name = attr.name;
										attributes[attr.name].id = attr.id;
									};
								});
							}
						});
					}
				});
				$("#cat_content").css('opacity', 1).unbind('click');
				filter();
				
				for (var it in attributes){
					$("#shop").append($("<span style=\"padding-left:15px; text-transform:lowercase;\">shop by "+it+" </span>"));					
					$("#shop").append($("<select id=\"shop_by_"+it+"\"><option value=\"0\" selected=\"selected\"> All </option></select>"));	
					var options = attributes[it].options;
					
					var blah = new Array();
					for (var it2 in options){
						blah.push(options[it2]);
					}						
					var blah2 = blah.sort(function(a,b){  return parseInt(a.name) - parseInt(b.name);});
					$.each(blah2, function(j, opt){
						$("#shop_by_"+it).append($("<option value=\""+opt.id+"\">"+opt.name+"</option>"));
					});
											
					$("#shop_by_"+it).change(function(){filter();});
				}
				
			}
		);
		
		$("#sort_by").change(function(){filter();});
	} else {
		$("#shop_and_sort").hide();
	};
});

