var product = null;
var amount = null;
$(document).ready(function() {
	var form = $('#form__');
	form.find('select').change(function() {
		var folder = form.find('select[name=_folder_id]').attr('value');
		var vendor = form.find('select[name=_vendor_id]').attr('value');
		var url = '/magazin?mode=search&_folder_id=' + folder + '&_vendor_id=' + vendor + '&clear=1';
		$.get(url, null, function(text) {
			product = text.split('~');
			$('#currency_shortname').html(product[3]);
			$('#price').html(product[2]);
			form.find('input[type=text]').keyup();
		});
	}).eq(0).change();
	form.find('input[type=text]').keyup(function() {
		amount = parseInt($(this).attr('value'));
		if (!amount) 
			amount = 0;
		$('#price').html(product[2] * amount);
	});
	form.find('input[type=button]').click(function() {
		for (var i = 0; i < amount; i++) {
			widgets.addToCart(product[1], product[0], product[2], this); 	
		}
	});
	
	$('form[name=search]').each(function() {
		var f = $(this);
		var a = null;
		var p = null;
		f.find('select').change(function() {
			var folder = f.find('select[name=_folder_id]').attr('value');
			var vendor = f.find('select[name=_vendor_id]').attr('value');
			var url = '/magazin?mode=search&_folder_id=' + folder + '&_vendor_id=' + vendor + '&clear=1';
		$.get(url, null, function(text) {
				p = text.split('~');
				f.find('span.currency_shortname').html(p[3]);
				f.find('span.price').html(p[2]);
				f.find('input[type=text]').keyup();
			});
		}).eq(0).change();
	
		f.find('input[type=text]').keyup(function() {
			a = parseInt($(this).attr('value'));
			if (!a) 
				a = 0;
			f.find('span.price').html(p[2] * a);
		});
		
		f.find('input[type=button]').click(function() {
			for (var i = 0; i < a; i++) {
				widgets.addToCart(p[1], p[0], p[2], this); 	
			}
		});
	});
});
