var as_json1;

function scrollToTop() {
	window.scrollTo(0, 1);
}

function setupAutocomplete(userid, id) {
	var options;
	if(document.getElementById(id).value == "Type your own tag here...")
	{	document.getElementById(id).value = ""; }

	options = {
	script:"ajaxAutocomplete.php?json=true&limit=5&uid="+userid+"&",
	varname:"input",
	className:"autosuggest",
	json:true,
	delay:50,
	timeout:5000,
	shownoresults:false,
	maxresults:5,
	offsety:5 };
	
	as_json1 = new bsn.AutoSuggest(id, options);
	setTimeout(function(){scrollFunction(id)},500);
}

function setupSearchcomplete(userid, id) {
	var options;
	if(document.getElementById(id).value == "Type your own tag here...")
	{	document.getElementById(id).value = ""; }

	options = {
	script:"ajaxSearchcomplete.php?json=true&limit=5&uid="+userid+"&",
	varname:"input",
	className:"autosuggest",
	json:true,
	delay:50,
	timeout:5000,
	shownoresults:false,
	maxresults:5,
	offsety:5 };
	
	as_json1 = new bsn.AutoSuggest(id, options);
	setTimeout(function(){window.scrollTo(0, 102);},500);
}

function closeComplete(id) {
	setTimeout(function(){
		var ac = document.getElementById("as_"+id);
		if(ac != null) {
			ac.style.display = "none";
		}
		as_json = null;
	}, 5000);
}

function submitTag(id, obid, uid) {
	closeComplete(id);
	var tag = document.getElementById(id).value;
	if(tag == "Type your own tag here..." || tag == "" || tag == " "){
		return;
	}
	xmlHttp=GetXmlHttpObject();
	var url = "submittext.php?obid="+obid+"&tag="+tag+"&uid="+uid;
	xmlHttp.onreadystatechange=function(){ if (xmlHttp.readyState==4){updateTagList(id)} };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function resetAddButton(){
    document.getElementById('tagbutton').className = "load_tags";
	document.getElementById('tagbutton').href = document.getElementById('tagbutton').href.replace("tags.php", "addtags.php");
	bodyEl.style.height = el.offsetHeight + "px";
}

function checkabox(id){
    event.preventDefault();
    document.getElementById(id).checked = !document.getElementById(id).checked;   
}

function toggleadd(){
	if(	document.getElementById("tagbutton-intro").className == "cancel")
	{
		document.getElementById("tagbutton-intro").className = "add";
		document.getElementById("tagbutton-intro").src="images/addbutton.png";
	}
	else
	{
		document.getElementById("tagbutton-intro").className = "cancel";
		document.getElementById("tagbutton-intro").src="images/cancelbutton.png";
	}
}
   
function scrollFunction(id)
{
destination = document.getElementById(id);
ypos = findPosY(destination);
xpos = findPosX(destination);
scrollTo(xpos, ypos);
}
function findPosX(obj)
  {
var curleft = 0;
if(obj.offsetParent)
    while(1) 
    {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
else if(obj.x)
    curleft += obj.x;
return curleft;
  }
function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
    while(1)
    {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
else if(obj.y)
    curtop += obj.y;
return curtop;
}

function submitHappy(obid, tag, uid) {
	xmlHttp=GetXmlHttpObject();
	var amt = 1;
	if(document.getElementById("tag-"+tag+"-happy").className.indexOf("checked") != -1)
	{
		amt = -1;
	}
	else if(document.getElementById("tag-"+tag+"-sad").className.indexOf("checked") != -1)
	{
		amt = 2;
	}
	var url = "submitcheck.php?obid="+obid+"&tag="+tag+"&uid="+uid+"&amt="+amt;
	xmlHttp.onreadystatechange=function(){ if (xmlHttp.readyState==4){updateHappy(tag, amt)} };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function submitSad(obid, tag, uid) {
	var amt = -1;
	if(document.getElementById("tag-"+tag+"-sad").className.indexOf("checked") != -1)
	{
		amt = 1;
	}
	else if(document.getElementById("tag-"+tag+"-happy").className.indexOf("checked") != -1)
	{
		amt = -2;
	}
	xmlHttp=GetXmlHttpObject();
	var url = "submitcheck.php?obid="+obid+"&tag="+tag+"&uid="+uid+"&amt="+amt;
	xmlHttp.onreadystatechange=function(){ if (xmlHttp.readyState==4){updateSad(tag, amt)} };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function updateHappy(tag) 
{ 
	document.getElementById("tag-"+tag).className = document.getElementById("tag-"+tag).className.substring(0, document.getElementById("tag-"+tag).className.indexOf("out")) + "out " + xmlHttp.responseText;

	if(document.getElementById("tag-"+tag+"-happy").className.indexOf("checked") != -1)
	{
		document.getElementById("tag-"+tag+"-happy").className = "happy";
	}
	else
	{
		document.getElementById("tag-"+tag+"-happy").className = "happy-checked";
	}
	
	if(document.getElementById("tag-"+tag+"-sad").className.indexOf("checked") != -1)
	{
		document.getElementById("tag-"+tag+"-sad").className = "sad";
	}
}
function updateSad(tag) 
{ 
	document.getElementById("tag-"+tag).className = document.getElementById("tag-"+tag).className.substring(0, document.getElementById("tag-"+tag).className.indexOf("out")) + "out " + xmlHttp.responseText;
	// document.getElementById("tag-"+tag).className = xmlHttp.responseText + " outfillbox";
	
	if(document.getElementById("tag-"+tag+"-sad").className.indexOf("checked") != -1)
	{
		document.getElementById("tag-"+tag+"-sad").className = "sad";
	}
	else
	{
		document.getElementById("tag-"+tag+"-sad").className = "sad-checked";
	}
	
	if(document.getElementById("tag-"+tag+"-happy").className.indexOf("checked") != -1)
	{
		document.getElementById("tag-"+tag+"-happy").className = "happy";
	}
}

function updateTagList(id) 
{
if(id == "typetag1") {
	document.getElementById("taglist").innerHTML = xmlHttp.responseText + document.getElementById("taglist").innerHTML;
}
else {
	document.getElementById("taglist").innerHTML = document.getElementById("taglist").innerHTML + xmlHttp.responseText;
}
document.getElementById(id).value="";
document.getElementById("iphone_body").style.height = document.getElementById("iphone_body").offsetHeight + 50 + "px";
}

function introUpdateTagList() 
{
	var id = document.getElementById("typetag1").value
	document.getElementById("taglist").innerHTML = document.getElementById("taglist").innerHTML + '<div class="out newtag"><a href="" id="tag-'+id+'" class="smallest do_nothing" onclick="updateCheck(\''+id+'\')"><div class="outfillbox"><div class="fillbox">&nbsp;</div></div>'+id+'</a> <a href="" class="medium do_nothing righttext" title="Tag: '+id+'">n objects</a></div>';
	document.getElementById("typetag1").value="";
	document.getElementById("iphone_body").style.height = document.getElementById("iphone_body").offsetHeight + 50 + "px";
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
