$.fn.clearForm = function() {
	
	return this.each(function() {
		
		
	  var type = this.type, tag = this.tagName.toLowerCase();
	  if (tag == 'form') {
		return $(':input',this).clearForm();
	  }
	  if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'hidden') {
		$(this).val('');
	  } else if (type == 'checkbox' || type == 'radio') {
		$(this).removeAttr('checked');
	  } else if (tag == 'select') {
		$(this).removeAttr('selected');
		this.selectedIndex = -1;
	}
	});
};

function cleanFilter() {
	$('#form_catalog_filter').clearForm();	
}

function cleanVendors() {
	vendorArray = new Array();
	$('#form_vendors input:checkbox').each(function(){
		if (this.checked == true) this.checked = false;
	});
	
	$('#form_vendors img:not(.selected)').each(function(){
		$(this).attr("src", $(this).attr("d_src"));
	});
}


var vendorArray = new Array();
var filthand_tmout;

$(document).ready(function(){

	bgTr();
	$('#catalog_category div.cat img:not(.notgrace)').each(function(){
		var src_graceimg = $(this).attr("graceimg");
		$(this).attr("src", src_graceimg);
	});
	$('#form_vendors img:not(.selected)').each(function(){

		$(this).attr("src", $(this).attr("d_src"));
	});
	
	$('#form_catalog_filter input.text').keyup(function(){
		clearTimeout(filthand_tmout);
		filthand_tmout = setTimeout('filterHandler()', 800);
	});

	
});

function selectVendorFromTable(vendor_id) {
	cleanVendors();
	$('#vendorfilter_' + vendor_id).attr("checked", "checked");
}

function setFilterValue(filter_type, filter_name, value_to_set) {

//	cleanFilter();

	if (filter_type == 'txt') {
		$('input[name=' + filter_name + '_to]').val(value_to_set);
		$('input[name=' + filter_name + '_from]').val(value_to_set);
	} else {

		$("select[name=" + filter_name + "] option")
		.each(function() { this.selected = (this.value == value_to_set); });
	}
	filterHandler();
}

function vendorHandler(vendor_name, vendor_id, inp_checkbox) {

	var val = $('#' + inp_checkbox).is(':checked');
	
	if (val == true) {
		
		$('#imgvendorid_' + vendor_id).attr("src", $('#imgvendorid_' + vendor_id).attr("def_src"));
		vendorArray[vendor_id] = vendor_name;
	} else {
		delete vendorArray[vendor_id];
		$('#imgvendorid_' + vendor_id).attr("src", $('#imgvendorid_' + vendor_id).attr("d_src"));
	}
	
	filterHandler();
}

function setFilterOrder(orderby, order_element) {
	
	$('.product_list th.filterordering').removeClass('sort_desc').removeClass('sort_asc');
	
	$('#filter_order').val(orderby);
	
	if ($('#filter_order_type').val() == 'asc') {
		$('#filter_order_type').val('desc');
	} else {
		$('#filter_order_type').val('asc');
	}
	
	filterHandler(null, true, order_element);
	
	return false;
}


function filterHandler(change_category, bysorting, order_element) {
	
	
	var count_vend = 0;
	var query = '';
	var vendors = '';
	
	for (var key in vendorArray) {
		var val = vendorArray[key]; 
		vendors = vendors + val + ',';
	}
	
	
	$.ajax({
		url: '/catalog/',
		type: 'GET',
		dataType: 'html',
		data: $('#form_catalog_filter').serialize() + '&vend=' + vendors + '&type=' + $('#category_type').val(),
		beforeSend: function() {
			$('#catalog_filter div.blocktitle').append('<span class=\"loadingbar\">&nbsp;</span>');
			
			showajaxLwindow();
			
		},
		complete: function() {
			$('div.loading_ajax_big').fadeTo('fast', 0, function(){
				$(this).remove();
			});
			$('#catalog_filter div.blocktitle span.loadingbar').remove();
		},
		success: function(response) {
			
			
			$('#catalog_content').html(response);
			resizeCatalogTable();
			bgTr();
			if (change_category == true) {
				getNewFilter();
				getNewVendors();
			}
			
			if (bysorting == true) {
				if ($('#filter_order_type').val() == 'asc') {
					$('#' + order_element).addClass("sort_asc");
				} else {
					$('#' + order_element).addClass("sort_desc");
				}
			}
		}
	});
	
	
}

function defScroll() {
      var x = y = 0;
      x = (window.scrollX) ? window.scrollX : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
      y = y = (window.scrollY) ? window.scrollY : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
      return {x:x, y:y};
}

function showajaxLwindow() {

// Проверка, вывод методом alert
	var top = defScroll().y + (screen.height /3);
	var left = ($(document).width() / 2);
	var load_div = $('<div>').addClass('loading_ajax_big').html('Обновление выборки товаров...').css("top", top).appendTo('body').css("left", left);
	load_div.fadeTo('slow', .9);
}

function setCategory(value, categoryid) {
	
	cleanFilter();
	$('#radiocategory_' + categoryid).attr("checked", "checked");
	$('#catalog_category div.cat img').each(function(){
		var imgid = $(this).attr("id");
		if (imgid == 'filterimgcategory_'+categoryid) {
			var src_normalimg = $(this).attr("normimg");
			$(this).attr("src", src_normalimg);
		} else {
			var src_graceimg = $(this).attr("graceimg");
			$(this).attr("src", src_graceimg);
		}
	});
	
	
	$("#category_type").val(value);
	
	cleanVendors();
	filterHandler(true);
	
}

function getNewFilter() {
	$.ajax({
		url: '/index.php?com=catalog&action=getnewfilter',
		type: 'POST',
		dataType: 'html',
		data: { productloaded: $('#products_loaded').val()}, 
		success: function(response) {
			$('#catalog_filter').html(response);
			$('#form_catalog_filter input.text').keyup(function(){
				setTimeout('filterHandler()', 700);
			});
			
		}
	});
}

function getNewVendors() {
	$.ajax({
		url: '/index.php?com=catalog&action=getnewvendors',
		type: 'POST',
		dataType: 'html',
		data: { productloaded: $('#products_loaded').val()}, 
		success: function(response) {
			$('#catalog_vendors').html(response);
			gray_vendor();			
		}
	});
	
	
}

function gray_vendor() {
	
	setTimeout('grayscale($("#form_vendors img"))', 200);
	
}


function bgTr() {



	$('table.product_list tr').mouseenter(function(){
		$(this).addClass('seltr');
	}).mouseleave(function(){
		$(this).removeClass('seltr');
	});
	
	var tproduct_height = $('table.product_list').height();
	$('#catalog_wrapper').height(tproduct_height);
	
}

function resizeCatalogTable() {

}
