var ProductSearch = {};
ProductSearch.xhr = null;
ProductSearch.categories = {};

ProductSearch.categories.cats = [];

ProductSearch.searchDone = false;

ProductSearch.add_category = function(cat) {
	if (!ProductSearch.categories[cat]) {
		$('#categories').append('<li>' + cat + ' <a href="#" onclick="return ProductSearch.removeCategory(\''+cat+'\')"><img src="../../ps_assets/images/remove.png"/></a><li>');
		ProductSearch.categories.cats.push(cat);
		ProductSearch.categories[cat] = true;

		var url = ProductSearch.makeUrl();
		$("#category-modal").jqmHide();
		ProductSearch.showResults();
		
	}
	return false;
}

ProductSearch.removeCategory = function(cat) {
	ProductSearch.categories[cat] = false;
	
	var cats = ProductSearch.categories.cats;
	for(i in cats) {
		if (cats[i] == cat) {
			cats.splice(i,1);
			break;
		}
	}
	
	$("#categories li:contains("+cat+")").fadeOut(function() {$(this).remove()});
	
	ProductSearch.showResults(true);
	
	return false;
	
};

ProductSearch.category_select = function() {
	$('#category-modal').jqm({ajax:"/product_search/categories.php", overlay: 0, onLoad: function(hash) {hash.w.append('<a href="#" id="jqmClose-bottom" class="jqmClose png">Close</a>').children('div').children('div').children('form').hide()}}).jqmShow();
	return false;
};

ProductSearch.more_info = function(entry_id) {
    $('<div>').attr('class', 'jqmWindow').appendTo('body').jqm({ajax:"/product_search/modal.php?id=" + entry_id, onLoad: function(hash) {hash.w.append('<a href="#" id="jqmClose-bottom" class="jqmClose png">Close</a>').children('div').children('div').children('form').hide()}}).jqmShow();
};

ProductSearch.suggestBox = function() {
	$('<div>').attr('class', 'jqmWindow').appendTo('body').jqm({ajax:"/index.php/product_search/suggest", onLoad: function(hash) {hash.w.append('<a href="#" id="jqmClose-bottom" class="jqmClose png">Close</a>').children('div').children('div').children('form').hide()}}).jqmShow();
};
ProductSearch.sort = null;
ProductSearch.dir = null;

ProductSearch.doSort = function() {
};

ProductSearch.makeUrl = function() {
	var company = $("#company").attr('value');
	var brand = $("#brand").attr('value');
	var product = $("#products").attr('value');
	
	
	var url = "/product_search/product_results.php?"; + "/" + product + "/" + escape(company) + '/' + escape(brand) + '/' ;
		 
	if (product != '') {
		url += "product=" + escape(product) + '&';
	}
	
	if (brand != '') {
		url += 'brand=' + escape(brand) + '&';
	}		
	if ($("#cholov").attr('checked')) {
		url += 'cy&'
	}
	
	if ($("#pas").attr('checked')) {
		url += 'py&'
	}

	if ($("#yoshon").attr('checked')) {
		url += 'y&'
	}

	if ($("#meat").attr('checked')) {
		url += 'm&'
	}


	if ($("#dairy").attr('checked')) {
		url += 'd&'
	}
	if ($("#pareve").attr('checked')) {
		url += 'p&'
	}
	if (ProductSearch.sort) {
		url+= 'order=' + ProductSearch.sort + '&';
	}

	if ($("#consumer").attr('checked')) {
		url += 'c&'
	}

	if ($("#industrial").attr('checked')) {
		url += 'i&'
	}

	if (ProductSearch.dir) {
		url+= 'sort=' + ProductSearch.dir + '&';
	}

	if (ProductSearch.categories.cats.length > 0) {
		  for(var i in ProductSearch.categories.cats) {
			url += 'cat[]=' + escape(ProductSearch.categories.cats[i]) + '&';
		  }
		  //url += 'cat=' + escape(ProductSearch.categories.cats.join('|')) + ':';
	}

	return url;
};

ProductSearch.reset = function() {
	$(":input").attr('value', '');
	$(":checkbox").attr('checked', '');
	$("tbody").html("");
}

ProductSearch.showResults = function(override) {
	if (!override) {
		if ($("#products").attr("value") == "" && $("#brand").attr("value") == "" && ProductSearch.categories.cats.length == 0) {
			return;
		}
	}
	$("#results").block();
	if (ProductSearch.xhr != null) {
		ProductSearch.xhr.abort();
	}
	
	ProductSearch.xhr = $.get(ProductSearch.makeUrl(), function(data) {
		$('#results').html(data);
		$("#results").unblock()
		ProductSearch.xhr = null;
	});
}
$(document).ready(function() {
	//$().ajaxStart(function() { $("#results").block()}).ajaxStop(function() {$("#results").unblock()});
	$.preloadCssImages();

	$("ul.ypc-search").jHelperTip({
 		trigger: "hover", 
		source: "attribute", 
 		attrName: "alt", 
 		opacity: 0.8, 
 		autoClose:true
	});


	$('#content').append($('#category-modal'));
	
	$('.jqmWindow').jqm();

	$('#content').cornerz({radius:10, background:"#EAEDDB"});
	$('.box').cornerz({radius:5, background:"#fff"});
			
	$('table.modal-table tbody tr:even').addClass('even'); 
	
	$('#dairy, #meat, #pareve, #consumer, #industrial, #cholov, #pas, #yoshon').bind('click', function() {
		ProductSearch.showResults();
	});

	$('#products').autocomplete('/index.php/product_search/product_results', {minChars: 2});

	$('#brand').autocomplete2('/product_search/brand_results.php',
	{
		minChars: 1,
		cacheLength: 10,
		maxItemsToShow:10,
		selectFirst: true,
		autoFill: true,
		onItemSelect: function(li) {ProductSearch.showResults();} 
	});
	
});
