﻿
	//====================================面要素类============================================
	
	/**
	 * 定义面要素类
	 */		
	function KPolygon(points, color, bgcolor,lineWidth, scale)
    {
        this.points = points;							//经过点数组
        this.color = color ? color : "blue";			//颜色
        this.bgcolor = bgcolor ? bgcolor : "#99FFCC";	//背景颜色
        this.lineWidth = lineWidth ? lineWidth : 2;		//线宽度
        this.opacity = scale ? scale : 0.5;				//透明度
        this.added=false;
        
        if (KBase.getBrowserType()=="IE")
        {
            //创建面形状
            this.polygon = KBase.createElement("v:shape");
            KBase.getStyle(this.polygon)["position"] = "absolute";
            this.polygon.unselectable = "on";            
            this.polygon.fill = true;    	
			//创建线样式
            this.stroke = KBase.createElement("v:stroke");
            this.stroke.weight = this.weight;
            this.stroke.color = this.color;
            this.stroke.joinstyle = "round";
            this.stroke.endcap = "round";
            this.stroke.opacity = this.opacity;
            this.polygon.appendChild(this.stroke);
            //创建填充样式
            this.fill = KBase.createElement("v:fill");
            this.fill.color = this.bgcolor;
            this.fill.opacity = this.opacity;
            this.polygon.appendChild(this.fill);                        
        }
        else
        {
            this.polygon = KBase.createDiv(0);
            this.polygon.ID = "KPolygonDiv";
            this.graphics = new jsGraphics(this.polygon);            
            KBase.getStyle(this.polygon)["MozOpacity"] = this.opacity;
        }
        KBase.getStyle(this.polygon)["position"] = "absolute";
        KBase.setZIndex(this.polygon, 10);
    }
    
	/**
	 * 初始化
	 */		
    var KPolygon_initialize = function(mapObject)
    {
        var polygonObject = this;
        polygonObject.Map = mapObject; 
        
        //获取地图容器大小
        var mapSize=polygonObject.Map.getContainerSize();
        
        //设置线要素画布大小和刻度
        this.polygon.style.width=mapSize[0];        
		this.polygon.style.height=mapSize[1];				
        polygonObject.polygon.coordsize =mapSize;
        
        polygonObject.added = true;
    }
    
    /**
	 * 获取VML画线字符串--用于IE画线
	 */		
    var KPolygon_getPointsString = function()
    {
        var polygonObject = this;
        var pointText="";        
        for (var i = 0; i < polygonObject.points.length;i++)
        {            
            if (i==0)
            {
				pointText += "m" + polygonObject.points[i][0] + "," + polygonObject.points[i][1] + " ";				
			}
			else if (i<polygonObject.points.length-1)
			{
				pointText += "l" + polygonObject.points[i][0] + "," + polygonObject.points[i][1] + " ";
			}
			else
			{
				pointText += "l" + polygonObject.points[i][0] + "," + polygonObject.points[i][1] + " ";
				pointText += "l" + polygonObject.points[0][0] + "," + polygonObject.points[0][1] + " ";
			}
			
        }
        return pointText;
    }
    
    /**
	 * 画线--用于FireFox
	 */		
    var KPolygon_drawGraphics = function()
    {
        var polygonObject = this;
        var Xs = new Array(0);
        var Ys = new Array(0);      
        for (var i = 0; i < polygonObject.points.length;i++)
        {            
            Xs.push(parseInt(polygonObject.points[i][0]));
            Ys.push(parseInt(polygonObject.points[i][1]));
		}
        
        polygonObject.graphics.setColor(polygonObject.bgcolor);
        polygonObject.graphics.fillPolygon(Xs, Ys);
        polygonObject.graphics.setStroke(polygonObject.lineWidth);
        polygonObject.graphics.setColor(polygonObject.color);
        polygonObject.graphics.drawPolygon(Xs, Ys);        
        polygonObject.graphics.paint();        
    }
    
    /**
	 * 重新绘制
	 */	
    var KPolygon_reDraw = function()
    {
        var polygonObject = this;        
        if (KBase.getBrowserType()=="IE")
        {                    
            polygonObject.stroke.weight = polygonObject.lineWidth;
            polygonObject.stroke.color = polygonObject.color;            
            this.polygon.path=polygonObject.getPointsString();  
        }
        else
        {
            polygonObject.graphics.clear();
            polygonObject.drawGraphics();
        }        
    }   
    
    /**
	 * 获取HTML控件
	 */	
    var KPolygon_getObject = function()
    {
        var polygonObject = this;
        return polygonObject.polygon;
    }
    
    /**
	 * 释放面要素
	 */	
    var KPolygon_depose = function()
    {
        var polygonObject = this;
        if (!KBase.getBrowserType()=="IE")
        {
            polygonObject.graphics.clear();
            polygonObject.graphics = null;
        }
        polygonObject.added = false;
    }
    
    /**
	 * 获取边框线的颜色
	 */	
    var KPolygon_getLineColor = function()
    {
        var polygonObject = this;
        return polygonObject.color;
    }
    
    /**
	 * 获取面的颜色
	 */	
    var KPolygon_getFillColor = function()
    {
        var polygonObject = this;
        return polygonObject.bgcolor;
    }
    
    /**
	 * 获取边框线的宽度
	 */	
    var KPolygon_getLineWidth = function()
    {
        var polygonObject = this;
        return polygonObject.lineWidth;
    }
    
    /**
	 * 设置经过点的集合
	 */	
    var KPolygon_setPoints = function(points)
    {
        var polygonObject = this;
        polygonObject.points = points;        
        if (polygonObject.Map)
        {
            polygonObject.reDraw(true);
        }
    }
    
    /**
	 * 设置边框线的颜色
	 */	
    var KPolygon_setLineColor = function(color)
    {
        var polygonObject = this;
        polygonObject.color = color;                
    }
    
    /**
	 * 设置面的透明度
	 */	
    var KPolygon_setOpacity = function(scale)
    {
        var polygonObject = this;
        polygonObject.opacity = scale;
        if (KBase.getBrowserType()=="IE")
        {
            polygonObject.fill.opacity= polygonObject.opacity;
        }
        else
        {
            KBase.getStyle(polygonObject.polygon)["MozOpacity"] = polygonObject.opacity;
        }
    }
    
    /**
	 * 设置边框线的宽度
	 */	
    var KPolygon_setLineWidth = function(width)
    {
        var polygonObject = this;
        polygonObject.lineWidth = width;        
    }
    
    /**
	 * 设置面的填充色
	 */	
    var KPolygon_setFillColor = function(bgcolor)
    {
        var polygonObject = this;    
        polygonObject.bgcolor =bgcolor;
        if (KBase.getBrowserType()=="IE")
        {
			polygonObject.fill.color = bgcolor; 
		} 
    }
    
    
    KConfig.defineClass("KPolygon", KPolygon);    

    KConfig.defineFunction(KPolygon, "initialize", KPolygon_initialize);
    KConfig.defineFunction(KPolygon, "getPointsString", KPolygon_getPointsString);
	KConfig.defineFunction(KPolygon, "drawGraphics", KPolygon_drawGraphics);
    KConfig.defineFunction(KPolygon, "reDraw", KPolygon_reDraw);
    
    KConfig.defineFunction(KPolygon, "getObject", KPolygon_getObject);
    KConfig.defineFunction(KPolygon, "depose", KPolygon_depose);
    
    KConfig.defineFunction(KPolygon, "getLineColor", KPolygon_getLineColor);
    KConfig.defineFunction(KPolygon, "getLineWidth", KPolygon_getLineWidth);
    KConfig.defineFunction(KPolygon, "getFillColor", KPolygon_getFillColor);
    
    KConfig.defineFunction(KPolygon, "setPoints", KPolygon_setPoints);
    
    KConfig.defineFunction(KPolygon, "setLineColor", KPolygon_setLineColor);    
    KConfig.defineFunction(KPolygon, "setLineWidth", KPolygon_setLineWidth);
    KConfig.defineFunction(KPolygon, "setFillColor", KPolygon_setFillColor);    
    KConfig.defineFunction(KPolygon, "setOpacity", KPolygon_setOpacity);
    
    //====================================面要素类[END]==========================================
	
