/* Author: Øyvind Smestad os@ixd.no
 */


jQuery(document).ready( function ($) {
  // Handle fixed sometimes design, not on touch devices
  if ($('html.touch').length === 0) {
    $('#audience').addClass('sticky');
  }

  $("#tweetFeed").tweet({
    query: "#frontend2011",
    join_text: "auto",
    count: 16,
    refresh_interval: 30,
    loading_text: "loading tweets..."
  });

  $('#share').click(function (e) {
    return false;
  });

  $('body').socialmodal({
    uniqueName: 'sharebuttom',
    title: 'Share',
    subtitle: 'Share this video',
    count: 0,
    manualClose: true,
    onclick: '#share',
    callback: function(success) {
      //window.location.href = '#';
    },
  });


 /* $.waypoints.settings.scrollThrottle = 50;
  $('#footer').waypoint( function (event, direction) {
    $('#audience').toggleClass('sticky', direction === "up");
  }, {
    offset: $.waypoints('viewportHeight')
  });*/

  // Cookie handling / design switching
  var config = {
    cookieName: 'visitCount',
    cookieSettings: {
      expires: 7,
      path: '/'
    }
  },
  currentState = $.cookie(config.cookieName);
  if ( !currentState ) {
    currentState = 1;
  } else {
    currentState = parseInt(currentState, 10) + 1;
  }
  $.cookie(config.cookieName, currentState /*, config.cookieSettings*/);
  //console.log(currentState);
  for (var i = 1; i <= currentState; i++) {
    $('#audience .a' + i).removeClass('hidden');
    $('#lightingrig .o' + i).removeClass('hidden');
  }

  // Fake fixed positioning for IOS devices (currently triggers on all touch devices)
  /*$('body').bind('touchstart', function (e) {
    $('#audience')
      .addClass('hidden')
      .css('bottom', 'auto');
  });

  // Trigger this when the window stops moving/scrolling, not when the user
  //  stops touching (which can happen earlier).
  if ($('html.touch').length) { // touch is supported Modernizr
    $(window).bind('scroll', function () {
      var viewportBottom = $(window).scrollTop() + $(window).height();
      $('#audience')
        .css('top', viewportBottom)
        .removeClass('hidden')
        .animate({top: viewportBottom - $('#audience').height()}, 250);
    });
  }*/

  // Toggler for show hide content
  // Init
  $('.less').addClass('hidden');
  $('.toToggle').addClass('visuallyhidden');
  // Event handling
  $('body').delegate('.more, .less', 'click', function(e){
    e.preventDefault();
    $(this).parent().find('.less, .more').toggleClass('hidden');
    $(this).parent().find('.toToggle').toggleClass('visuallyhidden');
  });

  // Open talk details when opening page with hash in url
  if (document.location.hash) {
    $(document.location.hash).next().find('.more').click();
    
    // Make sure scroll is correct after toggling of content
    $(document).scrollTop($(document.location.hash).offset().top);
  }

  // Animate balloons on page load
  $('#balloons').addClass('anim');

  // Switch to string representation of date
  $(".date").prettierFutureDate();

  // Add animation to hash-links
  $('.links a').click(function (e) {
    e.preventDefault();
    var link = e.currentTarget;
    link.blur();
    $('html,body').animate({scrollTop: $(link.hash).offset().top}, 800, 'easeOutCubic',function(whereTo){
      window.location.hash = link.hash;
      /*if (pageTracker) {
        pageTracker._trackPageview(whereTo);
      }*/
    });
  });

  // Hide email from some spiders
  ixdHideEmails();

  // Temp balloon hacking
  // For this to work we would need the output canvas to be behind everything (so clicks would end up on correct elements)
  //  and everything would have to be slightly transparent. Also this code runs way too slow, at least in FF4.
 /* var test = $('<canvas id="output_canvas">');
  test[0].width = $(window).width();
  test[0].height = $(window).height();
  test.appendTo('body');
  init(); */

});


var ixdHideEmails = function() {
  if (!document.getElementById || !document.createElement) {
    return;
  }

  var links = document.getElementsByTagName('a');

  for (var i = 0, link = null, email = null, em = null; (link = links[i]); i++) {
    if (link.rel == 'email') {
      em = jQuery.trim(link.innerHTML).split(' a ');
      email = em[0] + '@' + em[1];
      link.href = 'mailto:' + email;
      link.innerHTML = email;
    }
  }
};

