
// twitSpeech actions

var base = 'http://www.twitspeech.com';
var speech_text_limit = 320;
var speech_text_status = 'span#speech_count';
var tweet_text_limit = 140;
var tweet_text_status = 'span#tweet_count';
var comment_text_limit = 280;
var comment_text_status = 'span#comment_count';

// Cheer a speech
function cheer(id) {
  $(this).blur();
  $('#action-' + id).html('Cheered!');
  $.post(base + '/ajax/cheer', {id: id}, function(data) {
    if(data.split(':')[0] != 1) return false;
    var score = data.split(':')[1];
    var sign = '';
    var attrib = 'green';
    if (score < 0) {attrib = 'red'; sign = '- ';}
    else if (score > 0) {attrib = 'green'; sign = '+ ';}
    $('.score-' + id).attr('class', 'score-' + id + ' ' + attrib);
    $('.score-' + id).html(sign + score.replace('-', ''));
  });
}

// Boo a speech
function boo(id) {
  $(this).blur();
  $('#action-' + id).html('Booed!');
  $.post(base + '/ajax/boo', {id: id}, function(data) {
    if(data.split(':')[0] != 1) return false;
    var score = data.split(':')[1];
    var sign = '';
    var attrib = 'green';
    if (score < 0) {attrib = 'red'; sign = '- ';}
    else if (score > 0) {attrib = 'green'; sign = '+ ';}
    $('.score-' + id).attr('class', 'score-' + id + ' ' + attrib);
    $('.score-' + id).html(sign + score.replace('-', ''));
  });
}

// Remove a speech
function remove(id) {
  var target = $('#action-' + id);
  var content = target.html();
  target.html('<span class="question">Really remove?</span> <a href="javascript:void(0);" name="yes" class="greenbtn"><span>Yes</span></a> <a href="javascript:void(0);" name="no" class="redbtn"><span>No</span></a>');
  target.children('a[name=yes]').click(function() {
    location.href = base + '/previous/remove/' + id;
  });
  target.children('a[name=no]').click(function() {
     target.html(content);
  });
}

// Withdraw a speech
function withdraw(id) {
  var target = $('#action-' + id);
  var content = target.html();
  target.html('<span class="question">Really withdraw?</span> <a href="javascript:void(0);" name="yes" class="greenbtn"><span>Yes</span></a> <a href="javascript:void(0);" name="no" class="redbtn"><span>No</span></a>');
  target.children('a[name=yes]').click(function() {
    location.href = base + '/previous/withdraw/' + id;
  });
  target.children('a[name=no]').click(function() {
     target.html(content);
  });
}

// Layout
$(document).ready(function() {
  
  // Speech character limit
  if($('textarea[name=speech_text]').length) {
    $('textarea[name=speech_text]').maxlength({
      maxCharacters: speech_text_limit,
      statusElement: speech_text_status,
      statusText: ''
    });
  }
  
  // Tweet character limit
  if($('textarea[name=tweet_text]').length) {
    $('textarea[name=tweet_text]').maxlength({
      maxCharacters: tweet_text_limit,
      statusElement: tweet_text_status,
      statusText: ''
    });
  }
  
  // Comment character limit
  if($('textarea[name=comment_text]').length) {
    $('textarea[name=comment_text]').maxlength({
      maxCharacters: comment_text_limit,
      statusElement: comment_text_status,
      statusText: ''
    });
  }
  
});