

var CONST_MAP_POINT_IMG_PATH = "";
var CONST_RESOURCE_XML_PATH = "";

var MapPointTarget = Class.create();
MapPointTarget.prototype = Object.extend(new MapPoint(), {
 mouseoverListener : function ()
 {
  this.div.style.zIndex = 70;
  if(this.opt.info_bubble != null && this.opt.info_bubble instanceof OverlayInfoWindow && this.opt.info_bubble.div.style.display != "none")
  {
   this.div.style.zIndex = 70;
  }
 },
 mouseoutListener : function ()
 {
  this.div.style.zIndex = 60;
  if(this.opt.info_bubble != null && this.opt.info_bubble instanceof OverlayInfoWindow && this.opt.info_bubble.div.style.display != "none")
  {
   this.div.style.zIndex = 60;
  } 
 } 
});
var MapPointContent = Class.create();
MapPointContent.prototype = Object.extend(new MapPoint(), {
 mouseoverListener : function ()
 {
  this.div.style.zIndex = 70;
  if(this.opt.info_bubble != null && this.opt.info_bubble instanceof OverlayInfoWindow && this.opt.info_bubble.div.style.display != "none")
  {
   this.div.style.zIndex = 70;
  }
 },
 mouseoutListener : function ()
 {
  this.div.style.zIndex = 50;
  if(this.opt.info_bubble != null && this.opt.info_bubble instanceof OverlayInfoWindow && this.opt.info_bubble.div.style.display != "none")
  {
   this.div.style.zIndex = 50;
  } 
 } 
});

var ResourcesOnMap = Class.create();
ResourcesOnMap.prototype = {
	with_border : false,
	map : null,
	start : 0,
	max : 500,
	map_points : [],
	map_point_pointer : 0,
	map_point_count : 0,
	add_map_point_timeout : null,
	current_item_id : 0,
	opacity : 1,
	parent_category : '',
	afterFinish : null,
	is_initialize : true,
	zoom_level : 3,
	initialize : function (map)
	{
		if(map == null)
			return false;
			
		this.map = map;
	},
	makeMapPoint : function ()
	{
		try{
			if(this.map_points[this.map_point_pointer].new_add) {
				this.map.addMapPoint(this.map_points[this.map_point_pointer]);
				this.map_points[this.map_point_pointer].new_add = false;
			}
/*		if(this.map_points[this.map_point_pointer].change_pos == false)
			{
				//this.map.addPoint(this.map_points[this.map_point_pointer]);
				this.map.addMapPoint(this.map_points[this.map_point_pointer]);
			}
			else
			{
				this.map_points[this.map_point_pointer].setPosition(true);
			}		*/
			this.map_point_pointer++;
			//alert(this.map_point_count);
			if(this.map_point_pointer >= this.map_point_count && this.add_map_point_timeout != null && typeof(this.add_map_point_timeout.stop) == "function")
			{
				this.add_map_point_timeout.stop();		
				this.hideRemainMapPoint();
				if(typeof(this.afterFinish) == "function")
					this.afterFinish();
				if(this.loading_img != null)
					this.loading_img.style.display = "none";					
			}
		}catch(e){}
	},	
	showContentMapPoints : function ()
	{
		var c = '';
		//alert('a');
		/*var check_box_ary = document.getElementsByName("content_type");
		//alert('b');
		for (var x=0;x<check_box_ary.length;x++){
			if(check_box_ary[x].checked) 
				c+=(c?','+check_box_ary[x].value:check_box_ary[x].value);
		}*/
		//alert('c');
		c = this.parent_category;
		/*var c = this.select.options[this.select.selectedIndex].value;
		this.select.blur();*/
		/*this.result_layer.innerHTML = "";
		//if(this.with_border)
		//{
			this.shad.style.width = this.div.offsetWidth+"px";
			this.shad.style.height = this.div.offsetHeight+"px";
		//}		*/
		if(this.add_map_point_timeout != null && typeof(this.add_map_point_timeout.stop) == "function")
		{
			this.add_map_point_timeout.stop();		
		}		
		
		if(c == "")
		{
			this.map_point_pointer = 0;
			this.map_point_count = 0;			
			this.hideRemainMapPoint();
			return false;
		}

		
		var BoundaryCoor = map.getBoundaryCoor();
		//alert(BoundaryCoor);
		this.start = 0;
		var url = sRelativePath+"xml/getResourcesMulti.php?c="+c+"&s="+this.start+"&l="+this.max+"&b="+BoundaryCoor;
		if(this.loading_img != null)
			this.loading_img.style.display = "";
			
		this.result_ajax_request = new Ajax.Request(
			url,
			{
				method: 'get', 
				onComplete: this.xmlResponseHandlerMulti.bind(this)
			}		
			);
	},
	

	xmlResponseHandlerMulti : function(req)
	{
		try{
		var xml = req.responseXML;
		var coors = xml.getElementsByTagName("c");
		
		
		this.map_point_pointer = 0;
		
		var old_map_point = [];
		var old_map_point_img = [];
		
		this.map_point_count = this.map_points.length;

		var item_cnt = 0;
		if(coors.length == 0)
		{
			if(this.loading_img != null)
				this.loading_img.style.display = "none";				
		}
		if (this.is_initialize){
			var no_of_content = coors.length;
			while ((no_of_content > 30)&&(this.zoom_level >= 0)){
				this.zoom_level -= 1;
				no_of_content -= 30;
			}
			if (no_of_content != coors.length)
				map.setZoomLevel(this.zoom_level);

			this.is_initialize = false;
			//alert(coors.length);
			
			//this.is_initialize = false;
		}
		for(var c=0; c < coors.length; c++)
		{
			var xml_items = coors[c].getElementsByTagName("i");
			var address_items = coors[c].getElementsByTagName("a");
			if(address_items.length == 1)
			{
				address_items = address_items[0].firstChild.nodeValue;	
			}

			item_cnt = item_cnt+xml_items.length;
			var items = [];
			
			var coor_val = coors[c].getAttribute("coor");

			if(coor_val.length != 10)
				continue;
			
			var ln = parseInt("8"+coor_val.substr(0,5));
			var la = parseInt("8"+coor_val.substr(5,5));
						
			for(var i=0; i < xml_items.length; i++)
			{	
				items.push(xml_items[i].firstChild.nodeValue.split("##"));
			}

			var map_point_img = sRelativePath+"living_in_hk/images/map_icons/"+icon_type+"1.gif";

			var tmp_map_point_element ='';
			tmp_map_point_element = document.getElementById(items[0][0]); 
			if (tmp_map_point_element) {
				old_map_point[items[0][0]]     = true;
				old_map_point_img[items[0][0]] = map_point_img;

				if (tmp_map_point_element.style.display == "none") {
					tmp_map_point_element.style.display = 'block';
				}

			}else{
				try{
					tmp_map_point_element.style.display = 'block';
				}
				catch(e){}

				var content_map_icon = new MapPointIcon(map_point_img);
				content_map_icon.setMargin(11);//half of the image width
				this.map_points[this.map_point_count] = new MapPointContent(ln, la, {icon:content_map_icon});
				this.map_points[this.map_point_count].div.className = items[0][0];
				this.map_points[this.map_point_count].div.id = items[0][0];
				//this.map_points[this.map_point_count].change_pos = false;
				this.map_points[this.map_point_count].new_add = true;

				/*this.map_points[this.map_point_count].setInfoBubble(new OverlayInfoWindow(100, 100));*/
				
				//this.map.addMapPoint(this.map_points[this.map_point_count]);	
				var bubble_html = '';
				bubble_html += '<div style="overflow:auto;width:270px;height:110px" draggable="false">';
				var tmp_name_with_link = '';
				for (var x=0; x<items.length; x++){
					if (items[x][6])
						tmp_name_with_link = "<a onClick='window.location=\""+items[x][6]+"\"' href='"+items[x][6]+"' >"+items[x][1]+"</a><br /><a onClick='window.location=\""+items[x][6]+"\"' href='"+items[x][6]+"' >"+"<img src='"+sRelativePath+"living_in_hk/images/directory/btn_more.gif' border='0'>"+"</a>";
					else
						tmp_name_with_link = items[x][1];
					bubble_html +="<table width='250px' style='border:0px;' border='0'><tr><td valign='top'><b>"+tmp_name_with_link+"</b>"+(address_items?'<br />'+address_items:'')+(items[x][3]?'<br />'+items[x][3]:'')+(items[x][4]?'<br />'+items[x][4]:'')+(items[x][5]?'<br />'+items[x][5]:'')+"</td><td width='50px'  valign='top'><img id='item_img_"+'id'+"' src='/homes_and_living/photo_cnt.php?k="+items[x][2]+"' width='100px' height='75'></td></tr></table>";
				}
				bubble_html += '</div>';
				this.map_points[this.map_point_count].addOnClickListener(map_point_click_action_listener);
				this.map_points[this.map_point_count].bubble_html = bubble_html;

				this.map_points[this.map_point_count].div.style.cursor = "pointer";
				this.map_point_count++;
			}
		}
		
		for (var tmp_map_point_count = 0; tmp_map_point_count<this.map_points.length ; tmp_map_point_count++){
			var check_hide_id = this.map_points[tmp_map_point_count].div.id;
			if (!old_map_point[check_hide_id]){
				//alert('here?????');
				this.map_points[tmp_map_point_count].hide();
			}else{
				this.map_points[tmp_map_point_count].opt.icon.image.src = old_map_point_img[check_hide_id];
			}
		}
		
		//this.result_layer.innerHTML = item_cnt+" item"+((item_cnt>1)?"s":"")+" found.";
		if(this.map_point_count > 0)
		{
			this.add_map_point_timeout = new PeriodicalExecuter(this.makeMapPoint.bind(this), 0.01);
		}
		
		//if(this.with_border)
		//{
			//this.shad.style.width = this.div.offsetWidth+"px";
			//this.shad.style.height = this.div.offsetHeight+"px";
		//}		
		}
		catch(e)
		{
			alert(e.message);	
			if(this.loading_img != null)
				this.loading_img.style.display = "none";			
		}
	},	

	addMapPoint : function()
	{
		
	},
	hideRemainMapPoint : function ()
	{
		if(this.add_map_point_timeout != null && typeof(this.add_map_point_timeout.stop) == "function")
		{
			this.add_map_point_timeout.stop();		
		}		
		if(this.map_point_pointer < this.map_points.length)
		{
		for(var i=this.map_point_pointer; i < this.map_points.length; i++)
		{
			this.map_points[i].no_reset = true;
			this.map_points[i].hide();
		}
		}
	},
	initializeLayout : function()
	{
		if(this.map == null)
			return false;

	},
	targetPointClickListener : function(evt)
	{
		
	}
};


