 $(document).ready(function(){
		var this_item;
		var this_name;
		$("select").change(function () {
			var val = $(this).find("option:selected").attr('value');
			$("select").hide();
			var val2 = val;
			var val3 = val;
			//alert(val+'/'+val.substr(0, val.length-2));
			reset_options('#'+val+' option');
			while(val2 != 'opt'){
				$("#"+val2).show();
				val2 = val2.substr(0, val2.lastIndexOf('_'));
			}
			this_id = '['+val.substr(4).replace(/_/g, '][')+']';
			var arr = eval('selectors'+this_id);
			do_it(arr);
			function do_it(this_arr){
				if(this_arr['cost']){
					$('#cost_inp').val(' Цена: '+this_arr['cost']+' AZN ');
					this_item = this_arr;
					this_true_id = val3.substr(6);
				}else if(this_arr[1]){
					this_id = this_id + '[1]';
					val3 = val3+'_1';
					reset_options('#'+val3+' option');
					$('#'+val3).show();
					do_it(this_arr[1]);
				}
			}
		});
		var delete_onclick = function(){
			var this_true_id = $(this).attr('item_id');
			var self = this;
			$.ajaxSend("ajax/act.php", {id: this_true_id, action:'del'}, function(data){
				$('#items_count').text(parseInt($('#items_count').text())-1);
				$('#items_cost').text((parseFloat($('#items_cost').text()) - parseFloat($(self).parent().find('span.span_cost').text())).toFixed(2));
				$(self).parent().remove();
			});
		}
		$('#opt_0').change();
		$('#buy_butt').click(function(){
			//alert(this_true_id)
			$.ajaxSend("ajax/act.php", {id: this_true_id, action:'add'}, function(data){
				if(data.error > 0) return;
				$('#items_count').text(parseInt($('#items_count').text())+1);
				$('#items_cost').text((parseFloat($('#items_cost').text())+this_item['cost']).toFixed(2));
				var n_this_id = this_id;
				//alert(this_id)
				var name = '';
				n_this_id = n_this_id.substr(0, n_this_id.lastIndexOf('['));
				while(n_this_id.lastIndexOf('[') > 0){
					name =  eval('selectors'+n_this_id+'[0]') + '->' + name
					//alert(n_this_id)
					n_this_id = n_this_id.substr(0, n_this_id.lastIndexOf('['));
				}
				$('#items').append('<span>'+name+this_item[0]+'(<span class="span_cost">'+this_item['cost']+'</span> AZN ) <input item_id="'+this_true_id+'" type="button" class="button" value="X"><br/></span>');
				$('#items input[type=button]').click(delete_onclick);
			});
		});
		$('#items input[type=button]').click(delete_onclick);
		$('#make_s').click(function(){
			document.location.href = 'index2.php';
		});
});
function reset_options(selector){
	var start = true;
	$(selector).each(function(){
		if(start) $(this).attr('selected', 'selected');
		else $(this).attr('selected', '');
		start = false;
	});
}


