/**
 * @author itc073
 */

var SvgRenderer = function(elementId) {
	this.container = document.getElementById(elementId);
	this.container.style.MozUserSelect = 'none';
    this.container.style.overflow = 'hidden';
    var svgNamespace = 'http://www.w3.org/2000/svg';
	this.svgRoot = this.container.ownerDocument.createElementNS(svgNamespace, "svg");
    this.svgRoot.style.position = 'absolute';
    this.svgRoot.style.zIndex = '1';
    this.container.appendChild(this.svgRoot);
	this.pathData = null;

}

SvgRenderer.prototype = {

	create : function(shape, fillColor, lineColor, lineWidth, left, top, width, height) {

	  var svgNamespace = 'http://www.w3.org/2000/svg';
	  var svg;

	  if (shape == 'rect') {
	    svg = this.container.ownerDocument.createElementNS(svgNamespace, 'rect');
	    svg.setAttributeNS(null, 'x', left + 'px');
	    svg.setAttributeNS(null, 'y', top + 'px');
	    svg.setAttributeNS(null, 'width', width + 'px');
	    svg.setAttributeNS(null, 'height', height + 'px');
	  }
	  else if (shape == 'oval') {
	    svg = this.container.ownerDocument.createElementNS(svgNamespace, 'ellipse');
	    svg.setAttributeNS(null, 'cx', (left + width / 2) + 'px');
	    svg.setAttributeNS(null, 'cy', (top + height / 2) + 'px');
	    svg.setAttributeNS(null, 'rx', (width / 2) + 'px');
	    svg.setAttributeNS(null, 'ry', (height / 2) + 'px');
	  }
	  else if (shape == 'circle') {
	    svg = this.container.ownerDocument.createElementNS(svgNamespace, 'circle');
	    svg.setAttributeNS(null, 'cx', (left + width / 2) + 'px');
	    svg.setAttributeNS(null, 'cy', (top + height / 2) + 'px');
	    svg.setAttributeNS(null, 'r', (width / 2) + 'px');

	  }
	  else if (shape == 'roundrect') {
	    svg = this.container.ownerDocument.createElementNS(svgNamespace, 'rect');
	    svg.setAttributeNS(null, 'x', left + 'px');
	    svg.setAttributeNS(null, 'y', top + 'px');
	    svg.setAttributeNS(null, 'rx', '20px');
	    svg.setAttributeNS(null, 'ry', '20px');
	    svg.setAttributeNS(null, 'width', width + 'px');
	    svg.setAttributeNS(null, 'height', height + 'px');
	  }

	  else if (shape == 'shape' || shape == 'line') {

        this.pathData = new Array();
	    svg = this.container.ownerDocument.createElementNS(svgNamespace, 'path');
	    this.pathData.push("M " + left + " " + top + " L ");

	  }


	  //svg.style.position = 'absolute';
     // svg.style.zIndex = '100000';

      if (fillColor.length == 0 ||  shape == 'line')
	    fillColor = 'none';
	  svg.setAttributeNS(null, 'fill', fillColor);

	  if (lineColor.length == 0)
	    lineColor = 'none';
	  svg.setAttributeNS(null, 'stroke', lineColor);
	  svg.setAttributeNS(null, 'stroke-width', lineWidth);
	  svg.setAttributeNS(null, 'fill-opacity', 0.5);

      this.svgRoot.appendChild(svg);

      return svg;
	},

	closePolygone: function(shape) {
		shape.setAttribute("d", this.pathData.join(" ") + "Z");
		//this.pathData = null;
		//this.pathData = new Array();

	},
	addPoint: function(x,y) {
		this.pathData.push(x + " " + y);
	},

	removePoint: function(x, y) {
		for(point in this.pathData) {
			;
		}
	},

    removeLastPoint: function() {
        this.pathData.pop();
        this.pathData.pop();
    },

	resize : function(shape, fromX, fromY, toX, toY) {
	  var deltaX = toX - fromX;
	  var deltaY = toY - fromY;

	  if (shape.tagName == 'line') {
	    shape.setAttributeNS(null, 'x2', toX);
	    shape.setAttributeNS(null, 'y2', toY);
	  }
	  else if (shape.tagName == 'ellipse') {
	    if (deltaX < 0) {
	      shape.setAttributeNS(null, 'cx', (fromX + deltaX / 2) + 'px');
	      shape.setAttributeNS(null, 'rx', (-deltaX / 2) + 'px');
	    }
	    else {
	      shape.setAttributeNS(null, 'cx', (fromX + deltaX / 2) + 'px');
	      shape.setAttributeNS(null, 'rx', (deltaX / 2) + 'px');
	    }

	    if (deltaY < 0) {
	      shape.setAttributeNS(null, 'cy', (fromY + deltaY / 2) + 'px');
	      shape.setAttributeNS(null, 'ry', (-deltaY / 2) + 'px');
	    }
	    else {
	      shape.setAttributeNS(null, 'cy', (fromY + deltaY / 2) + 'px');
	      shape.setAttributeNS(null, 'ry', (deltaY / 2) + 'px');
	    }
	  } else if (shape.tagName == 'circle') {
	      shape.setAttributeNS(null, 'r', (deltaX ) + 'px');
	  }
	  else if(shape.tagName == 'path')	{

          shape.setAttribute("d", this.pathData.join(" ") + " L " + toX + " " + toY );

	  }
	  else {
	    if (deltaX < 0) {
	      shape.setAttributeNS(null, 'x', toX + 'px');
	      shape.setAttributeNS(null, 'width', -deltaX + 'px');
	    }
	    else {
	      shape.setAttributeNS(null, 'width', deltaX + 'px');
	    }

	    if (deltaY < 0) {
	      shape.setAttributeNS(null, 'y', toY + 'px');
	      shape.setAttributeNS(null, 'height', -deltaY + 'px');
	    }
	    else {
	      shape.setAttributeNS(null, 'height', deltaY + 'px');
	    }
	  }
	},

	clear: function() {
		while (this.svgRoot.hasChildNodes()) {
			this.svgRoot.removeChild(this.svgRoot.firstChild);
		}
		//this.container.innerHTML = ""
	}


};