/* Adapted from VoteItUp */
/* A 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)
*/

/* Note: This script is meant for the bar theme of the plugin */

var xmlHttp
var currentobj
var voteobj
var aftervotetext
var id
var type
var Gupdown
var baseURL
baseURL = '/wp-content/plugins/vote-it-up';

//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 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.style.width = voteno;

	//voteobj_obj.innerHTML = aftervotetext;
	if(Gupdown=='recommend'){
		document.getElementById(Gupdown+'_'+type+'_'+id).innerHTML = voteno;
		document.getElementById(Gupdown+'_'+type+'_'+id).className += '_voted';
		document.getElementById(Gupdown+'_'+type+'_'+id).href = 'javascript:alert(\'you have voted\')';
	}else if(document.getElementById(Gupdown+'_'+type+'_'+id)){
		document.getElementById(Gupdown+'_'+type+'_'+id).innerHTML = voteno;
		document.getElementById('up_'+type+'_'+id).className += '_voted';
		document.getElementById('up_'+type+'_'+id).href = 'javascript:alert(\'you have voted\')';
		if(document.getElementById('down_'+type+'_'+id)){
			document.getElementById('down_'+type+'_'+id).className += '_voted';
			document.getElementById('down_'+type+'_'+id).href = 'javascript:alert(\'you have voted\')';
		}
	}else if(document.getElementById(Gupdown+'_'+id)){
		document.getElementById(Gupdown+'_'+id).innerHTML = voteno;
		document.getElementById('up_'+id).className += '_voted';
		document.getElementById('up_'+id).href = 'javascript:alert(\'you have voted\')';
		if(document.getElementById('down_'+id)){
			document.getElementById('down_'+id).className += '_voted';
			document.getElementById('down_'+id).href = 'javascript:alert(\'you have voted\')';
		}
	}
}


}

function updown(thisID,thisupdown,thistype){
	Gupdown = thisupdown;
	type = thistype;
	id = thisID;
	var scripturl = baseURL+"/voteinterface.php?updown="+Gupdown+"&type="+type+"&id="+id+"&auth="+Math.random();
	lg_AJAXrequest(scripturl);
}

function vote(postID) {
	Gupdown = 'up';
	type = 'post';
	id = postID;
	var scripturl = baseURL+"/voteinterface.php?updown=up&type=post&id="+postID+"&auth="+Math.random();
	lg_AJAXrequest(scripturl);
}

function sink(postID) {
	Gupdown = 'down';
	type = 'post';
	id = postID;
	var scripturl = baseURL+"/voteinterface.php?updown=down&type=post&id="+postID+"&auth="+Math.random();
	lg_AJAXrequest(scripturl);
}