function simple_tooltip(target_items, name){
    $(target_items).each(function(i)
    {
	//$("body").append("<div class='"+name+"' id='"+name+i+"'><table cellspacing='0'><tr><td width='1'><p><img width='320px' src='"+$(this).attr('src').replace('covers','big')+"' /></p></td></tr><tr><td><p>"+$(this).attr('alt')+"</p></td></tr></table></div>");
	var my_tooltip = $("#"+name+i);

	$(this).removeAttr("title").mouseover(function(){
	    my_tooltip.css({opacity:0.9, display:"none"}).fadeIn(400);
	}).mousemove(function(kmouse){
	    my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
	}).mouseout(function(){
	    my_tooltip.fadeOut(400);
	});
    });
}

$(document).ready(function() {

	$('#choose_city').change(function() {
		var city = $('#choose_city option:selected').val();
		var delivery = 0;
		if(city == 'c1'){
			delivery = 250;
		}
		if(city == 'c2'){
			delivery = 400;
		}
		delivery_old = $('.delivery span').text();
		$('.delivery span').text(delivery);
		
		total_old = $('.total span').text();
		
		total_new = (total_old - delivery_old) + delivery;
		
		$('.total span').text(total_new);
		
	});

    simple_tooltip(".item img","tooltip");

    $("#SortList").sortable({
	update : function () {
	serial = $('#SortList').sortable('serialize');
	$.ajax({
		url: "/inc/sortdata.php",
		type: "post",
		data: serial,
		error: function(){
			alert("theres an error with AJAX");
			}
		});
	}
    });

    $("#dialog1").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
    });

    $("#dialog2").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
    });

    $("#dialog3").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
    });

    $("#dialog_rest_closed").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
    });
	
    $("#d1click").click(function(){ $("#dialog1").dialog("open"); });
    $("#d2click").click(function(){ $("#dialog2").dialog("open"); });
    $("#d3click").click(function(){ $("#dialog3").dialog("open"); });
	if (typeof rest_active !== "undefined")
		if (!rest_active)
			$(window).load(function(){ $("#dialog_rest_closed").dialog("open"); });

    jQuery.validator.messages.required = "!";
    $("#orderform").validate();
    $("#SortList").disableSelection();

    $('#cities').multiselect2side({
	selectedPosition: 'left',
	moveOptions: false,
	labelsx: 'Выбранные города',
	labeldx: 'Все города'
    });
	
    $('#stationsP').multiselect2side({
	selectedPosition: 'left',
	moveOptions: false,
	labelsx: 'Выбранные станции',
	labeldx: 'Все станции'
    });
	
	$('#stationsM').multiselect2side({
	selectedPosition: 'left',
	moveOptions: false,
	labelsx: 'Выбранные станции',
	labeldx: 'Все станции'
    });
	
    $('#kitchens').multiselect2side({
	selectedPosition: 'left',
	moveOptions: false,
	labelsx: 'Выбранные кухни',
	labeldx: 'Все кухни'
    });

    $('#districts').multiselect2side({
	selectedPosition: 'left',
	moveOptions: false,
	labelsx: 'Выбранные районы',
	labeldx: 'Все районы'
    });
	
	$('#city_filter').change(showRestaurants);
	$('#district_filter').change(showRestaurants);
	$('#kitchen_filter').change(showRestaurants);
	$('#station_filter').change(showRestaurants);
	$('#reset_filter').click(function(){
		$('#station_filter option:first').attr('selected', 'selected');
		$('#kitchen_filter option:first').attr('selected', 'selected');
		$('#district_filter option:first').attr('selected', 'selected');
		$('#city_filter option:first').attr('selected', 'selected');
		$('#station_filter').change();
	});
	
   if($(".desc-restaurant").length){
	  $(".desc-restaurant p").not(".desc-restaurant p:first").hide();
	  $(".desc-restaurant a.more").click(function(){
		 $(".desc-restaurant p").not(".desc-restaurant p:first").slideToggle();
		 return false;
	  })
   }
	/*
	$("div.desc-restaurant a.more").click(function(){
		$("div.desc-restaurant a.more").hide("slow", function() {
			$("div.desc-restaurant a.more").remove();
		});
		$("div.desc-restaurant span.more-info").show("slow");
	});
	*/
});

function showRestaurants()
{
	_showRestaurants($(this).attr('id'));
}

function _showRestaurants(filter_name)
{
	var city = $.cookie("jgeolocation");
	
	if (filter_name == 'city_filter')
	{
		
		var options = $('#city_filter').find('option');
		var id = $('#city_filter :selected').attr('id');
		$.each(options, function(key, value) { 
		  if($(value).attr("geo") == 'selected'){
			var geo = $(value).text();
			id = $(value).attr('id');
			$(".city a.#current").html(geo);
		  }
		  if($(value).attr("geo") == ''){
			$(".city a.#current").html($('#city_filter option:selected').html());
		  }
		});
		
		$('#district_filter option:first').attr('selected', 'selected');
		//$('#kitchen_filter option:first').attr('selected', 'selected');
		$('#station_filter option:first').attr('selected', 'selected');
	
		//if ($('#city_filter :selected').attr('id') == 0)
		
		if (id == 0)
		{
			
			$('#district_filter option').css('display', 'block');
			$('#station_filter option').css('display', 'block');
		}
		else
		{
		
			$('#district_filter option').css('display', 'none');
			$('#district_filter option:first').css('display', 'block');
			$('#district_filter option.'+id).css('display', 'block');
			$('#station_filter option').css('display', 'none');
			$('#station_filter option:first').css('display', 'block');
			$('#station_filter option.'+id).css('display', 'block');
		}
	}
	else if (filter_name == 'district_filter')
	{
		//$('#city_filter option:first').attr('selected', 'selected');
		//$('#kitchen_filter option:first').attr('selected', 'selected');
		$('#station_filter option:first').attr('selected', 'selected');
	}
	else if (filter_name == 'kitchen_filter')
	{
		//$('#city_filter option:first').attr('selected', 'selected');
		//$('#district_filter option:first').attr('selected', 'selected');
		//$('#station_filter option:first').attr('selected', 'selected');
	}
	else// if (filter_name == 'station_filter')
	{
		//$('#city_filter option:first').attr('selected', 'selected');
		//$('#kitchen_filter option:first').attr('selected', 'selected');
		$('#district_filter option:first').attr('selected', 'selected');
	}
	
	// показываем и прячем рестораны
	city_val = $('#city_filter :selected').val();
	district_val = $('#district_filter :selected').val();
	station_val = $('#station_filter :selected').val();
	kitchen_val = $('#kitchen_filter :selected').val();
	selected_filter = $('#'+filter_name+' :selected').val();
	
	var options = $('#city_filter').find('option');

	$.each(options, function(key, value) { 
	  if($(value).attr("geo") == 'selected'){
		city_val = $(value).val();
		selected_filter = $(value).val();
		$(value).attr("geo", '')
	  }
	});
	
	$('#units li').each(function(){
		if	(
				(city_val == 'c0' ? false : (filter[city_val] == null || filter[city_val].indexOf($(this).attr('id'))) == -1) ||
				(district_val == 'c0' ? false : (filter[district_val] == null || filter[district_val].indexOf($(this).attr('id'))) == -1) ||
				(station_val == 'c0' ? false : (filter[station_val] == null || filter[station_val].indexOf($(this).attr('id'))) == -1) ||
				(kitchen_val == 'c0' ? false : (filter[kitchen_val] == null || filter[kitchen_val].indexOf($(this).attr('id'))) == -1)
			)
		{
			$(this).hide("fast");
		}
		else
		{
			$(this).show("fast");
		}
	});
	if (filter_name == 'city_filter' || filter_name == 'station_filter' || filter_name == 'district_filter')
	{
		if (selected_filter == 'c0'  ||
			selected_filter == 'd0'  ||
			selected_filter == 's0'  )
		{
			$.cookie("filter", null);
		}
		else
		{
			$.cookie("filter", selected_filter);
		}
		/*
		$('#units li a').each(function(){
			$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1"));
		});
		if (selected_filter != 'd0' && selected_filter != 's0' && selected_filter != 'c0')
		{
			$('#units li a').each(function(){
				$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1" + selected_filter));
			});
		}
		if ((selected_filter == 'd0' || selected_filter == 's0') && $('#city_filter :selected').val() != 'c0')
		{
			$('#units li a').each(function(){
				$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1" + $('#city_filter :selected').val()));
			});
		}
		*/
	}
	/*
	if (filter_name == 'city_filter' || filter_name == 'station_filter' || filter_name == 'district_filter')
	{
		$('#units li a').each(function(){
			$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1"));
		});
		if (selected_filter != 'd0' && selected_filter != 's0' && selected_filter != 'c0')
		{
			$('#units li a').each(function(){
				$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1" + selected_filter));
			});
		}
		if ((selected_filter == 'd0' || selected_filter == 's0') && $('#city_filter :selected').val() != 'c0')
		{
			$('#units li a').each(function(){
				$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1" + $('#city_filter :selected').val()));
			});
		}
	}
	*/
	/*
	if (filter_name == 'city_filter' || filter_name == 'station_filter' || filter_name == 'district_filter')
	{
		$('#units li a').each(function(){
			$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1"));
		});
		if (selected_filter != 'd0' && selected_filter != 's0' && filter_name != 'city_filter')
		{
			$('#units li a').each(function(){
				$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1" + selected_filter));
			});
		}
	}
	*/
	
	/*
	if (selected_filter == 'c0'  ||
		selected_filter == 'd0'  ||
		selected_filter == 's0'  ||
		selected_filter == 'k0'  )
	{
		$('#units li').show("fast", function () {
			$(this).next().show("fast", arguments.callee); 
		});
		if (filter_name == 'station_filter' || filter_name == 'district_filter')
		{
			$('#units li a').each(function(){
				$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1"));
			});
		}
	}
	else
	{
		$('#units li').each(function(){
			if (filter[selected_filter] == null ||
				filter[selected_filter].indexOf($(this).attr('id')) == -1)
			{
				$(this).hide("fast");
			}
			else
			{
				$(this).show("fast");
			}
		});
		if (filter_name == 'station_filter' || filter_name == 'district_filter')
		{
			$('#units li a').each(function(){
				$(this).attr("href", $(this).attr("href").replace(/^(\/menu\/.*?\/).*$/, "$1" + selected_filter));
			});
		}
	}
	*/
}

