(function(jQuery) {

	jQuery.fn.initInteractiveMap = function(smallMap){
		this.children('a.bullet').each(function(){
			var coords = jQuery(this).attr('rel').split('-');
			jQuery(this).css({left: coords[0] + 'px', top: coords[1] + 'px'});
			var id = jQuery(this).attr('id');
			var id_name = '#' + id + '_name';
			var position = jQuery(id_name).attr('position');
			var new_coords = getCoordsNameCity(position, jQuery(id_name).width(), parseInt(coords[0]),parseInt(coords[1]));
			jQuery(id_name).css({left: new_coords[0] + 'px', top: new_coords[1] + 'px'});
			var id_display = '#' + id + '_display';
			
			if(jQuery(id_display).length > 0) {
				new_coords = getCoordsDisplayCity(jQuery(id_display).width(), parseInt(coords[0]),parseInt(coords[1]),smallMap);
				jQuery(id_display).css({left: new_coords[0] + 'px', top: new_coords[1] + 'px'});
				jQuery(this).hover(function(){showPopup(id);}, function(){hidePopup(id);});
				jQuery(this).next().hover(function(){showPopup(id);}, function(){hidePopup(id);});
			}
		});
	}
	
	function getCoordsNameCity (position, width, coord_x, coord_y) {
		var x = coord_x;
		var y = coord_y;
		
		if(position == 'bottom') {
			x = x - width/2 + 4;
			y = y + 3;
		
		} else if(position == 'top') {
			x = x - width/2 + 4;
			y = y - 14;
		
		} else if(position == 'right') {
			x = x + 10;
			y = y - 5;
		
		} else if(position == 'left') {
			x = x - width - 3;
			y = y - 5;
		
		}
		
		return [x,y];
	}
	
/*	function getCoordsDisplayCity (width, coord_x, coord_y) {
		var x = coord_x;
		var y = coord_y;
		
		x = x - width/2 + 4; 
		y = y + 20;
		
		return [x,y];
	}
*/	
	function getCoordsDisplayCity (width, coord_x, coord_y, smallMap) {
		var x = coord_x;
		var y = coord_y;
		
		x = x - width/2 + 4; 
		y = y + 5;
		
		if(smallMap) {
			y = y + 30;
		}
		return [x,y];
	}
	
	function showPopup(id){
		var id_display = '#' + id + '_display';
		jQuery(id_display).fadeIn();
	}

/*	function hidePopup(id){
		  var id_display = '#' + id + '_display';
		  alert('hola entro')
		  jQuery(id_display).animate({opacity:1},{duration:5000, complete:function(){jQuery(this).fadeIn();}}); 
	}*/
	/*
	function hidePopup(id){
		var id_display = '#' + id + '_display';
		jQuery(id_display).fadeOut();
	}
	*/
	function hidePopup(id){
		var id_display = '#' + id + '_display';
		jQuery(id_display).animate({opacity:1},{duration:500, complete:function(){jQuery(this).fadeOut();}});
	}
	
})(jQuery);

