(function($) {
  var newMethods = {
    "centra"      : function() { var wh=$(window).height(), wl=$(window).width(), sh=$("body").scrollTop()+$(window).scrollTop(), sl=$("body").scrollLeft();
        return this.each( function(){ 
        var h=$(this).outerHeight(true), l=$(this).outerWidth(true); 
        $(this).css({top: (sh+(wh-h)/2)+"px", left: (sl+(wl-l)/2)+"px"});
      });
    }
  };
  $.each(newMethods, function(i) { $.fn[i] = this; });
})(jQuery);

$(function(){
  $("li .riga").css("cursor", "pointer")
  .mouseenter(function(){ $(this).addClass("sfondo2 colore2"); })
  .mouseleave(function(){ $(this).removeClass("sfondo2 colore2"); })
  .bind("click", function(){
    var box=$(this).next();
    if (box.is(":visible")){ box.hide(200); destinazione=null}
    else { destinazione=box.html("<div>Apro il sondaggio. Attendere...</div>").show(200).load("sondaggio.asp", {s: s, z: z, d: $(this).attr("rel")}); }  
  });
  if (sid>0) {
    $(window).load(mostrasondaggio);
  }
});

function vota(t){
  var f=t.form; var d=f.d.value, n=f.ns.value; var s=$("input[name=sond"+d+"]:checked", f).length;
  if (s==0) {
    alert("Scegliere almeno un valore."); return;
  } else if (s>n) {
    alert("Scegliere al massimo "+n+" valori."); return;
  } else if (s<n) {
    if (! confirm("Puoi scegliere "+n+" valori,\nma ne ha scelti "+s+".\nVuoi votare comunque?")) { return; } 
  }
  destinazione.html("<div>Voto in corso....</div>").show(200).load("sondaggio_vota.asp", $(f).serializeArray());
}

function entra(d){
  var uid=document.getElementById("uid"+d).value;
  var pass=document.getElementById("pass"+d).value;
  var ric=document.getElementById("ric"+d).checked?1:0;
  if (uid=="") {
    alert("Inserire Utente id!");
    return;
  }
  if (pass=="") {
    alert("Inserire la password!");
    return;
  }
  destinazione.html("<div>Login in corso....</div>").show(200).load("sondaggio.asp", {uid: uid, pass: pass, d: d, ricorda: ric, a: "login"});
}

function logout(d){
  destinazione.html("<div>Sconnessione in corso....</div>").show(200).load("sondaggio.asp", {d: d, a: "logout"});
}

function mostrasondaggio(){
  if ($("#popsfondo").length==0) {
    $("<div id='popsfondo'></div>").appendTo("body")
    .css({ position: "absolute", zIndex: 2000, backgroundColor: "#000", width: "100%", height: $(document).height()+"px", cursor: "pointer", opacity: .7})
    .click(function(){ $("#popsfondo, #boxavviso").remove(); }).centra();
  }
  if ($("#boxavviso").length==0) {
    $("<div style='padding: 3px; background-color: Black; border: 2px solid #000; color: #fff; border-radius: 10px; position: absolute; z-index: 2001;' id='boxavviso'></div>").appendTo("body"); 
  }
  destinazione=$("#boxavviso").html("Apertura sondaggio in corso, attendere...").load("sondaggio.asp", {s: s, z: z, d: sid, scid: scid}, function(){ $("#boxavviso").centra().centra() }).centra();
}

