
/**
 * object/render.js
 *
 * Render an object
 */

Universe.Object.prototype.render = function() {

	this.callEvent("rendering");

	this.universe.context.globalAlpha = this.style.opacity || 1;

	var context = this.universe.context;

	if (this.type.toLowerCase() === "rectangle") {

		var x = this.getX(),
			y = this.getY();

		var width = this.getWidth(),
			height = this.getHeight();

		if (this.style.boxShadow) {

			context.shadowColor = (this.style.boxShadow.split(",")[0] || "").replace(" ", "");
			context.shadowBlur = (this.style.boxShadow.split(",")[1] || "0").replace(" ", "");
			context.shadowOffsetX = (this.style.boxShadow.split(",")[2] || "0").replace(" ", "");
			context.shadowOffsetY = (this.style.boxShadow.split(",")[3] || "0").replace(" ", "");

		}

		context.beginPath();
		context.rect(0, 0, width, height);

		if (this.style.background instanceof Universe.Image && this.style.background.image.complete) {
			
			context.fillStyle = "transparent";

			this.style.backgroundSize = this.style.backgroundSize ? this.style.backgroundSize : "fill";

			var imageSX = 0,
				imageSY = 0;

			var imageSWidth = this.style.background.image.width,
				imageSHeight = this.style.background.image.height;

			var imageX = x,
				imageY = y;

			var imageWidth = this.style.background.image.width,
				imageHeight = this.style.background.image.height;

			var imageIsCenter = this.style.backgroundCenter;

			if (this.style.backgroundSize === "adapt") {

				var boxRatio = width / height,
					imageRatio = this.style.background.image.width / this.style.background.image.height;

				imageWidth = width;
				imageHeight = height;

				if (boxRatio > imageRatio) {

					imageWidth = height * imageRatio;

				}

				else if (boxRatio < imageRatio) {

					imageHeight = width / imageRatio;

				}

			}

			else if (this.style.backgroundSize === "cover") {

				imageIsCenter = true;

				var boxRatio = width / height,
					imageRatio = this.style.background.image.width / this.style.background.image.height;

				imageWidth = width;
				imageHeight = height;

				if (boxRatio > imageRatio) {

					imageWidth = width;
					imageHeight = imageWidth / imageRatio;

				}

				else if (boxRatio < imageRatio) {

					imageHeight = width / imageRatio;

				}

			}

			if (imageIsCenter) {

				imageX += (width - imageWidth) / 2;
				imageY += (height - imageHeight) / 2;

				if (this.style.backgroundSize === "cover") {

					var diffX = Math.round(x - imageX),
						diffY = Math.round(y - imageY),

					imageSX = diffX / 2;
					imageX += diffX;

					imageSY = diffY / 2;
					imageY += diffY;

					imageWidth = width + diffX / 2;
					imageHeight = height + diffY / 2;

				}

			}

			context.drawImage(this.style.background.image, imageSX, imageSY, imageSWidth, imageSHeight, imageX, imageY, imageWidth, imageHeight);

		}

		else {

			context.fillStyle = this.style.background;

		}

		context.fill();

		this.universe.resetShadows();

		if (this.style.border) {

			context.strokeStyle = this.style.border;
			context.lineWidth = this.style.borderSize || 1;

			if (parseInt(Number((this.style.borderSize || 1) / 2)) !== Number((this.style.borderSize || 1) / 2)) {

				context.strokeRect(x - .5, y - .5, width, height);

			}

			else {

				context.strokeRect(x, y, width, height);

			}

		}

	}

	if (this.type.toLowerCase() === "line") {

		if (typeof this.style.from !== "array") {

			this.style.from = [];

		}

		if (typeof this.style.top !== "array") {

			this.style.top = [];

		}

		var fromX = Number(this.from[0]) || 0,
			fromY = Number(this.from[1]) || 0,

			toX = Number(this.to[0]) || 0,
			toY = Number(this.to[1]) || 0;

		context.beginPath();

		context.moveTo(fromX, fromY);
		context.lineTo(toX, toY);

		context.lineWidth = Number(this.style.width) || 1;
		context.strokeStyle = this.style.color;
		context.lineCap = this.style.cap || "butt";

		context.stroke();

	}

	if (this.type.toLowerCase() === "circle") {

		var x = this.getX(),
			y = this.getY();

		var size = Number(this.style.size / 2) || 0;

		if (this.style.boxShadow) {

			context.shadowColor = (this.style.boxShadow.split(",")[0] || "").replace(" ", "");
			context.shadowBlur = (this.style.boxShadow.split(",")[1] || "0").replace(" ", "");
			context.shadowOffsetX = (this.style.boxShadow.split(",")[2] || "0").replace(" ", "");
			context.shadowOffsetY = (this.style.boxShadow.split(",")[3] || "0").replace(" ", "");

		}

		context.beginPath();
		context.arc(x, y, size, 0, 2 * Math.PI);
		context.fillStyle = this.style.background;
		context.fill();

		this.universe.resetShadows();

		if (this.style.border) {

			context.strokeStyle = this.style.border;
			context.lineWidth = this.style.borderSize || 1;

			if (parseInt(Number((this.style.borderSize || 1) / 2)) !== Number((this.style.borderSize || 1) / 2)) {

				context.beginPath();
				context.arc(x - .5, y - .5, size, 0, 2 * Math.PI);
				context.stroke();

			}

			else {

				context.beginPath();
				context.arc(x, y, size, 0, 2 * Math.PI);
				context.stroke();

			}

		}

	}

	if ((this.type.toLowerCase() === "rectangle" || this.type.toLowerCase() === "box") && this.content) {

		var x = this.getX(),
			y = this.getY();

		var width = this.getWidth() || 0,
			height = this.getHeight() || 0;

		var font = this.style.font || "sans-serif",
			fontStyle = this.style.fontStyle || "",
			fontSize = Number(this.style.fontSize) || 18;

		this.universe.resetFont();

		context.font = fontStyle + " " + fontSize + "px " + font;
		context.fillStyle = this.style.color || "black";

		var textX = x,
			textY = y;

		if ((" " + this.style.textAlign + " ").indexOf(" center ") > -1 ) {

			context.textAlign = "center";
			context.textBaseline = "middle";

			textX = Number(x + width / 2);
			textY = Number(y + height / 2);

		}

		else if ((" " + this.style.textAlign + " ").indexOf(" vertical-center ") > -1 ) {

			context.textBaseline = "middle";

			textY = Number(y + height / 2);

		}

		else if ((" " + this.style.textAlign + " ").indexOf(" horizontal-center ") > -1 ) {

			context.textAlign = "center";

			textX = Number(x + width / 2);
			textY = y + fontSize;

		}

		else {

			textY = y + fontSize;

		}

		if ((" " + this.style.textAlign + " ").indexOf(" top ") > -1 ) {

			context.textBaseline = "top";

			textY = y;

		}

		if ((" " + this.style.textAlign + " ").indexOf(" right ") > -1 ) {

			context.textAlign = "end";

			textX = x + width;

		}

		if ((" " + this.style.textAlign + " ").indexOf(" bottom ") > -1 ) {

			context.textBaseline = "alphabetic";

			textY = y + height;

		}

		if ((" " + this.style.textAlign + " ").indexOf(" left ") > -1 ) {

			context.textAlign = "start";

			textX = x;

		}


		if (this.style.textShadow) {

			context.shadowColor = (this.style.textShadow.split(",")[0] || "").replace(" ", "");
			context.shadowBlur = (this.style.textShadow.split(",")[1] || "0").replace(" ", "");
			context.shadowOffsetX = (this.style.textShadow.split(",")[2] || "0").replace(" ", "");
			context.shadowOffsetY = (this.style.textShadow.split(",")[3] || "0").replace(" ", "");

		}
		
		context.fillText(this.content, textX, textY);

		this.universe.resetShadows();

	}

	if (this.type.toLowerCase() === "circle" && this.content) {

		var x = this.getX() - size,
			y = this.getY() - size;

		var size = Number(this.style.size / 2) || 0;

		var font = this.style.font || "sans-serif",
			fontStyle = this.style.fontStyle || "",
			fontSize = Number(this.style.fontSize) || 18;

		this.universe.resetFont();

		context.font = fontStyle + " " + fontSize + "px " + font;
		context.fillStyle = this.style.color || "black";

		var textX = x,
			textY = y;

		if ((" " + this.style.textAlign + " ").indexOf(" center ") > -1 ) {

			context.textAlign = "center";
			context.textBaseline = "middle";

			textX = x + size;
			textY = y + size;

		}

		else if ((" " + this.style.textAlign + " ").indexOf(" vertical-center ") > -1 ) {

			context.textBaseline = "middle";

			textY = y + size;

		}

		else if ((" " + this.style.textAlign + " ").indexOf(" horizontal-center ") > -1 ) {

			context.textAlign = "center";

			textX = x + size;
			textY = y + fontSize;

		}

		else {

			textY = y + fontSize;

		}

		if ((" " + this.style.textAlign + " ").indexOf(" top ") > -1 ) {

			context.textBaseline = "top";

			textY = y;

		}

		if ((" " + this.style.textAlign + " ").indexOf(" right ") > -1 ) {

			context.textAlign = "end";

			textX = x + size * 2;

		}

		if ((" " + this.style.textAlign + " ").indexOf(" bottom ") > -1 ) {

			context.textBaseline = "alphabetic";

			textY = y + size * 2;

		}

		if ((" " + this.style.textAlign + " ").indexOf(" left ") > -1 ) {

			context.textAlign = "start";

			textX = x;

		}


		if (this.style.textShadow) {

			context.shadowColor = (this.style.textShadow.split(",")[0] || "").replace(" ", "");
			context.shadowBlur = (this.style.textShadow.split(",")[1] || "0").replace(" ", "");
			context.shadowOffsetX = (this.style.textShadow.split(",")[2] || "0").replace(" ", "");
			context.shadowOffsetY = (this.style.textShadow.split(",")[3] || "0").replace(" ", "");

		}
		
		context.fillText(this.content, textX, textY);

		this.universe.resetShadows();

	}

	context.globalAlpha = 1;

	this.callEvent("render");

};