/* 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 currentobj
var voteobj
var aftervotetext
var gpostid

//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) {
	xmlHttp=zGetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
	xmlHttp.onreadystatechange=zvoteChanged;
	xmlHttp.open("GET",scriptURL,true);
	xmlHttp.send(null);
}

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() 
{ 
	if (xmlHttp.readyState==4)
	{ 

	var votenodisp = document.getElementById(currentobj);

	var voteno = xmlHttp.responseText;

	var votelink_obj = votenodisp.parentNode;
	var votewidgetobj_obj = votelink_obj.parentNode;
	
	votenodisp.innerHTML = voteno;
	
	votelink_obj.setAttribute("onclick", "return false;");
	votelink_obj.setAttribute("title", "You already voted for this track");
	
	votewidgetobj_obj.setAttribute("class", "post_votewidget_closed");
	votewidgetobj_obj.setAttribute("className", "post_votewidget_closed"); 
	
	$(".post_button a").tipTip({defaultPosition: "top", edgeOffset: 1});
	$.prettyLoader.hide();

	}


}

function nop() {

}

function vote(obj, votelinkobj, aftervote, postID ,userID, baseURL) {
	currentobj = obj;
	voteobj = votelinkobj;
	gpostid = postID;
	
	$.prettyLoader.show(2000);
	
	var votenodisp = document.getElementById(currentobj);
	var votelink_obj = votenodisp.parentNode;
	var votewidgetobj_obj = votelink_obj.parentNode;
	/* 
	votewidgetobj_obj.setAttribute("class", "post_votewidget_ajax");
	votewidgetobj_obj.setAttribute("className", "post_votewidget_ajax"); 
	*/
	
	$.prettyLoader.show();
	
	aftervotetext = aftervote;
	var scripturl = baseURL+"/voteinterface.php?type=vote&tid=total&uid="+userID+"&pid="+postID+"&auth="+Math.random();
	lg_AJAXrequest(scripturl);
}

function sink(obj, votelinkobj, aftervote, postID ,userID, baseURL) {
	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);
}
