/* Main script for nybloggat.se */
$(function() {
  // url to media (used for switching images)
  var m = "http://static.nybloggat.se/";

  // attach onchange handler for category-listing
  $('#categories').change(function() { window.location = this[this.selectedIndex].value; });

  // add ajax call image
  $('small.blog').prepend('<img src="' + m + 'expand.gif" class="a1" alt="Se fler inlägg från denna blogg">');

  // calls home and fetches last 5 links from specific blog
  $("small.blog img").css({cursor: 'pointer'}).toggle(function() {
    var a = $(this);
    var b = $('div.more', a.parent());
    // we have a cached result
    if(b.length > 0) b.slideDown('fast');
    // no cache here, fetch from site
    else {
      a.attr('src', m + 'spinner.gif').css({cursor: 'default'});
      $.post('/fetch/', { id: a.next().attr('name').match(/\d+/) }, function(json) {
        var o = '';
        if(json.length == 1) o = '<div style="display: none" class="more"><h5>Det senaste inlägget:</h5><ul>';
        else o = '<div style="display: none" class="more"><h5>De ' + json.length + ' senaste inläggen:</h5><ul>';
        for(var i = 0; i < json.length; i++) {
          o += '<li><a href="/go/' +json[i].id + "/"+ json[i].url + '">' + json[i].title + '</a></li>';
        }
        o += "</ul></div>";
        a.attr('src', m + 'expand.gif').css({cursor: 'pointer'}).parent().append(o).find('div.more').slideDown('fast');
      }, "json");
    }
  // when clicked and result is showing - slide it up again
  }, function() {
    $(this).parent().find('div.more').slideUp('fast');
  });

  // pingform initiation - add an div for errors
  $('.initiate').append('<div id="pingerror"/>');
  
  // set focus to ping url
  $('#pingurl').focus();

  // create variable for storing value between submit checks
  var b = '';

  // override post submit
  $('.initiate').submit(function() {
    var a = $('#pingurl').val();
    // fetch data from specific uri
    if(a.length > 2 && b != a) {
      $('#fetch').after('<img id="spinner" src="' + m + 'spinner.gif">').attr('disabled',true);
      $('#pingerror:visible').slideUp('fast');
      $('#pingextended:visible').slideUp();
      $.post('/ping/fetch/', { url: a }, function(json) {
        // error from server
        if(json.error) {
          $('#pingerror').html(json.error).slideDown('fast');
        }
        // all looks good, populate form and show it
        else {
          // we have data, set it as 'cached'
          b = a;
          $('#permaurl').val(json.url);
          $('#blogname').val(json.blog_name);
          $('#entrytitle').val(json.title);

          // need to set a value for textarea before safari can populate this.
          if($.browser.safari)
            $('#entryexcerpt').css('display: inline');
          
          $('#entryexcerpt').text(json.excerpt);

          $('#pingextended').slideDown();
        }
        $('#fetch').attr('disabled',false);
        $('#spinner').remove();
      }, "json");
    }
    // something is wrong - notify user
    else {
      // user has willingly removed uri, reset any populated form data
      if(a.length <= 2) {
        if($('#pingextended:visible').length > 0) {
          $('#pingextended form')[0].reset();
          $('#pingextended').slideUp();
        }
        // notify error
        $('#pingerror').html('Din adress är för kort, försök igen')
        $('#pingerror:hidden').slideDown('fast');
      }
      else;
      $('#fetchBtn').attr('disabled',false);
      $('#spinner').remove();
    }
    return false;
  });
});

