var EmbedCarbon = (function() {
	var E = "", $window = window, $escape = window.escape, $unescape = window.unescape, $document = document, t = {
		mass : 'm',
		size : 'd'
	}, server = ("http://embed.carbonquilt.org/"), lt = $unescape('%3C'), gt = $unescape('%3E'), $write = function(
			s) {
		$document.write(s);
	}, append = "appendChild", opentag = function(t, id, cn, st) {
		return lt + t + (id ? ' id="' + id + '"' : E)
		+ (cn ? ' class="' + cn + '"' : E) + (st ? ' style="'+st+'"' : E) + gt;
	}, closetag = function(t) {
		return lt + '/' + t + gt;
	}, div = 'div', h3 = 'h3', sub = 'sub', abv = "above", bel = "below", defaultSize = 300, co2 = function(
			s) {
		return s.replace(/co2/i, "CO" + opentag(sub) + "2" + closetag(sub));
	}, options = function(opt) {
	};

	return {

		cube : function(id, opts) {
			if (typeof id !== "string") {
				opts = id;
				id = false;
			}
			var p = [], width = (opts.size || defaultSize), height = width, hasTitle = (opts.title && opts.title !== E), tt = E, tp = (opts.titlePosition || abv), titleAbove = (tp == abv), iframe = '', embed, html = '', caption = ((opts.caption && opts.caption !== E) ? co2(opts.caption)
					: false);
			for (k in opts) {
				if (opts.hasOwnProperty(k) && t[k]) {
					p[p.length] = t[k] + '=' + $escape(opts[k]);
				}
			};
			
			iframe = lt
					+ 'iframe frameborder="0" scrolling="no" style="border:0;padding:0;margin:0;width:'
					+ width + 'px;height:' + height + 'px;" src="' + server
					+ 'CarbonVisuals.html?' + p.join('&')
					+ $unescape('"%3E%3C/iframe%3E');

			if (id) {
				embed = function(s) {
					$document.getElementById(id).innerHTML = s;
				};
			} else {
				embed = $write;
			}

			html += opentag(div, opts.id, (opts.className || "cv-cube") + '  ' + (hasTitle ? tp : 'no-title') + (caption ? ' caption' : ' no-caption'), 'width:'+width+'px');
			if (hasTitle) {
				tt = opentag(h3) + co2(opts.title) + closetag(h3);
			}
			if (titleAbove) {
				html += tt;
			}
			html += iframe;
			if (!titleAbove) {
				html += tt;
			}
			if (caption) {
				html += opentag('p', null, 'caption') + caption + closetag('p')
			}
			html += closetag(div);
			embed(html);
		}
	}
})();

