var App, Listener, ShareContent, ShareEndpoint, View, getFakeData;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
App = {
  init: function() {
    this.embedFlashShowcase();
    this.getAllConversationData();
    return Listener.pageLoad();
  },
  getAllConversationData: function() {
    var hypeConfig, readOptions;
    hypeConfig = {
      readAuthToken: "",
      baseUrl: $("ul#conversation-list").attr('rel'),
      readCallback: App.parseConversationData
    };
    readOptions = {
      aggregate_id: "bestbuyyoutube-0",
      per_page: "50"
    };
    Hype.init('client', hypeConfig);
    return Hype.readSource('feed', readOptions);
  },
  parseConversationData: __bind(function(scope, data, id) {
    var firstPage, remainder;
    firstPage = data.feed_items.slice(0, 5);
    remainder = data.feed_items.slice(5, data.length);
    App.paginateConversationData(data.feed_items);
    App.chunkFeedItems(data.feed_items);
    return App.renderConversationData(firstPage);
  }, this),
  renderConversationData: function(data) {
    var target;
    target = $("#view-conversation ul#conversation-list");
    target.empty();
    _.each(data, function(item) {
      switch (item._type) {
        case 'FeedItem::FacebookItem':
          if (item.content.message != null) {
            return View.facebookItem(item);
          }
          break;
        case 'FeedItem::TweetItem':
          return View.twitterItem(item);
      }
    });
    return View.formatDate();
  },
  paginateConversationData: function(data) {
    var pageCount;
    pageCount = _.range(data.length / 5);
    return View.paginationItem(pageCount);
  },
  chunkFeedItems: function(data) {
    var pageCount, pages;
    pageCount = data.length / 5;
    pages = chunk(data, 5);
    return Listener.paginator(pages);
  },
  embedFlashShowcase: function() {
    var attributes, flashvars, params;
    flashvars = {};
    params = {
      wmode: 'transparent'
    };
    attributes = {};
    return swfobject.embedSWF('/flash/app.swf', 'flash', '960', '720', '10.0.0', '', flashvars, params, attributes);
  },
  share: function(share) {
    var baseUrl, endpoint, genericMessage, url, videoMessage, videoTitle;
    videoTitle = share.title != null ? share.title : "";
    switch (share.network) {
      case "twitter":
        genericMessage = ShareContent.sharePageTwitter();
        videoMessage = ShareContent.shareVideoTwitter(videoTitle);
        if (share.id != null) {
          endpoint = ShareEndpoint.video(share.id);
        } else {
          endpoint = ShareEndpoint.page();
        }
        baseUrl = "http://twitter.com/intent/tweet?source=bestbuyyoutube&text=";
        break;
      case "facebook":
        genericMessage = ShareContent.sharePageFacebook();
        videoMessage = ShareContent.shareVideoFacebook(videoTitle);
        if (share.id != null) {
          endpoint = ShareEndpoint.video(share.id);
        } else {
          endpoint = ShareEndpoint.page();
        }
        baseUrl = "https://www.facebook.com/sharer/sharer.php?u=" + endpoint + "&t=";
        break;
      case "plus":
        genericMessage = "";
        videoMessage = "";
        baseUrl = "http://plus.google.com";
    }
    if (share.message != null) {
      url = "" + baseUrl + share.message;
    } else if (share.id != null) {
      if (share.network === 'plus') {
        url = "" + baseUrl + videoMessage;
      } else {
        url = "" + baseUrl + videoMessage + " http://youtube.com/?watch=" + share.id;
      }
    } else {
      url = "" + baseUrl + genericMessage;
    }
    return window.open(url, share.network);
  }
};
View = {
  facebookItem: function(item) {
    var target, template;
    target = $("#view-conversation ul#conversation-list");
    template = _.template($("#facebook-item").html());
    return target.append(template(item));
  },
  twitterItem: function(item) {
    var target, template;
    target = $("#view-conversation ul#conversation-list");
    if (item.raw.profile_image_url != null) {
      template = _.template($("#twitter-item").html());
    } else {
      template = _.template($("#twitter-item-alt").html());
    }
    return target.append(template(item));
  },
  formatDate: function() {
    return $("#view-conversation ul#conversation-list li p.item-meta").each(function(i, el) {
      return $(el).text($.format.date($(el).text(), 'MMM dd, yyyy hh:mm a'));
    });
  },
  paginationItem: function(count) {
    var incrementer, target, template;
    target = $("section#pagination ul");
    template = $("#page-item").html();
    _.each(count, function(i) {
      return target.append(template);
    });
    target.find('li:first-child a').addClass('current');
    incrementer = 0;
    return $("section#pagination ul li a").each(function(i) {
      return $(this).attr('rel', incrementer++);
    });
  }
};
Listener = {
  pageLoad: function() {
    return this.navDropdown();
  },
  navDropdown: function() {
    $("nav#utility li.share").hover(function() {
      $(this).find('a').addClass('hover');
      return $(this).find('ul').addClass('drop');
    }, function() {
      $(this).find('ul').removeClass('drop');
      return $(this).find('a').removeClass('hover');
    });
    return $("nav#utility li ul").hover(function() {
      $(this).parent().find('a').addClass('hover');
      return $(this).addClass('drop');
    }, function() {
      $(this).removeClass('drop');
      return $(this).parent().find('a').removeClass('hover');
    });
  },
  paginator: function(pages) {
    return $("section#pagination ul li a").bind('click', function(e) {
      var index;
      e.preventDefault();
      index = $(this).attr('rel');
      $("section#pagination ul li a").removeClass('current');
      $(this).addClass('current');
      return App.renderConversationData(pages[index]);
    });
  }
};
ShareContent = {
  sharePageFacebook: function() {
    return "Check%20out%20the%20new%20%23BestBuy%20YT%20Channel%20and%20get%20more%20from%20your%20technology.";
  },
  sharePageTwitter: function() {
    return "Check%20out%20the%20new%20%23BestBuy%20YT%20Channel%20and%20get%20more%20from%20your%20technology.";
  },
  sharePagePlus: function() {
    return "Check%20out%20the%20new%20%23BestBuy%20YT%20Channel%20and%20get%20more%20from%20your%20technology.";
  },
  shareVideoFacebook: function(title) {
    return "Check%20out%20the%20" + title + "%20video%20on%20the%20new%20%23BestBuy%20YT%20Channel";
  },
  shareVideoTwitter: function(title) {
    return "Check%20out%20the%20" + title + "%20video%20on%20the%20new%20%23BestBuy%20YT%20Channel";
  },
  shareVideoPlus: function(title) {
    return "Check%20out%20the%20" + title + "%20video%20on%20the%20new%20%23BestBuy%20YT%20Channel";
  }
};
ShareEndpoint = {
  page: function() {
    return "http://youtube.com/bestbuy";
  },
  video: function(id) {
    return "http://youtube.com/watch?v=" + id;
  }
};
_.templateSettings = {
  interpolate: /\{\{(.+?)\}\}/g
};
function chunk(a, s){
    for(var x, i = 0, c = -1, l = a.length, n = []; i < l; i++)
        (x = i % s) ? n[c][x] = a[i] : n[++c] = [a[i]];
    return n;
}

function formatDate(date) {
    var d = new Date(date);
    var hh = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();
    var dd = "AM";
    var h = hh;
    if (h >= 12) {
        h = hh-12;
        dd = "PM";
    }
    if (h == 0) {
        h = 12;
    }
    m = m<10?"0"+m:m;

    s = s<10?"0"+s:s;

    /* if you want 2 digit hours: */
    h = h<10?"0"+h:h;

    var pattern = new RegExp("0?"+hh+":"+m+":"+s);
    return date.replace(pattern,h+":"+m+" "+dd)
};
$(document).ready(function() {
  return App.init();
});
getFakeData = function() {
  return $.getJSON('/javascripts/feed.json', function(data) {
    return App.parseConversationData(data);
  });
};
