var ProductFinder = {
	products: [],
	categories: {},
	productsByWeight: [],
	selectedAttribute: {},
	tempAttribute: {},
	colums: 0,
	addProduct: function(pid) {
		if (!this.productsByWeight[pid]) {
			this.products[this.products.length] = pid;
			this.productsByWeight[pid] = this.products.length - 1;
		}
	},
	init: function() {
	    jQuery('.category_value_link').mouseover(function(){
	    	ProductFinder.mouseover(this);
	    });
	    jQuery('.category_value_link').mouseout(function(){
	    	ProductFinder.mouseout(this);
	    });
	    jQuery('.category_value_link').click(function(){
	    	ProductFinder.click(this);
	    });
	    jQuery('#reset_finder_link').click(function(){
	    	ProductFinder.resetAll();
	    });
	    jQuery('.category_value_link').attr('href', 'javascript: void(0);');
	    jQuery('.category_value_link').attr('href', 'javascript: void(0);');
	},
	addCategoryValue: function(cat, val, prod) {
		if (!this.categories[cat]) {
			this.categories[cat] = {};
		}

		this.categories[cat][val] = prod;
	},
	click: function(obj){
		var catData = this.getCategoryData(obj);
		if (jQuery(obj).hasClass("cats_dis")) {
			this.resetAll();
			this.addAtribute(catData);
		} else {
			if (this.selectedAttribute[catData.name] && (this.selectedAttribute[catData.name] == catData.value)) {
				this.deleteAtribute(catData);
			} else {
				this.addAtribute(catData);
			}
		}

		this.regenerateAll();
		return false;
	},
	mouseover: function(obj){
		var catData = this.getCategoryData(obj);
		if (this.selectedAttribute[catData.name]) {
			return false;
		}

		if (jQuery(obj).hasClass("cats_dis")) {
			return false;
		}

		this.addTempAtribute(catData);
		this.regenerateAll();
	},
	mouseout: function(obj){
		var catData = this.getCategoryData(obj);
		if (this.selectedAttribute[catData.name]) {
			return false;
		}

		if (jQuery(obj).hasClass("cats_dis")) {
			return false;
		}

		this.deleteTempAtribute(catData);
		this.regenerateAll();
	},
	addTempAtribute: function(data) {
		this.tempAttribute[data.name] = data.value;
	},
	deleteTempAtribute: function(data) {
		this.tempAttribute[data.name] = null;
	},
	addAtribute: function(data) {
		this.selectedAttribute[data.name] = data.value;
	},
	deleteAtribute: function(data) {
		this.selectedAttribute[data.name] = null;
	},
	resetAll: function() {
	    this.selectedAttribute = {};
	    this.tempAttribute = {};
	    this.regenerateAll();
	},
	regenerateAll: function() {
		var attributes = {};
		jQuery.extend(true, attributes, this.selectedAttribute);
		for (var i in this.tempAttribute) {
			if (this.tempAttribute[i] && !this.selectedAttribute[i]) {
				attributes[i] = this.tempAttribute[i];
			}
		}

		var products = this.getProductsByAttr(attributes);
		jQuery(".products-grid li").hide();
		if (this.colums) {
			jQuery(".category-products-all").append(jQuery(".products-grid li"));
			jQuery(".category-products-all li").removeClass("first").removeClass("last");
			jQuery(".category-products-all ul").empty();
			jQuery(".category-products-all ul").remove();
		}

		var current_position = 0;
		var current_element;
		for (var i = 0; i < products.length; i++) {
			if (this.colums) {
				if (!current_position) {
					current_element = jQuery("<ul></ul>").addClass("products-grid");
					jQuery(".category-products-all").append(current_element);
				}

				current_element.append(jQuery("#product_list_"+products[i]));
				if (!current_position) {
					jQuery("#product_list_"+products[i]).addClass("first");
				}

				current_position++;
				if (current_position == this.colums) {
					current_position = 0;
					jQuery("#product_list_"+products[i]).addClass("last");
				}
			}

			jQuery("#product_list_"+products[i]).show();
		}

//		jQuery(".category-products").append("<textarea></textarea>").val(jQuery(".category-products").html());
		jQuery("#toolbar_items").html(products.length);
		var ti = 0;
		var tl = 0;
		var ttt = 0;
		var obj_id = '';
		var have_selected = false;
		for (var i in this.categories) {
			if (this.selectedAttribute[i]) {
				have_selected = true;
				for (var j in this.categories[i]) {
					obj_id = "category_link_"+i+"__"+j;
					obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
					jQuery("#"+obj_id).addClass("cats_dis");
					jQuery("#"+obj_id).css("fontWeight", "normal");
					obj_id = "category_count_"+i+"__"+j;
					obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
					jQuery("#"+obj_id).html("");
				}

				obj_id = "category_link_"+i+"__"+this.selectedAttribute[i];
				obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
				jQuery("#"+obj_id).removeClass("cats_dis");
				jQuery("#"+obj_id).addClass("cats_enabled");
			} else if (this.tempAttribute[i]) {
                                for (var j in this.categories[i]) {
                                        obj_id = "category_link_"+i+"__"+j;
                                        obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
                                        jQuery("#"+obj_id).addClass("cats_dis");
                                        jQuery("#"+obj_id).css("fontWeight", "normal");
                                        obj_id = "category_count_"+i+"__"+j;
                                        obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
                                        jQuery("#"+obj_id).html("");
                                }

                                obj_id = "category_link_"+i+"__"+this.tempAttribute[i];
                                obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
                                jQuery("#"+obj_id).removeClass("cats_dis");
                                //jQuery("#"+obj_id).addClass("cats_enabled");
			} else {
				for (var j in this.categories[i]) {
					ti = 0;
					tl =  this.categories[i][j].length;
					for (ttt = 0; ttt < tl; ttt++) {
						if (this.inArray(this.categories[i][j][ttt], products)) {
							ti++;
						}
					}

					obj_id = "category_link_"+i+"__"+j;
					obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
//					jQuery("#"+obj_id).css("fontWeight", "normal");
					jQuery("#"+obj_id).removeClass("cats_enabled");
					if (ti) {
						jQuery("#"+obj_id).removeClass("cats_dis");
						obj_id = "category_count_"+i+"__"+j;
						obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
						jQuery("#"+obj_id).html("(" + ti + ")");
					} else {
						jQuery("#"+obj_id).addClass("cats_dis");
						obj_id = "category_count_"+i+"__"+j;
						obj_id = obj_id.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g,'\\$1');
						jQuery("#"+obj_id).html("");
					}
				}
			}
		}

		obj_id = 'reset_finder_link';
		if (have_selected) {
		    jQuery("#"+obj_id).removeClass("finder_hide");
		} else {
		    jQuery("#"+obj_id).addClass("finder_hide");
		}
		paginate_products();
	},
	getProductsByAttr: function(attributes) {
		var temp_prod = [];
		var use_cat = false;
		var rrr;
		for (var cat in attributes) {
			if (attributes[cat]) {
				if (!use_cat) {
					temp_prod = this.categories[cat][attributes[cat]];
				} else {
					var t__prod = [];
					for (var p in this.categories[cat][attributes[cat]]) {
						rrr = this.categories[cat][attributes[cat]][p];
						if (this.inArray(rrr, temp_prod)) {
							t__prod[t__prod.length] = rrr;
						}
					}

					temp_prod = t__prod;
				}

				use_cat = true;
			}
		}

		if (!use_cat) {
			return this.products;
		}

		var prods = [];
		for (var i = 0, len = this.products.length; i < len; i++) {
			if (this.inArray(this.products[i], temp_prod)) {
				prods[prods.length] = this.products[i];
			}
		}

		return prods;
	},
	inArray: function(needle, haystack) {
		if (!haystack) {
			return false;
		}

		for (var i = 0; i < haystack.length; i++) {
			if (needle == haystack[i]) {
				return true;
			}
		}

		return false;
	},
	getCategoryData: function(obj) {
		return {
			name: jQuery(obj).attr('catname'),
			value: jQuery(obj).attr('catvalue')
		};
	},
	setColumnCount: function(cols) {
		this.colums = cols;
	},
	debug: function() {
		var __temp = document.getElementById("category_link_price__1,100");
//		alert(__temp);
		/*for (var i in this.categories) {
			alert(i);
		}*/
	}
};

