var _mapOpen=1; 
//var _dots={ 1: {x:150,y:200,t:"Загородный дом (23 200 т.р.)" }, 2: {x:200,y:300,t:"Квартира (1 532 т.р.)" }, 3: {x:130,y:400,t:"Коттедж (1 500 т.р.)" } };
//var _dots={};
var _setDots;
var _dotsShown=false;

function ShowDots()
{	
	$("#art").html('');	
	
	// установка точки
	if (_setDots)
	{		
		$("#art").css({width: $("#map_img").width(), height: $("#map_img").height(), border: "2px solid #66BE47", position: "absolute" });
		var cross=$('<div id="cross"></div>').appendTo($("#art")).css({ left: 0, top:0, display:"none" });

		$("#art").mousemove(function(e){
			var d=$(this).offset();
			e.stopPropagation();			
			$("#cross").css( {left:e.pageX-d.left-8, top: e.pageY-d.top-7, display: (e.pageX-d.left-8)>0 && (e.pageY-d.top)<$("#art").height() ? "block" : "none"} );
	    });		
		
		$("#map_img").mousemove(function(e){
			var d=$(this).offset();
			e.stopPropagation();			
			$("#cross").css( {left:e.pageX-d.left-8, top: e.pageY-d.top-7, display: "block"} );
	    });
	    
		$("#art").click(function(e){
			var d=$(this).offset();
			e.stopPropagation();
			
			if ((e.pageX-d.left-8)>0 && (e.pageY-d.top)<$("#art").height())
			{
				var xx=e.pageX-d.left-2;
				var yy=e.pageY-d.top-2;
				_dots={ 0: {x: xx, y: yy, t:"" }};
				$('#map_x').attr('value',xx);
				$('#map_y').attr('value',yy);
				
			}
			
			Map();
	    });
	    
	    if ($("#map_x").val()!="" &&  $("#map_y").val()!="")
	    	_dots={ 0: {x: $("#map_x").val(), y: $("#map_y").val(), t: "" }};
	}
	

	var i=0;	

		/*for (var id in _ddots)
		{		
			$('<div class="dot" id="d' + id + '"></div>')
				.appendTo($("#art"))
				.css( {"left":_ddots[id].x+"px", "top":_ddots[id].y+"px"} )
				.attr("title","Всего предложений: "+_ddots[id].t+"::"+( _ddots[id].id ? "::id="+_ddots[id].id+"&retype="+_ddots[id].retype : ""));
			i++;
		}	*/
	
		for (var id in _dots)
		{		
			$('<div class="dot" id="d' + id + '"></div>')
				.appendTo($("#art"))
				.css( {"left":_dots[id].x+"px", "top":_dots[id].y+"px"} )			
				.attr("title",
					_dots[id].t.indexOf('|')==-1 ? 
						"Предложений: "+_dots[id].t+":: ::?district="+_dots[id].id+"&retype="+_dots[id].retype
						: 
						_dots[id].t+( _dots[id].id ? "::show/?id="+_dots[id].id+"&retype="+_dots[id].retype : ""));
			i++;
		}	
		
		
	if (i>0) $('.dot').addTooltip();
	_dotsShown=true;
	
	
}

function Map()
{
  if ($('#map_img'))
  {	
  	tooltip.hide();
  	$("#art").html('');	
	if (_mapOpen==1) 
	{				
		_mapOpen=0; 			
        $("#map_img").parent().animate({"top": -$('#map_img').height()-2+'px'}, "fast", "swing", function()
		{						
			$('#map_but').attr('src','/view/images/h0.gif');
			$('#map_but').attr('title','Открыть карту');
			$("#screen").fadeOut( function() { 
				$('#screen').css({ height: 0, width: 0 }); 
				$('#map').css('z-index',5);
				$('#relpath').css('z-index',6);
				$('#map .h').css('height','18px');
 				_mapOpen=-1; 
			});
		});
	}
	else if (_mapOpen==-1) 
	{	
		_mapOpen=0; 		
		$('#screen').width($().width());
		$('#screen').height($().height()-154);
		$('#screen').css({ left:0, top:0 });
		$("#screen").fadeIn(function(){ $('#screen').css("filter","alpha(opacity=80)");
			$('#map .h').css('height',$('#map_img').height()+20+'px');
			$('#relpath').css('z-index',5);
			$('#map').css('z-index',6);
		    $("#map_img").parent().animate({"top": '0px'}, "fast", "swing", function()
			{				
				ShowDots();
				
				$('#map_but').attr('src','/view/images/h1.gif');
				$('#map_but').attr('title','Закрыть карту');			
				$('#map').click( function(e) { Map(); } );
				_mapOpen=1;
			});		
		});
	}
  }
}


function over(){
	$(this).addClass("selected");
	if (this.tooltipText!="") tooltip.show(this);
}
function out(){
	$(this).removeClass("selected");
	tooltip.hide();
}

function click(){
		
	if (this.url!=undefined) location="/realestate/"+this.url;
	
	out();
	$('#tooltip').css('display', 'none');
	Map(); 
}


(function ($) {

tooltip={
		_x: -10, _y: -96, _d: 10, 
		show: function(obj)
		{
			if (this._timer) clearTimeout(this._timer);
			$('#tooltip').css('display','block');
			
				if (this.helper.is(':animated')) {
						return;
				} else {
					this.helper.css({ left: $(obj).offset().left+this._x+'px', top: $(obj).offset().top+(obj.tooltipTitle==" "?-50:this._y)+'px', display: 'block', opacity: 0})
							.animate({ opacity: 0.9,	top: '+=' + this._d + 'px' }, 100);
		
					$("#"+$(this.helper).attr('id')+' .content').html(obj.tooltipText);
					$("#"+$(this.helper).attr('id')+' .title').html(obj.tooltipTitle);
					if (obj.tooltipTitle==" ") $("#"+$(this.helper).attr('id')+' .title').hide();
				}
			

		},
		hide: function()
		{
			if (this._timer) clearTimeout(this._timer);
			this._timer = setTimeout(function () {
				$('#tooltip').animate({ opacity: 0 }, 100, function(){ $('#tooltip').css('display','none') });
			},200);
		}

};

$.fn.extend({
	addTooltip: function(settings) {
			if (!tooltip.helper) tooltip.helper=$('<div id="tooltip"><div class="title"></div><div class="content"></div><img src="/view/images/u.gif"></div>').appendTo(document.body);
			return this.each(function() {
					var t=this.title.split(/::/);
					this.tooltipText = t[0].replace(/\|/g,"<br>");
					this.tooltipTitle = t[1];
					this.url = t[2];
					$(this).removeAttr("title");
					this.alt = "";
				})
				.hover(over, out)
				.click(click);
	}
});


/*
(function ($) {

tooltip={
		_x: -10, _y: -54, _d: 10, 
		show: function(obj)
		{
			if (this._timer) clearTimeout(this._timer);
			this.helper.show();

			if (this.helper.is(':animated')) {
					return;
			} else {
				this.helper.css({ left: $(obj).offset().left+this._x+'px', top: $(obj).offset().top+this._y+'px', opacity: 0})
						.animate({ opacity: 0.9,	top: '-=' + this._d + 'px' }, 200);
	
				$("#"+$(this.helper).attr('id')+' .title').html(obj.tooltipTitle);
				$("#"+$(this.helper).attr('id')+' .content').html(obj.tooltipText);
			}
		},
		hide: function()
		{
			if (this._timer) clearTimeout(this._timer);
			this._timer = setTimeout(function () {
				$('#tooltip').animate({ opacity: 0 }, 100);
			},300);
		}

};

$.fn.extend({
	addTooltip: function(settings) {
			tooltip.helper=$('<div id="tooltip"><div class="title"></div><div class="content"></div><img src="/view/images/u.gif"></div>').appendTo(document.body).hide();
			return this.each(function() {
					this.tooltipTitle = this.title.split("::")[0];
					this.tooltipText = this.title.split("::")[1];
					$(this).removeAttr("title");
					this.alt = "";
				})
				.hover(over, out)
				.click(click);
	}
});

*/

})(jQuery);

