/*


                  ________        .__.__  .__
                  \______ \_______|__|  | |  |
                   |    |  \_  __ \  |  | |  |
                   |    `   \  | \/  |  |_|  |__
                  /_______  /__|  |__|____/____/
                          \/
                             ___  ___
                             \  \/  /
                              >    < 
                             /__/\_ \
                                   \/
      ____  __.                                     __   .__
     |    |/ _|____ __  _  _______    ___________  |  | _|__|
     |      < \__  \\ \/ \/ /\__  \  /  ___/\__  \ |  |/ /  |
     |    |  \ / __ \\     /  / __ \_\___ \  / __ \|    <|  |
     |____|__ (____  /\/\_/  (____  /____  >(____  /__|_ \__|
             \/    \/             \/     \/      \/     \/
   _____          __    _________                __
  /  _  \________/  |_  \_   ___ \  ____   _____/  |_  ___________
 /  /_\  \_  __ \   __\ /    \  \/_/ __ \ /    \   __\/ __ \_  __ \
/    |    \  | \/|  |   \     \___\  ___/|   |  \  | \  ___/|  | \/
\____|__  /__|   |__|    \______  /\___  >___|  /__|  \___  >__|
        \/                      \/     \/     \/          \/




*/



var Hamlet = {

  translationWavePanel: null,
  onScrollTimer: null,
  
  switchTranslationWave: function(waveId) {
    $("#translationWaves li a[href=/#/translation/" + waveId + "]").parent("li").addClass("active").siblings("li").removeClass("active");
    
    if (!Hamlet.translationWavePanel) {
      var translationWaveFrame = document.getElementById("translationWaveFrame");
      var embedOptions = {
        target: translationWaveFrame,
        bgcolor: "#ffffff",
        header: false,
        footer: false,
        toolbar: true
      }
      Hamlet.translationWavePanel = new google.wave.WavePanel(embedOptions);
    }
    if (!$("a[href=/#/translation/" + waveId + "]").hasClass("active")) {
      Hamlet.translationWavePanel.loadWave("googlewave.com!" + waveId);
    }
  },
  
  updateLastOpenedTranslationWave: function() {
    $.getJSON("/translation/wave/last-opened", {}, function(rsp) {
      if (!$("a[href=/#/translation/" + rsp.waveId + "]").length) {
        $("<li><a href=\"/#/translation/" + rsp.waveId + "\">" + rsp.title + "</a></li>")
          .appendTo("ul#translationWavesList")
          .hide().slideDown();
      }
      if (!$("a[href=/#/translation/" + rsp.waveId + "]").parent("li").hasClass("lastOpened")) {
        $("a[href=/#/translation/" + rsp.waveId + "]").parent("li").addClass("lastOpened").siblings("li").removeClass("lastOpened");
        if (!$("#translationWaves li.active").length) {
          Hamlet.switchTranslationWave(rsp.waveId);
        }
      }
      if (rsp.isLive) {
        $("#translationWaves").addClass("live");
      } else {
        $("#translationWaves").removeClass("live");
      }
    });
  },
  
  goDeeplink: function(hash) {
    if (hash == "#/") {
      deeplink = "";
      scrollTarget = $("body");
    } else {
      scrollTarget = $("#" + hash.substr(2).split("/")[0]);
    }
    $.scrollTo(scrollTarget, 1000, {easing: "easeOutExpo", onAfter: function() {
      var waveId = hash.split("/translation/")[1];
      if (waveId) {
        $("#translationWaves li a[href=/#/translation/" + waveId + "]").click();
      }
    }});
  },
  
  onScroll: function() {
      if (($("#about").offset().top - $(window).scrollTop()) < $(window).height() * 0.3) {
        $("#siteNav li.about").addClass("active").siblings("li").removeClass("active");
        $(".bg").not("#bgAbout").fadeOut("normal");
        $("#bgAbout").fadeIn("normal");
        $("title").text('About - A Long Way to “90 min. HAMLET”');
        window.location.hash = $("#about h2 a").attr("href").substr(2);
      } else if (($("#people").offset().top - $(window).scrollTop()) < $(window).height() * 0.3) {
        $("#siteNav li.people").addClass("active").siblings("li").removeClass("active");
        $(".bg").not("#bgPeople").fadeOut("normal");
        $("#bgPeople").fadeIn("normal");
        $("title").text('People - A Long Way to “90 min. HAMLET”');
        window.location.hash = $("#people h2 a").attr("href").substr(2);
      } else if (($("#translation").offset().top - $(window).scrollTop()) < $(window).height() * 0.3) {
        $("#siteNav li.translation").addClass("active").siblings("li").removeClass("active");
        $(".bg").not("#bgTranslation").fadeOut("normal");
        $("#bgTranslation").fadeIn("normal");
        $("title").text('Translation - A Long Way to “90 min. HAMLET”');
        if (-1 == window.location.href.indexOf("/#/translation/")) {
          window.location.hash = $("#translation h2 a").attr("href").substr(2);
        }
      } else {
        $("#siteNav li.top").addClass("active").siblings("li").removeClass("active");
        $(".bg").not("#bgTop").fadeOut("normal");
        $("#bgTop").fadeIn("normal");
        $("title").text('A Long Way to “90 min. HAMLET”');
        window.location.hash = "/";
      }
      return false;
  },
  
  init: function() {
    
    $(window).scroll(function() {
      clearTimeout(Hamlet.onScrollTimer)
      Hamlet.onScrollTimer = setTimeout("Hamlet.onScroll()", 100);
    });
    
    $("#siteNav li a").add(".sectionHeading a").click(function() {
      if (!$(this).parent("li").hasClass("active")) {
        $(this).parent("li").addClass("active").siblings("li").removeClass("active");
        window.location.hash = $(this).attr("href").substr(2);
        Hamlet.goDeeplink(window.location.hash);
      }
      return false;
    });
    
    $("#shortAboutMore").click(function() {
      window.location.hash = $(this).attr("href").substr(2);
      Hamlet.goDeeplink(window.location.hash);
      return false;
    });
    
    $(".helpToggle").toggle(function() {
      $(this).addClass("open").siblings(".help").slideDown("normal");
      return false;
    }, function() {
      $(this).removeClass("open").siblings(".help").slideUp("normal");
      return false;
    });

    $('#translationWavesList li a').live("click", function() {
      if (!$(this).parent("li").hasClass("active")) {
        var waveId = $(this).attr("href").split("/translation/")[1]
        Hamlet.switchTranslationWave(waveId);
        window.location.hash = $(this).attr("href").substr(2);
      }
      return false;
    });
    
    $('#tbox').jTweetsAnywhere({
      showTweetBox:{
        counter: true,
        width: 250,
        height: 115,
        label: '<span style="color: #000000">Any thoughts?</span>',
        defaultContent: '#90minHAMLET #90minHAMLET_tr http://bit.ly/b3lnTR',
        onTweet: function(textTweet, htmlTweet){
          alert('Tweet しました。Thank you!');
        }
      }
    });
    
    $("#translationTimelineNav .everyone a").click(function() {
      $(this).parent("li").addClass("active").siblings("li").removeClass("active");
      $("#translationTweets h4").html("みんなの <a href=\"http://twitter.com/#search?q=%2390minHAMLET_tr\">#90minHAMLET_tr</a>");
      $("#timeline").html("");
      $("#timeline").jTweetsAnywhere({
        searchParams: ['q=%2390minHAMLET_tr'],
        count: 100
      });
      return false;
    });
    $("#translationTimelineNav .author a").click(function() {
      $(this).parent("li").addClass("active").siblings("li").removeClass("active");
      $("#translationTweets h4").html("長島の <a href=\"http://twitter.com/#search?q=%2390minHAMLET_tr\">#90minHAMLET_tr</a>");
      $("#timeline").html("");
      $("#timeline").jTweetsAnywhere({
        searchParams: ['from=textdub', 'q=%2390minHAMLET_tr'],
        count: 100
      });
      return false;
    });
    $("#translationTimelineNav .favorite a").click(function() {
      $(this).parent("li").addClass("active").siblings("li").removeClass("active");
      $("#translationTweets h4").text("copy[Kaku's fav tweets]");
      $("#timeline").html("");
      $("#timeline").jTweetsAnywhere({
        searchParams: [''],
        count: 100
      });
      return false;
    });
    $("#translationTimelineNav .up a").click(function() {
      $("#timeline").scrollTo({top: '-=300px', left: "0"}, 1000, {easing: "easeOutExpo"});
      return false;
    });
    $("#translationTimelineNav .down a").click(function() {
      $("#timeline").scrollTo({top: "+=300px", left: "0"}, 1000, {easing: "easeOutExpo"});
      return false;
    });
    
    if (!window.location.hash) {
      window.location.hash = "/";
    }
    
    if ("#/" == window.location.hash)
    {
      $("#siteNav li.top").addClass("active");
    }
    
    var imagesPreloaded = ["/images/bgs/top.jpg", "/images/bgs/translation.jpg", "/images/bgs/people.jpg", "/images/bgs/about.jpg"]
    for(var i = 0; i < imagesPreloaded.length; i++) {
      $("<img>").attr("src", imagesPreloaded[i]);
    }
    
    twttr.anywhere(function (T) {
      T('#shortAboutFollowButton').followButton("90minHAMLET");
    });
    
    setTimeout("Hamlet.updateLastOpenedTranslationWave()", 2000);
    setInterval("Hamlet.updateLastOpenedTranslationWave()", 5000);
    
    swfobject.embedSWF("http://www.ustream.tv/flash/live/1/4663544", "translationUstreamEmbed", "250", "180", "9.0.115", "expressInstall.swf", {
      autoplay: "true",
      brand: "embed",
      cid: "4663544",
      locale: "ja_JP"
    }, {bgcolor: "#000000"});
    
    $("#translationTimelineNav .everyone a").click();
    Hamlet.goDeeplink(window.location.hash);
  }
}


$(function() {
  Hamlet.init();
});
