var MAP_MODE = 'sectorSelect';
var CURRENT_SECTOR = '';
var START_POINT = false;
var END_POINT = false;

function MarkEndPoint(sectorId)
{
	$("#points").hide();
	$("#balon").hide();
	var x = $("#area_"+sectorId).attr("x")-16;
	var y = $("#area_"+sectorId).attr("y")-23;
	$("#endPointMarker").css("left",x);
	$("#endPointMarker").css("top",y);
	$("#endPointMarker").show();
	$("select[name='endPoint']").find("option[value='"+sectorId+"']").attr("selected","1");
	END_POINT = sectorId;
}

function CalculatePrice()
{
	if (START_POINT && END_POINT)
	{
		MAP_MODE = 'sectorSelect';
		$("#priceDiv").html('<img src="/loadIndicator.gif"/>');
		$.ajax(
		{
			type:"POST",
			dataType:"json",
			data:
			{
				startPoint: START_POINT,
				endPoint: END_POINT,
				tarif:$("select[name='tarif']").attr("value"),
				calcPrice:1
			},
			success: function(response)
			{
				$("#priceDiv").html("Примерная стоимость перевозки &mdash; <nobr>"+response.price+' р.</nobr>');
			}
		});
	}
}

function MarkStartPoint(sectorId)
{
	$("#points").hide();
	$("#balon").hide();
	var x = $("#area_"+sectorId).attr("x")-23;
	var y = $("#area_"+sectorId).attr("y")-23;
	$("#startPointMarker").css("left",x);
	$("#startPointMarker").css("top",y);
	$("#startPointMarker").show();
	$("select[name='startPoint']").find("option[value='"+sectorId+"']").attr("selected","1");
	START_POINT = sectorId;
}

$(document).ready(function() {

		
	$("#points").hide();
	$("#balon").hide();
	$("#endPointMarker").hide();
	$("#startPointMarker").hide();
	if (!$.browser.safari) {
	$("select[name='startPoint']").change(function()
	{
		MarkStartPoint($(this).attr("value"));
		CalculatePrice();
	});
	
	$("select[name='tarif']").change(function()
	{
		CalculatePrice();
	});
	
	$("select[name='endPoint']").change(function()
	{
		MarkEndPoint($(this).attr("value"));
		CalculatePrice();
	});
	
	$(".sectorArea").mouseover(function(e){
		if (MAP_MODE == 'sectorSelect')
		{
			$("#balon").css("top",e.pageY+20);
			$("#balon").css("left",e.pageX-20);
			$('#'+$(this).attr('sectorId')).css("visibility","");
			$('#'+$(this).attr('sectorId')).show();
			CURRENT_SECTOR = $(this).attr('sectorId');
			$("#balon").show();
			$("#sectorName").html($(this).attr("sectorName"));
		}
	});
	
	$(".sectorArea").mouseout(function(){
		if (MAP_MODE == 'sectorSelect')
		{
			$('#'+$(this).attr('sectorId')).hide();
			$("#balon").hide();
		}
	});
	
	
	
	$(".sectorArea").click(function(e){
		MAP_MODE = 'pointSelect';
		$("#points").fadeIn();
		$('#'+$(this).attr('sectorId')).hide();
		return false;
	});
	
	$("#startPointLink").click(function(e){
		MAP_MODE = 'sectorSelect';
		MarkStartPoint(CURRENT_SECTOR);
		CalculatePrice();
		return false;
	});
	
	$("#endPointLink").click(function(e){
		MAP_MODE = 'sectorSelect';
		MarkEndPoint(CURRENT_SECTOR);
		CalculatePrice();
		return false;
	});
	
	$("#cancelLink").click(function(e){
		MAP_MODE = 'sectorSelect';
		$("#points").hide();
		$("#balon").hide();
		return false;
	});
	
	$("body").mousemove(function(e)
	{
		if (MAP_MODE == 'sectorSelect')
		{
			$("#balon").css("top",e.pageY+20);
			$("#balon").css("left",e.pageX-20);
		}
	});
}
});
