$(document).ready(function() {
  $('#scroller').moodular({
    dispTimeout: 6000,
    speed: 800
  });
  
  $("#sampleDropper").click(function(){
    $("#dropDownContainer").animate({
      height: "toggle"
    }, 1000)
  });

  $.getJSON("http://www.amazingtunes.com/tune_of_the_day.json?callback=?", function(json){
    $(".tuneOfTheDay .artist").prepend("<a href='" + json.creator.location.html + "'>" + json.creator.name + "</a>");
    $(".tuneOfTheDay .tune").prepend("<a href='" + json.location.html + "'>" + json.title + "</a>");
    $(".tuneOfTheDay .innerContent").prepend("<img src='" + json.image + "' />");
  });

  playing_now_radio();
  
  $.getJSON("http://www.amazingtunes.com/chart.json?callback=?", function(json){
    var entry = json.entries[0];

    $(".chart .artist").prepend("<a href='" + entry.tune.artist.locations.html + "'>" + entry.tune.artist.display_name + "</a>");
    $(".chart .tune").prepend("<a href='" + entry.tune.locations.html + "'>" + entry.tune.title + "</a>");
    $(".chart .innerContent").prepend("<img src='" + entry.tune.image + "' />");
  });

  var posts = [];

  $.getJSON("http://blog.amazingtunes.com/?json=get_recent_posts&callback=?", function(json){
    $.each(json.posts.slice(0,3), function(i, item){
      var obj = item;
      obj.source = "tunes";
      posts.push(obj);
    })

    $.getJSON("http://amazingradio.co.uk/blog?json=get_recent_posts&callback=?", function(json){
      $.each(json.posts.slice(0,3), function(i, item){
        var obj = item;
        obj.source = "radio";
        posts.push(obj);
      })

      posts.sort(function(a, b){
        return Date.compare(Date.parse(b.date), Date.parse(a.date));
      });

      for(x in posts.slice(0,3)){
        var template = "";
        template += "<div class='newsItem " + posts[x].source + "'>";
        switch(posts[x].source){
          case "radio":
            template += "<h5><em><strong>amazing</strong>radio</em></h5>";
            break;
          case "tunes":
            template += "<h5><em><strong>amazing</strong>tunes</em></h5>";
            break;
        }
        template += "<div class='newsItemText'>";
        template += "<p class='newsItemTitle'><a href='" + posts[x].url + "'>" + posts[x].title_plain  + "</a></p>";
        template += "<p>" + posts[x].excerpt.substr(0, 60) + "...<a href='" + posts[x].url + "'>more</a></p>";
        template += "</div>";

        template += "<div class='newsItemDate'>";
        template += "<p>" + Date.parse(posts[x].date).toRelativeTime() + "</p>";
        template += "</div>";
        template += "</div>";

        $("#latestNewsContainer").append(template);
      }
    });
  });
});

function playing_now_radio(){
  $.getJSON("http://www.amazingtunes.com/radio/now_playing.json?callback=?", function(json){
    $("#listenContainer .artist").empty().prepend("<a href='" + json.creator.location.html + "'>" + json.creator.name + "</a>");
    $("#listenContainer .tune").empty().prepend("<a href='" + json.location.html + "'>" + json.title + "</a>");

     $("#listenContainer .artist a").textTruncate();
     $("#listenContainer .tune a").textTruncate();
  });
}

window.setInterval("playing_now_radio()", 15000);

