
//KMarker 实现 GOverlay 接口
var _index = 1;
 var proId;
 var listNum = 0;
 var listCount = 0;
 var currentKMarker = null; 
var panx = 0;
var pany = 0;
var tempTipzIndex;
var tempHtmlzIndex;
var tempmkIndex = null;
var currentCenter = null;
function KMarker(latlng, tip, markerContent, proId, clickParamer) 
{ 
    this._latlng = latlng; 
    this._tip = tip; 
    this._markerContent = markerContent; 
    this._clickParamer = clickParamer; 
    this._proId=proId;
} 
KMarker.prototype = new GOverlay(); 
KMarker.prototype.initialize = function(map_) { 
	this._map = map_; 
	
	//标注点 蓝色
    var markerHtml = document.createElement('div'); 
    markerHtml.setAttribute("index", this._clickParamer["index"]); 
    markerHtml.innerHTML = this._markerContent; 
    markerHtml.style.position = "absolute"; 
    map_.getPane(G_MAP_MARKER_PANE).appendChild(markerHtml); 
    //信息框 
    var markerTip = document.createElement("div"); 
	markerTip.style.overflow="hidden";       
    markerTip.setAttribute("id", "tip"+this._clickParamer["index"]);
    markerTip.setAttribute("index", this._clickParamer["index"]);

    markerTip.innerHTML = this._tip; 
    markerTip.style.position = "absolute"; 
    markerTip.style.display = "none";     
    map_.getPane(G_MAP_MARKER_PANE).appendChild(markerTip);
    var pId = this._proId;
    var mk = this;
    var point = this._latlng;
    GEvent.addDomListener(markerTip, "mousedown", function() { 
    	map.disableDragging();
    });
    GEvent.addDomListener(markerTip, "mouseup", function() { 
    	map.enableDragging();
    });
    GEvent.addDomListener(markerHtml, "mousedown", function() { 
    	map.disableDragging();
    });
    GEvent.addDomListener(markerHtml, "mouseup", function() { 
    	map.enableDragging();
    });
    
    
    //disableDoubleClickZoom() 
     /*GEvent.addDomListener(markerTip, "click", function() { 
        if (markerTip.style.display == "none") { 
            viewMarkerTip(markerHtml.getAttribute("index")); 
        } else {  
            hiddenMarkerTip(markerHtml.getAttribute("index"));
        } 
  
    }); 
     GEvent.addDomListener(markerTip, "onresizeend", function() {
		//大小改变
    });
    */
     /*GEvent.addDomListener(markerTip, "mouseover", function() {
        _index++; 
        markerTip.style.zIndex = _index; 
        markerHtml.style.zIndex = _index;
  
    }); */        

    /*GEvent.addDomListener(markerTip, "mouseout", function() {
        markerTip.style.zIndex = tempZIndex; 
        markerHtml.style.zIndex = tempZIndex; 
    	//alert("markerTip 鼠标移出");
        //markerHtml.innerHTML=tempMarkHtml; 
        //tempMarkHtml = ''; 
    }); */
    //鼠标点击  
    GEvent.addDomListener(markerHtml, "click", function() {
    	if(currentCenter==null){
    		currentCenter = map.getCenter();
    	}
        if (markerTip.style.display == "none") {            
    		proId = pId;
            listNum = 0;
		    listCount = 0;
	    	if(currentKMarker != null){    	
	            	currentKMarker._markerTip.style.display="none";
	            	tempmkIndex = null;
	            	currentKMarker = null;
	        }  		 	
 						
            //viewMarkerTip(tempmkIndex);
                    
            document.getElementById("tip"+mk._clickParamer["index"]).style.display="block";
 			findOfficeAjax(mk);
            
           	//根据信息框位置平移地图。区别是否是切换信息窗口 或 仅打开新窗口
			/*setTimeout(function(){
	       		var container = map.getContainer();
	           	var mkHtml = mk.markerHtml;
	           	var mkTip = mk.markerTip;
	           	//var position = map.fromLatLngToDivPixel(currentKMarker._latlng);
	           	var position = map.fromLatLngToContainerPixel(mk._latlng);
	           	var mkpanx = 0;
	           	var mkpany = 0;
	           	if(mk._markerTip.scrollWidth/2 > position.x){
	           		mkpanx = mk._markerTip.scrollWidth/2 - position.x;
	           	}else if(mk._markerTip.scrollWidth/2 + position.x > container.scrollWidth){
	           		mkpanx = container.scrollWidth - mk._markerTip.scrollWidth/2 - position.x;			            		
	           	}
	           	if(mk._markerTip.scrollHeight+mk._markerHtml.offsetHeight > position.y){
	           		mkpany = mk._markerTip.scrollHeight + mk._markerHtml.offsetHeight - position.y;
	           	}
	           	//alert(position.y);//test
	           	if(mkpanx!=0 || mkpany!=0){
	           		//map.panBy(new GSize(mkpanx - panx, mkpany - pany));
	           		map.panBy(new GSize(mkpanx, mkpany));  
	           	} 	
				panx = mkpanx + panx;
				pany = mkpany + pany;
				if(currentKMarker!=null){            
	            	//hiddenMarkerTip(currentKMarker._clickParamer["index"]);
	            	currentKMarker._markerTip.style.display="none";
	            	tempmkIndex = null;
	            }
	            currentKMarker = mk; 
	        },0);*/ currentKMarker = mk;                
        } else { 
            //hiddenMarkerTip(markerHtml.getAttribute("index"));
           	mk._markerTip.style.display="none";
	        tempmkIndex = null;
            map.panTo(currentCenter); 	
        }         
    }); 
 	//鼠标悬停
    GEvent.addDomListener(markerHtml, "mouseover", function() {
        tempTipzIndex = markerTip.style.zIndex;
        tempHtmlzIndex = markerHtml.style.zIndex;
        markerTip.style.zIndex = 1; 
        markerHtml.style.zIndex = 2;

    	var bwidth = markerHtml.offsetWidth;
    	var bheight = markerHtml.offsetHeight;
    	var i = markerHtml.getAttribute("index");
    	document.getElementById("mt"+i).style.display="none";
    	document.getElementById("ms"+i).style.display="block";
    	var awidth = markerHtml.offsetWidth;
    	var aheight = markerHtml.offsetHeight;     	   	
    	var mkleft = parseInt(markerHtml.style.left.substr( 0, markerHtml.style.left.indexOf("px") ));
    	var mktop = parseInt(markerHtml.style.top.substr( 0, markerHtml.style.top.indexOf("px") ));    	
		markerHtml.style.left = (mkleft-(awidth-bwidth)/2) + "px";   
		markerHtml.style.top = (mktop-(aheight-bheight)) + "px";        
    }); 

    //鼠标移出
    GEvent.addDomListener(markerHtml, "mouseout", function() {    
	        markerTip.style.zIndex = tempTipzIndex;
        	markerHtml.style.zIndex = tempHtmlzIndex;
	        var bwidth = markerHtml.offsetWidth;
	    	var bheight = markerHtml.offsetHeight;
	    	var i = markerHtml.getAttribute("index");
	    	document.getElementById("mt"+i).style.display="block";
	    	document.getElementById("ms"+i).style.display="none";
	    	var awidth = markerHtml.offsetWidth;
	    	var aheight = markerHtml.offsetHeight;     	   	
	    	var mkleft = parseInt(markerHtml.style.left.substr( 0, markerHtml.style.left.indexOf("px") ));
	    	var mktop = parseInt(markerHtml.style.top.substr( 0, markerHtml.style.top.indexOf("px") ));    	
			markerHtml.style.left = (mkleft-(awidth-bwidth)/2) + "px";   
			markerHtml.style.top = (mktop-(aheight-bheight)) + "px";			
    });     
    this._markerHtml = markerHtml; 
    this._markerTip = markerTip;
}; 
KMarker.prototype.remove = function() { 
    this._markerHtml.parentNode.removeChild(this._markerHtml); 
    this._markerTip.parentNode.removeChild(this._markerTip); 
}; 
KMarker.prototype.copy = function() { 
    return new (this._latlng,this._tip,this._markerContent,this._clickParamer); 
}; 
KMarker.prototype.redraw = function(force) {
	if (force){ 
		var position = this._map.fromLatLngToDivPixel(this._latlng);   
		this._markerHtml.style.left = (position.x-(this._markerHtml.offsetWidth/2)) + "px";   
		this._markerHtml.style.top = (position.y-this._markerHtml.offsetHeight) + "px";
		this._markerTip.style.display="block";
		this._markerTip.style.left = (position.x-(this._markerTip.offsetWidth/2)) + "px";
		this._markerTip.style.top = (position.y-this._markerTip.offsetHeight-this._markerHtml.offsetHeight) + "px";
		this._markerTip.style.display="none";
	}
}; 
  
KMarker.prototype.getMarkerHtml = function() { 
    return this._markerHtml; 
}  
KMarker.prototype.getMarkerTip = function() { 
    return this._markerTip; 
}
KMarker.prototype.getLatLng = function() { 
    return this._latlng; 
}
//显示信息框
 function viewMarkerTip(mkIndex){
 	document.getElementById("tip"+mkIndex).style.display="block";
 	findOfficeAjax();
 }
 //隐藏信息框
 function hiddenMarkerTip(mkIndex){
 	var markerTip = document.getElementById("tip"+mkIndex);
 	if(markerTip){
 		markerTip.style.display="none";
       	/*if(panx!=0 || pany!=0){
    		map.panBy(new GSize(0-panx,0-pany));
    		panx=0;
    		pany=0; 
    	}*/

 	}
 	tempmkIndex = null;
 }
 /***********
 * 数据分页
 ***********/
 function previous(){
 	listNum = listNum - 6;
 	findOfficeAjax(currentKMarker);
 }
 function next(){
 	listNum = listNum + 6;
 	findOfficeAjax(currentKMarker);
 }
  /************
 * 请求数据 Ajax
 *************/
 function findOfficeAjax(marker){ 	
 		
 		var proInfo = document.getElementById("proInfo"+marker._clickParamer["index"]);
 		proInfo.innerHTML = "<img src='/img/loading2.gif' width='20px' height='20px' style='top:10px'/>";			
		
				       setTimeout(function(){
		            		var container = map.getContainer();
			            	var mkHtml = marker._markerHtml;
			            	var mkTip = marker._markerTip;
			            	//var position = map.fromLatLngToDivPixel(currentKMarker._latlng);
			            	var position = map.fromLatLngToContainerPixel(marker._latlng);
			            	if(mkTip.scrollWidth/2 > position.x){
			            		panx = mkTip.scrollWidth/2 - position.x;
			            	}else if(mkTip.scrollWidth/2 + position.x > container.scrollWidth){
			            		panx = container.scrollWidth - mkTip.scrollWidth/2 - position.x;			            		
			            	}
			            	if(mkTip.scrollHeight+mkHtml.offsetHeight > position.y){
			            		pany = mkTip.scrollHeight + mkHtml.offsetHeight - position.y;
			            	}
			            	//alert(position.y);//test
			            	if(panx!=0 || pany!=0){
			            		map.panBy(new GSize(panx,pany));
			            		panx = 0;
			            		pany = 0; 
			            	} 	
			           },0); 
		
		
		var data = {proId:proId,listNum:listNum,listCount:listCount,where:marker._clickParamer["mkwhere"]};
	    var ajaxurl="/property/findOfficeByAjax.kemp";
	    jQuery.ajax({url: ajaxurl,  
	            type: 'POST',
	            data:data, 
	            dataType: 'json',//html or xml
	            timeout: 20000,//超时时间设置，20秒 20 000毫秒  proInfo
	            error: function(){
	                alert('error');
	            },  
	            success: function(html){
	            	html = eval(html);
	            	proInfo.innerHTML = html.proHtml;	            	
		    		listCount = html.listCount;//alert("总:"+listCount+"条 第:"+listNum+"条"); 
		    		//显示信息框
	            	setTimeout(function(){
			            	var mkHtml = marker._markerHtml;
			            	var mkTip = marker._markerTip;
			            	var position = map.fromLatLngToDivPixel(marker._latlng);
			            	//var position = map.fromLatLngToContainerPixel(currentKMarker._latlng);
			            	mkTip.style.top = (position.y - mkTip.offsetHeight - mkHtml.offsetHeight) + "px";
				    },100);
	            	//根据信息框位置平移地图，点击分页除外。
	            	//if(ispage==null){
		            	setTimeout(function(){
		            		var container = map.getContainer();
			            	var mkHtml = marker._markerHtml;
			            	var mkTip = marker._markerTip;
			            	//var position = map.fromLatLngToDivPixel(currentKMarker._latlng);
			            	var position = map.fromLatLngToContainerPixel(marker._latlng);
			            	if(mkTip.scrollWidth/2 > position.x){
			            		panx = mkTip.scrollWidth/2 - position.x;
			            	}else if(mkTip.scrollWidth/2 + position.x > container.scrollWidth){
			            		panx = container.scrollWidth - mkTip.scrollWidth/2 - position.x;			            		
			            	}
			            	if(mkTip.scrollHeight+mkHtml.offsetHeight > position.y){
			            		pany = mkTip.scrollHeight + mkHtml.offsetHeight - position.y;
			            	}
			            	//alert(position.y);//test
			            	if(panx!=0 || pany!=0){
			            		map.panBy(new GSize(panx,pany));
			            		panx = 0;
			            		pany = 0; 
			            	} 	
			            },500); 
		            //}
	            }
	     }); 		 
 }

