
  /*****
  * Stile js Layout SimplicioSito
  */
  
  $(document).ready(function(){
  
    /*******
    * Lightbox
    */
    
    try{
      $('#wrapper a.images').lightBox();
    }catch(e){
      void(0);
    }
    try{
      $('#thumbs a.images').lightBox();
    }catch(e){
      void(0);
    }
    try{
      $('#big a.images').lightBox();
    }catch(e){
      void(0);
    }
    try{
      $('.containerPhotogallery a.images').lightBox();
    }catch(e){
      void(0);
    }
    try{
      $('#dettaglio div.picture a.images').lightBox();
    }catch(e){
      void(0);
    }
    
    /******
    * Funzione recupero score
    */
    
    function getScore(identifier){
      return $.ajax({
        url: './includes/ajax/comments.php',
        type: 'GET',
        data: 'identifier='+identifier+'&getMode=score',
        async: false
      }).responseText;
    }
    
    /******
    * Form Commenti
    */
    
    var postCommentOptions = {
        beforeSubmit:  postCommentRequest,  // pre-submit callback 
        success:       postCommentResponse  // post-submit callback 
        
        //clearForm: true
        
        // other available options: 
        //target:        '#login',   // target element(s) to be updated with server response 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
    
    $('#container #subContainer #content #comment div.postComment div.postCommentBody form').ajaxForm(postCommentOptions);
    
    function postCommentRequest(formData, jqForm, options){
      
      for(i=0; i<formData.length; i++){
        name = formData[i].name;
        value = formData[i].value;
        while (value.substring(0,1) == ' ') value = value.substring(1, value.length); // LTrim
        
        if (name == "txtName" && value == ""){
          alert("Inserisci il tuo nome!");
          return false;
        }
        
        if (name == "txtEmail" && value == ""){
          alert("Inserisci il tuo indirizzo e-mail!");
          return false;
        }
        if (name == "txtEmail" && value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1){
          alert("Indirizzo e-mail non valido!");
          return false;
        }
        
        if (name == "commento" && value == ""){
          alert("Inserisci il testo del commento!");
          return false;
        }
      }
      
      var queryString = $.param(formData);
      jqForm.parent().parent().attr("id","commentProcessing");
      jqForm.parent().parent().block('<h1 class="processing">Processing</h1>');
      return true; 
    }
    
    function postCommentResponse(responseText, statusText){
      var divObj = $('#commentProcessing');
      divObj.unblock(); 
      divObj.block('<h1 class="sent">'+responseText+'</h1>');
      if (responseText != "Commento Inserito") divObj.unblock(); 
      divObj.removeAttr("id");
    }
    
    /******
    * Form Voto Positivo
    */
    
    var voteUpOptions = {
        beforeSubmit:  voteUpRequest,  // pre-submit callback 
        success:       voteUpResponse,  // post-submit callback 
        clearForm: true
    }; 
    
    $('#container #subContainer #content #comment div.containerList div.userComment div.userCommentHead div.userCommentVote div.userCommentVoteUp form').ajaxForm(voteUpOptions);
    
    function voteUpRequest(formData, jqForm, options){
      var queryString = $.param(formData);
      jqForm.parent().parent().html('<p id="voteWait" class="vote">wait..</p>');
      return true; 
    }
    
    function voteUpResponse(responseText, statusText){
      var identifier = $('#voteWait').parent().parent().parent().attr('id');
      var score = getScore(identifier);
      var color = score > 0 ? '#00cc00' : '#ff0000';
      color = score == 0 ? '#929292' : color;
      var content = '<div class="userCommentScore" style="color: '+color+';">'+score+'</div><div class="userCommentVoteDown handDownGray"></div><div class="userCommentVoteUp handUpGreen"></div>';
      $('#voteWait').parent().html(content).parent().parent().block('<h1 class="sent">'+responseText+'</h1>');
    }
    
    /******
    * Form Voto Negativo
    */
    
    var voteDownOptions = {
        beforeSubmit:  voteDownRequest,  // pre-submit callback 
        success:       voteDownResponse,  // post-submit callback 
        clearForm: true
    }; 
    
    $('#container #subContainer #content #comment div.containerList div.userComment div.userCommentHead div.userCommentVote div.userCommentVoteDown form').ajaxForm(voteDownOptions);
    
    function voteDownRequest(formData, jqForm, options){
      var queryString = $.param(formData);
      jqForm.parent().parent().html('<p id="voteWait" class="vote">wait..</p>');
      return true; 
    }
    
    function voteDownResponse(responseText, statusText){
      var identifier = $('#voteWait').parent().parent().parent().attr('id');
      var score = getScore(identifier);
      var color = score > 0 ? '#00cc00' : '#ff0000';
      color = score == 0 ? '#929292' : color;
      var content = '<div class="userCommentScore" style="color: '+color+';">'+score+'</div><div class="userCommentVoteDown handDownGreen"></div><div class="userCommentVoteUp handUpGray"></div>';
      $('#voteWait').parent().html(content).parent().parent().block('<h1 class="sent">'+responseText+'</h1>');
    }
    
    /*******
    * Conto alla rovescia
    */
    
    function keyupListener(element){
      var limit = 500;
      var jObj = $(element);
      var lunghezza = jObj.val().length;
      if(lunghezza >= limit){
        jObj.val(jObj.val().substr(0,limit-1));
      }
      
      $('#container #subContainer #content #comment div.postComment div.postCommentBody form .counter').val(limit-lunghezza);
    }
    
    $('#container #subContainer #content #comment div.postComment div.postCommentBody form textarea').bind('keydown',function(){
      keyupListener('#container #subContainer #content #comment div.postComment div.postCommentBody form textarea');
    });
    
    /******
    * Navigator & Refresh
    */
    
    function refresh(element, page){
      var target = $(element).attr('name');
      $(element).parent().block('<h1>Loading...</h1>');
      $.ajax({ 
        url: './includes/ajax/comments.php',
        type: 'GET',
        data: 'pagina='+page+'&target='+target+'&getMode=commenti',
        success:  function(html){
          $(element).html(html);
          /* Voto Negativo */
          $('#container #subContainer #content #comment div.containerList div.userComment div.userCommentHead div.userCommentVote div.userCommentVoteDown form').ajaxForm(voteDownOptions);
          /* Voto Positivo */
          $('#container #subContainer #content #comment div.containerList div.userComment div.userCommentHead div.userCommentVote div.userCommentVoteUp form').ajaxForm(voteUpOptions);
          $(element).parent().unblock();
        }
      });
    }
    
    function navigator(element, data, page){
      
      var id_record = 0;
      var str1 = $("input:hidden");
      for(i=0; i<str1.length; i++) {
        if (str1[i].name == "id_record") {
          id_record = str1[i].value;
        }
      }
      
      $.ajax({ 
        url: './includes/ajax/comments.php',
        type: 'GET',
        data: 'pagina='+page+'&getMode=navigator&id_record='+id_record,
        success:  function(html){
          var navigator = eval(html);
          $.each(navigator,function(index,value){
            var cssClass = value == page ? 'bold' : '';
            if(parseInt(value) > 0){
              $(element).append('<div class="page_'+value+' '+cssClass+'">'+value+'</div>');
              var subElement = element+' div.page_'+value;
              $(subElement).bind('click',function(){
                pager(element, data, value);
              });
            }else if(value == '&raquo;'){
              $(element).append('<div class="page_next '+cssClass+'">'+value+'</div>');
              var subElement = element+' div.page_next';
              $(subElement).bind('click',function(){
                pager(element, data, (page+1));
              });
            }else if(value == '&laquo;'){
              $(element).append('<div class="page_back '+cssClass+'">'+value+'</div>');
              var subElement = element+' div.page_back';
              $(subElement).bind('click',function(){
                pager(element, data, (page-1));
              });
            }
          });
        }
      });
    }
    
    function pager(pager,data,page){
      $(pager).html('');
      $(data).html('');
      navigator(pager, data, page);
      refresh(data, page);
    }
    
   navigator('#container #subContainer #content #comment div.postCommentPager',
   '#container #subContainer #content #comment div.containerList',1);

    
  });
