
/* $Id: class_poll2.js 12 2009-01-11 06:04:12Z john $ */

SocialEngineAPI.poll2s = new Class({
  
  Base : {},
  
  
  // Properties
  options: {
    'ajaxURL' : 'poll2_ajax.php',
    'maxRequestsInProgress' : 1
  },
  
  requestsInProgress: 0,
  
  currentConfirmDeleteID: 0,
  
  
  
  // Creation
  newpoll2Option: function()
  {
    var poll2OptionContainer = $('sepoll2Options');
    var currentpoll2OptionCount = poll2OptionContainer.getElements('input').length;
    var newpoll2OptionIndex = currentpoll2OptionCount+1;
    
    if( currentpoll2OptionCount>=20 )
    {
      alert(this.Base.Language.Translate(25002098));
    }
    
    else
    {
      var newOptionTemplate = $('sepoll2sOptionTemplate').getElement('.sepoll2sOption').clone();
      
      newOptionTemplate.setProperty('id', 'sepoll2sOption_'+newpoll2OptionIndex);
      newOptionTemplate.getElement('.sepoll2sIndex').setProperty('html', newpoll2OptionIndex);
      
      newOptionTemplate.inject(poll2OptionContainer);
      newOptionTemplate.focus();
    }
  },
  
  
  
  // Retrieval
  getpoll2Data: function(poll2ID)
  {
    // One request at a time please
    /*
    if( this.requestsInProgress>=this.options.maxRequestsInProgress )
    {
      alert(SELanguage.Translate(25002115));
      return false;
    }
    */
    
    // Ajax
    var bind = this;
    var request = new Request.JSON({
      'method' : 'post',
      'url' : this.options.ajaxURL,
      'data' : {
        'task' : 'infopoll2',
        'poll2_id' : poll2ID
      },
      'onComplete': function(responseObject)
      {
        //bind.requestsInProgress--;
        
        if( !responseObject || $type(responseObject)!="object" || responseObject.result=="failure" )
        {
          if( !responseObject.message )
            alert(bind.Base.Language.Translate(25002114));
          else
            alert(responseObject.message);
        }
        else
        {
          bind.generatepoll2Results(poll2ID, responseObject);
          bind.poll2ViewMode(poll2ID);
        }
      }
    });
    
    //this.requestsInProgress++;
    request.send();
  },
  
  
  generatepoll2Results: function(poll2ID, poll2DataObject)
  {
    var poll2ResultsContainer = $('poll2'+poll2ID+'_results');
    poll2ResultsContainer.empty();
    
    // Options
    var bind = this;
    var isFirst = true;
    var poll2ResultIndex = 1;
    poll2DataObject.poll2_options.each(function(poll2OptionLabel, poll2OptionIndex)
    {
      var poll2ResultTemplate = $('poll2ResultTemplate').getElement('.poll2Result').clone();
      
      // Generate Stats
      var poll2ResultID = "poll2" + poll2ID + "_bar" + poll2OptionIndex;
      var poll2ResultClass = "poll2_bar" + (poll2ResultIndex - (20 * Math.floor(poll2ResultIndex/20))).toString();
      
      var width = 3;
      var percentage = 0;
      if( poll2DataObject.poll2_answers[poll2OptionIndex] )
        width += Math.round((poll2DataObject.poll2_answers[poll2OptionIndex] / poll2DataObject.poll2_totalvotes) * 400);
      if( poll2DataObject.poll2_answers[poll2OptionIndex] )
        percentage += Math.round((poll2DataObject.poll2_answers[poll2OptionIndex] / poll2DataObject.poll2_totalvotes) * 100);
      
      // Apply to template
      var votesString = '('+bind.Base.Language.TranslateFormatted(25002028, [poll2DataObject.poll2_answers[poll2OptionIndex]])+')';
      
      poll2ResultTemplate.getElement('.poll2ResultLabel').setProperty('html', poll2OptionLabel);
      poll2ResultTemplate.getElement('.poll2ResultBar').addClass(poll2ResultClass);
      poll2ResultTemplate.getElement('.poll2ResultBar').setProperty('id', poll2ResultID);
      poll2ResultTemplate.getElement('.poll2ResultPercentage').setProperty('html', percentage+'%');
      poll2ResultTemplate.getElement('.poll2ResultVotes').setProperty('html', votesString);
      
      // Space them
      if( !isFirst )
      {
        (new Element('br')).inject(poll2ResultTemplate, 'top');
        //(new Element('br')).inject(poll2ResultTemplate, 'top');
      }
      
      // Inject
      poll2ResultTemplate.inject(poll2ResultsContainer);
      
      poll2ResultIndex++;
      isFirst = false;
    });
    
    // Effects
    poll2DataObject.poll2_options.each(function(poll2OptionLabel, poll2OptionIndex)
    {
      var poll2ResultElement = $("poll2" + poll2ID + "_bar" + poll2OptionIndex);
      
      var width = 3;
      var percentage = 0;
      if( poll2DataObject.poll2_answers[poll2OptionIndex] )
        width += Math.round((poll2DataObject.poll2_answers[poll2OptionIndex] / poll2DataObject.poll2_totalvotes) * 400);
      if( poll2DataObject.poll2_answers[poll2OptionIndex] )
        percentage += Math.round((poll2DataObject.poll2_answers[poll2OptionIndex] / poll2DataObject.poll2_totalvotes) * 100);
      
      var poll2Effect = new Fx.Tween(poll2ResultElement, {duration: 1000, transition: Fx.Transitions.Quad.easeOut});
      poll2Effect.start('width', 3, width);
    });
  },
  
  
  poll2ViewMode: function(poll2ID)
  {
    $('poll2'+poll2ID+'_results').style.display = "block";
    $('poll2'+poll2ID+'_results_actions').style.display = "block";
    $('poll2'+poll2ID+'_vote').style.display = "none";
    $('poll2'+poll2ID+'_vote_actions').style.display = "none";
  },
  
  
  poll2VoteMode: function(poll2ID)
  {
    $('poll2'+poll2ID+'_results').style.display = "none";
    $('poll2'+poll2ID+'_results_actions').style.display = "none";
    $('poll2'+poll2ID+'_vote').style.display = "block";
    $('poll2'+poll2ID+'_vote_actions').style.display = "block";
  },
  
  
  
  // Vote
  sendpoll2Vote: function(poll2ID)
  {
    // One request at a time please
    if( this.requestsInProgress>=this.options.maxRequestsInProgress )
    {
      alert(this.Base.Language.Translate(25002115));
      return false;
    }
    
    // Get Vote Value
    var voteValue;
    
    $('sepoll2'+poll2ID).getElements('.poll2VoteOption').each(function(optionElement)
    {
      if( !optionElement.checked ) return;
      voteValue = optionElement.value;
    });
    
    // Ajax
    var bind = this;
    var request = new Request.JSON({
      'method' : 'post',
      'url' : this.options.ajaxURL,
      'data' : {
        'task' : 'votepoll2',
        'poll2_id' : poll2ID,
        'vote' : voteValue
      },
      'onComplete': function(responseObject)
      {
        bind.requestsInProgress--;
        
        if( !responseObject || $type(responseObject)!="object" || responseObject.result=="failure" )
        {
          if( !responseObject.message )
            alert(bind.Base.Language.Translate(25002114));
          else
            alert(responseObject.message);
        }
        else
        {
          bind.generatepoll2Results(poll2ID, responseObject);
          bind.poll2ViewMode(poll2ID);
        }
      }
    });
    
    this.requestsInProgress++;
    request.send();
  },
  
  
  
  // Toggle Open/Closed
  togglepoll2: function(poll2ID, isClosed)
  {
    // One request at a time please
    if( this.requestsInProgress>=this.options.maxRequestsInProgress )
    {
      alert(this.Base.Language.Translate(25002115));
      return false;
    }
    
    // Display
    var poll2Container = $('sepoll2_'+poll2ID);
    
    if( isClosed )
    {
      poll2Container.getElement('.sepoll2sClose').style.display = 'none';
      poll2Container.getElement('.sepoll2sOpen').style.display = '';
    }
    else
    {
      poll2Container.getElement('.sepoll2sClose').style.display = '';
      poll2Container.getElement('.sepoll2sOpen').style.display = 'none';
    }
    
    // Ajax
    var bind = this;
    var request = new Request.JSON({
      'method' : 'post',
      'url' : this.options.ajaxURL,
      'data' : {
        'task' : 'togglepoll2',
        'poll2_id' : poll2ID,
        'poll2_closed' : (isClosed ? 1 : 0)
      },
      'onComplete': function(responseObject)
      {
        bind.requestsInProgress--;
        
        if( !responseObject || $type(responseObject)!="object" || responseObject.result=="failure" )
        {
          if( !responseObject.message )
            alert(bind.Base.Language.Translate(25002114));
          else
            alert(responseObject.message);
        }
        else
        {
          
        }
      }
    });
    
    this.requestsInProgress++;
    request.send();
  },
  
  
  
  // Delete
  deletepoll2: function(poll2ID)
  {
    this.showpoll2Delete(poll2ID);
  },
  
  
  deletepoll2Confirm: function(poll2ID)
  {
    // One request at a time please
    if( this.requestsInProgress>=this.options.maxRequestsInProgress )
    {
      alert(this.Base.Language.Translate(25002115));
      return false;
    }
    
    // Display
    $('sepoll2_'+poll2ID).destroy();
    
    // Ajax
    var bind = this;
    var request = new Request.JSON({
      'method' : 'post',
      'url' : this.options.ajaxURL,
      'data' : {
        'task' : 'deletepoll2',
        'poll2_id' : poll2ID
      },
      'onComplete': function(responseObject)
      {
        bind.requestsInProgress--;
        
        if( !responseObject || $type(responseObject)!="object" || responseObject.result=="failure" )
        {
          if( !responseObject.message )
            alert(bind.Base.Language.Translate(25002114));
          else
            alert(responseObject.message);
        }
      }
    });
    
    this.requestsInProgress++;
    request.send();
    
    
    
    // SHOW NEW MESSAGE
    if( $$('.sepoll2Row').length<1 )
      $('poll2nullmessage').style.display = 'block';
  },
  
  showpoll2Delete: function(poll2ID)
  {
    var poll2Container = $('sepoll2_'+poll2ID);
    this.currentConfirmDeleteID = poll2ID;
    TB_show(this.Base.Language.Translate(25002055), '#TB_inline?height=100&width=300&inlineId=confirmpoll2delete', '', '../images/trans.gif');
  }

});
