//
// Utility class to detect if the user has the Webshots Desktop client installed
//
// Note that client detection is only reliable on some platforms (current IE) due
// to the need for a client-side plugin to signal that it's installed.  So
// instead of a providing true/false boolean test method, the sniffer returns a
// tri-state status that includes an "UNKNOWN" state.  Be sure to account for this
// when checking the status so that users on undetectable platforms are handled
// sensibly.
//
// For example, if you wrote:
//
//   if (new DesktopSniffer().clientStatus() != DesktopClientStatus.INSTALLED)
//     { // download desktop }
//
// Then Firefox users, who register as UNKNOWN, will be repeatedly prompted to
// download the client, when they may, in fact, already have it.

function DesktopSniffer() {

	this.version=0;
	if (document.VersionDetector) {
 		if (document.VersionDetector.GetWSVersion) {
			this.version = document.VersionDetector.GetWSVersion();
 		}
	}
}

// determine status of desktop client installation
// three possible results: installed, not installed, or unknown
DesktopSniffer.prototype.clientStatus = function () {
	if (this.version > 0) {
		return DesktopClientStatus.INSTALLED;
	}
	
	if (!this.hasClient && (navigator.appName.indexOf("Microsoft Internet Explorer") >= 0)) {
		return DesktopClientStatus.NOT_INSTALLED;
	}
	
	return DesktopClientStatus.UNKNOWN;
}

// enum class for tri-state clientStatus response
function DesktopClientStatus(name) {
    this._name = name;
}

DesktopClientStatus.prototype.toString = function () {
    return this._name;
}

DesktopClientStatus.INSTALLED = new DesktopClientStatus('INSTALLED');
DesktopClientStatus.NOT_INSTALLED = new DesktopClientStatus('NOT_INSTALLED');
DesktopClientStatus.UNKNOWN = new DesktopClientStatus('UNKNOWN');

function setDesktopCookie() {
	document.cookie = "desktop-client=" + new DesktopSniffer().clientStatus() + "; path=/; domain=.webshots.com";
}

function setDesktopDownloadCookie() {
	var now = new Date();
	var nowInMillis = now.getTime();
	var oneDayInMillis = 24 * 60 * 60 * 1000;
	var expires = new Date(nowInMillis + oneDayInMillis);
	document.cookie = "desktop-download=" + now.toUTCString() + "; path=/; domain=.webshots.com; expires=" + expires.toUTCString();
}
