// Make sure the behaviors still work even after navigating to another page using the ajax navigation.
Event.addBehavior.reassignAfterAjax = true;

/////////////////////////////////////////////////////////////////
// Global behaviours : index (display_variations, destroy_var) //
Event.addBehavior({
  'body' : function() {
  }
});

//////////////////////////////////////////////////////////////////
// Front behaviours
Event.addBehavior({
  'a.listen:click' : function(e){
    Effect.toggle('front', 'blind', { duration: 0.5 });
    Element.toggleClassName('listen', 'close');
    e.stop();
  },
  
  '#back:click' : function(e) {
    Effect.toggle('front', 'blind', { duration: 0.5 });
    $('listen').toggleClassName('close','listen');
    e.stop();
  },
  
  'a#clip_toggle:click' : function(e){
    Effect.toggle('clip_show');
    e.stop();
  }
  
});

//////////////////////////////////////////////////////////////////
// Admin behaviours :
Event.addBehavior({
  'a.url_key:click' : function(e){
    var id = Global.getUniquePartBetweenEvenmentClassNameAndEvenmentID(e, null);
    Effect.toggle('url_value_'+id, 'blind', { duration: 0.5 });
    e.stop();
  }
});

function tumblrNews(tumblr_api_read) {
    for (var i = 0; i < 6; i++) {
        if (tumblr_api_read.posts[i]['type'] == "regular") {
          var permalink = tumblr_api_read.posts[i]['url'];
          var date = tumblr_api_read.posts[i]['date'].match(/[a-zA-Z]{3}, \d{2} [a-zA-Z]{3} \d{4}/);
          var title = tumblr_api_read.posts[i]['regular-title'];
          var body = tumblr_api_read.posts[i]['regular-body'];
          document.write('<div class=\"entry\"><h2><a href=\"' + permalink + '\">' + title + '</a></h2><div class=\"entry_body\">' + body + '</div><div class=\"entry_date\">' + date + '</div></div>');          
        }
        else if (tumblr_api_read.posts[i]['type'] == "quote") {
          var permalink = tumblr_api_read.posts[i]['url'];
          var date = tumblr_api_read.posts[i]['date'].match(/[a-zA-Z]{3}, \d{2} [a-zA-Z]{3} \d{4}/);
          var quote = tumblr_api_read.posts[i]['quote-text'];
          var source = tumblr_api_read.posts[i]['quote-source'];
          document.write('<div class=\"entry quote\"><blockquote>&ldquo;' + quote + '&rdquo;</blockquote><div class=\"entry_body\">—' + source + '</div><div class=\"entry_date\">' + date + '</div></div>');          
        }
        else if (tumblr_api_read.posts[i]['type'] == "photo") {
          var permalink = tumblr_api_read.posts[i]['url'];
          var date = tumblr_api_read.posts[i]['date'].match(/[a-zA-Z]{3}, \d{2} [a-zA-Z]{3} \d{4}/);
          var photo_src = tumblr_api_read.posts[i]['photo-url-75'];
          var photo_url = tumblr_api_read.posts[i]['photo-link-url'];
          var body = tumblr_api_read.posts[i]['photo-caption'];
          document.write('<div class=\"entry photo\"><div class="left"><a href="' + photo_url + '"><img src="' + photo_src + '" width="45" height="45" alt="Photo Cherry Bloom Blog" /></a></div><h2><a href=\"' + permalink + '\">Photo</a></h2><div class=\"entry_body\">' + body + '</div><div class=\"entry_date\">' + date + '</div></div>');
        }
        else if (tumblr_api_read.posts[i]['type'] == "video") {
          var permalink = tumblr_api_read.posts[i]['url'];
          var date = tumblr_api_read.posts[i]['date'].match(/[a-zA-Z]{3}, \d{2} [a-zA-Z]{3} \d{4}/);
          var video_url = tumblr_api_read.posts[i]['video-source'];
          var body = tumblr_api_read.posts[i]['video-caption'];
          document.write('<div class=\"entry video\"><div class="left"><a href="' + permalink + '"><img src="/images/extras/video.gif" width="45" height="45" alt="Video Cherry Bloom Blog" /></a></div><h2><a href=\"' + permalink + '\">Video</a></h2><div class=\"entry_body\">' + body + '</div><div class=\"entry_date\">' + date + '</div></div>');
        }
    }
}