// remi.org

// need to update to return self, for method chaining
jQuery.fn.update_with_latest_tweet = function(user) {
  var url = 'http://twitter.com/status/user_timeline/' + user + '.json?count=1&callback=?';
  var obj = this;
  if (/localhost/.test(window.location)) {
    obj.text('DISABLED FOR LOCALHOST');
  } else {
    $.getJSON(url, function(data) { 
      obj.text(data[0].text);
    });
  }
}
      
function switch_videos_to_ogg(){
  $('video').each(function(){
    var src       = $(this).attr('src').replace(/\.mp4$/, '.ogv');
    
    // make a whole new element
    var new_video = $(this).clone();
    new_video.attr('src', src);

    $(this).replaceWith(new_video);
  });
}

function switch_videos_to_mp4(){
  $('video').each(function(){
    var src       = $(this).attr('src').replace(/\.ogv$/, '.mp4');
    
    // make a whole new element
    var new_video = $(this).clone();
    new_video.attr('src', src);

    $(this).replaceWith(new_video);
  });
}

$(function(){

  // if ($.browser.mozilla == true){ switch_videos_to_ogg(); }

  $('ul.posts li.post>h1>a').click(function(){
    var link = $(this);
    var li   = link.parents('li');
    var post = li.find('div.post');
    var href = link.attr('href');

    if (! post.length){
      var loading = $('<span class="loading">loading ...</span>').appendTo(li);
      post        = $('<div class="post">post</div>').css('display', 'none').appendTo(li);
      post.load(href + ' div.post>*', function(){
        post.append('<a class="comments" href="' + 'http://remi.org' + href + '#disqus_thread">View Comments</a>');
        $.disqusLinks('remi');
        loading.fadeOut();
        link.click(); // call this function again, now that the post has been loaded
      });

    } else {
      post.slideToggle();
    }

    return false;
  });

  // $('a.tweet').update_with_latest_tweet('remitaylor');

  function finish_devfu_hover(element){
    element.finished_animations++;
    if (element.finished_animations >= 3){
      element.finished_animations = 0;
      element.hovering = false;
    }
  }

  $('a.devfu img').hover(function(){
    // don't fire off another hover call until the first one is finished
    var element = $(this).parent('a.devfu')[0];
    if (element.hovering != true){
      element.hovering = true;
      element.finished_animations = 0;
      $(this).parent('a.devfu').animate({ left: '115px'             }, function(){ finish_devfu_hover(element); });
      $(this).animate({ opacity: 1.0  }, function(){ finish_devfu_hover(element); });
      $(this).parent('a.devfu').find('span').animate({ opacity: 1.0 }, function(){ finish_devfu_hover(element); });
    }
  },function(){
    $(this).animate({ opacity: 0.08 });
    $(this).parent('a.devfu').find('span').animate({ opacity: 0 });
    $(this).parent('a.devfu').animate({ left: '90px' }); // go back to being hidden
  });

  // when you double click on a code example, remove the line numbers
  //
  // TODO I'd like to toggle them but ... even if they're hidden, they're selected if you copy the code
  //
  $('pre.highlight').dblclick(function(){
    $(this).find('span.lnr').remove();
  });

  // if we're on a post page and it has an embedded flash object 
  // make the post div bigger so it's big enough to show the object
  if ( $('div.post embed').length >= 1 ) {
    var width_of_embedded_flash_element = $('div.post embed')[0].width;
    var width_of_div = $('div.post').width();
    if ( width_of_embedded_flash_element > width_of_div )
      $('div.post').css({ width: width_of_embedded_flash_element + 'px' });
  }

  // tweek for the camping screencast (which uses a custom embedded flash player)
  if (window.setupSeekBar != undefined) {
    setupSeekBar();
    setTimeout(function() {
      $('button')[0].click(); // pause by clicking the generated button
    }, 200);
  }

  // track all external clicks
  trackpageview.track_all_external_links();

  function update_cart_total(){
    var total = 0.0;
    $('div.cart input[type=text]').each(function(){
      var value = parseFloat($(this).val());
      if (! isNaN(value))
        total = total + value;
    });
    $('div.cart .total .value').text('$' + total.toFixed(2));
  }

  $('div.cart input[type=text]').blur(update_cart_total);
  $('div.cart input[type=text]').change(update_cart_total);

  selected_cart_field      = null;
  last_selected_cart_field = null;

  $('div.cart input[type=text]').focus(function(){
    last_selected_cart_field = $(this);
  })

  function next_cart_field(change_next){
    // the user manually selected a field
    if (last_selected_cart_field != null){
      selected_cart_field = last_selected_cart_field;
      last_selected_cart_field = null;
      return selected_cart_field;
    }

    if (selected_cart_field == null){
      selected_cart_field = $('div.cart input[type=text]').first();
      return selected_cart_field;
    } else {
      var next           = null;
      var last_was_match = false;
      $('div.cart input[type=text]').each(function(){
        if (next == null){
          if (last_was_match == true) {
            next = $(this);
          } else if ($(this).attr('name') == selected_cart_field.attr('name')) {
            last_was_match = true; // we found the last field, so the next one is it
          }
        }
      });
      if (next == null && last_was_match)
        next = $('div.cart input[type=text]').first();
      if (change_next != false)
        selected_cart_field = next;
      return next;
    }
  }

  $('.cart ul.price_items li').click(function(){
    var price = parseFloat($(this).attr('data-price'));
    next_cart_field().val(price.toFixed(2));
    next_cart_field(false).focus();
    update_cart_total();
  });

  $('.message').each(function(){
    var message = $(this);
    // wait a second after the page loads, then highlight and fade out the message
    setTimeout(function(){ message.effect("highlight", {}, 1500); }, 1000);
    setTimeout(function(){ message.fadeOut(1500);                 }, 1500);
  });

  var sni_ssl_info_toggle_link = $('<a href="#">view security options</a>').css({ display: 'block', 'padding-top': '1em' }).click(function(){
    var config = $('.sni_ssl_option .config');
    config.toggle();
    if (config.css('display') == 'block')
      $(this).text('hide security options');
    else
      $(this).text('view security options');
  });
  $('.sni_ssl_option .config').after(sni_ssl_info_toggle_link);

  var user_agent = navigator.userAgent.toLowerCase();
  var is_chrome  = (user_agent.indexOf("chrome")  > 0);
  var is_windows = (user_agent.indexOf("windows") > 0);
  var is_mac     = (user_agent.indexOf("mac")     > 0);

  if (is_chrome && (is_windows || is_mac)){
    $('.sni_ssl_option .incompatible').show();
    $('.sni_ssl_option .config #sni_ssl').attr('checked', false);
  }

});
