/*
Software License Agreement (BSD License)
Copyright (c) 2009, Conquex Ltd
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Conquex Ltd nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Conquex Ltd ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Conquex Ltd BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
Cnx.SvgRenderer = function(parent, width, height) {
	this.parent=parent;
	this.width=width;
	this.height=height;
	this.rendererName="Cnx.SvgRenderer";
};

Cnx.SvgRenderer.SVG_NS="http://www.w3.org/2000/svg"; // SVG namespace
Cnx.SvgRenderer.gradientNumber=0; // we need this since gradients are referenced by id, might be a good idea to add a prefix

// Check if browser supports SVG
//   credits to crescentfresh for his post at http://stackoverflow.com/questions/654112/how-do-you-detect-support-for-vml-or-svg-in-a-browser
Cnx.SvgRenderer.isSupported = function() {
	try {
		if(document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Shape", "1.0")) return true;
		else if(document.childNodes && !document.all && !navigator.taintEnabled) return true; // Chrome?
		else return false;
	} catch(e) {
		return false;
	}
};

Cnx.SvgRenderer.prototype.init = function() { // Init renderer instance
	// Create SVG
	this.element=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "svg");
	this.element.setAttribute("xmlns", "http://www.w3.org/2000/svg");
	this.element.setAttribute("version", "1.1");
	this.element.setAttribute("viewBox", "0 0 "+this.width+" "+this.height);
	this.element.setAttribute("style", "width: "+this.width+"px; height: "+this.height+"px");
	this.parent.appendChild(this.element);

	// Create DEFS
	this.defs=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "defs");
	this.element.appendChild(this.defs);

	// Create G
	this.g=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "g");
	this.element.appendChild(this.g);
};

Cnx.SvgRenderer.prototype.removeAllChildren = function(node) { // DOM recursive remove children function
	for(var i = node.childNodes.length -1; i >= 0; i--) {
		this.removeAllChildren(node.childNodes[i]);
		node.removeChild(node.childNodes[i]);
	}	
};

Cnx.SvgRenderer.prototype.reset = function() { // Reset renderer
	this.removeAllChildren(this.element);

	// Create DEFS
	this.defs=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "defs");
	this.element.appendChild(this.defs);

	// Create G
	this.g=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "g");
	this.element.appendChild(this.g);
};

Cnx.SvgRenderer.prototype.drawImage = function(url, x, y, width, height, opacity) {
	var img=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "image");
	img.setAttribute("x", x);
	img.setAttribute("y", y);
	img.setAttribute("width", width);
	img.setAttribute("height", height);
	img.setAttribute("opacity", opacity);
	img.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", url);
	this.g.appendChild(img);
};

Cnx.SvgRenderer.prototype.drawLine = function(x1, y1, x2, y2, strokeWidth, strokeColor) {
	var line=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "line");
	line.setAttribute("style", "stroke: "+strokeColor+";");
	line.setAttribute("stroke-width", strokeWidth);
	line.setAttribute("x1", x1);
	line.setAttribute("x2", x2);
	line.setAttribute("y1", y1);
	line.setAttribute("y2", y2);
	this.g.appendChild(line);
};

Cnx.SvgRenderer.prototype.drawDashedLine = function(x1, y1, x2, y2, strokeWidth, strokeColor) {
	var line=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "line");
	line.setAttribute("style", "stroke-dasharray: 9, 5; stroke: "+strokeColor+";");
	line.setAttribute("stroke-width", strokeWidth);
	line.setAttribute("x1", x1);
	line.setAttribute("x2", x2);
	line.setAttribute("y1", y1);
	line.setAttribute("y2", y2);
	this.g.appendChild(line);
};

Cnx.SvgRenderer.prototype.drawText = function(text, x, y, fontFamily, fontSize, color, boldFlag, italicFlag, anchor, translateX, translateY, rotate) { 
	var txtInnerNode=document.createTextNode(text);
	var txt=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "text");
	txt.setAttribute("text-anchor", anchor);
	txt.setAttribute("x", x);
	txt.setAttribute("y", y);
	txt.setAttribute("font-family", fontFamily);
	txt.setAttribute("font-size", fontSize+"px");
	txt.setAttribute("fill", color);
	if(boldFlag) txt.setAttribute("font-weight", "bold");
	if(italicFlag) txt.setAttribute("font-style", "italic");
	var transform="";
	if( (translateX!=0) || (translateY!=0) ) transform+="translate("+translateX+", "+translateY+") ";
	if(rotate!=0) transform+="rotate("+rotate+") ";
	if(transform!="") txt.setAttribute("transform", transform);
	txt.appendChild(txtInnerNode);
	this.g.appendChild(txt);
};

Cnx.SvgRenderer.prototype.drawRectangleWithGradientAndTransform = function(
		x, y, width, height, drawBorderFlag, 
		startColor, startAlpha, 
		endColor, endAlpha, 
		translateX,
		translateY,
		rotate,
		shearX,
		shearY
	) {

	var startGradientColor=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "stop");
	startGradientColor.setAttribute("style", "stop-color: "+startColor+"; stop-opacity: "+startAlpha);
	startGradientColor.setAttribute("offset", "0%");
	var endGradientColor=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "stop");
	endGradientColor.setAttribute("style", "stop-color: "+endColor+"; stop-opacity: "+endAlpha);
	endGradientColor.setAttribute("offset", "100%");

	var gradient=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "linearGradient");
	gradient.id=Cnx.SvgRenderer.gradientNumber++;
	gradient.appendChild(startGradientColor);
	gradient.appendChild(endGradientColor);
	this.defs.appendChild(gradient);

	var rect=document.createElementNS(Cnx.SvgRenderer.SVG_NS, "rect");
	rect.setAttribute("x", x);
	rect.setAttribute("y", y);
	rect.setAttribute("width", width);
	rect.setAttribute("height", height);
	rect.setAttribute("style", "fill:url(#"+gradient.id+");");
	if(drawBorderFlag==1) rect.setAttribute("stroke", "black");

	var transform="";
	if( (translateX!=0) || (translateY!=0) ) transform+="translate("+translateX+", "+translateY+") ";
	if(rotate!=0) transform+="rotate("+rotate+") ";
	if(shearX!=0) transform+="skewX("+shearX+") ";
	if(shearY!=0) transform+="skewY("+shearY+") ";
	if(transform!="") rect.setAttribute("transform", transform);

	this.g.appendChild(rect);
};

Cnx.SvgRenderer.prototype.finish = function() {
};
