/* VOTING */
var vote = {UP:1,DOWN:-1,EMPTY:0}
var $direction = new Object();
$direction[vote.UP] = "up";
$direction[vote.DOWN] = "down";

function initVote(ID,u,d,voted,type)
{
	var votePlaceholder=document.getElementById("votePlaceholder_"+ID);
	votePlaceholder.oID=ID;
	votePlaceholder.upCnt=u;
	votePlaceholder.downCnt=d;
	votePlaceholder.voted=voted;
	votePlaceholder.type=type;
	votePlaceholder.className="votePlaceholder";
	refreshVote(votePlaceholder);
}

function refreshVote(div)
{
  while(div.firstChild) div.removeChild(div.firstChild);
  
  var votePaneTotal=document.createElement("div");
  votePaneTotal.className="votePaneTotal";
  votePaneTotal.appendChild(document.createTextNode(div.upCnt-div.downCnt));

  div.appendChild(votePaneTotal);
  

  for ( var $j in $direction )
  {
		  var votePane=document.createElement("div");
		  votePane.className="votePane_"+$direction[$j];
		  votePane.thumb=$j; 
      votePane.icon="highlight";      		
      var dirCount=$j==vote.UP?div.upCnt:div.downCnt;
      
      votePane.alt=dirCount+" thumbs "+$direction[$j];
    	votePane.title=votePane.alt;
      	
		    var votePaneVotes=document.createElement("div");
		    votePaneVotes.className="votePaneVotes";
	      votePaneVotes.appendChild(document.createTextNode(dirCount));
  			
  			if(div.voted==vote.EMPTY && div.type!=-1 && checkCookieCapability()){
  			votePane.style.cursor="pointer";
  			votePane.submitDiv=div;		
  			votePane.onclick=function(){	this.submitDiv.voted=this.thumb; castVote(this.submitDiv);};

        }
        else
        {
          if(div.voted==$j){
            votePane.className="votePane_"+$direction[$j]+"_highlight";
          }else{
            votePane.className="votePane_"+$direction[$j]+"_shade";
          }
        }
  			votePane.appendChild(votePaneVotes);
			div.appendChild(votePane);
		}
		
}

function castVote(div)
{
  var AJAX = null;                                 
  if (window.XMLHttpRequest) {                     
    AJAX=new XMLHttpRequest();                    
  } else {                                         
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");  
  }                                               
  if (AJAX==null) {                                
    alert("Your browser doesn't support AJAX.");                                              
    return false;                                  
  }
  AJAX.onreadystatechange = function() {
    if (AJAX.readyState==4 || AJAX.readyState=="complete") { 
      if(AJAX.status==200){
        div.voted==vote.UP?div.upCnt++:div.downCnt++;
        refreshVote(div);    
      }else if(AJAX.status==404 && AJAX.responseText=="Duplicate vote"){
        div.voted=0;
        div.type=-1;
        refreshVote(div);    
      }
      //else alert(AJAX.responseText);
    }                                                     
  }
   var url="/sendvote.php?"+"id="+div.oID+"&type="+div.type+"&vote="+div.voted+"&t="+(new Date).getTime();
   AJAX.open("GET", url, true);
   AJAX.send(null);
}

function checkCookieCapability(){
   var tmpcookie = new Date();
   chkcookie = (tmpcookie.getTime() + '');
   document.cookie = "chkcookie=" + chkcookie + "; path=/";
    if (document.cookie.indexOf(chkcookie,0) < 0)
      coo=false;
    else
      coo=true;
      
  return coo;
}
