if (typeof(headembed) != "object" || headembed == null) {
	var headembed = {
		_userGuid: null,
		_headGuid: '',
		_raceId: 0,
		_referrerUid: 0,

		_STATE_COLLECTABLE: 1, // ready to collect
		_STATE_COLLECTED: 2, // done collecting (after state 1)
		_STATE_MUSTLOGIN: 3,
		_STATE_ALREADYCOLLECTED: 4, // collected sometime in the past
		_STATE_OWNREALM: 5,
		_STATE_OWNHEAD: 6,
		_STATE_LIMITREACHED: 7,

		onload: function() {
			// get the uid to give credit for the embed
			var getVars = parseGetVars(window.location.href);

			this._userGuid = this.getUserGuid();
			this._headGuid = (getVars.guid != undefined? getVars.guid: '');
			this._referrerUid = (getVars.uid != undefined? getVars.uid: '0');

			this._setStatusLine('');
			this.headStatus();
		},

		onunload: function() {
			if (this._collectHeadAjaxRequest != null) {
				YAHOO.util.Connect.abort(this._collectHeadAjaxRequest);
				this._collectHeadAjaxRequest = null;
			}

			if (this._headStatusAjaxRequest != null) {
				YAHOO.util.Connect.abort(this._headStatusAjaxRequest);
				this._headStatusAjaxRequest = null;
			}
		},

		/**
		 * returns a path to a head image for a given state
		 * assumes rid= has been set in GET params
		 */
		_getHeadImagePath: function(state) {
			return 'battleimages/rtw_heads01_' + this._raceId + '_' + state + '.jpg'
		},

		_showHeadImage: function(state) {
			var e = YAHOO.util.Dom.get('headimage');
			if (e == null) { return; }

			e.src = this._getHeadImagePath(state);
		},

		_showProperHeadImage: function() {
			if (this._userGuid != null) {
				// we're logged in, show the login one
				this._showHeadImage(this._STATE_COLLECTABLE);
			} else {
				this._showHeadImage(this._STATE_MUSTLOGIN);
			}
		},

		_setStatusLine: function(s) {
			if (this._statusLineClearTimerHandle != null) {
				clearTimeout(this._statusLineClearTimerHandle);
			}

			var e = YAHOO.util.Dom.get('statusline');
			if (e == null) { return; }

			e.innerHTML = s;
		},

		setRefCookie: function() {
			// we're just going to use the same cookie as the
			// pollreferrer--misnomer, but hey.
			var myDate = new Date(); 
			myDate.setDate(myDate.getDate()+30); // 30 days in future
			YAHOO.util.Cookie.set("pollreferrer", this._referrerUid, { expires: myDate });
		},

		_statusLineClearTimerHandle: null,

		_statusLineClearTimer: function() {
			this._statusLineClearTimerHandle =
				setTimeout('headembed_clearStatusLine()', 4000);
		},

		/**
		 * ajax for head collection
		 */
		_collectHeadAjaxRequest: null,

		_collectHeadSuccess: function(o) {
			this._collectHeadAjaxRequest = null;
			this._setStatusLine('');

			var stat = getFirstChildNodeValue(o.responseXML.documentElement.getElementsByTagName('RedeemHeadResult'));

			// sorry for the dichotomy here between this and
			// errorStrToState()...
			switch(stat.toLowerCase()) {
				case 'already collected':
					this._showHeadImage(this._STATE_ALREADYCOLLECTED);
					return;

				case 'cannot collect head from same realm':
					this._showHeadImage(this._STATE_OWNREALM);
					return;

				case 'cannot collect own head':
					this._showHeadImage(this._STATE_OWNHEAD);
					return;

				case 'daily limit reached':
					this._showHeadImage(this._STATE_LIMITREACHED);
					return;
			}

			// success
			this._showHeadImage(this._STATE_COLLECTED);
		},

		_collectHeadFailure: function(o) {
			this._collectHeadAjaxRequest = null;
			this._setStatusLine('');

			if (!alertOnFault(o.responseXML.documentElement)) {
				alert("HTTP error: " + o.status + ' ' + o.statusText);
			}
		},

		collectHead: function() {
			if (this._userGuid == null) {
				return true; // follow <a href>
			}

			if (this._collectHeadAjaxRequest != null) {
				YAHOO.util.Connect.abort(this._collectHeadAjaxRequest);
				this._collectHeadAjaxRequest = null;
			}

			var soapXMLStr = buildSOAPXMLStr('RedeemHead',
				'<userGuid>' + this._userGuid + '</userGuid>' +
				'<headGuid>' + this._headGuid + '</headGuid>');

			YAHOO.util.Connect.setDefaultPostHeader('text/xml; charset=utf-8'); // SOAP 1.1
			YAHOO.util.Connect.initHeader('SOAPAction', '"' + _SOAP_namespace + 'RedeemHead"'); // SOAP 1.1
			//YAHOO.util.Connect.setDefaultPostHeader('application/soap+xml; charset=utf-8'); // SOAP 1.2

			this._collectHeadAjaxRequest = YAHOO.util.Connect.asyncRequest('POST', SOAP_URL, headembed_collectHeadResultCallback, soapXMLStr);

			this._setStatusLine('Collecting...');

			return false; // don't follow <a href>
		},

		_errorStrToState: function(s) {
			switch(s.toLowerCase()) {
				case 'already collected':
					return this._STATE_ALREADYCOLLECTED;

				case 'cannot collect head from same realm':
					return this._STATE_OWNREALM;

				case 'cannot collect own head':
					return this._STATE_OWNHEAD;

				case 'daily limit reached':
					return this._STATE_LIMITREACHED;
			}

			if (this._userGuid == null) {
				return this._STATE_MUSTLOGIN;
			}

			return this._STATE_COLLECTABLE;
		},

		/**
		 * ajax for getting current head status
		 */
		_headStatusAjaxRequest: null,

		_headStatusSuccess: function(o) {
			this._headStatusAjaxRequest = null;
			this._setStatusLine('');

			var result = o.responseXML.documentElement.getElementsByTagName('GetHeadPreClickDetailResult')[0];

			var stat = getFirstChildNodeValue(result.getElementsByTagName('Status'));
			this._raceId = getFirstChildNodeValue(result.getElementsByTagName('RaceId'));
			//var imagePath = getFirstChildNodeValue(result.getElementsByTagName('ImagePath'));
			//var headId = getFirstChildNodeValue(result.getElementsByTagName('HeadId'));

			var newstate = this._errorStrToState(stat);

			// success
			this._showHeadImage(newstate);
		},

		_headStatusFailure: function(o) {
			this._headStatusAjaxRequest = null;
			this._setStatusLine('');

			if (!alertOnFault(o.responseXML.documentElement)) {
				alert("HTTP error: " + o.status + ' ' + o.statusText);
			}
		},

		headStatus: function() {
			if (this._userGuid == null) {
				this.setRefCookie(this._referrerId);
			}

			if (this._headStatusAjaxRequest != null) {
				YAHOO.util.Connect.abort(this._headStatusAjaxRequest);
				this._headStatusAjaxRequest = null;
			}

			var userGuid = (this._userGuid == null? '': this._userGuid);

			var soapXMLStr = buildSOAPXMLStr('GetHeadPreClickDetail',
				'<userGuid>' + userGuid + '</userGuid>' +
				'<headGuid>' + this._headGuid + '</headGuid>');

			YAHOO.util.Connect.setDefaultPostHeader('text/xml; charset=utf-8'); // SOAP 1.1
			YAHOO.util.Connect.initHeader('SOAPAction', '"' + _SOAP_namespace + 'GetHeadPreClickDetail"'); // SOAP 1.1
			//YAHOO.util.Connect.setDefaultPostHeader('application/soap+xml; charset=utf-8'); // SOAP 1.2

			this._headStatusAjaxRequest = YAHOO.util.Connect.asyncRequest('POST', SOAP_URL, headembed_headStatusResultCallback, soapXMLStr);

			return false; // don't follow <a href>
		},

		getUserGuid: function() {
			return YAHOO.util.Cookie.get("userguid");
		}
	}

	function headembed_clearStatusLine() {
		headembed._statusLineClearTimerHandle = null;
		headembed._setStatusLine('');
	}

	headembed_collectHeadResultCallback = {
		success: headembed._collectHeadSuccess,
		failure: headembed._collectHeadFailure,
		scope: headembed
	}

	headembed_headStatusResultCallback = {
		success: headembed._headStatusSuccess,
		failure: headembed._headStatusFailure,
		scope: headembed
	}
}
