
function Advertisement() {
	var counter = 0;
	var newtime = 0;
	this.load = function (divId, time, foldername, columnNos, rowNos) {
		var _this = this;
		this.setAdvertisement = function () {
			dojo.xhrGet({url:"servlet/Advertisement", handleAs:"text", content:{folder:foldername}, load:function (response) {
				ads = response.split("@");
				html = "<table>";
				nums = new Array(columnNos * rowNos);
				hint = 0;
				for (var x = 0; x < nums.length; x++) {
					Continue = true;
					num = 0;
					while (Continue) {
						already = false;
						num = Math.floor(Math.random() * (ads.length - 1));
						for (var y = 0; y < nums.length; y++) {
							if (nums[y] == num) {
								already = true;
							}
						}
						if (!already) {
							Continue = false;
						}
					}
					nums[x] = num;
					if (hint == 0) {
						html = html + "<tr>";
					}
					hint++;
					html = html + "<td>" + ads[num] + "</td>";
					if (hint >= columnNos) {
						html = html + "</tr>";
						hint = 0;
					}
				}
				html = html + "</table>";
				dojo.byId(divId).innerHTML = html;
			}, error:function (data) {
				console.error("Error: ", data);
			}});
		};
		this.updateTime = function () {
			_this.counter++;
			if (_this.counter < time) {
				newtime = setTimeout(dojo.hitch(this, "updateTime"), 5000);
			} else {
				_this.counter = 0;
				this.setAdvertisement();
				this.updateTime();
			}
		};
		this.setAdvertisement();
		if (time > 0) {
			this.updateTime();
		}
	};
}

