/**
 * This is the JavaScript draw library. Other draw libraries must implement the same
 * methods.
 */

function DrawLib() {
    
    //Sets up inheritance.
    DrawLibBase.call();
    DrawLib.prototype.inheritFrom(DrawLibBase);
    
    DrawLib.prototype.addRectangle = function(parentCanvas, id, left, top, width, height, zIndex, borderWidth, borderColor, fillColor, opacity) {
    
        return this.addMarker(parentCanvas, id, left, top, width, height, zIndex, borderWidth, borderColor, fillColor, opacity)
    }
    
    DrawLib.prototype.resizeShape = function(shapeObj, x1, y1, x2, y2) {
    
        if (shapeObj == null) {
            return;
        }
        
        var width = x2 - x1;
        var height = y2 - y1;
        
        if (width < 0) {
            shapeObj.style.left = x2 + "px";
            shapeObj.style.width = Math.abs(width) + "px";
        }
        else {
            shapeObj.style.width = width + "px";
        }
        if (height < 0) {
            shapeObj.style.top = y2 + "px";
            shapeObj.style.height = Math.abs(height) + "px";
        }
        else {
            shapeObj.style.height = height + "px";
        }
		
    }
    
    DrawLib.prototype.deleteShape = function(shapeObj) {
            
	    if (!shapeObj) {
	        return;
	    }
		shapeObj.parentNode.removeChild(shapeObj);
		return null;
    }
    
    DrawLib.prototype.addPolyLine = function(parentCanvas, id, color, weight, coordList, zIndex) {
        
        var xA = new Array();
        var yA = new Array();
        
        var shapeContainer = document.createElement("div");
        shapeContainer.id = id;
        shapeContainer.style.zIndex = -1;
        shapeContainer.shapeColor = color;
        shapeContainer.shapeWeight = weight;
        
        parentCanvas.appendChild(shapeContainer);
        
        var g=new jsGraphics(shapeContainer.id);
		g.setColor(color);
		g.setStroke(weight);
        
		var length = coordList.length;
		var pointList = "";
		
		for (var i=0; i<length; i++) {
		    xA[i] = coordList[i][0];
		    yA[i] = coordList[i][1];
        }
        
	    g.drawPolyline(xA, yA);
		g.paint();
		
		if (zIndex) {
		    //Set the zIndex of all of the graphics stuff.
		    if (shapeContainer.firstChild) {
		        var child = shapeContainer.firstChild;
		        while(child) {
		            child.style.zIndex = zIndex;
		            child = child.nextSibling;
		        }
		    }
		    shapeContainer.style.zIndex = zIndex;
		}
		
		return shapeContainer;
    }
    
    DrawLib.prototype.addPolygon = function(parentCanvas, id, color, weight, coordList, zIndex) {
        
        //HACK: Fill the polygon?
        //Add that last point.
        var length = coordList.length;
        if (length > 2) {
            coordList.push(new Array(coordList[0][0], coordList[0][1]));
            length = coordList.length;
        }
	    var pointList = "";
	    for (var i=0; i<length; i++) {
	        pointList += coordList[i][0] + "," + coordList[i][1] + ",";
	    }
	    
	    var shapeContainer = this.addPolyLine(parentCanvas, id, color, weight, coordList, zIndex);
    
        //Remove the added point
	    if (length > 2) {
	        coordList.pop();
	    }
	    
	    shapeContainer.isPolygon = true;
	    
        return shapeContainer;
    }
    
    DrawLib.prototype.reshapeShape = function(shape, coordList) {
        
        var parentCanvas = shape.parentNode;
        var id = shape.id;
        var color = shape.shapeColor;
        var weight = shape.shapeWeight;
        var zIndex = shape.style.zIndex;
        var isPolygon = shape.isPolygon;
        
        this.deleteShape(shape);
        
        if (isPolygon) {
            shape = this.addPolygon(parentCanvas, id, color, weight, coordList, zIndex);
        }
        else {
            shape = this.addPolyLine(parentCanvas, id, color, weight, coordList, zIndex);
        }
        
        return shape;
    }
    
    DrawLib.prototype.addCircle = function(parentCanvas, id, x, y, radius, color, weight, zIndex) {
        
		var shapeContainer = document.createElement("div");
        shapeContainer.id = id;
        shapeContainer.style.zIndex = -1;
        shapeContainer.shapeColor = color;
        shapeContainer.shapeWeight = weight;
        
        parentCanvas.appendChild(shapeContainer);
        
        var g=new jsGraphics(shapeContainer.id);
		g.setColor(color);
		g.setStroke(weight);
		g.drawOval(x-radius, y-radius, radius*2, radius*2);
		g.paint();
		
		if (zIndex) {
		    //Set the zIndex of all of the graphics stuff.
		    if (shapeContainer.firstChild) {
		        var child = shapeContainer.firstChild;
		        while(child) {
		            child.style.zIndex = zIndex;
		            child = child.nextSibling;
		        }
		    }
		    shapeContainer.style.zIndex = zIndex;
		}
		
		return shapeContainer;	
    }
}