﻿	
	/*
     * 创建一个图标对象来定义自己的图标样式
     */
	function KIcon(imgUrl)
    {
        this.img = new Image();
        if (imgUrl)
        {
            this.img.src =KConfig.ToolImagePath + imgUrl;
        }
        else
        {
			this.img.src = KConfig.ToolImagePath + "default.gif";
        }
    }
    
    /*
     * 通过传入绝对地址或相对地址的图标路径来设置图标
     */
    var KIcon_setImageUrl = function(imgUrl)
    {
        var iconObject = this;
        iconObject.img.src =KConfig.ToolImagePath + imgUrl;
    }
    
    /*
     * 通过传入的参数来设置图标的宽度和高度
     */
    var KIcon_setSize = function(size)
    {
        var iconObject = this;
        KBase.getStyle(iconObject.img)["width"] = KBase.getUserInput(size[0]);
        KBase.getStyle(iconObject.img)["height"] = KBase.getUserInput(size[1]);
    }   
    
    /*
     * 通过传入一个字符串值来设置鼠标划过的提示信息,此功能只限于IE内核的浏览器
     */
    var KIcon_setAlt = function(info)
    {
        var iconObject = this;
        iconObject.img.alt = info;
    }
    
    /*
     * 设置图标坐标
     */
    var KIcon_setPosition = function(position)
    {
        var iconObject = this;
        KBase.setPosition(iconObject.img,position);
    }
    
    /*
     * 获取图标对象
     */
    var KIcon_getObject = function()
    {
        var iconObject = this;        
        return iconObject.img;
    }    
    
    /*
     * 把一个点对象和对应的图标包装成一个合法的叠加物对象
     */
    function KMarker(position, iconObject)
    {
        this.Map = null;
        this.isAutoClose=false;
        this.InfoWindow=null;
        this.point = position;
        this.icon = (typeof iconObject == "object") ? iconObject : new KIcon(iconObject);
        this.icon = this.icon.getObject();
        KBase.setCursorStyle(this.icon, "hand");
        KBase.getStyle(this.icon)["position"] = "absolute";
        KBase.setZIndex(this.icon, 500);
        this.infoWinWidth = 200;
        this.infoWinHeight = 120;
        
        KEvent.addListener(this.icon, "click", this, this.onClick);
        KEvent.addListener(this.icon, "mouseover", this, this.onMouseOver);
        KEvent.addListener(this.icon, "mouseout", this, this.onMouseOut);
        if (KBase.getBrowserType()=="IE")
        {
            this.icon.attachEvent("onmousedown", KEvent.cancelBubble);
            this.icon.attachEvent("ondblclick", KEvent.cancelBubble);
        }
        else
        {
            this.icon.addEventListener("mousedown", KEvent.cancelBubble, false);
            this.icon.addEventListener("dblclick", KEvent.cancelBubble, false);
        }
    }    
    
    
    /*
     * 初始化
     */
    var KMarker_initialize = function(mapObject)
    {
        var markerObject = this;
        markerObject.Map = mapObject;         
        if (!markerObject.Map._MarkerInfoWin)
        {
            markerObject.InfoWindow = new KInfoWindow();            
            markerObject.Map._MarkerInfoWin=markerObject.InfoWindow;            
        }
        else
        {
			markerObject.InfoWindow=markerObject.Map._MarkerInfoWin;
        }
        
    }
    
    /*
     * 图标点击事件
     */
    var KMarker_onClick = function(x)
    {
        var markerObject = this;
        KEvent.cancelBubble(x);
        var T = KBase.getEventPosition(x, markerObject.Map.container);
        KEvent.trigger(markerObject, "click", [T]);        
    }
    
    /*
     * 图标鼠标划过事件
     */
    var KMarker_onMouseOver = function(x)
    {
        var markerObject = this;
        KBase.setZIndex(markerObject.icon, 520);
        var T = KBase.getEventPosition(x, markerObject.Map.container);
        KEvent.trigger(markerObject, "mouseover",[T]);        
    }
    
    /*
     * 图标鼠标移出事件
     */
    var KMarker_onMouseOut = function(x)
    {
        var markerObject = this;
        KBase.setZIndex(markerObject.icon, 500);
        var T = KBase.getEventPosition(x, markerObject.Map.container);
        KEvent.trigger(markerObject, "mouseout",[T]);
    }
    
    /*
     * 获取标注图标对象
     */
    var KMarker_getObject = function()
    {
        var markerObject = this;
        return markerObject.icon;
    }
    
    /**
     *	对象释放
     */
    var KMarker_depose = function()
    {
		if(this.Map.isLogout)
		{
			this.icon = null;
			this.Map = null;        
			this.InfoWindow=null;
			this.point = null;        
			this.infoWinWidth = null;
			this.infoWinHeight = null;
        }
    }
    
    /**
     *	重新绘画
     */
    var KMarker_reDraw = function(isReDraw)
    {
        var markerObject = this;
        if (!isReDraw)	return;
        
        var containerSize=markerObject.Map.getViewSize();
        var isAppend=false;
        if (markerObject.point[0]>0&&markerObject.point[1]>0&&markerObject.point[0]<containerSize[0]&&markerObject.point[1]<containerSize[1])
		{
			isAppend=true;
		}         
        if (isAppend)
        {
            if (!markerObject.icon.parentNode)
            {
                markerObject.Map.overlaysDiv.appendChild(markerObject.icon);                
            }            
            KBase.setPosition(markerObject.icon, [markerObject.point[0] - markerObject.icon.offsetWidth / 2, markerObject.point[1] - markerObject.icon.offsetHeight/2]);
        }
        else
        {
            if (markerObject.icon.parentNode)
            {                
                KBase.getStyle(markerObject.icon)["display"]="none";
            }
        }
    }
    
    /*
     * 设置信息窗口的宽度
     */
    var KMarker_setInfoWinWidth = function(width)
    {
        var markerObject = this;
        if (markerObject.Map)
        {
            markerObject.InfoWindow.setWidth(width);
        }
        markerObject.infoWinWidth=width;       
    }
    
    /*
     * 设置信息窗口的高度
     */
    var KMarker_setInfoWinHeight = function(height)
    {
        var markerObject = this;
        if (markerObject.Map)
        {
            markerObject.InfoWindow.setHeight(height);
        }       
        markerObject.infoWinHeight=height;        
    }
    
    /*
     *  设置信息窗口的自动关闭
     */
    var KMarker_setupAutoClose = function()
    {
        var markerObject = this;
        if (markerObject.Map)
        {
            markerObject.InfoWindow.setupAutoClose();
        }
        markerObject.isAutoClose=true;        
    }
    
    
    /*
     * 清除信息窗口的自动关闭功能
     */
    var KMarker_clearAutoClose = function()
    {
        var markerObject = this;
        if (markerObject.Map)
        {
            markerObject.InfoWindow.clearAutoClose();
        }
        markerObject.isAutoClose=false;        
    }
    
    /*
     * 通过传入一个合法的DOM节点对象来添加到信息窗口中
     */
    var KMarker_openInfoWinElement = function(title,obj)
    {
        var markerObject = this;
        if (markerObject.isAutoClose)	markerObject.InfoWindow.setupAutoClose();
        else	markerObject.InfoWindow.clearAutoClose();         
        markerObject.setInfoWinWidth(markerObject.infoWinWidth);
        markerObject.setInfoWinHeight(markerObject.infoWinHeight);
        markerObject.InfoWindow.setTitle(title);
        markerObject.InfoWindow.setContent(obj);
        markerObject.InfoWindow.setPoint(markerObject);
        markerObject.Map.addOverLay(markerObject.InfoWindow);
        return markerObject.InfoWindow;
    }
    
    /*
     * 通过传入一个合法的网络地址来使信息窗口链接到该地址
     */
    var KMarker_openInfoWinUrl = function(title,url)
    {
        var markerObject = this;
        if (markerObject.isAutoClose)	markerObject.InfoWindow.setupAutoClose();
        else	markerObject.InfoWindow.clearAutoClose();         
        markerObject.setInfoWinWidth(markerObject.infoWinWidth);
        markerObject.setInfoWinHeight(markerObject.infoWinHeight);
        markerObject.InfoWindow.setTitle(title);
        var iframe = "<iframe src='" + url + "' width='100%' height='100%'>";
        markerObject.InfoWindow.setContent(iframe);
        markerObject.InfoWindow.setPoint(markerObject);
        markerObject.Map.addOverLay(markerObject.InfoWindow);
        return markerObject.InfoWindow;
    }
    
    /*
     * 通过传入合法的Html语言来添加到信息窗口中
     */
    var KMarker_openInfoWinHtml = function(title,htmlText)
    {
        var markerObject = this;        
        if (markerObject.isAutoClose)	markerObject.InfoWindow.setupAutoClose();
        else	markerObject.InfoWindow.clearAutoClose();         
        markerObject.setInfoWinWidth(markerObject.infoWinWidth);
        markerObject.setInfoWinHeight(markerObject.infoWinHeight);
        markerObject.InfoWindow.setTitle(title);
        markerObject.InfoWindow.setContent(htmlText);
        markerObject.InfoWindow.setPoint(markerObject);
        markerObject.Map.addOverLay(markerObject.InfoWindow);
        return markerObject.InfoWindow;
    }
    
    /*
     * 设置标注的位置
     */
    var KMarker_setPoint = function(T)
    {
        var markerObject = this;
        markerObject.point = T;
        markerObject.reDraw(true);
    }
    
    /*
     * 获取标注的位置
     */
    var KMarker_getPoint = function()
    {
        var markerObject = this;
        return markerObject.point;
    }
    
    /*
     * 设置图标路径
     */
    var KMarker_setIconImage = function(imgUrl)
    {
        var markerObject = this;
        markerObject.icon.src =KConfig.ToolImagePath +imgUrl;
    }
    
    /*
     * 通过传入一个标记对象来创建一个信息窗口
     */
    function KInfoWindow(T)
    {
        this.imgSrc = KConfig.ToolImagePath;
        this.point = T;
        this.div = KBase.createDiv(1, null, 560);
        this.div.ID = "KInfoWindowDiv";
        this.title = KBase.createDiv(0);
        this.title.ID = "KInfoWindowTitleDiv";
        KBase.getStyle(this.title)["overflow"] = "hidden";
        KBase.setSize(this.title, ["100%", "12"]);
        KBase.getStyle(this.title)["fontSize"] = "12px";
        this.content = KBase.createDiv(1);
        this.content.ID = "KInfoWindowContentDiv";
        KEvent.addListener(this.div,"mousedown", KEvent.cancelBubble);
        KEvent.addListener(this.div,"dblclick", KEvent.cancelBubble);
        KEvent.addListener(this.content, "mousedown", KEvent.returnTrue);
        KEvent.addListener(this.content, "selectstart", KEvent.returnTrue);
        KEvent.addListener(this.content, "click", KEvent.returnTrue);
        KEvent.addListener(this.content, "dblclick", KEvent.returnTrue);        
        this.markerOffset = [0, 0];
        this.createInfoWin();        
        this.created = true;
    }
    /*
     * 创建一个信息表格
     */
    var KInfoWindow_createInfoWin = function()
    {
        var infoObject = this;
        infoObject.setSize([0,0]);
        infoObject.markerOffset = [0.5, 0];
        infoObject.tab = KBase.createElement("TABLE");
        infoObject.tab.cellPadding = 2;
        infoObject.tab.cellSpacing = 1;
		infoObject.tab.border=0;
		infoObject.tab.bgColor="#8f8f8e";        
        KBase.getStyle(infoObject.tab)["fontSize"] = "12px";
        infoObject.div.appendChild(infoObject.tab); 
        //第一行               
        var firTr = infoObject.tab.insertRow(0);                
        var firTd = firTr.insertCell(0);         
        firTd.vAlign = "middle";           
        KBase.getStyle(firTd)["backgroundImage"] = "url(" + infoObject.imgSrc + "bgLine.gif)";  
        firTd.height = 25;
        
        //第一行中的表格
        var table=KBase.createElement("TABLE");
        table.width="100%";
        table.height="100%";
        
        var tableFirTr=table.insertRow(0);
        var tableFirTd=tableFirTr.insertCell(0);
        tableFirTd.width=16;
        tableFirTd.innerHTML = "<img src=" + infoObject.imgSrc + "info.gif>";
        
        var tableSecTd=tableFirTr.insertCell(1);
        tableSecTd.appendChild(infoObject.title);//插入标题         
        
        var tableThiTd=tableFirTr.insertCell(2);
        tableThiTd.width=1;
        var closeHref = KBase.createElement("A");
        closeHref.href = "javascript:void(0)";
        KEvent.addListener(closeHref, "mousedown", KEvent.cancelBubble);
        KEvent.addListener(closeHref, "click", infoObject, this.closeInfoWindow);
        KBase.getStyle(closeHref)["textDecoration"] = "none";        
        KBase.getStyle(closeHref)["top"] = "14px";
        closeHref.innerHTML = "<font color='#333333' style='font-size:12px'>×</font>";
        tableThiTd.appendChild(closeHref);
        firTd.appendChild(table);           
        
        //第二行               
        var secTr = infoObject.tab.insertRow(1); 
        var secTd = secTr.insertCell(0);
        secTd.bgColor="#FFFFFF";
        secTd.vAlign="top";
        secTd.appendChild(infoObject.content); //插入内容
    }
    
    
    /*
     * 创建一个信息内容
     */
    var KInfoWindow_changeInfoWin = function()
    {
        var infoObject = this;        
        infoObject.size = [Math.max(infoObject.content.offsetWidth, infoObject.size[0]), Math.max(infoObject.content.offsetHeight+28, infoObject.size[1])];
        infoObject.tab.rows[1].cells[0].style.height = (infoObject.size[1]-25) + "px";
        KBase.setSize(infoObject.tab, [infoObject.size[0],infoObject.size[1]]);        
    }
    /*
     * 设置窗口大小
     */
    var KInfoWindow_setSize = function(J)
    {
        var infoObject = this;
        infoObject.size = J;        
    }
    
    /*
     * 初始化
     */
    var KInfoWindow_initialize = function(mapObject)
    {
        var infoObject = this;
        infoObject.Map = mapObject;
    }
    
    /*
     * 获取对象
     */
    var KInfoWindow_getObject = function()
    {
        var infoObject = this;       
        return infoObject.div;
    }
    
    /*
     * 对象释放
     */
    var KInfoWindow_depose = function()
    {
		if(this.Map.isLogout)
		{
			this.imgSrc = null;
			this.point = null;
			this.div = null;        
			this.title = null;       
			this.content = null;
			this.markerOffset = null;       
        }
    }
    
    /*
     * 重新显示信息框
     */
    var KInfoWindow_reDraw = function(isReDraw)
    {
        var infoObject = this;
        if (!isReDraw)    return;
        var mapSize=infoObject.Map.getViewSize();
        infoObject.changeInfoWin();       
       
        if (infoObject.point.icon)
        {
            if (isReDraw)
            {
                if (!infoObject.div.parentNode)
                {
                    infoObject.Map.overlaysDiv.appendChild(infoObject.div);                    
                }
                //计算显示最佳位置
                var left=infoObject.point.icon.offsetLeft+10;
                var top=infoObject.point.icon.offsetTop+10-infoObject.size[1];                    
                if ((left+infoObject.size[0])>mapSize[0]) left=left-infoObject.size[0];
                if (top<0) top=top+infoObject.size[1];                
                if ((top + infoObject.size[1])>mapSize[1]) top=mapSize[1]-infoObject.size[1]-10;
                if (top<0) top=10;                
                KBase.setPosition(infoObject.div,[left,top]);
            }
        }
        else
        {
            if (isReDraw)
            {
                if (!infoObject.div.parentNode)
                {
                    infoObject.Map.overlaysDiv.appendChild(infoObject.div);                    
                }
                //计算显示最佳位置
                var left=infoObject.point[0];
                var top=infoObject.point[1]-infoObject.size[1];                     
                if ((left+infoObject.size[0])>mapSize[0]) left=left-infoObject.size[0];
                if (top<0) top=top+infoObject.size[1];                
                if ((top + infoObject.size[1])>mapSize[1]) top=mapSize[1]-infoObject.size[1];
                if (top<0) top=10;               
                KBase.setPosition(infoObject.div,[left,top]);
            }
        }            
    }
    
    /*
     * 设置内容
     */
    var KInfoWindow_setContent = function(text)
    {
        var infoObject = this;
        if (typeof(text) == "object")
        {
            infoObject.content.appendChild(text);
        }
        else
        {
            infoObject.content.innerHTML = text;
        }        
    }
    
    /*
     * 设置标题
     */
    var KInfoWindow_setTitle = function(title)
    {
        var infoObject = this;
        if (typeof(title) == "object")
        {
            infoObject.title.appendChild(title);
        }
        else
        {
            infoObject.title.innerHTML = title;            
        }        
    }
    
    /*
     * 关闭信息框
     */
    var KInfoWindow_closeInfoWindow = function()
    {
        var infoObject = this;
        infoObject.Map.removeOverLay(infoObject);
    }
    
    /*
     * 设置宽度
     */
    var KInfoWindow_setWidth = function(width)
    {
        var infoObject = this;
        infoObject.size[0] = parseInt(width);
        KBase.getStyle(infoObject.content)["width"]=width;    
    }
    
    /*
     * 设置高度
     */
    var KInfoWindow_setHeight = function(height)
    {
        var infoObject = this;
        infoObject.size[1] = parseInt(height);        
    }
    
    /*
     * 设置提示点
     */
    var KInfoWindow_setPoint = function(point)
    {
        var infoObject = this;
        infoObject.point = point;
    }
    
    /*
     * 开启依据鼠标位置来判断是否关闭信息窗口的功能
     */
    var KInfoWindow_setupAutoClose = function()
    {
        var infoObject = this;
        
        if (!infoObject.mouseMoveListener)
        {            
            infoObject.mouseMoveListener = KEvent.addListener(document, "mousemove", infoObject, this.onMouseMove);
        }
    }
    
    /*
     * 鼠标移动事件，用于关闭窗口
     */
    var KInfoWindow_onMouseMove = function(x)
    {
        var infoObject = this;
        if (!infoObject.div.parentNode || infoObject.div.parentNode.nodeName == "#document-fragment")
        {
            return;
        }
        var xPosition = KBase.getEventPosition(x, infoObject.Map.container);
        var w = KBase.getPageOffset(infoObject.div);        
        var X = KBase.getPageOffset(infoObject.Map.container);
        w = [w[0] - X[0], w[1] - X[1]];
        if (infoObject.point.icon)
        {
			
			if (xPosition[0] < w[0] - infoObject.point.icon.offsetWidth || xPosition[0] > w[0] + infoObject.div.offsetWidth + infoObject.point.icon.offsetWidth ||
			xPosition[1] < w[1] - infoObject.point.icon.offsetHeight || xPosition[1] > w[1] + infoObject.div.offsetHeight + infoObject.point.icon.offsetHeight)
			{            
				infoObject.closeInfoWindow();
			}
        }
        else
        {
			if (xPosition[0] < w[0] - 12 || xPosition[0] > w[0] + infoObject.div.offsetWidth + 12 ||
			xPosition[1] < w[1] - 12 || xPosition[1] > w[1] + infoObject.div.offsetHeight + 12)
			{            
				infoObject.closeInfoWindow();
			}
        }

                
    }
    
    /*
     * 清除已经打的closeInfoWindowWithMouse的功能
     */
    var KInfoWindow_clearAutoClose = function()
    {
        var infoObject = this;
        if (infoObject.mouseMoveListener)
        {
            KEvent.removeListener(infoObject.mouseMoveListener);
            infoObject.mouseMoveListener=null;
        }
    }


	KConfig.defineFunction(KIcon, "setImageUrl", KIcon_setImageUrl);
    KConfig.defineFunction(KIcon, "setSize", KIcon_setSize);   
    KConfig.defineFunction(KIcon, "setAlt", KIcon_setAlt);
    KConfig.defineFunction(KIcon, "getObject", KIcon_getObject);
    KConfig.defineFunction(KIcon, "setPosition", KIcon_setPosition);

    KConfig.defineFunction(KInfoWindow, "createInfoWin", KInfoWindow_createInfoWin);
   
    KConfig.defineFunction(KInfoWindow, "changeInfoWin", KInfoWindow_changeInfoWin);
    KConfig.defineFunction(KInfoWindow, "setSize", KInfoWindow_setSize);
    KConfig.defineFunction(KInfoWindow, "initialize", KInfoWindow_initialize);
    KConfig.defineFunction(KInfoWindow, "getObject", KInfoWindow_getObject);
    KConfig.defineFunction(KInfoWindow, "depose", KInfoWindow_depose);
    KConfig.defineFunction(KInfoWindow, "reDraw", KInfoWindow_reDraw);
    KConfig.defineFunction(KInfoWindow, "setContent", KInfoWindow_setContent);
    KConfig.defineFunction(KInfoWindow, "setTitle", KInfoWindow_setTitle);
    KConfig.defineFunction(KInfoWindow, "closeInfoWindow", KInfoWindow_closeInfoWindow);
    KConfig.defineFunction(KInfoWindow, "setWidth", KInfoWindow_setWidth);
    KConfig.defineFunction(KInfoWindow, "setHeight", KInfoWindow_setHeight);
    KConfig.defineFunction(KInfoWindow, "setPoint", KInfoWindow_setPoint);
    KConfig.defineFunction(KInfoWindow, "setupAutoClose", KInfoWindow_setupAutoClose);
    KConfig.defineFunction(KInfoWindow, "onMouseMove", KInfoWindow_onMouseMove);
    KConfig.defineFunction(KInfoWindow, "clearAutoClose", KInfoWindow_clearAutoClose);

    

    KConfig.defineFunction(KMarker, "initialize", KMarker_initialize);
    KConfig.defineFunction(KMarker, "onClick", KMarker_onClick);
    KConfig.defineFunction(KMarker, "onMouseOver", KMarker_onMouseOver);
    KConfig.defineFunction(KMarker, "onMouseOut", KMarker_onMouseOut);
    KConfig.defineFunction(KMarker, "getObject", KMarker_getObject);
    KConfig.defineFunction(KMarker, "depose", KMarker_depose);
    KConfig.defineFunction(KMarker, "reDraw", KMarker_reDraw);
    KConfig.defineFunction(KMarker, "setInfoWinWidth", KMarker_setInfoWinWidth);
    KConfig.defineFunction(KMarker, "setInfoWinHeight", KMarker_setInfoWinHeight);
    KConfig.defineFunction(KMarker, "openInfoWinElement", KMarker_openInfoWinElement);
    KConfig.defineFunction(KMarker, "openInfoWinUrl", KMarker_openInfoWinUrl);
    KConfig.defineFunction(KMarker, "openInfoWinHtml", KMarker_openInfoWinHtml);
    KConfig.defineFunction(KMarker, "setPoint", KMarker_setPoint);
    KConfig.defineFunction(KMarker, "getPoint", KMarker_getPoint);
    KConfig.defineFunction(KMarker, "setIconImage", KMarker_setIconImage);
    KConfig.defineFunction(KMarker, "clearAutoClose", KMarker_clearAutoClose);
    KConfig.defineFunction(KMarker, "setupAutoClose", KMarker_setupAutoClose);

	KConfig.defineClass("KIcon", KIcon);
    KConfig.defineClass("KInfoWindow", KInfoWindow);    
    KConfig.defineClass("KMarker", KMarker);
	
	
