function comment() {
  var parent=$(this).parent();
  var frame=parent.children('iframe');
  frame.toggle();
  if (frame.length==0) {
    parent.append($('<iframe frameborder="0"></iframe>')
      .attr('src','http://kozmice.appspot.com/'+parent.attr('id'))
      .css({'width':'100%','height':'400px'}));
  }
}

function get_num(self) {
  $.getJSON('http://kozmice.appspot.com/get.json?key='+self.attr('id')+'&callback=?',
    function(data) {
      self.append($('<span>('+data[0]+')</span><br>'));
    }
  ); 
}

$(document).ready(
  function() {
    $("div.agcomment").each( function(i) {
      var self=$(this);
      self.append($("<a>Komentarze</a>").click(comment));
      get_num(self);
    });
  }
)
