var cursor_on_voting_text = false;
var current_voting_id;
var showing_voting = false;
var previous_voting;
var cursor_on_voting_label = false;
var aid;
$(document).ready(function(){

  aid = d("article_id").innerHTML;
  
  vids = "";
  $("[id*=vid_]").each(function(){
    vid = this.id.replace("vid_","");
    vids += vid + ",";
  });
  if(vids != "")  {
    vids = delete_last_i4(vids);
    ajax("php/article_texts_votings.php", "&get_voting_results=true" + "&article_id=" + aid + "&vids=" + vids, "", "get voting results", "POST")
  }//if
     
  $("[id*=vid_]").hover(
    function(){
      if(current_voting_id == cursor_on_voting_text) return; 
      vid = this.id.replace("vid_","");    
      //console.log("over"+vid)
      current_voting_id = vid;
      cursor_on_voting_text = vid;
      if(showing_voting && previous_voting != cursor_on_voting_text) {
        //console.log("hide previous"+previous_voting)
        hide_vr(previous_voting);
      }//if   
      setTimeout(function(){
        if(!showing_voting)
          show_vr(vid);
      },1000);
    },
    function(){
      previous_voting = cursor_on_voting_text;
      cursor_on_voting_text = false;
      vid = this.id.replace("vid_","");
      //console.log("out"+vid)
      setTimeout(function(){       
        if(!cursor_on_voting_label && previous_voting != cursor_on_voting_text) {
          hide_vr(previous_voting);
        }//if
      },1000);
    }//function
  ); 
  
  $("[id*=voting_label_]").hover(
    function() {
      if(this.id.indexOf("_content") != -1 || this.id.indexOf("_top") != -1 || this.id.indexOf("_bottom") != -1) return;
      cursor_on_voting_label=true;
      if(this.id.indexOf("voting_label_0") == -1) {
        this.style.width = 138;
        d(this.id + "_content").style.width = 138;
        add_yn(this.id); 
      }//if
    },
    function() {
      if(this.id.indexOf("_content") != -1 || this.id.indexOf("_top") != -1 || this.id.indexOf("_bottom") != -1) return;
      cursor_on_voting_label=false;
      setTimeout(function(){     
        if(cursor_on_voting_text != previous_voting) {
          //console.log("hide label"+previous_voting)
          hide_vr(previous_voting);
        }//if
      },1000);
    }
  ); 
});

function show_vr(vid)  {
  if(cursor_on_voting_text != vid) return;
  //console.log("show" + vid);
  vidr = d("vidr_"+vid).innerHTML.split("|");
  r = vidr[0] - vidr[1];    
  if(r > 0) {
    icon = "<div id=\"vlyi\" style=\"width:16px;height:16px;background-image:url(icons/good.gif);\"></div>";
    d("vid_"+vid).style.backgroundColor = "#FFE9D6";
    d("voting_label_y").style.left = ajfr_x + 4;
    d("voting_label_y").style.top = ajfr_y + 4;
    if(nav == "Microsoft Internet Explorer") {
      d("voting_label_y").style.left = parseInt(d("voting_label_y").style.left) + document.body.scrollLeft;
      d("voting_label_y").style.top = parseInt(d("voting_label_y").style.top) + document.body.scrollTop;
    }//if
    d("voting_label_y_content").innerHTML = "<table style=\"width:48px;font-size:10px;\"><tr><td>" + icon + "</td><td><div id=\"vly_r\" style=\"width:20px;\">" + "+" + r + "</div></td><td>" + "<span id=\"vly_yn\"></span></td></tr></table>";
    $("#voting_label_y").css("opacity",0);
    $("#voting_label_y").animate({opacity:100});
    d("voting_label_y").style.display = "";      
  } else if(r < 0)  {
    icon = "<div id=\"vlni\" style=\"width:16px;height:16px;background-image:url(icons/bad.gif);\"></div>";
    d("vid_"+vid).style.backgroundColor = "#EFFFED";
    d("voting_label_n").style.left = ajfr_x + 4;
    d("voting_label_n").style.top = ajfr_y + 4;
    if(nav == "Microsoft Internet Explorer") {
      d("voting_label_n").style.left = parseInt(d("voting_label_n").style.left) + document.body.scrollLeft;
      d("voting_label_n").style.top = parseInt(d("voting_label_n").style.top) + document.body.scrollTop;
    }//if
    d("voting_label_n_content").innerHTML = "<table style=\"width:48px;font-size:10px;\"><tr><td>" + icon + "</td><td><div id=\"vln_r\" style=\"width:20px;\">" + r + "</div></td><td>" + "<span id=\"vln_yn\"></span></td></tr></table>";
    $("#voting_label_n").css("opacity",0);
    $("#voting_label_n").animate({opacity:100});
    d("voting_label_n").style.display = "";        
  } else {
    if(vidr[0] == undefined || vidr[0] == "") vidr[0] = 0;
    if(vidr[1] == undefined || vidr[1] == "") vidr[1] = 0;
    d("vid_"+vid).style.backgroundColor = "#EDEDFF";
    d("voting_label_0").style.left = ajfr_x + 4;
    d("voting_label_0").style.top = ajfr_y + 4;
    if(nav == "Microsoft Internet Explorer") {
      d("voting_label_0").style.left = parseInt(d("voting_label_0").style.left) + document.body.scrollLeft;
      d("voting_label_0").style.top = parseInt(d("voting_label_0").style.top) + document.body.scrollTop;
    }//if    
    d("voting_label_0_content").innerHTML = "<table style=\"font-weight:bold;font-size:10px;width:76px;height:20px;margin-left:3px;\" cellpadding=0 cellspacing=0><tr><td style=\"width:18px;\" align=\"center\"><div style=\"cursor:pointer;cursor:hand;width:16px;height:16px;background-image:url('icons/good_light.gif');\" onMouseOver=\"this.style.backgroundImage='url(icons/good.gif)';\" onMouseOut=\"this.style.backgroundImage='url(icons/good_light.gif)';\" onClick=\"vote_for_text('y','" + vid + "');\"></div></td><td><div id=\"vl0_yn_yr\" style=\"width:24px;\">" + vidr[0] + "</div></td><td><div style=\"cursor:pointer;cursor:hand;width:16px;height:16px;background-image:url('icons/bad_light.gif');\" onMouseOver=\"this.style.backgroundImage='url(icons/bad.gif)';\" onMouseOut=\"this.style.backgroundImage='url(icons/bad_light.gif)';\" onClick=\"vote_for_text('n','" + vid + "');\"></div></td><td><div id=\"vl0_yn_nr\" style=\"width:24px;\">" + vidr[1] + "</div></td></tr></table>";
    $("#voting_label_0").css("opacity",0);
    $("#voting_label_0").animate({opacity:100});
    d("voting_label_0").style.display = "";       
  }//if-elseif
  showing_voting = vid; 
  
  setTimeout(function(){       
    if(!cursor_on_voting_label && cursor_on_voting_text == showing_voting && showing_voting != false) {
      //console.log("timed hide"+showing_voting)
      hide_vr(showing_voting);
    }//if
  },7000);
    
}//function

function hide_vr(vid)  {
  if(cursor_on_voting_label || showing_voting != vid) return;
  //console.log("hide"+vid)
  d("vid_"+vid).style.backgroundColor = "";
  d("voting_label_y").style.display = "none";
  d("voting_label_y").style.width = 60;
  d("voting_label_y_content").style.width = 60;
  d("voting_label_n").style.display = "none";
  d("voting_label_n").style.width = 60;
  d("voting_label_n_content").style.width = 60;
  d("voting_label_0").style.display = "none";
  d("voting_label_0").style.width = 84;
  d("voting_label_0_content").style.width = 84;  
  showing_voting = false;
}//function

function add_yn(eid) {
  yn = eid.replace("voting_label_","")
  if(yn == "y")  {
    vidr = d("vidr_"+current_voting_id).innerHTML.split("|");
    d("vly_yn").innerHTML = "<table style=\"font-weight:bold;font-size:10px;width:76px;\" cellpadding=0 cellspacing=0><tr><td align=\"center\"><div style=\"margin-right:5px;width:1px;font-size:1px;height:16px;background:#fe7e00;\"></div></td><td style=\"width:18px;\" align=\"center\"><div style=\"cursor:pointer;cursor:hand;width:16px;height:16px;background-image:url('icons/good_light.gif');\" onMouseOver=\"this.style.backgroundImage='url(icons/good.gif)';\" onMouseOut=\"this.style.backgroundImage='url(icons/good_light.gif)';\" onClick=\"vote_for_text('y','" + current_voting_id + "');\"></div></td><td><div id=\"vly_yn_yr\" style=\"width:24px;\">" + vidr[0] + "</div></td><td><div style=\"cursor:pointer;cursor:hand;width:16px;height:16px;background-image:url('icons/bad_light.gif');\" onMouseOver=\"this.style.backgroundImage='url(icons/bad.gif)';\" onMouseOut=\"this.style.backgroundImage='url(icons/bad_light.gif)';\" onClick=\"vote_for_text('n','" + current_voting_id + "');\"></div></td><td><div id=\"vly_yn_nr\" style=\"width:24px;\">" + vidr[1] + "</div></td></tr></table>";
  }//if
  if(yn == "n") {
    d("vln_yn").innerHTML = "<table style=\"font-weight:bold;font-size:10px;width:76px;\" cellpadding=0 cellspacing=0><tr><td align=\"center\"><div style=\"margin-right:5px;width:1px;font-size:1px;height:16px;background:#27ff05;\"></div></td><td style=\"width:18px;\" align=\"center\"><div style=\"cursor:pointer;cursor:hand;width:16px;height:16px;background-image:url('icons/good_light.gif');\" onMouseOver=\"this.style.backgroundImage='url(icons/good.gif)';\" onMouseOut=\"this.style.backgroundImage='url(icons/good_light.gif)';\" onClick=\"vote_for_text('y','" + current_voting_id + "');\"></div></td><td><div id=\"vln_yn_yr\" style=\"width:24px;\">" + vidr[0] + "</div></td><td><div style=\"cursor:pointer;cursor:hand;width:16px;height:16px;background-image:url('icons/bad_light.gif');\" onMouseOver=\"this.style.backgroundImage='url(icons/bad.gif)';\" onMouseOut=\"this.style.backgroundImage='url(icons/bad_light.gif)';\" onClick=\"vote_for_text('n','" + current_voting_id + "');\"></div></td><td><div id=\"vln_yn_nr\" style=\"width:24px;\">" + vidr[1] + "</div></td></tr></table>";
  }//if
}//function

function vote_for_text(yn,vid) {
  add = "";
  if(d("vidr_"+vid).innerHTML == "") add = "&new_voting=true";
  ajax("php/article_texts_votings.php", "&vote_for_text=true" + "&article_id=" + aid + "&vid=" + vid + "&yn=" + yn + add, "", "vote for text", "POST");
}//function
