jQuery(document).ready(function($) {

	var KEY = {
		UP: 38,
		DOWN: 40,
		RETURN: 13,
		ESC: 27
	};

	var timeout;
	var myinput = $("#keyword");
	myinput.bind(($.browser.opera ? "keypress" : "keydown") + ".autocomplete", function(event) {
		// track last key pressed
		switch(event.keyCode) {
			case KEY.UP:
			event.preventDefault();
			//alert('up key pressed');
			//if(!$("tr.hovered")){ //if no tr has the hovered class
			if($("tr.hovered").length == 0){
				$("#searchres_list tr:last").addClass("hovered");
			}else{
				$("tr.hovered").eq(0).removeClass("hovered").prev().addClass("hovered");
			}
			break;

			case KEY.DOWN:
			event.preventDefault();
			//alert('down key pressed');
			// if(!$("tr.hovered")){ //if no tr has the hovered class
			if($("tr.hovered").length == 0){
				$("#searchres_list tr").eq(0).addClass("hovered");
				// alert('first class added');
			}else{
				$("tr.hovered").eq(0).removeClass("hovered").next().addClass("hovered");
				// alert('next class added');
				// alert($("#searchres_list tr").eq(0).html());
			}
			break;

			case KEY.RETURN:
			//alert('enter key pressed');
			event.preventDefault();
			if($("tr.hovered").length == 0){
				$('#search').submit();
			}else{
				//alert($("tr.hovered a").attr("href"));
				document.location = $("tr.hovered a").attr("href");
			}

			//}
			return false;
			break;

			case KEY.ESC:
			//alert('esc key pressed');
			jQuery('#searchres_list').hide();
			break;

			default:
			lastKeyPressCode = event.keyCode;
			clearTimeout(timeout);
			timeout = setTimeout(startMySearch, 500);
			break;
		}
	});
	//var select = $.Autocompleter.Select(options, input, selectCurrent, config);
	function startMySearch() {
		var myinput = $("#keyword");
		//var myinput = $("#keyword").attr("autocomplete", "off").addClass('jq_active');
		myinput.addClass('jq_active');
		var currentValue = myinput.val();
		if ( currentValue.length >= 3) {
			myinput.addClass('jq_loading');// тут надо втыкать в инпут-поле справа крутящееся колёсико - как символ ожидания
			currentValue = currentValue.toLowerCase();
			lets_search(currentValue);
		} else {
			myinput.removeClass('jq_active');
			$('#searchres').hide();
		}
	}

	function lets_search(what_find){
		//var what_find =  jQuery("#where_position").val();
		//alert(what_find);
		jQuery.post('/ajax/find_items/', { what_find : what_find},
		function(data){
			jQuery('#searchres_list').html(data);
			jQuery("#searchres_list").fadeIn(500);
		}, "html");
	}

});


function add_to_cart(cat_id)
{
	var cnt = $('#item_' + cat_id).val();
	cnt = cnt.replace(',','.');
	if(cnt>0){
		$.get('/ajax/add_to_cart/' + cat_id + '/' + cnt, function(data) {
			$('#ebay_' + cat_id).html('Добавлено');
			$('#cart_results').html(data);
			ajax_view_cart();
		});
	}else{
		$('#item_' + cat_id).select();
		$('#item_' + cat_id).focus();
	}
}
function focus_item(cat_id){
	var cnt = $('#item_' + cat_id).val();
	if(cnt=='0'){
		$('#item_' + cat_id).select();
	}
}

function select_subcat(cat_id)
{
	//$(".subcats").hide();
	if ( $('#cat_' + cat_id).is(':visible') ) {
		$('#cat_' + cat_id).hide();
		$('#cs_img_' + cat_id).attr('src', 'i/expand.gif');
	}else{
		$.get('/ajax/get_subcat/' + cat_id, function(data) {
			$('#cat_' + cat_id).html(data);
			$('#cat_' + cat_id).show();
			$('#cs_img_' + cat_id).attr('src', 'i/collapse.gif');
		});
	}
}

function switchDisplay(cid)
{
	if(document.getElementById(cid).style.display=='')
	{
		document.getElementById(cid).style.display='none';
	}
	else
	{
		document.getElementById(cid).style.display='';
	}
}

function rem_from_cart(cat_id)
{
	$.get('/ajax/rem_from_cart/' + cat_id, function(data) {
		$('#cart_results').html(data);
		ajax_view_cart();
	});
}


jQuery.cookie = function (key, value, options) {

	// key and at least value given, set cookie...
	if (arguments.length > 1 && String(value) !== "[object Object]") {
		options = jQuery.extend({}, options);

		if (value === null || value === undefined) {
			options.expires = -1;
		}

		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}

		value = String(value);

		return (document.cookie = [
		encodeURIComponent(key), '=',
		options.raw ? value : encodeURIComponent(value),
		options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
		options.path ? '; path=' + options.path : '',
		options.domain ? '; domain=' + options.domain : '',
		options.secure ? '; secure' : ''
		].join(''));
	}

	// key and possibly options given, get cookie...
	options = value || {};
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
