// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//window.loadFirebugConsole();

function wire_up_datepicker()
{
  Date.firstDayOfWeek = 7;
  Date.format = 'yyyy-mm-dd';
  var allDatepickers = $('input.datepicker');

  jQuery.each(allDatepickers, function () {
    var initDate = ($(this).val() == '' || $(this).val() == '000-00-00') ? new Date().asString() : $(this).val();
    $(this).datePicker();
    //    $(this).datePicker({clickInput:true, createButton:false, startDate: '1996/01/01'}).val(initDate).trigger('change')
  });
}

function wire_up_tablesorter()
{
  $.tablesorter.defaults.widgets = ['zebra'];
  if ($(".tablesorter").length != 0)
  {
    var table = $(".tablesorter:first");
    if ($(table).parent().attr('class') == 'tabbed_content submissions')
      $.tablesorter.defaults.sortList = [[1,0]];
    else
      $.tablesorter.defaults.sortList = [[2,0]];

    // setup table sorter options
    var ts_options = {};
    ts_options['textExtraction'] = function(node)
    {
      if ($(node).children('a').html())
        return $(node).children('a').html();
      else
        return $(node).html();
    };

    // if th has class 'js-no-sort', turn off sorting via table sorter options
    var no_sort_indicies = [];
    $('table.tablesorter thead tr th').each(function(i)
    {
      if ($(this).hasClass('js_no_sort'))
      {
        no_sort_indicies.push(i);
      }
    });

    if (no_sort_indicies.length > 0)
    {
      no_sort_opts = {};
      for (var i = 0; i < no_sort_indicies.length; i++)
      {
        no_sort_opts[no_sort_indicies[i]] = {sorter: false};
      }
      ts_options['headers'] = no_sort_opts;
    }

    //    testing for the existence of
    //    rows in the table before deciding whether or not to apply tablesorter
    //    using jQuery's :has content filter. Fixes tablesorter parse error in ff3

    $(".tablesorter:has(tbody tr)").tablesorter(ts_options);
  }
}

function wire_up_tabs()
{
  var all_tabs = $('div.tabs').find('a');
  $(all_tabs).click(function(e) {
    e.preventDefault();
    $(all_tabs).removeClass('selected');
    var table_to_show = $(this).attr('class');
    $(this).addClass('selected');
    $('div.tabbed_content table').hide();
    //    console.log($('div.tabbed_content table.'+table_to_show));
    $('div.tabbed_content table.' + table_to_show).show();
  });
}

function wire_up_wysiwyg()
{
  $('textarea.wysiwyg').each(function() {
    new punymce.Editor({
      id : this.id,
      min_width: 500,
      height: 200,
      toolbar : 'bold,italic,underline,increasefontsize,decreasefontsize,ul,ol,indent,outdent,left,center,right,style,removeformat'
    });
  });
}

function wire_up_media_picker() {
  if ($('#media_picker').length == 0) return;

  $('#add_image').jqm({ modal: true, trigger: 'a.add_image' });
  $('#add_video').jqm({ modal: true, trigger: 'a.add_video' });


  $('input.featured_check_box').click(function(event) {
    if (event.target.checked) {
      $(event.target).parents('li').find('input.selected_check_box').each(function() {
        this.checked = true;
      });
    }
    $('input.featured_check_box').each(function() {
      if (this != event.target) this.checked = false;
    });
  });

  $('input.selected_check_box').click(function(event) {
    if (!event.target.checked) {
      $(event.target).parents('li').find('input.featured_check_box').each(function() {
        this.checked = false;
      });
    }
  });
}

function wire_up_featured_media() {

  var thumbnail_anchors = $('#featured_media').find('ul li a');
  if (thumbnail_anchors.size() > 4) {
    $('#media_carousel').jCarouselLite({
      btnNext: ".carousel_down",
      btnPrev: ".carousel_up",
      vertical: true,
      circular: true,
      speed: 950,
      visible: 4
    });
  }

  thumbnail_anchors.click(function (e) {
    e.preventDefault();
    $('#featured_media').find('ul li a').removeClass('selected');
    $('#featured_media').find('div.featured').hide();
    $(this).addClass('selected');
    var to_show = $(this).attr('id').substring(5, $(this).attr('id').length);
    $('#view_' + to_show).show();
  });
}

function wire_up_story_search() {
  if ($('#story_search').length == 0) return;

  if ($('#story_search').val() == "")
  {
    $('#story_search').val('Search');
  }
  $('#story_search').focus(function() {
    this.select();
  });

}

function wire_up_modal() {
  $('#dialog').jqm({
    overlay: 80
  });
}

function should_scroll_to_top() {
  if ($.browser.msie && jQuery.browser.version.substr(0, 1) == '6') {
    window.scrollTo(0, 0);
  }
}

$(function()
{
  wire_up_tablesorter();
  wire_up_datepicker();
  wire_up_tabs();
  wire_up_wysiwyg();
  wire_up_modal();
  wire_up_media_picker();
  wire_up_featured_media();
  wire_up_story_search();
});
