/*
**
**  What's On Your Mind
**  
**
**
**
*/

/*
****   Setup the tokens on the home page for drag-n-drop voting
*/
function activate_tokens(action)
{
  var active_token;
  var draggable_options = {
    cursor: "move",
    helper: "clone",
    opacity: ".8",
    revert: "invalid",
    start: function()
    {
      active_token = this;
    }
  }

  var droppable_options = {
    accept: ".token",
    tolerance: "pointer",
    hoverClass: "droppable-hover",
    drop: function(ev, ui) {
      $(this)
        .droppable("disable")
        .addClass("selected")
        .draggable("enable");
      $(active_token)
        .droppable("enable")
        .draggable("disable")
        .removeClass("selected");

      var drop_parent = $(this).parent();
      var drop_parent_tag = $(drop_parent).get(0).tagName.toLowerCase();
      var drag_parent = $(active_token).parent();
      var drag_parent_tag = $(drag_parent).get(0).tagName.toLowerCase();

      if (drop_parent_tag == "li" && drag_parent_tag == "ul") {
        // Upvote drop_parent
        var up_id = $(drop_parent).attr("id").substring(1);
        vote_topic(up_id, 1, action);
      } else if (drop_parent_tag == "li" && drag_parent_tag == "li") {
        // Upvote drop_parent
        var up_id = $(drop_parent).attr("id").substring(1);
        vote_topic(up_id, 1, action);
        // Downvote drag_parent
        var down_id = $(drag_parent).attr("id").substring(1);
        vote_topic(down_id, -1, action);
      } else if (drop_parent_tag == "ul" && drag_parent_tag == "li") {
        // Downvote drag_parent
        var down_id = $(drag_parent).attr("id").substring(1);
        vote_topic(down_id, -1, action);
      }
      write_session(action);
    }
  }
  
  $(".token").draggable( draggable_options );
  $(".token:not(.selected)").draggable("disable");
  $(".token").droppable( droppable_options );
  $(".token.selected").droppable("disable");
}

/*
****   Do the vote via POST
*/
function vote_topic(id, direction, action)
{ 
	if (action == "people") {
	  $.ajax({
	    type: "POST",
	    url: "/process/do-votes/",
	    data: {
	      'fields[0][parent]': id,
	      'fields[0][vote]': direction,
	      'fields[0][date]': new Date(),
	      'action[edit-people-votes]' : ""
	    }
	  });
	}
	else
	{
		$.ajax({
	    type: "POST",
	    url: "/process/do-votes/",
	    data: {
	      'fields[0][parent]': id,
	      'fields[0][vote]': direction,
	      'fields[0][date]': new Date(),
	      'action[edit-topic-votes]' : ""
	    }
	  });
	}
}

/*
****   Submit a GET request of the active topics to populate the session
*/
function write_session(action)
{
	if (action == "people") {
		var active_people = new Array();
	  $("#people-list .token.selected:first-child").each(function() {
	    var id = $(this).parent().attr("id");
	    active_people.push(id.substring(1));
	  });
	  var str = "";
	  str += active_people.join(",");
	  $.get("/", { "active-people": str } );
	}
	else
	{
	  var active_topics = new Array();
	  $("#topic-list .token.selected:first-child").each(function() {
	    var id = $(this).parent().attr("id");
	    active_topics.push(id.substring(1));
	  });
	  var str = "";
	  str += active_topics.join(",");
	  $.get("/", { "active-topics": str } );	
	}
}

/*
****   If there's a #flash div in the content, pop it in a jQuery UI dialog
*/
function show_flash()
{
  var flash = $("#flash[rel!=ignore]");
  if (flash.get(0)) {
    var flash_type = flash.attr('class');
    flash.dialog({
      dialogClass: flash_type,
      modal: true,
      resizable: false,
      draggable: false,
      width: 600,
      closeOnEscape: true
    });
    $(".ui-widget-overlay").click(function() 
    {
      flash.dialog( "close" );
    });
  }
}

/*
****   Pop the suggest box on the home into a jQuery UI dialgo
*/

function activate_suggest_dialog()
{
  var suggest_form = $("#suggest-form");
  suggest_form.dialog({
    dialogClass: "success",
    modal: true,
    resizable: false,
    draggable: false,
    width: 790,
    closeOnEscape: true,
    autoOpen: false
  });
  
  $(".suggest .detail a").click(function() {
    suggest_form.dialog( "open" );
    $(".ui-widget-overlay").click(function() 
    {
      suggest_form.dialog( "close" );
      return false;
    });
    $("a.cancel").click(function() 
    {
      suggest_form.dialog( "close" );
      return false;
    });
    return false;
  });
}


/* Call document.ready */
$(document).ready(function()
{
  $(".js-remove").remove();
  show_flash();
});