function log(msg) {
  if (window._rails_env == 'development')
    console.log(msg);
}

function track(page) {
  if (typeof window._pageTracker != 'undefined') {
    try {
      window._pageTracker._trackPageview(page);
      log('trackPageview(' + page + ')');
    }
    catch(e) {
      log('trackPageview(' + page + '): ' + e.message);
    }
  }
  else {
    log('trackPageview(' + page + '): analytics disabled');
  }
}

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function tb_behaviors(element) {
  $('.class-player', element).each(function(){
    var cp_url = $('.class-player-movie', this).attr('href');
    var cp_id = $(this).attr('id').replace(/class-player-/, '');
    var mc_id = $(this).parents('.player-wrapper').attr('id').replace(/class-/, '');
    $f(this, '/flash/flowplayer.swf', {
        playlist: [ 
            {     
                url: cp_url, 
                autoPlay: true, 
                onStart: function(clip) {
                  $('#class-participation-' + cp_id + ' .im-done').show();
                  track('/meditation-classes/' + mc_id + '/start');
                },
                onFinish: function(clip) {
                  track('/meditation-classes/' + mc_id + '/end');
                  tb_remove();
                  dashboard.onClassComplete(cp_id);
                }
            } 
        ] 
    }).onCuepoint(
      [
        {time: 30000, page: '/meditation-classes/'+mc_id+'/30sec'},
        {time: 1 * 60000, page: '/meditation-classes/'+mc_id+'/1min'},
        {time: 2 * 60000, page: '/meditation-classes/'+mc_id+'/2min'},
        {time: 3 * 60000, page: '/meditation-classes/'+mc_id+'/3min'},
        {time: 4 * 60000, page: '/meditation-classes/'+mc_id+'/4min'},
        {time: 5 * 60000, page: '/meditation-classes/'+mc_id+'/5min'},
        {time: 10 * 60000, page: '/meditation-classes/'+mc_id+'/10min'},
        {time: 15 * 60000, page: '/meditation-classes/'+mc_id+'/15min'}
      ],
      function(clip, cuepoint) {
        track(cuepoint.page);
      }
    );
    $f(this).load();
  });
  $('#TB_closeAjaxWindow').pngFix();
}

function dashboardClass() {}

dashboardClass.prototype = {
  onClassComplete: function(cp_id) {
    $('#class-participation-' + cp_id + ' .new-comment').slideDown('normal', function(){
      $('#class-participation-' + cp_id + ' .new-comment textarea').focus();      
    });
    $('#class-participation-' + cp_id + ' .im-done').hide();
  },
  toggleCommentForm: function(link) {
    var $comment_form = $(link).parents('.class-participation').find('.new-comment');
    if ($comment_form.is(':visible')) {
      $comment_form.slideUp();
      $(link).text('Add comment');
    }
    else {
      $comment_form.slideDown();
      $(link).text('Cancel');
    }
    return false;
  },

  toggleComments: function(link) {
    var $comments = $(link).parents('.class-participation').find('.comments-container');
    if ($comments.is(':visible')) {
      $comments.slideUp();
      $(link).text($comments.find('.comment').length + ' comments');
    }
    else {
      $comments.slideDown();
      $(link).text('Hide Comments');
    }
    return false;
  },

  toggleClassBody: function(link) {
    var $cb = $(link).parents('.class-participation').find('.class-body');
    if ($cb.is(':visible')) {
      $cb.slideUp();
    }
    else {
      $cb.slideDown();
    }
    $(link).blur();
    return false;
  }


}

if (typeof dashboard == 'undefined')
  dashboard = new dashboardClass();
  
$(document).ready(function(){
  $(document).pngFix();

  // Make body longer if there is not much content on the page
  var viewportHeight = $(window).height();
  var pageHeight = $("body>.container").height();
  var spacerHeight = viewportHeight-pageHeight;
  //log('viewportHeight:'+viewportHeight+', pageHeight:'+pageHeight+', spacerHeight:'+spacerHeight);
  if (spacerHeight < 1000 && spacerHeight > 0)
    $('#spacer').css('height', spacerHeight + 'px');
  
  // Expand/collapse classes
  $('.class-participation .class-title a,.class-participation .comments-count a').bind('click', function(){
    return dashboard.toggleClassBody(this);
  });
  
  // Expand a class if a comment was just added
  $('.class-participation .added-comment').parents('.class-participation').find('.class-body').slideDown();
  
  // Link that user can click when they complete a class, opening the 'feedback' box, if it doesn't open automatically
  $('.class-participation .im-done a').bind('click', function(){
    var cp_id = $(this).parents('.class-participation').attr('id').replace(/class-participation-/, '');
    dashboard.onClassComplete(cp_id);
    return false;
  });
  $('form#let_me_know input[@name="let_me_know"]').bind('change', function(){
    $('#let_me_know_check').hide();
    $('#let_me_know_spinner').show();
    $.post(
      '/home/let_me_know', 
      {
        authenticity_token: window._token,
        let_me_know: this.value
      },
      function(){
        $('#let_me_know_check').show();
        $('#let_me_know_spinner').hide();        
      }
    );
  });
  $('a#take_survey').bind('click', function(){
    $('#take_survey_check').show();
    $.get('/home/take_survey');
  });
});
