

if ($('map_container')){
	var move_map = new Draggable('map_container', { onEnd : function(){ checkPos() }, onStart : function(){ hide_detail() }});
}





	//THE PROBLEM
	
	var frame_x;
	var frame_y;
	
	var frame_size;
	var map_size;

	var center_x;
	var center_y;
	
	var active_dot;
	var center_offset = 0;
	
	
	function position_map(show){
	
		frame_pos = Position.cumulativeOffset($('map_frame'));
		frame_x = frame_pos[0] -6;
		frame_y = frame_pos[1];
		//alert(frame_pos);
		frame_size = $('map_frame').getDimensions();
		map_size = $('map_container').getDimensions();
		
		center_x = frame_x + (frame_size.width/2);
		center_y = frame_y + (frame_size.height/2);
		
		if (show == 1){
			center_offset = -30;
			detail(NID, $('target_nh'), AID);
		}
	
	}
	
	
	function checkPos(){//called onEnd of map move
		var map_pos = Position.cumulativeOffset($('map_container'));
		var move_x = map_pos[0] > frame_x ? frame_x - map_pos[0] : 0;
		var move_y = map_pos[1] > frame_y ? frame_y - map_pos[1]: 0;
		
		var move_x = Math.abs(map_pos[0] - frame_pos[0]) > map_size.width - frame_size.width ? frame_size.width - map_size.width + Math.abs(map_pos[0] - frame_pos[0]) : move_x;
		var move_y = Math.abs(map_pos[1] - frame_pos[1]) > map_size.height - frame_size.height ? frame_size.height - map_size.height + Math.abs(map_pos[1] - frame_pos[1]) : move_y;
		
		if (move_x != 0 || move_y != 0){
		new Effect.Move('map_container', { x: move_x, y: move_y, duration:  0.1});
		}
	}
	
	function center_on_point(point){
		var point_pos = Position.cumulativeOffset(point);
		var move_y = center_y - point_pos[1] + 120 + center_offset;//add 120px to bring popup closer to the bottom of frame
		var move_x = center_x - point_pos[0];
		
		new Effect.Move('map_container', { x: move_x, y: move_y, duration: 0.1});
		setTimeout('checkPos()', 120);
		
		
	}
	

	function detail(id, dot, UID){
	
		$('detail').hide();
		active_dot = dot;
		center_on_point(dot);
		
		var pars = 'id='+id;
		if (UID){
			pars = pars+"&UID="+UID;
		}
		var return_div_id = 'detail';
		var url = 'neighborhoods/ajax_neighborhood_detail.php';
		var myAjax = new Ajax.Updater(
		return_div_id,
			url, 
			{
				method: 'post', 
				parameters: pars, 
				evalScripts: true
			});
		
		
		setTimeout('showDetail('+id+')', 300);
	}
	
	function showDetail(id){
		
		var dot_pos = Position.cumulativeOffset(active_dot);
		var dim = $('detail').getDimensions();
		
		var detail_pos_y = dot_pos[1] - dim.height + 18;
		var detail_pos_x = dot_pos[0] - 93;
		
		$('detail').style.top = detail_pos_y + "px";
		$('detail').style.left = detail_pos_x + "px";
		
		$('detail').appear({duration: 0.3});
	}
	
	
	

	function hide_detail(){
		
		$('detail').appear({
				from: 1.0,
				to: 0.0,
				duration: 0.2,
				onEnd : hide_move_detail() 
			});
	}


	function hide_move_detail(){
		if ($('balloon_container')){
		$('balloon_container').remove();
		}
	}
	
	
	
	function show_pictures(NID){
		$('popup').style.background = '#fff';
		show_popup(800, 500, 'neighborhoods/pictures.php?NID='+NID);
	}
	
	function swap_nb_img(url){
		//unload_video();
		$('nbhood_pics_stage').show();
		$('picture_detail_stage').src=url;
	}
	
	
	//video player
	
	function show_video(NID){
		$('popup').style.background = '#000';
		show_popup(450, 284, 'neighborhoods/ajax_neighborhood_video.php?NID='+NID);
	}
	
	
	
	
	/*FOR THE DETAIL PAGE*/
	
	
	
	
	
	function show_nbhood_menu(obj, UID){
		var pos = Position.cumulativeOffset(obj);
		
		var pars = 'ajax=1';
		var return_div_id = 'nbh_index_content';
		if (UID){
				var url = 'http://www.dev-redoak.com/agentsites_logic/neighborhoods/ajax_nbhood_menu.php';
				pars = pars +"&UID="+UID;
		}
		else {var url = 'ajax_nbhood_menu.php';}
		var myAjax = new Ajax.Updater(
		return_div_id,
			url, 
			{
				method: 'post', 
				parameters: pars, 
				evalScripts: true
			});
			
		var return_div_obj = $('nbhood_index');
			return_div_obj.style.top = (pos[1] -12) + 'px';
			return_div_obj.style.left = (pos[0] - 150) + 'px';
			return_div_obj.show();
	}
	
