
var blog = {
  
  pic_loading: false,
  
  post: function(id) {
    var post = $('post-' + id);
    var post_brief = post.getElement('.brief');
    var post_details = post.getElement('.details');
    
    var show = function() {
      post_brief.setStyle('display', 'none');
      post_details.setStyles({
        'display': 'block',
        'opacity': 0
      }).tween('opacity', 1);
    };
    if (post_details.getElement('.post-content')) {
      post_brief.get('tween').start('opacity', 0).chain(show);
    } else {
      post_brief.getParent().addClass('post-loading');
      post_brief.get('tween').start('opacity', 0).chain(function() {
        new Request.HTML({
          url: '/blog/post/' + id,
          method: 'get',
          //update: 'post-details-' + id,
          onSuccess: function(tree, elements, html) {
            post_details.set('html', html); // Avoid WebKit XML entity bug!
            post_brief.getParent().removeClass('post-loading');
            show();
            init_cufon();
          }
        }).send();
      });
    }
  },
  
  collapse: function(id) {
    var post = $('post-' + id);
    var post_brief = post.getElement('.brief');
    var post_details = post.getElement('.details');
    
    post_details.get('tween').start('opacity', 0).chain(function() {
      post_details.setStyles({
        'opacity': 1,
        'display': 'none'
      });
      post_brief.setStyle('opacity', 1);
      post_brief.setStyle('display', 'block');
    });
  },
  
  pic: function(el, id) {
    if (blog.pic_loading) return;
    
    var url = el.href;
    el = $(el).getParent();
    if (el.hasClass('current')) return;
    var container = $('post-pic-' + id);
    if (!container) return;

    var img = container.getElement('img');
    if (!img) return;

    blog.pic_loading = true;
    
    el.getParent('.thumbs').getElements('.thumb').erase(el).removeClass('current');
    el.addClass('current');
    
    var new_img;
    
    img.get('tween').start('opacity', 0).chain(function() {
      container.addClass('loading');
      container.setStyles({'overflow': 'hidden', 'height': img.height});
      img.destroy();
      new_img = new Element('img').setStyle('opacity', 0);
      new_img.onload = function() {
        new_img.tween('opacity', 1);
        container.tween('height', new_img.height);
        
        blog.pic_loading = false;
      }
      container.adopt(new_img);
      new_img.src = url;
    });
  },
  
  next_pic: function(id) {
    el = $('post-brief-' + id);
    if (!el) return;
    var thumbs = el.getElements('.thumbs .thumb');
    if (thumbs.length < 2) return;
    var current = thumbs[0];
    for (i = 0; i < thumbs.length; i++) {
      if (thumbs[i].hasClass('current')) {
        current = thumbs[i];
        break;
      }
    }
    
    pic = current.getNext();
    if (!pic || !pic.hasClass('thumb')) {
      pic = current.getParent('.thumbs').getFirst();
    }
    if (!pic) return;
    blog.pic(pic.getElement('a'), id);
  },
  
  all_tags: function() {
    var wheight = window.getSize().y;
    
    var startY = (Browser.Engine.trident ? 602 : 20);
    var startOpacity = Browser.Engine.trident ? 1 : 0;
    
    if (blog.overlay) blog.overlay.unpin().destroy();
    blog.overlay = new Element('div', {
      'styles': {
        'background': '#FFFFFF',
        'opacity': 0.02,
        'z-index': 1000
      }
    }).inject(document.body).pin().addEvent('click', blog.hide_all_tags);
    
    if (Browser.Engine.trident4) {
      var top = window.getScroll().y;
      $('cloud').setStyles({
        'z-index': blog.overlay.getStyle('z-index').toInt() + 1,
        'display': 'block',
        'opacity': startOpacity,
        'top': top - startY
      }).morph({
        'top': top + Math.round((wheight - 602) / 2),
        'opacity': 1
      });
    } else {
      $('cloud').setStyles({
        'z-index': blog.overlay.getStyle('z-index').toInt() + 1,
        'position': 'fixed',
        'display': 'block',
        'opacity': startOpacity,
        'top': -startY
      }).morph({
        'top': Math.round((wheight - 602) / 2),
        'opacity': 1
      });
    }
  },
  
  hide_all_tags: function() {
    if (blog.overlay) blog.overlay.unpin().destroy();
    blog.overlay = null;
    
    var wheight = window.getSize().y;
    
    var endY = (Browser.Engine.trident ? 602 : 20);
    var endOpacity = Browser.Engine.trident ? 1 : 0;
    
    var finish = function() {
      $('cloud').setStyle('display', 'none');
    };

    if (Browser.Engine.trident4) {
      var top = window.getScroll().y;
      $('cloud').get('morph').start({
        'top': top - endY,
        'opacity': endOpacity
      }).chain(finish);
    } else {
      $('cloud').get('morph').start({
        'top': -endY,
        'opacity': endOpacity
      }).chain(finish);
    }
  },
  
  load_images: function() {
    if (!$('posts')) return;
    
    blog.images = [];
    // Fix for Opera 9.6
    $('posts').getElements('.post').each(function(div) {
      var a = $(div).getElement('figure a');
      if (a) {
        var id = a.getAttribute('data-id');
        if (id) {
          a.setStyle('visibility', 'hidden');
          blog.images.push({id: id, url: a.href, el: div});
        }
      }
    });

    blog.next_image();
  },

  next_image: function() {
    if (blog.images.length == 0) return;//this.finish();

    var info = blog.images.shift();
    
    var div = info.el.getElement('figure');
    if (!div) return;
    div.addClass('image-loading');
    
    var img = new Element('img', {id: 'img_' + info.id}).setStyle('opacity', 0);
    img.onload = blog.image_loaded.pass(info);
    img.src = info.url;
    var p;
    if (info.el.hasClass('post-short') || !info.el.hasClass('expandable')) {
      p = img;
    } else {
      p = new Element('a', {
        'src': 'javascript://', 'events': {
          'click': function() {
            blur();
            blog.post(info.id);
          }
        }
      }).adopt(img);
    }
    div.empty().adopt(p);
    
    if (info.el.hasClass('post-short')) {
      var post = p.getParent('.post');
      if (post) {
        var thumbs = post.getElements('.thumbs .thumb');
        if (thumbs.length > 0) {
          p.getParent().setStyle('cursor', 'pointer').addEvent('click', blog.next_pic.pass([info.id]));
        }
      }
    }
  },
  
  image_loaded: function(info) {
    var div = info.el.getElement('figure');
    if (div) {
      div.removeClass('image-loading').addClass('image-loaded');

      var img = div.getElement('img');
      if (img) {
        img.onload = '';
        // IE: fix loading previews
        img.setStyles({'width': 'auto', 'height': 'auto'});
        //img.effect('opacity', {duration: this.picFxDelay}).start(1);
        img.setStyle('opacity', 1);
      }
    }
   
    blog.next_image();
  }

};

Asset.image('/resources/i/blog/cloud.png');
window.addEvent('domready', function() {
  if ($('cloud')) $('cloud').inject(document.body);
  blog.load_images();
});

