$(document).ready(function() {

  /* ==== Dialog ==== */

  $('.dialog').dialog({
    autoOpen: false,
    modal: true,
    resizable: false,
    buttons: {
      "Ok": function() {
        $(this).dialog("close");
      }
    }
  });


  /* ==== Scroller ==== */
  
  $('.scrollPane').jScrollPane({"scrollbarWidth":4});


  /* ==== Events ==== */

  // focus su cerca
  $('#inputCerca').focus(function() {
    $(this).val("");
    $(this).css("color", "#000");
  })

  // invio ricerca
  $('#formCerca').submit(function() {
    if("" != $('#inputCerca').val()) {
      var s = this.action;
      var search_url = s.replace("QUERY", $('#inputCerca').val(), "gi");
      location.href = search_url;
    } else {
      alertBox('Non puoi effettuare una ricerca vuota.', 'Ricerca')
    }
    return false;
  });

  // fade risposta alla domanda
  $('.esito').fadeIn(500, function() {
    var p1 = parseFloat($('#gainedPoints').html());
    var p2 = parseFloat($('#userScore').html());
    if(p1 > 0) {
      var p = p1 + p2;
      yoyo('#userScore', p);
    }
  });
  
  // invio gallery per evento
  $('#event').change(function() {
    location.href = $('#event').val();
  });

  // open answer box
  $('.answerOpen').click(function() {
    $('#'+this.rel).toggle('medium');
  });

  // vota fotografia
  $('#btnvota').click(function() {
    if("" !== this.rel) {
      $.ajax({
        type: "GET",
        url: this.rel,
        dataType: "text",
        success: function(msg) {
          var result = msg.split("|");
          $('#alert').dialog('option', 'width', 450);
          $('#alert').bind('dialogclose', function() {
             yoyo('#totalVotes', result[1]);
          });
          alertBox(result[0], 'Vota lo slogan')
          $('#btnvota').fadeTo(500, .5);
          $('#btnvota').attr("rel","");
        }
      });
    }
  });

});

