jQuery(function($) {
  
  //$('body').append($('<div>', {id:'grid'})).hover(function(){$('#grid').hide()},function(){$('#grid').show()});
  
  // EMAIL LINKS
  
  var e = ['com', 'interactivethings', 'olleh'];
  $('a[href=#email]').each(function() {
    $(this)
    .attr('href', 'mailto:'+e[2].split('').reverse().join('')+'@'+e[1]+'.'+e[0])
    .text($(this).text().replace('(at)','@').replace('(dot)','.'));
  });
  
  
  // LOAD POPUP CONTENTS
       
    var create_popup = function(id, title) {
      return $('<div>', {
        id:'popup_' + id, 
        'class':'popup',
        html: '<div class="triangle"></div><h5>' + title + '</h5><p></p>'
      });
    };
      
    var popup_datavis = create_popup('datavis', 'Latest Post:');
    var popup_twitter = create_popup('twitter', 'Latest Tweet:');
    var popup_tumblr = create_popup('tumblr', 'Latest Post:');
    var popup_dribbble = create_popup('dribbble', 'Latest Shot:');
    
    // Datavis
    $.get('http://interactivethings.com/util/xhr_proxy.php?url=http://feeds.feedburner.com/Datavisualization?format=xml', function(data){
       var p = $(data).find('item:first');
       // console.log(data);
       var title = p.find('title').text();
       var content = $(p.find('encoded').text());
       var img = content.children('img:first');
       if (content) {
         popup_datavis.click(function() {
          window.location.href = p.attr('rdf:about'); 
         }).addClass('clickable');
         popup_datavis.children('p').append(img.addClass('preview')).append(title);
         popup_datavis.appendTo('#content');
       }
    });
    
    // Tumblr
    $.get('http://interactivethings.com/util/xhr_proxy.php?url=http://interactivethings.tumblr.com/api/read?num=1', function(data){
      var p = $(data).find('post');
      // console.log(data);
      var content = '';
      switch(p.attr('type')) {
        case 'regular':
          content = p.children('regular-title').text();
          break;
        case 'link':
          content = $('<a>', {href:p.children('link-url').text(), text:p.children('link-text').text()});
          break;
        case 'quote':
          break;
        case 'photo':
          content = $('<img>', {'class':'preview', src:p.children('photo-url[max-width=75]').text()});
          break;
        case 'conversation':
          break;
        case 'video':
          content = p.children('video-caption').text();
          break;
        case 'audio':
          break;
      }
      if (content != '') {
        popup_tumblr.click(function() {
           window.location.href = p.attr('url'); 
        }).addClass('clickable');
        popup_tumblr.children('p').append(content);
        popup_tumblr.appendTo('#content');
      }
    });
        
    // Twitter
    $.getJSON('http://twitter.com/status/user_timeline/ixt.json?count=10&callback=?', function(data){
      if (data) {
        //console.log(data)
        popup_twitter.click(function() {
           window.location = 'http://twitter.com/ixt/statuses/' + data[0].id; 
        }).addClass('clickable');
        popup_twitter.children('p').append(data[0].text);
        popup_twitter.appendTo('#content');
      };
    });
    
    // Dribbble
    var dribbble_init = (function() {
      $.get('http://interactivethings.com/util/xhr_proxy.php?url=http://dribbble.com/players/interactivethings', function(data){
        if (data) {
          var src = $('.dribbble-img:first img', data).attr('src');
          var title = $('.dribbble-img:first .dribbble-over strong', data).text();
          var shot_link = $('.dribbble-img:first .dribbble-link', data).attr('href');
          // console.log(shot_link);
          var img = $('<img/>', {src: 'http://dribbble.com' + src});
          popup_dribbble.click(function() {
             window.location = 'http://dribbble.com' + shot_link; 
          }).addClass('clickable');
          popup_dribbble.children('p').append(img.addClass('preview')).append(title);
          popup_dribbble.appendTo('#content');
        };          
      });
    })();
    
    
  // TOGGLE POPUPS
    
    $.fn.hideAfterDelay = function() {
      return this.each(function() {
        var el = $(this);
        window.setTimeout(function() {
          if(!el.data('preventHide')) {
            el.animate({top: el.data('origin'), opacity: 'hide'}, 200);
          }
        }, 500);
      });
    };
    
    $('a.toggle_popup').hover(function() {
      var popup = $('#popup_' + $(this).attr('id'));
      popup.data('preventHide', true);
      if (popup.is(':visible')) return;
      $('div.popup').hide();
      var origin = $(this).position().top + $(this).outerHeight() + 5;
      popup.data('origin', origin)
      .css({top: origin, left:$(this).position().left -21 })
      .animate({top: origin + 5, opacity: 'show'}, 200);
    }, function() {
      $('#popup_' + $(this).attr('id')).data('preventHide', false).hideAfterDelay();      
    });
    
    $('.popup').live('mouseenter', function() {
      $(this).data('preventHide', true); 
    });
    $('.popup').live('mouseleave', function() {
      $(this).data('preventHide', false).hideAfterDelay(); 
    });
});