/*global document */
/*jslint evil: true */
var PLISTA = PLISTA || {
	baseurl: 'http://plugin.plista.com/',
	type: 'embedded',
	addScript: function (filename) {
		var heads, plistascript;
		heads = document.getElementsByTagName('head');
		if (heads.length > 0) {
			plistascript = document.createElement('script');
			plistascript.src = PLISTA.baseurl + filename;
			heads[0].appendChild(plistascript);
		}
	}
};
PLISTA.type = 'embedded';
PLISTA.widgetsToPrepare = PLISTA.widgetsToPrepare || [];
PLISTA.loadScriptJs = PLISTA.loadScriptJs || (function () {
	var scriptTagAdded = false;
	return function () {
		if (!scriptTagAdded) {
			PLISTA.addScript('script_embedded.js');
			scriptTagAdded = true;
		}
	};
}());
PLISTA.loadScriptJs();
PLISTA.addWidget = PLISTA.addWidget || (function () {
	var cnt = 0;
	return function (widget) {
		var divID = 'plista_widget_' + cnt;
		cnt += 1;

		PLISTA.widgetsToPrepare.push(widget);
		if ((widget.type === 'list') && !widget.spec.target) {
			document.write('<ul id="' + divID + '" style="display: block; height: ' + widget.height + ';width: ' + widget.width + '; list-style-type: none"></ul>');
			widget.spec.target = document.getElementById(divID);
		} else if ((widget.type === 'ratingbar') && !widget.target) {
			document.write('<div id="' + divID + '"></div>');
			widget.target = document.getElementById(divID);
		}

		// the init method is augmented and executed by the script.js
		// make sure the widget is actually added, even after the plista object has been created
		if (typeof PLISTA.widgetsToPrepare.init === 'function') {
			PLISTA.widgetsToPrepare.init();
		}
	};
}());
