$(document).ready(function() {
  var submitData = { "guid[]": [], "asin[]": [], "position[]" : []};
  $(".annotationGet").each( function() {
      submitData["guid[]"].push($(this).nextAll(".guid:first").text());  
      submitData["asin[]"].push($(this).nextAll(".asin:first").text());  
      submitData["position[]"].push($(this).attr('id'));
  });

  if( submitData["guid[]"].length > 0 ) {
    $.post("/kindle/excerpts", submitData,
      function(data){
        $.each(data, function(i, ex){
          $("#" + ex.id + "_before, #a" + ex.id + "_before").html(ex.before);
          $("#" + ex.id + ", #a" + ex.id).removeClass('annotationGet').html(ex.text);
          $("#" + ex.id + "_after, #a" + ex.id + "_after").html(ex.after);
          $("#" + ex.id + "_start, #a" + ex.id + "_start").html(ex.start);
          $("#" + ex.id + "_end, #a" + ex.id + "_end").html(ex.end);
      });
    }, "json");
  }

  $(".annotationPageNav a").click(
    function() {
      // Toggle selected page
      $(this).parent().parent().removeClass('selected')
      $(".annotationPage#"+$(this).attr('id')).addClass('selected');

      $.post("/kindle/anno_nav_history",{record: $(this).attr('id')});
      return false;
  });

  $("a.annotationTabNav").click(
    function() {
	var li = $(this).parent('li');
	if (!li.hasClass('selected')) {
            // Toggle selected tab content
            $(".annotationTab").removeClass('selected');
            $("#" + li.attr('id') + '_content').addClass('selected');

            // Toggle tab title
            li.siblings().removeClass('selected');
            li.addClass('selected');

            // Switch content of selector between this and selected one
	    var selectors = li.parent('ul').siblings('.navSelector');
            var selected_selector = selectors.find('.selected');
            var this_selector = selectors.find('#' + li.attr('id') + '_sel');
            var selected_html = selected_selector.html();
            selected_selector.html(this_selector.html());
            this_selector.html(selected_html);
	    
            // Toggle selected selector
            selected_selector.removeClass('selected')
            this_selector.addClass('selected')
	    
            $.post("/kindle/anno_nav_history",{record: $(this).attr('id')});
	}
	return false;
    });
});



