var SFpopurl      = 'http://www.searchfeast.com/index.php?id=Q5S5VWKS';
var SFcookie      = 'SFpopped';
var SFwinfeatures = 'width=650,height=550,scrollbars=1,resizable=1,toolbar=0,location=0,menubar=1,status=0,directories=0';
var SFflashurl    = 'popunder.swf';
var SFbrowser     = SFBrowserDetect();
SFpopunder();

function SFpopunder() {
	if ( SFGetCookie(SFcookie) ) return;

	if (SFbrowser.isIE) {
		hasGoogleBar = SFDetectGoogleBar();
		if (!hasGoogleBar) {
			SFWinIEPopunder();
		}
	}
	else if (SFbrowser.isFirefox && (SFbrowser.version < 1.5) ) {
		SFInitSwf();
	}
	else {
		SFNormalPopunder();
	}

	SFPopOnClick(); // fallback method

	SFSetCookie(SFcookie,1,'','/');
}

function SFNormalPopunder() {
	SFwin = window.open(SFpopurl, 'SFpop', SFwinfeatures);
	if (SFwin) {
		SFwin.blur();
	}
	self.focus();
}

function SFInitSwf() {
	if (typeof SFSwfTimeout == 'undefined') {
		SFSwfTimeout = setTimeout('SFInitSwf()', 300);
	}
	else {
		var o = document.createElement('object');
		o.data = SFflashurl;
		o.width = 1;
		o.height = 1;
		o.type = 'application/x-shockwave-flash';
		document.body.appendChild(o);
	}
}

function SFWinIEPopunder() {
	if (typeof SFWinIETimeout == 'undefined') {
	window.attachEvent('onload', function() {
			var o = document.createElement('object');
			var p1 = document.createElement('param');
			var p2 = document.createElement('param');
			o.id = 'sfx';
			o.classid = 'clsid:2D360201-FFF5-11d1-8D03-00A0C959BC0A';
			o.width = '1';
			o.height = '1';
			p1.name = 'ActivateApplets';
			p1.value = '1'
			p2.name = 'ActivateActiveXControls';
			p2.value = '1'
			o.appendChild(p1);
			o.appendChild(p2);
			document.body.appendChild(o);
			SFWinIETimeout = setTimeout('SFWinIEPopunder()', 300);
		});
	}
	else {
		var jsStr = 'function openscript() { \
			SFwin = open("' + SFpopurl + '", "SFWin", "' + SFwinfeatures + '"); \
		}';
		var sfx = document.getElementById('sfx');
		sfx.DOM.body.insertAdjacentHTML('AfterBegin', '<body><script>' + jsStr + '</script>');
		sfx.DOM.Script.execScript(jsStr); // compiles the script
		sfx.DOM.Script.openscript();      // calls the script
		SFwin = sfx.DOM.Script.SFwin;
		SFwin.blur();
		self.focus();
		clearTimeout(SFWinIETimeout);
	}
}

function SFPopOnClick() {
	if (typeof SFPopOnClickInterval == 'undefined') {
		SFPopOnClickInterval = setInterval('SFPopOnClick()', 100);
		SFCounter = 0;
	}
	else {
		if (typeof SFwin != 'undefined') {
			if (SFwin) {
				clearInterval(SFPopOnClickInterval);
			}
		}
		
		if (SFCounter > 10) {	
			
			oldOnclick = null;
			
			if (SFbrowser.isIE && !hasGoogleBar) {			
				
				if (typeof document.onclick != 'undefined') {
					oldOnclick = document.onclick;
				}
				
				document.onclick = function() {
					if (oldOnclick) {
						oldOnclick();
					}
					
					SFNormalPopunder();
				}
			}
			else {
				if (typeof window.onclick != 'undefined') {
					oldOnclick = window.onclick;
				}
				
				window.onclick = function() {
					if (oldOnclick) {
						oldOnclick();
					}
					
					SFNormalPopunder();
				}
			}
			
			clearInterval(SFPopOnClickInterval);
		}
		
		SFCounter++;
	}
}

// a very incomplete browser sniffer
function SFBrowserDetect() {
	var browser = {};
	var ua = navigator.userAgent.toLowerCase();
	var appVer = navigator.appVersion;
	browser.isFirefox  = (ua.indexOf('firefox/') != -1);
	browser.isIE = ((ua.indexOf('msie') != -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1));
	if (browser.isFirefox) {
		browser.version = parseFloat(navigator.vendorSub);
	}
	else if (browser.isIE) {
		var iePos  = appVer.indexOf('msie');
		if (iePos !=-1) {
		   browser.version = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));;
		}
	}
	browser.isSP2 = (ua.indexOf("sv1") != -1);
	return browser;
}

function SFDetectGoogleBar() {
	var hasGoogleBar = false;
	document.write('<object id="googleBar" classid="clsid:00EF2092-6AC5-47c0-BD25-CF2D5D657FEB" style="display: none;"><\/object>');
	var googleBar = document.getElementById('googleBar');
	if (googleBar && typeof(googleBar.Search) != "undefined") {
		hasGoogleBar = true;
	}
	return hasGoogleBar;
}

function SFgetCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function SFGetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return SFgetCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SFSetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

window.onerror = function() { return true; }