// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(document).ready(function(){
    $('.hide-me').addClass("hidden"); 
    $('.row_candidate').horizontalBarGraph({interval: 10});
    $("td :checked").addYourVoteLabel();
    $(".rest_in_place").rest_in_place();
    $("a.rest_in_place_link").click(function(){$(this).siblings("span").click()});
		$('#contact').contactable({
	 		subject: 'A Feedback Message'
	 	});
    invitation.init();
    $(".edit_op").change(function(){
      _form = $(this) 
      $.post(_form.attr("action"), function(data) {

        $(_form +" span.selected").remove();
        $(_form +" :checked").addYourVoteLabel()
        JAVASCRIPT
        console.info(data)
        eval("data");
        });
      });
		$.facebox.settings.closeImage = '/images/facebox/closelabel.png';
		$.facebox.settings.loadingImage = '/images/facebox/loading.gif';
		if ($("#onload_box").size() > 0 ) { $.facebox({ div: '#onload_box' }); };
		$("#onload_box").click(function(){jQuery(document).trigger('close.facebox')}); /* does not work? */ 
    });

	  $('.filter_button').click(function(){
			_form = $(this).parent;
			$(this).preventDefault();
			if ($(this).hasClass('on')) {
				$(this).removeClass('on');
			} else {
				$(this).addClass('on');
			}
			// _form.submit();
	  });


$.fn.showFullOpinion = function(){
	id = $(this).parents(".op_summary").attr("id").split("_")[1];
	$(this).parents(".op_summary").addClass('hidden');
	$("#op_" + id + "_full").removeClass('hidden');
}

$.fn.showShortOpinion = function(){
	id = $(this).parents(".op_full").attr("id").split("_")[1];
	$(this).parents(".op_full").addClass("hidden");
	$("#op_" + id + "_summary").removeClass("hidden");	
}

// This sets up the proper header for rails to understand the request type,
// and therefore properly respond to js requests (via respond_to block, for example)
$.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

$.fn.addYourVoteLabel = function(){
  $("<span/>").html("*").addClass("selected").appendTo(this.parent());
}

$.fn.moveToNotIncluded = function() {
    $(this).closest("li").appendTo("#all_sources");
}

$.fn.moveToIncluded = function() {
    $(this).closest("li").appendTo("#included_sources");
}

$.fn.moveToPropperDiv = function () {
  
  orly = $(this).parents("ul").hasClass("included")
  orly ? $(this).moveToNotIncluded() : $(this).moveToIncluded();
}

var invitation = {
  init: function(){
          invitation.watchEvents();
          //invitation.connectLists();
        },
  watchEvents: function() {
                 $("li.source input").change(function(){$(this).moveToPropperDiv()});
                },
  connectLists: function() {
                  $('#included_sources, #all_sources').sortable(
                      {connectWith: '.connectedSortable', dropOnEmpty: true}).disableSelection();
               }
}
