var Tooltip = new Object({
	build:
		function()
		{
			this.tooltip 	= $("<div class='tooltip'/>");
			this.title 		= $("<div class='title'/>");
			this.bottom 	= $("<div class='bottom'/>");
			$("body").append(this.tooltip);
			$(this.tooltip).append(this.title).append(this.bottom);
		},
	init:
		function(type, background, data)
		{			
			this.title.empty();
			this.bottom.empty();
			
			this.data = data;
			
			this.titleContent = "";
			this.bottomContent = "";
			
			if(this.data.title != undefined && this.data.title.length > 0)
				this.titleContent = this.data.title;

			if(this.data.bottom != undefined && this.data.bottom.length > 0)
				this.bottomContent = this.data.bottom;	

			this.type = type;
			
			switch(this.type)
			{
				case "text":
					this.initText();
					break;
				case "image":
					this.initLoader();
					this.initImage();
					break;
			}
			
			switch(background)
			{
				case "fadeIN":
					$(this.tooltip).css({"background": "none"});
					$(this.title).css({"background": "url("+_baseUrl+"/images/tooltips/background.png) no-repeat top left"});
					$(this.bottom).css({"background": "url("+_baseUrl+"/images/tooltips/background.png) no-repeat bottom left"});
					break;
				case "fadeOUT":
					$(this.tooltip).css({"background": "#333"});
					$(this.title).css({"background": "none"});
					$(this.bottom).css({"background": "none"});								
					break;
			}
			
		},
	initText:
		function()
		{
			this.tooltip.width(208);
			if(this.data.title != undefined && this.data.title.search("RSS") != -1)
				this.bottom.css("text-align", "left");
			else
				this.bottom.css("text-align", "center");
		},
	initLoader:
		function()
		{
			this.loader = new Image(15,5);
			this.loader.src = _baseUrl+'/images/tooltips/indicator.gif';
			$(this.loader).hide();
			$(this.bottom).append(this.loader);
		},
	initImage:
		function()
		{
			this.image = new Image;
			$(this.image).hide();
			$(this.bottom).append(this.image);
			if(this.data.size.width != undefined && this.data.size.width > 0)
			{
				this.tooltip.width((this.data.size.width)+5);
			}
		},
	showImage:
		function(src, size)
		{			
			$(this.loader).show();
			$(this.image).load(function() {
				$(Tooltip.loader).hide();
				$(this).attr({"height": size.height, "width": size.width}).show();
			}).attr('src', src);
			
		},
	show:
		function()
		{			
			if(this.titleContent.length > 0)
			{
				$(this.title).html(this.titleContent);
			}
			if(this.bottomContent.length > 0)
			{
				$(this.bottom).html(this.bottomContent);
			}
			
			$(this.tooltip).show();
			
			switch(this.type)
			{
				case "text":
					break;
				case "image":
						this.showImage(this.data.src, this.data.size);
					break;
			}
		},
		
	move:
		function(e)
		{
			var border_top = $(window).scrollTop();
			var border_left = $(window).scrollLeft();
			var border_bottom = $(window).height();
			var border_right = $(window).width();
			var tooltipHeight = $(this.tooltip).height();
			var tooltipWidth = $(this.tooltip).width();
			
			if((border_right - tooltipWidth) > (tooltipWidth + e.pageX + 15 - border_left)){	
				$(this.tooltip).css("left",(e.pageX + 15) + "px");
			} else {
				$(this.tooltip).css("left",(e.pageX - tooltipWidth - 15) + "px");
			}
		
			if((border_bottom - tooltipHeight) > (tooltipHeight + e.pageY + 15 - border_top)){
				$(this.tooltip).css("top",(e.pageY + 15) + "px");
			} else {
				$(this.tooltip).css("top",(e.pageY - tooltipHeight - 15) + "px");
			}
		},
		
	hide:
		function()
		{
			$(this.tooltip).hide();
			switch(this.type)
			{
				case "text":
					break;
				case "image":
					$(this.image).hide();
					$(this.loader).show();
					break;
			}
		},
		
	ajax:
		function()
		{
			var data = new Object({
    			title: "", 
    			src: _baseUrl+'/images/tooltips/indicator.gif',
    			size: new Object({width: 15})
    		});
    		this.init("image", "fadeOUT", data);
			this.show();
			
			$("body").mousemove(function(e){		
				Tooltip.move(e);
			});
		}

});

$(document).ready(function(){
	Tooltip.build();
	
	if(_listType != undefined && _listType == 2)
	{ 
		$(".forPreload").each(function(i, val){
	 		var src = "http://images.logicimmo.com/getimage2.php?v5=1&path=thumb&wm=800&hm=600&type=prop&id="+$(this).children("img").attr("rel");
	 		pictures[$(this).children("img").attr("rel")] 		= new Image;
           	pictures[$(this).children("img").attr("rel")].src 	= src;
		});
	}
	
	$(".snapshot").mouseover(function(){ 
		$this = $(this);
		var data = new Object;
		if($this.hasClass("image"))
		{
			var type = "image";
			if($this.hasClass("gallery"))
			{
				data.src = "http://images.logicimmo.com/getimage2.php?v5=1&path=thumb&wm=800&hm=600&type=prop&id="+$this.attr("rel");
				data.title = $this.parents(".large_view").children("div.title").find("a").text();
				if($this.attr("width") > $this.attr("height")) {
					data.size = new Object({"height": 205, "width": 274});
				} else {
					data.size = new Object({"height": 274, "width": 205});
				}
			} else if($this.hasClass("web")) {
				var href = $this.attr("href");
				data.src = "http://logicimmo.websnapr.com?url="+href+"&size=S";
				data.title = href;
				data.size = new Object({"height": 152, "width": 202});
			}
		} else if($this.hasClass("text")) {
			var type = "text";
			if($this.hasClass("rss"))
			{
				data.title = "RSS (Really Simple Syndication)";
				data.bottom = _translate.rss.rss_what;
				
				data.bottom += _translate.rss.type+": "+_translate.rss.propertyType+"<br>";
				data.bottom += _translate.rss.locality+": "+_translate.rss.localityName+"<br>";
			}
		}
		
		if($this.hasClass("fadeIN"))
		{
			var background = "fadeIN";
		} else if($this.hasClass("fadeOUT")) {
			var background = "fadeOUT";
		}
		
		Tooltip.init(type, background, data);
		Tooltip.show();
		
	}).mouseout(function(){ 
		Tooltip.hide();
	}).mousemove(function(e){
		Tooltip.move(e);
	});
});