/*
Copyright (c) 2010 DemDash (www.demdash.us)
*/

var positionlabel = [ 'noposition', 'strongsupport', 'support', 'undecided', 'oppose', 'strongoppose'];
var positionname = [ 'No Position', 'Strongly Support', 'Support', 'Undecided', 'Oppose', 'Strongly Oppose'];

$(document).ready(function() {
  $("#yesplease-form").validate();
  
  $('.slate-editposition-button').click(
    function () {
      if ($('#chooser-editposition').is(':hidden')) {
        var line_id = $(this).attr('id').split('-');
        var currentresponse = $(this).children('span.editposition').text();
        var currentcomment = $(this).children('span.currentcomment').text();
        if (currentcomment == "None") {
            currentcomment = "";
        }
        $('#chooser-position_id').attr({'value':line_id[2]});
        $('#chooser-position_type').attr({'value':line_id[1]});
        $('.slate-editposition-button').removeClass('down');
        $('.slate-editposition-arrow').removeClass('down');
        $(this).addClass('down');
        $(this).children('.slate-editposition-arrow').addClass('down');
        SetChooserStatus(currentresponse);
        $('#editposition-' + currentresponse).attr({'checked':'checked'});
        $('#chooser-text').attr({'value': currentcomment});
        $("#chooser-editposition").show().css({'top':$(this).position().top, 'left': $(this).position().left + 82});  
      } else {
        CloseChooser();
      }
    });
        
  $("input:radio[name=position]").change(function(){
      var slate_id = $('#chooser-slate_id').attr('value');
      var position_id = $('#chooser-position_id').attr('value');
      var position_type = $('#chooser-position_type').attr('value');
      var newresponse = $("input:radio[name=position]:checked").val();
      var comment = $('#chooser-text').attr('value');
      //UpdatePosition(slate_id, position_id, position_type, newresponse, comment);
      EZUpdate();
  });

  // change "Done" button to "Save" if the text has changed
  $("textarea#chooser-text").keyup(function(){
      $('#chooser-editposition-done').attr({'value':'Save'});
  });

  $('#chooser-editposition-done').click(function(event) {
      CloseChooser();
      event.preventDefault();
    });
});

function EZUpdate() {
  SetChooserStatus('loader');
  var slate_id = $('#chooser-slate_id').attr('value');
  var position_id = $('#chooser-position_id').attr('value');
  var position_type = $('#chooser-position_type').attr('value');
  var position = $("input:radio[name=position]:checked").val();
  var comment = $('#chooser-text').attr('value');
  //if (comment == "") comment = "Open the Your Position box at left to say why." 
  // this should probably just adjust visibility of this
  $('#yourposcomment').text("\"" + comment + "\"");
  // get id=editposition-candidate-4632
  var idtoget = '#editposition-' + position_type + '-' + position_id;
  $(idtoget).children('span.currentcomment').empty().append(comment);
  //alert(idtoget,$(idtoget).children('span.currentcomment').attr('value'));
  $.post("/slate/change",  
    { slate_id: slate_id, position_type: position_type, position_id: position_id, position: position, comment: comment },  
    function(data){
      if (data.success === "True") {
        var thisitem = '#editposition-' + position_type + '-' + position_id;
        $(thisitem).children('span.editposition').text(position);
        $(thisitem).children('.slate-editposition-icon').removeAttr('class')
          .addClass('slate-editposition-icon').addClass('editposition-' + positionlabel[data.response]);
        SetChooserStatus(data.response);
      } else {
        SetChooserStatus('error');
      }
    }, 
  "json");
}

function UpdatePosition(slate_id, position_id, position_type, position, comment) {
  SetChooserStatus('loader');
  $.post("/slate/change",  
    { slate_id: slate_id, position_type: position_type, position_id: position_id, position: position, comment: comment },  
    function(data){
      if (data.success === "True") {
        var thisitem = '#editposition-' + position_type + '-' + position_id;
        $(thisitem).children('span.editposition').text(position);
        $(thisitem).children('.slate-editposition-icon').removeAttr('class')
          .addClass('slate-editposition-icon').addClass('editposition-' + positionlabel[data.response]);
        SetChooserStatus(data.response);
      } else {
        SetChooserStatus('error');
      }
    }, 
  "json");  
}

function CloseChooser() {
  // make sure the comment is saved
  EZUpdate()
  $('.slate-editposition-button').removeClass('down');
  $('.slate-editposition-arrow').removeClass('down');
  $('#chooser-editposition-done').attr({'value':'Done'});
  $("#chooser-editposition").hide();
  var comment = $('#chooser-text').attr('value');
  if (comment != "") {
    $('#yourposcommenttop').show("slow");
  }
  if (comment == "") {
    $('#yourposcommenttop').hide("slow");
  }

}

function SetChooserStatus(msg) {
  if (msg === 'loader') {
    status = '<img src="media/img/loader-blue.gif" />';
  } else if (msg === 'error') {
    status = '<span class="error">Oops! Try again!</span>';
  } else {
    status = '<span class="' + positionlabel[msg] + '">' + positionname[msg] + '</span>';
  }
  $('#chooser-editposition-status').empty().append(status);

}
