document.onclick=check;
var xmlhttp;
var ssxmlhttp=null;
var recentHash = "";
var recentQuery = "";
var today = new Date(); 
var expiry = new Date(today.getTime() + 364 * 86400 * 1000);

function check(e){
	var target = (e && e.target) || (event && event.srcElement);
	var obj = document.getElementById('smartbox');
	checkParent(target)?obj.style.display='none':null;
	}
function checkParent(t){
	while(t.parentNode){
	if(t==document.getElementById('smartbox')){
	return false
	}
	t=t.parentNode
	}
	return true
	} 
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
function stateChanged()
{
if (xmlhttp.readyState==4)
	{
		clearSmartBox();
	  	window.scrollTo(0,0);
  		document.getElementById("searchresults").innerHTML=xmlhttp.responseText;
  	}
}
function suggest()
{
if (ssxmlhttp.readyState==4)
  {
  document.getElementById("smartbox").innerHTML=ssxmlhttp.responseText;
  document.getElementById('smartbox').style.display="block";
  document.getElementById('smartbox').style.zIndex="2000";
  var zIndexNumber = 1000;
  $('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
  });
  }
}

function updateHash(par, str) {
	if (par.length == 0) return;
	//firefox bug -- it decodes the hash
	var hashstart = location.href.indexOf("#");
//	var hash = window.location.hash;
	if (hashstart > -1) {
		var hash = location.href.substring(hashstart);
		var parTag = par + "=";
		if (hash.indexOf(parTag) == -1 && str.length>0) {
			window.location.hash = hash + "&" + par + "=" + str;
		} else {
			var arrout = new Array();
			var arr = hash.split("&");
			for (var i = 0; i<arr.length; i++) {
				if (arr[i].indexOf(parTag) != -1) {
					if (str.length > 0) {
						var thisItem = par + "=" + str;
						arrout.push(thisItem);
					} 
				} else {
					arrout.push(arr[i]);
				}
			}
			window.location.hash = arrout.join("&");
			return;
		}
	} else {
		window.location.hash = par + "=" + str;
		return;
	}		
}

function showHint()
{
if (ssxmlhttp) {
	ssxmlhttp.abort();
}
clearSmartBox();
var hash = window.location.hash;
if (hash==null)
  {
  document.getElementById("searchresults").innerHTML="";
  return;
  }
//firefox bug -- it decodes the hash
var hashstart = location.href.indexOf("#");
hash = location.href.substring(hashstart);
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  return;
  }
var tempString = "<h3 style='margin-top: 20px; margin-bottom: 40em;'>thinking about <b>" + document.forms["myform"]["q"].value + "</b>...</h3>";
document.getElementById("searchresults").innerHTML = tempString;
var url="Search";
var arr = document.forms["myform"].elements;
url=url+"?"+ hash.substr(1);
var opts = 0;
if (getHashParameter("t") == "") {
for (var i=0; i < arr.length; i++) {
	if (arr[i].name == "t" && arr[i].checked) {
		if (opts == 0) {
			url=url+"&t=";
		} else {
			url=url+",";
		}
		opts++;
		url=url+arr[i].value;
	}
}
}
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function showSuggestions()
{
if (ssxmlhttp) {
	ssxmlhttp.abort();
}
ssxmlhttp=GetXmlHttpObject();
if (ssxmlhttp==null)
  {
  return;
  }
var url="SearchSuggestionHtml";
var arr = document.forms["myform"].elements;
url=url+"?q="+ document.forms["myform"]["q"].value;
ssxmlhttp.onreadystatechange=suggest;
ssxmlhttp.open("GET",url,true);
ssxmlhttp.send(null);
}
function submit(click) {
	var clickHttp = GetXmlHttpObject();
	if (clickHttp==null)
	  {
	  return;
	  }
	var url = "Click"
	url = url + "?c=" + click;
	clickHttp.open("GET",url,true);
	clickHttp.send(null);
}

function single(type) {
	zeroCheckboxes();
	updateHash("t", "");
	checkSingleBox(type);
	if (type == "twitter" || type == "collecta" || type == "oneriot") {
		checkSingleBox("rt");
		updateHash("g", type);
	}
	showHint();
}


function refreshquery(str) {
	updateHash("q", encodeURIComponent(str).replace(/%20/g, "+"));
	updateHash("p", 1);
	//	window.location.hash = encodeURIComponent(str).replace(/%20/g, "+");
   	load();
}

function pollHash() {

  if (window.location.hash==recentHash) {
    return; // Nothing's changed since last polled.
  }
  recentHash = window.location.hash;

  // URL has changed, update the UI accordingly.
  load();
}
function showNavigator() {
var navigator = "";
var hash = window.location.hash;
var page = 1;
if (hash.length > 0) {
	var qIndex = hash.indexOf("p=");
	if (qIndex > -1) {
		var finish = hash.indexOf("&", qIndex + 2);
		if (finish == -1) {
			page = parseInt(hash.substr(qIndex+2));
		} else {
			page = parseInt(hash.substring(qIndex+2, finish));
		}
	}
}
navigator += "<li class=\"navigator\"><table align=\"center\"><tbody><tr>";
navigator += "<td>Page: </td>";
if (page > 1) {
	var prevPage = page - 1;
	navigator += "<td><a style=\"text-decoration: none\" href=\"javascript:updateHash('p','" + prevPage + "');\">" + "&lArr;" + "</a></td>";
}
for (var i = 1 ; i <= 5; i ++) {
//document.write(page);
	if (i != page) {
		navigator += "<td><a href=\"javascript:updateHash('p','" + i + "');\">" + i + "</a></td>";
	} else {
		navigator += "<td>" + i + "</td>";
	}
}
var nextPage = page + 1;
navigator += "<td><a style=\"text-decoration: none\"href=\"javascript:updateHash('p','" + nextPage + "');\">" + "&rArr;" + "</a></td>";
navigator += "</tr></tbody></table></li>";
document.getElementById("navigator").innerHTML=navigator;
}

function clearSmartBox() {
	  document.getElementById("smartbox").innerHTML="";
	  document.getElementById('smartbox').style.zIndex="0";
}


function setCookie(name, value) {
	document.cookie = name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/"; 
}
function getCookie(name) { 
	var re = new RegExp(name + "=([^;]+)");
	var value = re.exec(document.cookie); 
	return (value != null) ? unescape(value[1]) : null; 
} 
function submitPrefs() { 
	var arr = document.forms["myform"].elements;
	for (var i=0; i < arr.length; i++) {
		if (arr[i].name == "pref") {
			if (true == arr[i].checked) {
				setCookie(arr[i].value, "true");
			} else {
				setCookie(arr[i].value, "false");
			}					
		} else if (arr[i].name == "realtime") {
			if (arr[i].checked) {
				setCookie(arr[i].name, arr[i].value);
			}
		}
	}
} 
function loadPrefs() {
	cookies = document.cookie; 
	var arr = document.forms["myform"].elements;
	for (var i=0; i < arr.length; i++) {
		if (arr[i].name == "pref" || arr[i].name == "t" || arr[i].name == "realtime")  {
			if (arr[i].name == "realtime") {
				var checked = getCookie(arr[i].name);
				if (arr[i].value == checked) {
					document.forms["myform"].elements[i].checked = true;
				} 
			} else if (arr[i].value== "adult") {
				var checked1 = getCookie(arr[i].value);

				if (checked1 == "true") {
					document.forms["myform"].elements[i].checked = true;
				} else {
					document.forms["myform"].elements[i].checked = false;
				}				
			} else {
				var checked1 = getCookie(arr[i].value);
				if (arr[i].value == "rt") {
					var tChecked = getCookie("twitter");
					var cChecked = getCookie("collecta");
					var oChecked = getCookie("oneriot");
					checked1 = ((tChecked==null && cChecked==null && oChecked==null) || "true" == tChecked || "true" == cChecked || "true" == oChecked) ? "true" : "false";
				}

				if (checked1 == null || checked1 == "true") {
					document.forms["myform"].elements[i].checked = true;
				} else {
					document.forms["myform"].elements[i].checked = false;
				}
			}
		}
	}

}
function zeroCheckboxes() {
	var arr = document.forms["myform"].elements;
	for (var i=0; i < arr.length; i++) {
		if (arr[i].name == "pref" || arr[i].name == "t")  {
					document.forms["myform"].elements[i].checked = false;
		}
	}
}

function updateCheckboxesFromHash() {
	var types = getHashParameter("t");
	if (types == null || types.length==0) return;
	zeroCheckboxes();
	var splitType = types.split(","); 
	for (var i=0; i < splitType.length; i++) {
		checkSingleBox(splitType[i]);
	}
}

function checkSingleBox(contentType) {
	var arr = document.forms["myform"].elements;
	for (var i=0; i < arr.length; i++) {
		if (arr[i].name == "pref" || arr[i].name == "t")  {
					if (arr[i].value == contentType)
					document.forms["myform"].elements[i].checked = true;
		}
	}	
}

function acknowledge() {
	alert("Preferences Have Been Changed");
}

function getHashParameter( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
//var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regexS = "[\\#&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.hash );
  if( results == null )
    return "";
  else
    return results[1];
}

function checkForPlugin() {
	if (typeOf(window.external.IsSearchProviderInstalled) != 'undefined') {
		return window.external.IsSearchProviderInstalled("http://www.stinkyteddy.com");
	} else {
		return 0;
	}
}

function isFirefox() {
	var ff = 0;
	if (navigator.appName) {
		var browser=navigator.userAgent;
		if (browser.indexOf("Firefox")>-1) {
			ff = 1;
		}
	}
	return ff;
}
function isIE() {
	var ie = 0;
	if (navigator.appName) {
		var browser=navigator.userAgent;
		if (browser.indexOf("MSIE")>-1) {
			ie = 1;
		}
	}
	return ie;
}
function isIE8() {
	var ie = 0;
	if (navigator.appName) {
		var browser=navigator.userAgent;
		if (browser.indexOf("MSIE 8.0")>-1) {
			ie = 1;
		}
	}
	return ie;
}
function isSafari() {
	var ie = 0;
	if (navigator.appName) {
		var browser=navigator.userAgent;
		if (browser.indexOf("Safari")>-1 && browser.indexOf("Version") > -1) {
			ie = 1;
		}
	}
	return ie;
}
function isChrome() {
	var ie = 0;
	if (navigator.appName) {
		var browser=navigator.userAgent;
		if (browser.indexOf("Chrome")>-1) {
			ie = 1;
		}
	}
	return ie;
}
function isOpera() {
	var ie = 0;
	if (navigator.appName) {
		var browser=navigator.userAgent;
		if (browser.indexOf("Opera")>-1) {
			ie = 1;
		}
	}
	return ie;
}

