﻿function ProductWidget(id) {
	this.__my_id = id;
}
ProductWidget.prototype.__my_id = null;
ProductWidget.prototype.product_id = null;
ProductWidget.prototype.product_name = null;
ProductWidget.prototype.__deferred = null;
ProductWidget.prototype.fetch = function(container_id) {
	var ph = this.__getPlaceholder();
	if (container_id != undefined) {
		var cc = document.getElementById(container_id);
		if (cc != null) cc.innerHTML = ph;
	} else {
		document.write(ph);
	}
	var args = {};
	if (this.product_id != null) {
		args.pid = this.product_id;
	} else if (ProductWidget.__extract_id != null) {
		args.pid = ProductWidget.__extract_id();
	} else if (this.product_name != null) {
		args.pname = this.product_name;
	} else {
		args = ProductWidget.__fallback();
	}
	args.sid = ProductWidget.__shop_id;
	args.id = this.__my_id;
	var d = new Date();
	args.no_cache = d.getTime();
	var qs = [];
	for (k in args) {
		qs[qs.length] = k + '=' + escape(args[k]);
	}
	qs = qs.join('&');
	var js = document.createElement('script');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', 'http://www.opineo.pl/shop/product.php?' + qs);
	var head = document.getElementsByTagName('head').item(0);
	head.appendChild(js);
};
ProductWidget.prototype.fold = function () {
	var d = document.getElementById(ProductWidget.__id_prefix + this.__my_id);
	d.style.display = "none";
};
ProductWidget.prototype.fill = function (data) {
	var d = document.getElementById(ProductWidget.__id_prefix + this.__my_id);
	
	if (d != null) {
		if (!('product' in data && 'reviews' in data)) {
			throw("empty");
			return;
		}
		var pp = d.childNodes[1].childNodes[0];
		var inner = d.childNodes[2];
		if (data.product == false) {
			pp.innerHTML = 'Brak informacji o produkcie.';
			inner.innerHTML = '<p style="height:35px;margin:0;padding:0;"></p>';
			throw("empty");
		} else {
			pp.innerHTML = 'Opinie o <b>'+ data.product.firm +' '+ data.product.model +'</b>';
			if (ProductWidget.__linkTop) {
				pp.setAttribute('href', 'http://www.galineo.pl/?keyword=' + data.product.firm +' '+ data.product.model + '&searchphrase=all&page=shop.browse&option=com_virtuemart' );
			} else {
				pp.removeAttribute('href');
			}
 			var w = parseFloat(data.product.avg);
			w *= 0.2 * 115;
			w = Math.round(w);
			var html = "<div style='padding:11px 14px 4px;color:#0A5F83;font-size: 13px;margin:0;vertical-align:top;position:relative;zoom:1'><strong style='color:#478A00; margin: 0 14px 0 0;padding:0;'>Ogólna ocena:</strong>"
				+ "<b style='font-size: 16px;font-weight:900;font-family:Arial,sans-serif'>"+ data.product.avg +"</b>"
				+ "<div style='background:url(http://"+ ProductWidget.__server +"/gfx/layout/stars_bg.png) repeat-x;height:22px;padding:0;margin:0;width:115px;position:absolute;left:160px;top:5px'>"
					+"<div style='width:"+ w +"px;position:absolute;top:0;left:0;background:url(http://www.opineo.pl/gfx/layout/stars_bg.png) repeat-x 0 -22px;height:22px;margin:0;padding:0;'></div></div>"
				+ "<span style='position:absolute;left:300px;top:12px'><b>"+ data.product.rev_num +"</b> "+ data.product.o
				+ "</span></div>";
			html += "<table style='margin:0;padding:0;border:none;width:100%;text-align:left' cellspacing='0'>"
				+ "<tr><td style='background: transparent url(http://www.opineo.pl/gfx/layout/tag_p.png) no-repeat 8px 3px;padding:6px 0 5px 36px;text-align:left;font-weight:900;font-size:11px;color:#2C2C2C;width:50%'>Zalety</td>"
				+ "<td style='background: transparent url(http://www.opineo.pl/gfx/layout/tag_m.png) no-repeat 8px 3px;padding:6px 0 5px 36px;text-align:left;font-weight:900;font-size:11px;color:#2C2C2C;width:50%'>Wady</td></tr>";

			html += "<tr>";
			if (data.pros.length > 0) {
				html += "<td style='vertical-align:top'><ul style='margin:0;padding:0 0 0 20px;list-style: square inside;color:#478A00'>";
				for (var i = 0; i < data.pros.length; i++) {
					html += "<li><span style='color:#2C2C2C;'>"+ data.pros[i].name +" ("+ data.pros[i].num +")</span></li>";
				}
				html += "</ul></td>";
			} else { html += "<td style='padding-left:40px'><i>brak</i></td>"; }
			if (data.cons.length > 0) {
				html += "<td style='vertical-align:top'><ul style='margin:0;padding:0 0 0 20px;list-style: square inside;color:#478A00'>";
				for (var i = 0; i < data.cons.length; i++) {
					html += "<li><span style='color:#2C2C2C;'>"+ data.cons[i].name +" ("+ data.cons[i].num +")</span></li>";
				}
				html += "</ul></td>";
			} else { html += "<td style='padding-left:40px'><i>brak</i></td>"; }
			html += "</tr><tr height='5'></tr>";
			html += "</table>";
			inner.style.textAlign = 'left';
			var bgs = ['FFF', 'F1F5F8'];
			if (data.reviews.length > 0) {
				for (var r = 0; r < data.reviews.length; r++) {
					html += "<div style='position:relative;padding:0;background:#"+ bgs[r % bgs.length] +";border-top: 1px solid rgb(255, 255, 255); border-bottom: 1px solid rgb(255, 255, 255); margin: 0pt; clear: left; text-align: left;'>"
						+ "<div style='margin: 0pt; padding: 9px 5px 0pt 15px;'>Nick: <b style='color: rgb(71, 138, 0); font-size: 12px;'>"+ data.reviews[r].author +"</b>, dodano: <b>"+ data.reviews[r].timestamp +"</b><br></div>"
						+ "<div style='font-size: 12px; background: url(http://www.opineo.pl/gfx/layout/bubble.gif) no-repeat 15px 8px; padding: 6px 18px 8px 45px; margin: 0pt;'>"+ data.reviews[r].review +"</div>";
						if (data.reviews[r].pros.length + data.reviews[r].cons.length) {
							html += "<table cellspacing='0' style='width:100%'><tr>";
							if (data.reviews[r].pros.length) {
								html += "<td style='background:url(\"http://www.opineo.pl/gfx/layout/tag_p.png\") no-repeat 12px 0;color:#2C2C2C;margin-top:4px;padding:3px 0 5px 38px;width:50%'>"+ data.reviews[r].pros.join(" | ") +"</td>";
							}
							if (data.reviews[r].cons.length) {
								html += "<td style='background:url(\"http://www.opineo.pl/gfx/layout/tag_m.png\") no-repeat 12px 0;color:#2C2C2C;margin-top:4px;padding:3px 0 5px 38px;width:50%'>"+ data.reviews[r].cons.join(" | ") +"</td>";
							}
							html += "</tr><tr height='6'></tr></table>";
						}
					html += "</div>";
				}
				/*html += "<div style='position:relative;padding:4px 0 8px 18px;background:#"+ bgs[(data.reviews.length - 1) % bgs.length] +"; margin: 0pt; clear: left; text-align: left;'>"
					+ "<a style='text-decoration:none;font-weight:900;color:#0A5F83;font-size:12px' href='http://" + ProductWidget.__server + data.url +"' target='_blank'><u>Zobacz wszystkie opinie</u> "
					+ "<img src='http://www.opineo.pl/gfx/layout/go_on.gif' alt='Zobacz wszystkie opinie' style='margin:0 0 0 8px;border:none;padding:0;vertical-align:middle'/></a>"
					+ "</div>";
				*/
			} else {
				html += "<div style='position:relative;padding:0;background:#"+ bgs[0] +";border-top: 1px solid rgb(255, 255, 255); border-bottom: 1px solid rgb(255, 255, 255); margin: 0pt; clear: left; text-align: left;'>"
					+ "<div style='font-size: 12px; background: url(http://www.opineo.pl/gfx/layout/bubble.gif) no-repeat 15px 8px; padding: 6px 18px 8px 45px; margin: 0pt;text-align:center'><i>Brak recenzji produktu</i></div>";
				html += "</div>";
				throw("empty");
			}
			inner.innerHTML = html;
		}
	}
};
ProductWidget.prototype.defer = function (id) {
	ProductWidget.__deferred[this.__my_id] = id;
	ProductWidget.__attach();
};
ProductWidget.prototype.__getPlaceholder = function() {
	var html = "<div id='"+ ProductWidget.__id_prefix + this.__my_id +"' style='width: "+ ProductWidget.width +"px; font-family: Tahoma,Arial,sans-serif; font-size: 11px; color: #2C2C2C; line-height: 14px; text-align: left; margin: 0pt; padding: 0pt; background: rgb(255, 255, 255);text-align:left;border:1px solid #DEE5EB;position:relative'>"
		+ '<a target="_blank"' + (ProductWidget.__linkTop ? (' href="http://www.galineo.pl"') : '') +'><img style="padding: 0pt 10px 0pt 0pt; border-width: medium 1px medium medium; border-style: none solid none none; border-color: rgb(195, 205, 212); margin: 3px 10px 2px 3px; float: left;z-index:10" alt="" ></a>'
		+ '<p style="float:left;position: relative; margin: 0pt; padding: 9px 0pt 0pt;"><a style="font-size: 13px; font-family: Tahoma,Arial,sans-serif; color: #2C2C2C; text-decoration: none; font-weight: 400;" target="_blank" href="http://www.galineo.pl">Proszę czekać...</a></p>'
		+ '<div style="margin:0;padding:0;background:#F1F5F8;clear:left;text-align:center"><img src="http://www.opineo.pl/gfx/layout/wait_flower.gif" alt="Proszę czekać" style="margin:20px auto;padding:0;"/></div>'
		+ "</div>";
	return html;
};

ProductWidget.__shop_id = null;
ProductWidget.__id_prefix = 'productwidget:';
ProductWidget.__extract_id = null;
ProductWidget.__useHref = null;
ProductWidget.__linkTop = true;
ProductWidget.__attached = false;
ProductWidget.__deferred = [];
ProductWidget.__fetched = false;
ProductWidget.width = 400;
ProductWidget.__ready = function () {
	if (ProductWidget.__fetched)
		return;
	ProductWidget.__fetched = true;
	for (var k in ProductWidget.__deferred) {
		ProductWidget.__widgets[k].fetch(ProductWidget.__deferred[k]);
	}
};

ProductWidget.__attach = function () {
	if (!ProductWidget.__attached) {
		ProductWidget.__attached = true;
		if (document.addEventListener) {
			document.addEventListener ("DOMContentLoaded", function(){
				document.removeEventListener("DOMContentLoaded", arguments.callee, false);
				ProductWidget.__ready();
			}, false);
		} else if (document.attachEvent) {
			document.attachEvent("onreadystatechange", function(){
				if (document.readyState === "complete") {
					document.detachEvent("onreadystatechange", arguments.callee);
					ProductWidget.__ready();
				}
			});
			if (document.documentElement.doScroll && window == window.top) (function(){
				if (ProductWidget.__fetched) return;
				try {
					// http://javascript.nwbox.com/IEContentLoaded/
					document.documentElement.doScroll("left");
				} catch (e) {
					setTimeout (arguments.callee, 0);
					return;
				}
				ProductWidget.__ready();
			})();
		}
	}
};
ProductWidget.__fallback = function () {
	return {purl: ProductWidget.__useHref == null ? document.location.href : ProductWidget.__useHref};
};
ProductWidget.__widgets = [];
ProductWidget.go = function (product_id, product_name, deferred) {
	var w = new ProductWidget(ProductWidget.__widgets.length);
	ProductWidget.__widgets[ProductWidget.__widgets.length] = w;
	if (product_id != null) {
		w.product_id = product_id;
	}
	if (product_name != null) {
		w.product_name = product_name;
	}
	if (typeof deferred == 'string') {
		w.defer(deferred);
	} else {
		w.fetch();
	}
};
ProductWidget.__shop_id = 6024;
ProductWidget.width = 480;
ProductWidget.__server = "www.opineo.pl";
ProductWidget.__linkTop = true;