/**
 * A JavaScript wrapper class around the NCSSimpleVectorLayer
 */
function NCSVectorLayer(NCSView, layerName)
{
	this.layerName = layerName;
	this.ncsView = NCSView;
	this.fillColor = "#000000";
	this.penColor  = "#000000";
	this.lineThickness = 2.0;
	this.currentType = 0;
    this.objectPointsX = new Array();
    this.objectPointsY = new Array();
    this.objectNumPoints = 0;
}
NCSVectorLayer.POLYLINE      = 0;
NCSVectorLayer.POLYGON       = 1;
NCSVectorLayer.ELLIPSE       = 2;
NCSVectorLayer.CIRCLE        = 3;
NCSVectorLayer.FILLEDCIRCLE  = 4;
NCSVectorLayer.FILLEDPOLYGON = 5;
NCSVectorLayer.FILLEDELLIPSE = 6;
NCSVectorLayer.RECTANGLE     = 7;
NCSVectorLayer.FILLEDRECTANGLE = 8;
NCSVectorLayer.prototype.setView = function(value)
{
	this.ncsView = value;
}
NCSVectorLayer.prototype.setTransparency = function(value)
{
    this.ncsView.SetLayerTransparency(this.layerName, this.fillColor, value);
}
NCSVectorLayer.prototype.redraw = function()
{
    this.ncsView.SetLayerParameter("VectorOverlay", "fillcolor=" + this.fillColor + ";color=" + this.penColor + ";linewidth=" + this.lineThickness + ";");
}
NCSVectorLayer.prototype.setMode = function(value)
{
	if (value == "polyline")
    {
    	this.currentType = NCSVectorLayer.POLYLINE;
    }
	else if (value == "polygon")
	{
		this.currentType = NCSVectorLayer.POLYGON;
    }
    else if (value == "elipse")
    {
    	this.currentType = NCSVectorLayer.ELLIPSE;
    }
    else if (value == "circle")
    {
    	this.currentType = NCSVectorLayer.CIRCLE;
    }
    else if (value == "filledcircle")
    {
    	this.currentType = NCSVectorLayer.FILLEDCIRCLE;
    }
    else if (value == "filledpolygon")
    {
    	this.currentType = NCSVectorLayer.FILLEDPOLYGON;
    }
    else if (value == "filledelipse")
	{
		this.currentType = NCSVectorLayer.FILLEDELLIPSE;
    }
    else if (value == "rectangle")
	{
		this.currentType = NCSVectorLayer.RECTANGLE;
    }
    else if (value == "filledrectangle")
	{
		this.currentType = NCSVectorLayer.FILLEDRECTANGLE;
    }
    else
    {
    	this.currentType = NCSVectorLayer.POLYLINE;
    }
}
NCSVectorLayer.prototype.mouseDown = function(worldx,worldy)
{
    switch(this.currentType)
	{
		case 0:
		case 1:
		case 5:
		    if (this.objectNumPoints == 0)
		    {
                this.objectPointsX[this.objectNumPoints] = worldx;
                this.objectPointsY[this.objectNumPoints] = worldy;
                this.objectNumPoints ++;
            }
        break;

		case 2:
		     // havent done ellipse code yet
        break;

		case 3:
		case 4:
		case 7:
		case 8:
        	this.objectNumPoints = 0;
            this.objectPointsX = new Array();
            this.objectPointsY = new Array();
            this.objectPointsX[this.objectNumPoints] = worldx;
            this.objectPointsY[this.objectNumPoints] = worldy;
            this.objectNumPoints ++;
		break;
		case 5:
		     // filled polygon not done yet
		break;
		case 6:
		     // filled ellipse not done yet.
		break;

		default:
		break;
    }
}
NCSVectorLayer.prototype.mouseUp = function(worldx,worldy)
{
	var vecObject = "";

    switch(this.currentType)
	{
		case 0:
		case 1:
		case 5:
		    var objectTag = "";
		    if (this.currentType == 0) objectTag = "polyline1";
		    if (this.currentType == 1) objectTag = "polygon1";
		    if (this.currentType == 5) objectTag = "filledpolygon1";
		    this.objectPointsX[this.objectNumPoints] = worldx;
            this.objectPointsY[this.objectNumPoints] = worldy;
            this.objectNumPoints++;

            this.objectPointsX[this.objectNumPoints] = worldx;
            this.objectPointsY[this.objectNumPoints] = worldy;
            vecObject = "linewidth=" + this.lineThickness + ";color=" + this.penColor + ";fillcolor=" + this.fillColor + ";" + objectTag + "=";
            for (i=0; i<this.objectPointsX.length; i++)
            {
                vecObject += this.objectPointsX[i] + "," + this.objectPointsY[i] + (i==this.objectPointsX.length? "" : "|");
            }
            vecObject += ";";
		break;

		case 2:
		break;
		case 3:
		    this.objectPointsX[1] = worldx;
            this.objectPointsY[1] = worldy;
		break;
		case 4:
		    this.objectPointsX[1] = worldx;
            this.objectPointsY[1] = worldy;
		break;
		case 5:
		    this.objectPointsX[this.objectNumPoints] = worldx;
            this.objectPointsY[this.objectNumPoints] = worldy;
            this.objectNumPoints++;
		break;
		case 6:
		break;
		case 7:
		break;
		case 8:
		break;
    }
    this.ncsView.SetLayerParameter("VectorOverlay", vecObject);
}
NCSVectorLayer.prototype.mouseMove = function(worldx,worldy)
{
	var vecObject = "";

	switch(this.currentType)
	{
		case 0:
		default:
		break;
		
        case 1:
		    this.objectPointsX[this.objectNumPoints] = worldx;
            this.objectPointsY[this.objectNumPoints] = worldy;
            vecObject = "linewidth=" + this.lineThickness + ";color=" + this.penColor + ";fillcolor=" + this.fillColor + ";polygon1=";
            for (i=0; i<this.objectPointsX.length; i++)
            {
                vecObject += this.objectPointsX[i] + "," + this.objectPointsY[i] + (i==this.objectPointsX.length? "" : "|");
            }
            vecObject += ";";
		break;

        case 2:
		break;

        case 3:
		    this.objectPointsX[1] = worldx;
            this.objectPointsY[1] = worldy;
            vecObject = "linewidth=" + this.lineThickness + ";color=" + this.penColor + ";fillcolor=" + this.fillColor + ";circle1="
	               + this.objectPointsX[0] + "," + this.objectPointsY[0] + "|"
                   + this.objectPointsX[1] + "," + this.objectPointsY[1] + ";";
        break;

        case 4:
		    this.objectPointsX[1] = worldx;
            this.objectPointsY[1] = worldy;
            vecObject = "linewidth=" + this.lineThickness + ";color=" + this.penColor + ";fillcolor=" + this.fillColor + ";filledcircle1="
	               + this.objectPointsX[0] + "," + this.objectPointsY[0] + "|"
                   + this.objectPointsX[1] + "," + this.objectPointsY[1] + ";";
		break;

        case 5:
		    this.objectPointsX[this.objectNumPoints] = worldx;
            this.objectPointsY[this.objectNumPoints] = worldy;
            vecObject = "linewidth=" + this.lineThickness + ";color=" + this.penColor + ";fillcolor=" + this.fillColor + ";filledpolygon1=";
            for (i=0; i<this.objectPointsX.length; i++)
            {
                vecObject += this.objectPointsX[i] + "," + this.objectPointsY[i] + (i==this.objectPointsX.length? "" : "|");
            }
            vecObject += ";";
		break;

		case 6:
		break;

		case 7:
		    this.objectPointsX[1] = worldx;
            this.objectPointsY[1] = worldy;
            vecObject = "linewidth=" + this.lineThickness + ";color=" + this.penColor + ";fillcolor=" + this.fillColor + ";polygon1="
	               + this.objectPointsX[0] + "," + this.objectPointsY[0] + "|"
                   + this.objectPointsX[1] + "," + this.objectPointsY[0] + "|"
                   + this.objectPointsX[1] + "," + this.objectPointsY[1] + "|"
                   + this.objectPointsX[0] + "," + this.objectPointsY[1]  + ";";
		break;
		case 8:
		    this.objectPointsX[1] = worldx;
            this.objectPointsY[1] = worldy;
            vecObject = "linewidth=" + this.lineThickness + ";color=" + this.penColor + ";fillcolor=" + this.fillColor + ";filledpolygon1="
	               + this.objectPointsX[0] + "," + this.objectPointsY[0] + "|"
                   + this.objectPointsX[1] + "," + this.objectPointsY[0] + "|"
                   + this.objectPointsX[1] + "," + this.objectPointsY[1] + "|"
                   + this.objectPointsX[0] + "," + this.objectPointsY[1]  + ";";
		break;
    }
    this.ncsView.SetLayerParameter("VectorOverlay", vecObject);
}
NCSVectorLayer.prototype.clear = function()
{
	this.ncsView.SetLayerParameter("VectorOverlay", "polyline1=;");
	this.objectNumPoints = 0;
    this.objectPointsX = new Array();
    this.objectPointsY = new Array();	
}
NCSVectorLayer.prototype.save = function()
{
	return "<?xml version='1.0' ?><ECWERROR value='0'>Not Yet Implemented.</ECWERROR>";
}
NCSVectorLayer.prototype.load = function(xml)
{
	alert(xml);
}

