$(document).ready(function() {
  
  $.humanize();
  
  imagePreview();
  
  $('.panda_video').flash({
      src: 'http://tra.pandastream.com/flvplayer.swf',
      width: 320,
      height: 240
  });
  
  // $('a[@href$="mp3"]').flash(
  //       { src: '/flash/player.swf', height: 20, width: 100 },
  //       { version: 7 },
  //       function(htmlOptions) {
  //           $this = $(this);
  //           htmlOptions.flashvars.file = $this.attr('href');
  //           $this.before($.fn.flash.transform(htmlOptions));           
  //       }
  //   );
  
  
  // confirm boxes
  $('.confirm').click(function() {
    return confirm('Are you sure?');
  });
  
  $('.date').datepicker({
   showAnim: "fadeIn", 
   firstDay: 1, 
     changeFirstDay: false, 
     dateFormat: $.datepicker.W3C, 
     showOn: "both", 
     buttonImage: "/images/css/calendar.png", 
     buttonImageOnly: true 
   });
   
  $('a[rel*=facebox]').facebox();

  // bg_picker
  // $.cookie('groupthing_style', 'bg06');
  
  $('#bg_picker a').click(function(){
    var bg_class = $(this).find("img").attr("src").match(/\d+/)[0];
    bg_class = "bg" + bg_class;
    $('body').removeClass("bg00 bg01 bg02 bg03 bg04 bg05 bg06").addClass(bg_class);
    $.cookie('groupthing_style', bg_class, {expires: 1000});
    return false;
  });
  
  // set external links to open in new window
  $(document).ready(function() {
    $("a[@rel^=external]").each(function() {
      if(this.href.indexOf(location.hostname) == -1) {
        $(this).click(function(){window.open(this.href);return false;});
      }
    });
  });
  
  // This is a horrible hack because nicEdit was putting font tags inside
  // object tags on blog posts and safari doesn't like it
  if($.browser.safari) {
    $('div.post_body object').each(function(){
      $(this).after($(this).find('font')); // Move font tags after object tags, not inside
    });
  }

  
	// General editable class
	// HTML should be like (leaving out irrelevant stuff):
	//
	//  <form class="js_toggle_edit">
	//    <a class="js_te_edit_link"...>blah...</a>
	//    <div class="js_te_show">
	//      This is shown first of all...
	//    </div>
  //    <div class="js_te_edit">
	//      This is shown when edit link clicked...
	//    </div>
	//    <a class="js_te_cancel_link"...>blah...</a> ...
	//  </form>
	var switchToShowMode = function(container){
	  container.
	    find('.js_te_show, .js_te_edit_link').show().end().
	    find('.js_te_edit, .js_te_cancel_link').hide();
  };
	$('.js_toggle_edit .js_te_edit_link').click(function() {
	  $(this).parents('.js_toggle_edit').eq(0).
	    find('.js_te_show, .js_te_edit_link').hide().end().
	    find('.js_te_edit, .js_te_cancel_link').show();
		return false;
	});
	$('.js_toggle_edit .js_te_cancel_link').click(function() {
	  switchToShowMode( $(this).parents('.js_toggle_edit').eq(0) );
		return false;
	});
	switchToShowMode($('.js_toggle_edit'));

});