/* 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);
}

//Javascript Function for JavaScript to communicate with Server-side scripts
function AJAXrequest(scriptURL) {
	xmlHttp=zGetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
	xmlHttp.onreadystatechange=voteChanged;
	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);
	votewidgetobj_obj = currentobj_obj.parentNode;
	votebuttonobj_obj = voteobj_obj.parentNode;
	currentobj_obj.innerHTML = voteno;
	voteobj_obj.innerHTML = aftervotetext;
	if (aftervotetext == '') {
	votebuttonobj_obj.style.display = 'none';

	votewidgetobj_obj.setAttribute("class", "post_votewidget_closed");
	votewidgetobj_obj.setAttribute("className", "post_votewidget_closed"); 
	}

}


}


function vote(obj, votelinkobj, aftervote, postID ,userID, baseURL) {
	currentobj = obj;
	voteobj = votelinkobj;
	gpostid = postID;
	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);
}

// Customized function added by Arunkumar on 13/07/2009
function selectvote(select_id, post_id, nominee_id, path, mode)
{
	var id = select_id+post_id;

	if(document.getElementById(nominee_id).value != "")
	{
		var prev_id = document.getElementById(nominee_id).value;
		document.getElementById(prev_id).src = 'wp-content/plugins/vote-it-up/skins/orange_ticker/img/vote_unselect.png';
	}
	
	if(document.getElementById('vote_path').value != "")
		document.getElementById('vote_path').value = '';	

	if(mode == 'unselect')
	{	
		document.getElementById(id).src = 'wp-content/plugins/vote-it-up/skins/orange_ticker/img/vote_select.gif';
		document.getElementById(nominee_id).value = id;
		document.getElementById('vote_path').value = path;
	} 
	
}

function castvote(nominee_id, path)
{
	if(document.getElementById('user_id'))
	{
		alert("Authenticated user cannot vote the nominee!");
		return false;
	}	
	if(document.getElementById(nominee_id).value == "" || document.getElementById(path).value == "") 
	{
		alert("select one of the nominee to vote!");
		return false;
	} else {
		//vote('votecount'+post_id, 'voteid'+post_id, '', post_id, 0, path);
		/* Vote Machine */
			if ((CB_Cookie.get('vote_machine_name')) != "") 
				voteMachine =  CB_Cookie.get('vote_machine_name');
			
			
			var userID =0;
			baseURL = document.getElementById(path).value;
			nominee_value = document.getElementById(nominee_id).value;
			get_post_id = nominee_value.split('_');
			post_id		= get_post_id[2];
			var scripturl = baseURL+"/voteinterface.php?type=vote&tid=total&uid="+userID+"&pid="+post_id+"&auth="+Math.random()+"&voteMachine="+voteMachine;
			AJAXrequest(scripturl);
	}
	//'votecount32','voteid32','Voted',32,0,'http://localhost.localdomain/feedingdreams_demo/memphis/wp-content/plugins/vote-it-up'	
	//obj, votelinkobj, aftervote, postID ,userID, baseURL
}

function voteChanged()
{
	if (xmlHttp.readyState==4)
	{ 
		var voteno = xmlHttp.responseText;
		
		if(voteno == 'closed')
		{
			alert('VOTING HAS ENDED! Winners will be announced shortly!');
			document.voteRequest.submit();
		}	
		
		//document.getElementById('votewidget_skin').innerHTML  = '<div class="vote_shutter"></div>' ;
		//document.getElementById('votecount_value').innerHTML  = voteno;
		var cookieVal = CB_Cookie.get('vote_cookie');
		var ckblogid = CB_Cookie.get('vote_blogid');
		var flashEnabled =	navigator.mimeTypes && navigator.mimeTypes	 ["application/x-shockwave-flash"]&& navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin && navigator.plugins && navigator.plugins["Shockwave Flash"];
		
		
		if((cookieVal !="" || cookieVal == "undefined") &&  (ckblogid != "" || ckblogid == "undefined"))
		{
			document.getElementById('op').value= cookieVal;
			document.getElementById('ckblogid').value = ckblogid;
			document.getElementById('flash').value = flashEnabled;
			document.voteRequest.submit();
			//redirectUrl = voteUrl+"&op="+cookieVal;
			//window.location.href = redirectUrl;
		} else {
			document.getElementById('op').value= '';
			document.getElementById('ckblogid').value= '';
			document.getElementById('flash').value = flashEnabled;
			document.voteRequest.submit();
			//window.location.href = voteUrl;
		}		
	}

}
