var section = document.createElement("section"); // Temp nasty shim for section element

$.fn.add_markedit_toolbar_unless_present = function(element){
  if ( $(element).find('.markedit').length < 1 ) {
    element.find('#new_comment textarea#comment_body').markedit(
      {'toolbar': { 'layout': 'bold italic quote numberlist bulletlist heading'}
    });
  }
};
  
$(document).ready(function(){

  try {
  // invite IE 7 and IE 6 user to install Chrome Frame
  site_name   = new String(window.location.protocol + '//' + window.location.host);
  prompt_page = new String(site_name + '/install_chrome_frame');
  CFInstall.check({
       mode:        "overlay",
       url:         prompt_page,
       destination: site_name
     });
  }
  catch(e) {}   
  
  $('.comments').hide();
  $('.show_comments').click(function(){
    $(this).toggleClass('up');
    var these_comments = $(this).parent().next('.comments');
    $(these_comments).animate({height: "toggle"}, 150);
    $().add_markedit_toolbar_unless_present($(these_comments));
    return false;
  });

  // Show the comments if answer hash linked

  if (window.location.hash.split('_')[0] == '#answer') {
    $(window.location.hash).find('.comments').animate({height: "toggle"}, 250);
  }
  
  // Setup User Profile Subject Subscriptions Show Toggle
  
  $('#users .dashboard_header .subject_subscriptions dd').append('<span class="pointer"></span>')
  
  $('#users .dashboard_header .subject_subscriptions dt.spinButton a.show').click(function(){
    $('#users .dashboard_header .subject_subscriptions dd').toggle();
    $(this).toggleClass('hide');
  });

  // Hide flash message container and form error messages
  $('#flash').hide();
  
  // Check for flash errors
  
  if ($('#flash').length > 0) {
    flashType = $('#flash div').attr('id');
    if (flashType == 'error') {
      stayType = true;
    } else {
      stayType = false;
    }
    $.noticeAdd({
     text: $('#flash div').text(),
     stay:stayType,
     type: flashType
    });
  }
  
  if(!$("input[name='user[tutor]'][type=checkbox]").is(':checked')) {
    $('span.tutor_highlight').show();
    $('span.subject_highlight').hide();
    $('.tutor_only').hide();
  } else {
    $('span.tutor_highlight').hide();
    $('span.subject_highlight').show();
    $('.tutor_only').show();
  }
  
  $("input[name='user[tutor]'][type=checkbox]").click(function(e) {
    if ($("input[name='user[tutor]'][type=checkbox]").is(':checked')) {
      $('.tutor_only').slideDown(function(){
        $('span.subject_highlight').fadeIn();
      });
      $('span.tutor_highlight').fadeOut();
    } else {
      $('.tutor_only').slideUp();
      $('span.subject_highlight').hide();
      $('span.tutor_highlight').fadeIn();
    }
  });
  
  // Question body preview toggle
  
  $('td.question_text div.full').each(function(i,e){
    e = $(e);
    e.children().wrapAll('<div class="sized" />');
    e.css('height', 54).css('padding-right', '18px');
  });

  $('td.question_text div.full div.sized').each(function(i,e){
    e = $(e);
    if (e.height() > e.parent().height()) {
      e.append('<a class="toggle down" href="javascript:;">more</a>');
    }
  });

  $('td.question_text div.full div.sized a.toggle').live('click',function(){
    e = $(this);
    if (e.text() == 'more') {
      e.text('less').removeClass('down').addClass('up');
      e.parent().parent().animate({
        height: e.parent().height()
      },150);
    } else {
      e.text('more').removeClass('up').addClass('down');
      e.parent().parent().animate({
        height: '54px'
      },150);
    }
  });
  
  
  // update the dates with a pretty date formatter
  $("abbr").timeago();


  // latex to html conversion and markedit WYSIWYG editor
  // ----------------------------------------------------
  
  // apply the markedit wysiwyg editor to the ask a question page
  $('#new_question textarea#question_body').markedit({'toolbar': {
      'layout': 'bold italic quote numberlist bulletlist heading'
    }
  });
  
  $('#new_answer textarea#answer_body').markedit({'toolbar': {
      'layout': 'bold italic quote numberlist bulletlist heading'
    }
  });
  
  $('form#new_question').submit(function(){
    var original = $(this).find('#question_body').val();
    $(this).find('#question_body').val($.convert_latex_to_markdown(original));
  });

  $('form#new_answer').submit(function(){
    var original = $(this).find('#answer_body').val();
    $(this).find('#answer_body').val($.convert_latex_to_markdown(original));
  });

  $('form#new_comment').submit(function(){
    var original = $(this).find('#comment_body').val();
    $(this).find('#comment_body').val($.convert_latex_to_markdown(original));
  });
});

