/**
 * @author itc073
 */

//Konstruktor
var VmlRenderer = function(element) {

		//Namespace und css behavior
        var mapDiv = document.getElementById(element);
        var container = mapDiv.ownerDocument.createElement("div");
        //@todo hack wegen border der karte
        container.style.left = "-2px";
        container.style.top = "-2px";



        mapDiv.style.overflow = 'hidden';
        mapDiv.appendChild(container);
        this.container = container;






        this.pathData = null;
		this.container.ownerDocument.namespaces.add("v", "urn:schemas-microsoft-com:vml");
		this.style = this.container.ownerDocument.createStyleSheet();
		//this.style.addRule('v\\:*', "behavior: url(#default#VML);antialias: true;");
        this.style.addRule('v\\:*', "behavior: url(#default#VML);");


};



VmlRenderer.prototype = {



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

		var vml = null;

		if (shape == 'rect') {
		    vml = this.container.ownerDocument.createElement('v:rect');
		} else if (shape == 'roundrect') {
		    vml = this.container.ownerDocument.createElement('v:roundrect');
		} else if (shape == 'oval' || shape == 'circle') {
		    vml = this.container.ownerDocument.createElement('v:oval');
		} else if (shape == 'line') {
		    vml = this.container.ownerDocument.createElement('v:shape');
		} else if (shape == 'shape') {
		    vml = this.container.ownerDocument.createElement('v:shape');
		} else {
			return null;
		}


		// handle fill
		var fill = this.container.ownerDocument.createElement("v:fill");
		if (fillColor != '' && shape !== 'line') {
			fill.setAttribute("color", fillColor);

			fill.setAttribute("opacity","0.5");
		} else {
			fill.setAttribute("on", false);
		}

		// handle stroke
		var stroke = this.container.ownerDocument.createElement("v:stroke");
		if (strokeColor != '') {
			stroke.setAttribute("color", strokeColor);
			stroke.setAttribute("weight","2px")
		} else {
			stroke.setAttribute("on", false);
		}





	  vml.appendChild(fill);
	  vml.appendChild(stroke);
	  vml.style.position = 'absolute';



      if(shape == 'shape' || shape == 'line') {
	  		vml.style.left = 0;
	    	vml.style.top = 0;


			vml.setAttribute("coordsize","600 600");
			vml.setAttribute("coordorigin","0 0");
			vml.style.width = "600px";
	    	vml.style.height = "600px";
			this.pathData = new Array();
			this.pathData.push("m " + left + "," + top + " l ");
	  }  else  {
		    vml.style.left = left;
		    vml.style.top = top;
		    vml.style.width = width;
		    vml.style.height = height;
	  }
	  this.container.appendChild(vml);
	  return vml;
	},


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

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

    closePolygone: function(shape) {
		shape.setAttribute("path", this.pathData.join(" ") + "xe");
		//alert(this.pathData.join(" ") +  "  e ");
	},

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

	  if(shape.tagName == 'shape')	{
	  	shape.setAttribute("path", this.pathData.join(" ") + " "+ toX + "," + toY + "  e ");

	  }

	  else if (shape.tagName == 'polyline') {
	  	//alert(shape.toString());
	   	shape.setAttribute("points", "0,60,50,90,60,10,100,100");
	  }
	  else {
	    if (deltaX < 0) {
	      shape.style.left = toX + 'px';
	      shape.style.width = -deltaX + 'px';
	    }
	    else {
          shape.style.left = fromX + 'px';
          shape.style.width = deltaX + 'px';
	    }

	    if (deltaY < 0) {
	      shape.style.top = toY + 'px';
	      shape.style.height = -deltaY + 'px';
	    }
	    else {
          shape.style.top = fromY + 'px';
          shape.style.height = deltaY + 'px';
	    }
	  }
	},

	clear: function() {

        while (this.container.hasChildNodes()) {
            this.container.removeChild(this.container.firstChild);
        }
        this.create("rect", "" ,"",  "0" , 1 , 1, 1,1);
    }
}






