function VVCIsNumeric(text) {
	return /^[-+]?\d+(\.\d+)?$/.test(text);
}

if (typeof VVC_WIDGET_COUNTER == "undefined") {
	VVC_WIDGET_COUNTER = 0;
}

function ViralVideoChart(config, showId) {
	VVC_WIDGET_COUNTER++;
	this.WIDGET_ID = VVC_WIDGET_COUNTER;

	if (typeof showId != "undefined") {
		this.divId = showId;
	}
	else {
		document.write('<div id="vvc_widget_' + this.WIDGET_ID + '"></div>');
		this.divId = "vvc_widget_" + this.WIDGET_ID;
	}

	this.SITE_URL = "http://www.viralvideochart.com";
	this.FEED_PARAM = "format=atomW";
	this.allFeeds = new Array("/", "/2006/overall", "/2006/homemade", "/2006/adverts", "/2006/news", "/2006/funny", "/2006/xmas", "/2006/celebrity", "/2006/drunk");
	this.feedPointer = 0;
	this.feed = null;

	this.numberToShow = -1;
	this.req = false;
	this.fixedHeight = false;
	this.width = 160;
	this.height = 200;
	this.showThumbnails = true;
	this.thumbnailScale = 0.5;
	this.useCssClass = false;
	this.fontSize = "10px";
	this.fontColor = "";
	this.titleFontSize = "12px";
	this.fontFamily = "verdana";
	this.background = "white";
	this.entrySpacing = "1";
	this.showChartPos = true;
	this.numbersOnThumb = false;
	this.numberPattern = "$num. ";
	this.currentStart = 0;
	this.title = "";
	this.hoverBackground = "#DDD";
	this.ruleColor = "#DDD";

	if (typeof(config) != "undefined") {
		if (typeof(config.feedUrl) != "undefined") {
			var found = false;
			for (var i = 0; i < this.allFeeds.length; i++) {
				if (config.feedUrl == this.allFeeds[i]) {
					this.feedPointer = i;
					found = true;
					break;
				}
			}
			if (!found) {
				this.feedPointer = this.allFeeds.length;
				this.allFeeds[this.allFeeds.length] = config.feedUrl;
			}
		}

		if (typeof(config.height) != "undefined") {
		      this.height = config.height;
		      this.fixedHeight = true;
		}

		var opts = new Array(
			"numberToShow", "showThumbnails", "width", "fontSize", "fontFamily", "background", "useCssClass",
			"entrySpacing", "showChartPos", "numbersOnThumb", "numberPattern", "thumbnailScale", "title",
			"titleFontSize", "hoverBackground", "SITE_URL", "fontColor", "ruleColor"
		);

		for (var i = 0; i < opts.length; i++) {
			var opt = opts[i];

			var confOption = eval("config." + opt);
			if (typeof(confOption) != "undefined") {
				if (VVCIsNumeric(confOption))
					eval("this." + opt + " = " + confOption + " * 1");
				else {
					if (typeof(confOption) == "boolean" || confOption == "true" || confOption == "false")
						eval("this." + opt + " = " + confOption);
					else
						eval("this." + opt + " = \"" + confOption + "\"");
				}
			}
		}

		if (this.ruleColor == "") this.ruleColor = "#DDD";
	}

	this.renderInterface = function() {
		var s = "";
		s += '<img src="' + this.SITE_URL + '/resource/img/spinner.gif" id="vvc_spinner_' + this.WIDGET_ID + '" style="display:none; float: right"/>';
		s += '<div id="vvc_title_' + this.WIDGET_ID + '" style="' + this.getCssProperty("color", "fontColor") + ' font-size: ' + this.titleFontSize + '; font-weight: bold; text-align: center; border-bottom: solid ' + this.ruleColor + ' 2px; padding-bottom: 4px">' + this.title + '</div>';
		s += '<div id="vvc_feed_display_' + this.WIDGET_ID + '"></div>';
		s += '<div style="padding: 2px 2px 5px 2px; text-align: right">';
		s += '  <span id="vvc_prev_' + this.WIDGET_ID + '"></span><span id="vvc_next_' + this.WIDGET_ID + '"></span>';
		s += '</div>';
		s += '<div style="text-align: center;">';
		s += '<div style="border-top: solid ' + this.ruleColor + ' 1px; border-bottom: solid ' + this.ruleColor + ' 1px; padding: 4px;' + (document.all ? 'padding-bottom:8px' : '') + '">';
		s += '  <div style="float: left; width: 32%">';
		s += '	  <a href="' + this.SITE_URL + '/" style="text-decoration: none"><img src="' + this.SITE_URL + '/favicon.ico" style="vertical-align: middle; border: 0"></a>';
		if (this.width <= 180) s += '<br/>';
		s += '    <a href="' + this.SITE_URL + '/" style="' + this.getCssProperty("color", "fontColor") + '">Videos</a>';
		s += '  </div>';

		s += '  <div style="float: left; width: 32%">';
		s += '	  <a href="' + this.SITE_URL + '/?format=atom" style="text-decoration: none"><img src="' + this.SITE_URL + '/resource/img/feed.gif" style="vertical-align: middle; border: 0"></a>';
		if (this.width <= 180) s += '<br/>';
		s += '    <a href="' + this.SITE_URL + '/?format=atom" style="' + this.getCssProperty("color", "fontColor") + '">Feed</a>';
		s += '  </div>';

		s += '  <div style="float: left; width: 32%">';
		s += '	  <a href="' + this.SITE_URL + '/?format=widget" style="text-decoration: none"><img src="' + this.SITE_URL + '/resource/img/icons/cog.gif" style="vertical-align: middle; border: 0"></a>';
		if (this.width <= 180) s += '<br/>';
		s += '    <a href="' + this.SITE_URL + '/?format=widget" style="' + this.getCssProperty("color", "fontColor") + '">Widget</a>';
		s += '  </div>';
		s += '  <br style="clear: left"/>'
		s += '</div>';
		s += '</div>'

		return s;
	}

	this.getCssProperty = function(style, name) {
		var prop = eval("this." + name);
		return (prop == null || prop == "") ? '' : style + ": " + prop + ";";
	}

	this.applyJSFunctions = function() {
		var thiss = this;
		var prevA = document.getElementById('vvc_prev_a_' + this.WIDGET_ID);
		if (prevA != null) prevA.onclick = function() {thiss.showPreviousX(); return false;}

		var nextA = document.getElementById('vvc_next_a_' + this.WIDGET_ID);
		if (nextA != null) nextA.onclick = function() {thiss.showNextX(); return false;}
	}

	this.linkHack = function(el, entry) {
		el.onclick = function() {window.open(entry.link); return false;};
	}

	this.renderTableTemplate = function(feed) {
		var el = document.getElementById('vvc_feed_display_' + this.WIDGET_ID);
		var oldTable = el.getElementsByTagName("table");
		if (oldTable.length > 0) el.removeChild(oldTable[0]);

		var table = document.createElement("table");
		table.setAttribute("width", "100%");
		table.setAttribute("cellspacing", "0");
		table.setAttribute("cellpadding", "1");

		if (!this.useCssClass) {
			table.setAttribute("style", '' + this.getCssProperty("color", "fontColor") + ' font-size: ' + this.fontSize + '; font-family:' + this.fontFamily);
			table.style.color = this.fontColor;
			table.style.fontSize = this.fontSize;
			table.style.fontFamily = this.fontFamily;
		}

		var tbody = document.createElement("tbody");
		table.appendChild(tbody);

		var spacing = Math.round(this.entrySpacing / 2);

		var thiss = this;
		for (var i = this.currentStart; i < feed.entries.length; i++) {
			if (this.numberToShow != -1 && i >= this.currentStart + this.numberToShow) break;
			var entry = feed.entries[i];

			var tr = document.createElement("tr");
			tbody.appendChild(tr);
			tr.onmouseover = function() {this.style.background = thiss.hoverBackground};
			tr.onmouseout = function() {this.style.background = thiss.background};
			tr.style.margin = 0;
			tr.style.padding = 0;
			this.linkHack(tr, entry);
			tr.setAttribute("style", "cursor: pointer");

			if (this.showThumbnails) {
				var td1 = document.createElement("td");
				tr.appendChild(td1);

				td1.setAttribute("style", "padding: " + spacing + "px 0 " + spacing + "px 0");
				td1.style.paddingTop = spacing + "px";
				td1.style.paddingBottom = spacing + "px";
				td1.setAttribute("valign", "middle");
				td1.align = "center";
				td1.style.margin = 0;
				var divRel = document.createElement("div");
				divRel.setAttribute("style", "position: relative");
				divRel.style.position = "relative";
				td1.appendChild(divRel);

				var a1 = document.createElement("a");
				divRel.appendChild(a1);
				a1.setAttribute("href", entry.link);
				if (!this.useCssClass) {
					a1.setAttribute("style", this.getCssProperty("color", "fontColor") + " text-decoration: none;");
					if (this.fontColor != null && this.fontColor.length > 0) a1.style.color = this.fontColor;
					a1.style.textDecoration = "none";
				}

				if (entry.thumbnailUrl != null) {
					var thumb = document.createElement("img");
					var w = 100 * this.thumbnailScale;
					var h = Math.round(75 * this.thumbnailScale);

					thumb.setAttribute("src", entry.thumbnailUrl);
					thumb.setAttribute("width", w);
					thumb.setAttribute("height", h);
					thumb.setAttribute("border", "0");

					td1.setAttribute("width", w + 5);
					td1.setAttribute("height", h);

					a1.appendChild(thumb);
				}
				else
					a1.appendChild(document.createTextNode("No Thumb"));


				if (this.numbersOnThumb) {
					var divNum = document.createElement("div");
					divNum.setAttribute("style", "position: absolute; top: 0; left: 0; background: black; color: white; text-align: center; width: 1.3em; font-size: " + this.fontSize + "; font-family:" + this.fontFamily);
					divNum.style.position = "absolute";
					divNum.style.top = 0;
					divNum.style.left = 0;
					divNum.style.background = "black";
					divNum.style.color = "white";
					divNum.style.textAlign = "center";
					divNum.style.width = "1.3em";
					divNum.style.fontSize = this.fontSize;
					divNum.style.fontFamily = this.fontFamily;
					divNum.appendChild(document.createTextNode(entry.chartPos));

					divRel.appendChild(divNum);
				}
			}

			var td2 = document.createElement("td");
			tr.appendChild(td2);
			td2.setAttribute("style", "padding: " + spacing + "px 0 " + spacing + "px 4px");

			var a2 = document.createElement("a");
			td2.appendChild(a2);
			td2.setAttribute("valign", "middle");
			td2.style.margin = 0;
			td2.style.paddingTop = spacing + "px";
			td2.style.paddingBottom = spacing + "px";
			td2.style.paddingLeft = "4px";
			a2.setAttribute("href", entry.link);
			if (!this.useCssClass) {
				a2.setAttribute("style", this.getCssProperty("color", "fontColor") + " font-size: " + this.fontSize + "; font-family:" + this.fontFamily);
				if (this.fontColor != null && this.fontColor.length > 0) a2.style.color = this.fontColor;
				a2.style.fontSize = this.fontSize;
				a2.style.fontFamily = this.fontFamily;
			}

			var text = "";
			if (this.showChartPos) text += this.renderNumber(entry.chartPos) + ' ';
			text += entry.title;

			a2.innerHTML = text;
		}

		el.appendChild(table);

		this.updatePrevNext();
	}

	this.renderNumber = function(pos) {
		return this.numberPattern.replace("$num", pos);
	}

	this.getFeedUrl = function(hideParam) {
		var url = this.SITE_URL + this.allFeeds[this.feedPointer];
		if (!hideParam) {
			if (url.indexOf("?") == -1)
				url += "?" + this.FEED_PARAM;
			else
				url += "&" + this.FEED_PARAM;
		}
		return url;
	}

	this.showNextX = function() {
		this.currentStart += this.numberToShow;
		if (this.currentStart >= this.feed.entries.length) {
			this.currentStart = this.feed.entries.length - this.numberToShow;
		}

		this.renderTableTemplate(this.feed);
	}

	this.showPreviousX = function() {
		this.currentStart -= this.numberToShow;
		if (this.currentStart < 0)
			this.currentStart = 0;

		this.renderTableTemplate(this.feed);
	}

	this.updatePrevNext = function() {
		var prevT = '&lt; prev ' + this.numberToShow;
		var nextT = 'next ' + this.numberToShow + ' &gt';
		var prevA = '<a href="#" id="vvc_prev_a_' + this.WIDGET_ID + '" style="' + this.getCssProperty("color", "fontColor") + '">' + prevT + '</a>';
		var nextA = '<a href="#" id="vvc_next_a_' + this.WIDGET_ID + '" style="' + this.getCssProperty("color", "fontColor") + '">' + nextT + '</a>';

		if (this.currentStart + this.numberToShow >= this.feed.entries.length) {
			if (this.numberToShow < this.feed.entries.length) {
				document.getElementById('vvc_prev_' + this.WIDGET_ID).innerHTML = prevA + " | ";
				document.getElementById('vvc_next_' + this.WIDGET_ID).innerHTML = nextT;
			}
			else {
				document.getElementById('vvc_prev_' + this.WIDGET_ID).innerHTML = '';
				document.getElementById('vvc_next_' + this.WIDGET_ID).innerHTML = '';
			}
		}
		else if (this.currentStart <= 0) {
			document.getElementById('vvc_prev_' + this.WIDGET_ID).innerHTML = prevT + " | ";
			document.getElementById('vvc_next_' + this.WIDGET_ID).innerHTML = nextA;
		}
		else {
			document.getElementById('vvc_prev_' + this.WIDGET_ID).innerHTML = prevA + " | ";
			document.getElementById('vvc_next_' + this.WIDGET_ID).innerHTML = nextA;
		}

		this.applyJSFunctions();
	}

	this.feedNotLoaded = function() {
		var el = document.getElementById('vvc_feed_display_' + this.WIDGET_ID);

		var div = document.createElement("div");
		div.style.textAlign = "center";

		div.innerHTML = "Sorry, the widget has gone AWOL.<br/>Tell us +44.7974.328.052"

		el.appendChild(div);
	}

	this.setup = function() {
		var el = document.getElementById(this.divId);

		el.style.width = this.width + "px";
		if (this.fixedHeight) {
			el.style.height = this.height + "px";
		}
		el.style.overflow = "hidden";
		if (!this.useCssClass) {
			el.style.fontFamily = this.fontFamily;
			el.style.fontSize = this.fontSize;
			if (this.fontColor != null && this.fontColor.length > 0)
				el.style.color = this.fontColor;
			el.style.background = this.background;
		}
		el.setAttribute("class", "VVCWidget");
		el.className = "VVCWidget";

		el.innerHTML = this.renderInterface();

		if (typeof vvcMyFeed == "undefined" || vvcMyFeed == null) {
			this.feedNotLoaded();
		}
		else {
			this.feed = vvcMyFeed;
			this.renderTableTemplate(this.feed);
		}
	}

	this.setup();
}
