/*
 * SurveyWidget.js
 *
 * Javascript class for adding a demographics survey item to a page.
 */

function SurveyWidget(params) { }

SurveyWidget.prototype = new SurveyWidget();
SurveyWidget.prototype.constructor = SurveyWidget();

SurveyWidget.prototype.insertSurveyor = 
	function(params) {
		var host;
		var showid;
		var hasProductInstall = DetectFlashVer(6, 0, 65);
		var hasRequestedVersion = DetectFlashVer(8, 0, 0);

		if(hasProductInstall && hasRequestedVersion) {
			var path = "/scripts/flash/appDemoSurvey.swf";
			if (params && params.host) { host = params.host; }
			else { host = "blip.tv"; }
			if (params && params.showid) { showid = params.showid; }
			else { showid = 0; }
			this.insertFlashMovie(path,host,showid);
		}
		else {
			document.writeln("<p>The survey requires Flash 8 or better</p>");
		}
	};

SurveyWidget.prototype.insertFlashMovie =
	function(url,host,showid) {
		document.writeln("<object type=\"application/x-shockwave-flash\"" +
				 "  data=\"" + url + "\"" +
				 "  width=\"480\"" +
				 "  height=\"360\">");
		this.insertParam("movie",url);
		this.insertParam("quality","best");
		this.insertParam("bgColor","#FFFFFF");
		this.insertParam("scale","noScale");
		this.insertParam("salign","TL");
		this.insertParam("FlashVars","host=" + host + "&showid=" + showid);
		document.writeln("</object>");
	};

SurveyWidget.prototype.insertParam = 
	function(name,value) {
		document.writeln("<param name=\"" + name + "\" value=\"" + value + "\">");
	};


SurveyWidget.prototype.insertChecker = function() {
	document.writeln('<div style="display: block;">' +
			 '<object id="surveychecker" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="1" height="1" align="middle">' +
			'<param name="allowScriptAccess" value="sameDomain" \/>' +
			'<param name="movie" value="/scripts/flash/appSurveyChecker.swf" \/>' +
			'<param name="quality" value="high" \/>' +
			'<param name="bgcolor" value="#ffffff" \/>' +
			'<embed src="/scripts/flash/appSurveyChecker.swf" quality="high" bgcolor="#ffffff" width="1" height="1" name="surveychecker" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" \/>' +
			'<\/object>' +
			'<\/div>');

	/* AM - 07/18/2007 - Screamingly ugly hack to work around an IE bug.
	 * See: http://tinyurl.com/365m6g
	 */

	window.onbeforeunload = function() { window.onunload = null; }
}

SurveyWidget.prototype.findChecker = function() {
	if (navigator.appName.indexOf("Microsoft") != -1) { return window['surveychecker'] }
	else { return document['surveychecker'] }
}

SurveyWidget.prototype.getResponse = function(key) {
	try {
		if (this.surveychecker == null) { this.surveychecker = this.findChecker(); }
		var value = this.surveychecker.getResponse(key);
		if (value == "null") { return; }
		else { return value; }
	}
	catch(err) {
		window.status = ("SurveyWidget: couldn't get response because of error = " + err);
		return 0;
	}
}

SurveyWidget.prototype.hasBeenSurveyed = function() {
	// 07/27/2007 Hack to disable this temporarily.

	return true;
	try {
		if (this.surveychecker == null) { this.surveychecker = this.findChecker(); }
		return this.surveychecker.hasBeenSurveyed();
	}
	catch(err) {
		window.status = ("SurveyWidget: couldn't check status because of error = " + err);
		return false;
	}
}

SurveyWidget.prototype.insertSurveyBadge = function(host) {
	if (!this.hasBeenSurveyed()) {
		var badgediv = document.getElementById('blipsurveybadge');
		badgediv.innerHTML = "<a href='http://" + host + "/demographics/ask'><img src='http://" + host + "/skin/blipnew/icons/badgeSurvey.jpg' width='160' height='40' alt='Take the blip.tv survey' /></a><p></p>";
	}
}

SurveyWidget.prototype.reset = function() {
	try {
		if (this.surveychecker == null) { this.surveychecker = this.findChecker(); }
		return this.surveychecker.reset();
	}
	catch(err) {
		alert("An error occurred while trying to reset the survey data: " + err);	
	}
}

