/* Adapted from VoteItUp */
/* A general script for updating the contents of the vote widget on the fly */
/*
USAGE:
vote (object to update with vote count, object to update with after vote text, post id, user id, base url)
*/

var xmlHttp;
var xmlHttp2;
var currentobj;
var voteobj;
var responseobj;
var aftervotetext;
var sorttype;
var lastreftime;

//Useful for compatibility
function function_exists( function_name ) { 
    if (typeof function_name == 'string'){
        return (typeof window[function_name] == 'function');
    } else{
        return (function_name instanceof Function);
    }
}

//Javascript Function for JavaScript to communicate with Server-side scripts
function lg_AJAXrequest(scriptURL,type) {
	xmlHttp=zGetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
    if (type == 1) {
		xmlHttp.open("POST",scriptURL,false);
		xmlHttp.send(null);
		if (xmlHttp.status==200)
		{
			zvoteChanged();
		}
	} else if (type == 2) {
		xmlHttp.open("POST",scriptURL,false);
		xmlHttp.send(null);
	} else {
		xmlHttp.open("POST",scriptURL,false);
		xmlHttp.send(null);
		if (xmlHttp.status==200)
		{
			suggested();
		}
	}
	delete xmlHttp;
}

function lg_AJAXrequest2(scriptURL,time,ishome) {
	xmlHttp2=zGetXmlHttpObject();
	if (xmlHttp2==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var params = "time="+time+"&ishome="+ishome;
	xmlHttp2.open("POST",scriptURL,true);
	
	//Send the proper header information along with the request
	xmlHttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	xmlHttp2.onreadystatechange = function() {//Call a function when the state changes.
		if(xmlHttp2.readyState == 4 && xmlHttp.status == 200) {
			alert(xmlHttp2.responseText);
		}
	}
	xmlHttp2.onreadystatechange=zmostVoted;
	xmlHttp2.send(params);
	delete xmlHttp2;
}

function lg_AJAXsort(baseURL,query,ishome,type,page_uri) {
	if (type === undefined) sorttype = 0;
	if (sorttype == 0) {
		document.getElementById('sortarea1').style.display = 'none';
		document.getElementById('resorting').style.display = 'inline-block';
	}
	var scriptURL = baseURL + '/postloop.php'
	xmlHttp=zGetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var params = "query="+query+"&ishome="+ishome+"&page_uri="+page_uri;
	xmlHttp.open("POST",scriptURL,true);
	
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	xmlHttp.onreadystatechange=sortMethodChanged;
	xmlHttp.send(params);
	delete xmlHttp;
}

function zGetXmlHttpObject()
{
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;
}

function zvoteChanged() 
{ 
	var votedisp = document.getElementById('voteid' + currentobj);
	var votenodisp = document.getElementById('votes' + currentobj);
	var voteno = xmlHttp.responseText;
	
	currentobj_obj = document.getElementById(currentobj);
	voteobj_obj = document.getElementById(voteobj);
	currentobj_obj.innerHTML = voteno;
	voteobj_obj.innerHTML = aftervotetext;
	if (aftervotetext == '') {
		voteobj_obj.style.display = 'none';
	}

}

function updateDelayed()
{
}

function sortMethodChanged()
{
	if (xmlHttp.readyState == 3)
	{ 
		var d = new Date();
		var curtime = d.getTime();
		if (lastreftime === null) lastreftime = curtime;
		if (curtime < lastreftime + 1000) return;
		lastreftime = curtime;
		var postlooptxt = xmlHttp.responseText;
		//if (postlooptxt.indexOf('New papers') == -1) return;
		var postloopobj = document.getElementById('postloop');

		postloopobj.innerHTML = postlooptxt;
		document.getElementById('sortarea1').style.display = 'none';
		document.getElementById('resorting').style.display = 'none';
		document.getElementById('loading').style.display = 'inline-block';
		toggleCat(false);
		return;
	}
	if (xmlHttp.readyState == 4)
	{ 
		var postlooptxt = xmlHttp.responseText;
		var postloopobj = document.getElementById('postloop');

		postloopobj.innerHTML = postlooptxt;
		toggleCat(false);
		if (sorttype == 0) {
			document.getElementById('resorting').style.display = 'none';
			document.getElementById('loading').style.display = 'none';
			document.getElementById('sortarea1').style.display = 'block';
		}

		MathJax.Hub.Configured();
	}
}

function suggested()
{
	var suggest_elem = document.getElementById(responseobj);
	var suggest_text = xmlHttp.responseText;
	
	suggest_elem.innerHTML = suggest_text;
	suggest_elem.style.display = 'inline-block';
}

function zmostVoted() 
{ 
	if (xmlHttp2.readyState==4)
	{ 
		var mostvotedtxt = xmlHttp2.responseText;
		
		var mostvotedobj = document.getElementById('mostvoted');
		mostvotedobj.innerHTML = mostvotedtxt;
	}
}

function vote(obj, votelinkobj, aftervote, postID ,userID, baseURL, today, ishome) {
	currentobj = obj;
	voteobj = votelinkobj;
	aftervotetext = aftervote;
	var scripturl = baseURL+"/voteinterface.php?type=vote&tid=total&uid="+userID+"&pid="+postID+"&auth="+Math.random();
	lg_AJAXrequest(scripturl, 1);
	if (ishome == 1 && document.getElementById('mostvoted')) {
		scripturl = baseURL+"/skins/ticker/mostvoted.php";
		lg_AJAXrequest2(scripturl, today, ishome);
	}
	setTimeout(function(){
		voteobj_obj = document.getElementById(voteobj);
		var unvote_text = '<a href="javascript:unvote(\''+obj+'\',\''+votelinkobj+'\',\'Done!\','+postID+','+userID+',\''+baseURL+'\','+today+','+ishome+');">Unvote</a>';
		voteobj_obj.innerHTML = unvote_text;
	}, 2500);
}

function sink(obj, votelinkobj, aftervote, postID ,userID, baseURL, today, ishome) {
	currentobj = obj;
	voteobj = votelinkobj;
	aftervotetext = aftervote;
	var scripturl = baseURL+"/voteinterface.php?type=sink&tid=total&uid="+userID+"&pid="+postID+"&auth="+Math.random();
	lg_AJAXrequest(scripturl, 1);
	if (ishome == 1 && document.getElementById('mostvoted')) {
		scripturl = baseURL+"/skins/ticker/mostvoted.php";
		lg_AJAXrequest2(scripturl, today, ishome);
	}
	setTimeout(function(){
		voteobj_obj = document.getElementById(voteobj);
		var unvote_text = '<a href="javascript:unvote(\''+obj+'\',\''+votelinkobj+'\',\'Done!\','+postID+','+userID+',\''+baseURL+'\','+today+','+ishome+');">Unvote</a>';
		voteobj_obj.innerHTML = unvote_text;
	}, 2500);
}

function discuss(postID, baseURL, today, ishome) {
	var scripturl = baseURL+"/voteinterface.php?type=discuss&pid="+postID+"&today="+today+"&auth="+Math.random();
	lg_AJAXrequest(scripturl, 2);
	scripturl = baseURL+"/skins/ticker/mostvoted.php";
	lg_AJAXrequest2(scripturl, today, ishome);
}

function suggest(obj, respobj, postID ,userID, baseURL) {
	currentobj = obj;
	responseobj = respobj;
	var sugg_elem = document.getElementById(currentobj);
	var sugg = sugg_elem.value;
	aftervotetext = '';
	var scripturl = baseURL+"/voteinterface.php?type=suggest&uid="+userID+"&pid="+postID+"&sname="+sugg+"&auth="+Math.random();
	lg_AJAXrequest(scripturl, 3);
}

function bump(obj, votelinkobj, aftervote, postID ,userID, baseURL) {
	currentobj = obj;
	voteobj = votelinkobj;
	aftervotetext = aftervote;
	var scripturl = baseURL+"/voteinterface.php?type=bump&tid=total&uid="+userID+"&pid="+postID+"&auth="+Math.random();
	lg_AJAXrequest(scripturl, 1);
}

function unvote(obj, votelinkobj, aftervote, postID ,userID, baseURL, today, ishome) {
	currentobj = obj;
	voteobj = votelinkobj;
	aftervotetext = aftervote;
	var scripturl = baseURL+"/voteinterface.php?type=unvote&tid=total&uid="+userID+"&pid="+postID+"&auth="+Math.random();
	lg_AJAXrequest(scripturl, 1);
	if (ishome == 1 && document.getElementById('mostvoted')) {
		scripturl = baseURL+"/skins/ticker/mostvoted.php";
		lg_AJAXrequest2(scripturl, today, ishome);
	}
}

