jQuery.noConflict();
(function($) { 
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;
var ageent_height;
var count=0;
var onlyoneclick=0;
var ageent_content=new String();
  $(function() {
    if(template_nice_list==1) Custom_Nice.init(); 
    $("#nicepollsend").live("click",function() {
        ageent_content = $("#poll-container").html();
        checked = $(".ageent_main_only input:checked'").val();
        if(checked!= undefined) {
            onlyoneclick=1;
            real_send();
        } else {
            return false;
        }
    });
    
    $("#nicepollresult").live("click",function() {
        $(".ageent_main_only input[type=radio]").removeAttr('checked');
        real_send();
    });
    

    fix_height=$(".ageent_main_only").height();
    $(".ageent_main_only").css({"min-height":fix_height+"px"});
    ageent_content = $("#poll-container").html();
    $("#back_please").live("click",function() {
        $("#poll-container").html(ageent_content);
        time_height=$(".ageent_main_only").height();
        $(".ageent_main_only").css({height: time_height+"px"}).animate({ height: ageent_height}, 'slow');
    });
    
    real_one();
    get_cookei = getCookie(cookei);
    if(get_cookei) {
        if(ag_fast_refrash) {
            $(".ageent_main_only input[type=radio]").attr("disabled", true); 
            if(ag_disabled_or_del) {
              $("#nicepollsend").attr("disabled", true);   
            } else {
              $("#nicepollsend").remove();   
            } 
            ageent_content = $("#poll-container").html();
            var id_poll =  $("input[name=number_poll]").attr("value");
            var id_option = $("input[name=vote]:checked").attr("value");
            adress = $(".ageent_main_only #poll").attr("action");
            $("#poll-container").empty();
            ageent_height = $(".ageent_main_only").height();
            $.getJSON(adress+"?number_poll="+id_poll+"&id_option="+id_option, loadResults);
        } else {
            $(".ageent_main_only input[type=radio]").attr("disabled", true); 
            if(ag_disabled_or_del) {
              $("#nicepollsend").attr("disabled", true);   
            } else {
              $("#nicepollsend").remove();   
            } 
            ageent_content = $("#poll-container").html();
        }
    }
  });
  
 function real_send() {
    $("#poll").submit(formProcess); // setup the submit handler
 }
 
 function real_one() {
    cookei = $("input[name=cookieName]").attr("value");
 }
  
function formProcess(event){
  ageent_height = $(".ageent_main_only").height();
  $("#poll-container").parent().next().remove();  
  event.preventDefault();
  if(onlyoneclick){
    $(".ageent_main_only input[type=radio]").attr("disabled", true); 
    if(ag_disabled_or_del) {
        $("#nicepollsend").attr("disabled", true);   
    } else {
        $("#nicepollsend").remove();   
    } 
    ageent_content = $("#poll-container").html();
  }
  var id_poll =  $("input[name=number_poll]").attr("value");
  var id_option = $("input[name=vote]:checked").attr("value");
            
  $("#poll-container").fadeOut("slow",function(){
    adress = $(".ageent_main_only #poll").attr("action");
    $(this).empty();
    $.getJSON(adress+"?number_poll="+id_poll+"&id_option="+id_option, loadResults);
  });
}

function randomNumber(m,n){
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}

function animateResults(){
  $(".i_find_you").each(function(){
      var percentage = $(this).attr("title");
      $(this).css({width: "0%"}).animate({ width: percentage}, 'slow');
  });
}

function loadResults(data_ageent) {
  var total_votes = 0;
  var percent="";  
  count=0;
  var colors = new Array("#ed1c24","#39b54a","#0000ff","#cc9900","#c7b299","#0076a3","#005e20","#000","#ed1c24","#39b54a","#0000ff","#fff200","#c7b299","#0076a3","#005e20","#000"); 
  
  for (id in data_ageent) {
    if(data_ageent[id][OPT_VOTES]>=0) {  
        total_votes = total_votes+parseInt(data_ageent[id][OPT_VOTES]);
    }
  }
  
  var results_html = "<div id='poll-results'>\n<table>\n";
  for (id in data_ageent) {
    if(data_ageent[id][OPT_VOTES]>=0) {  
    results_html = results_html + "<tr>";
    percent = Math.round(((parseInt(data_ageent[id][OPT_VOTES])/parseInt(total_votes))*nicepoll_width_percent));
    real_percent = Math.round((parseInt(data_ageent[id][OPT_VOTES])/parseInt(total_votes))*100);
    vievpercent = real_percent;
    if(percent == 0) {
      percent = 1;
      vievpercent=0;   
    }
    if(nicepoll_template_list==1) {
        results_html = results_html+"<td colspan=\"2\" class=\"temp_two\">"+data_ageent[id][OPT_TITLE]+" ("+data_ageent[id][OPT_VOTES]+")</td></tr><tr><td align='left' width='"+nicepoll_width_percent+"'><div class='goad_lol'><div style='width:0%; background-color:"+colors[count]+";' class='i_find_you' title='"+percent+"'>&nbsp;</div></div></td><td align='right'><strong>"+vievpercent+"%</strong></td>\n";
    } else {
        results_html = results_html+"<td class='one_point'>"+data_ageent[id][OPT_TITLE]+"</td><td align='left' width='"+nicepoll_width_percent+"'><div class='goad_lol'><div style='width:0%; background-color:"+colors[count]+";' class='i_find_you' title='"+percent+"'>&nbsp;</div></div></td><td align='right'><strong>"+vievpercent+"%</strong></td>\n";
    }
    results_html = results_html + "</tr>";
    count = count + 1;
    }
  }
  
  if(ag_real_back) {
    yourealback=" <a href='' id='back_please' onclick='return false'>"+ag_back+" в‡‘</a>";  
  } else {
    yourealback="";  
  }
  
  if(mod_nicepoll) {
     if(ag_total_votes.length!=0) { 
    results_html = results_html+"</table><p class='total'>"+ag_total_votes+": <b>"+total_votes+"</b>"+yourealback+"<br /><a href='"+full_url_nicepoll+"index.php?option=com_poll' title='"+ag_all_poll+"' class='polls'>"+ag_all_poll+"</a></p></div>\n";
     } else {
    results_html = results_html+"</table><p class='total'><a href='"+full_url_nicepoll+"index.php?option=com_poll' title='"+ag_all_poll+"' class='polls'>"+ag_all_poll+"</a>"+yourealback+"</p></div>\n";
     }
  } else {
     if(ag_total_votes.length!=0) { 
         results_html = results_html+"</table><p class='total'>"+ag_total_votes+": <b>"+total_votes+"</b>"+yourealback+"</p></div>\n";
     } else {
         results_html = results_html+"</table></div>\n";
     }
  }
  
  $("#poll-container").append(results_html).fadeIn("slow", function(){ animateResults();});
  real_height_one = $(".one_quesion").height();
  real_height_two = $("#poll-container").height();
  real_height = real_height_one + real_height_two;
  $(".ageent_main_only").css({"height":ageent_height+"px","min-height":"0px"});
  $(".ageent_main_only").css({"min-height":"0px"});
  $(".ageent_main_only").css({height: ageent_height+"px"}).animate({ height: real_height}, 'slow');
}

function getCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}
})(jQuery);
