$(function () {

  $("noscript").empty();
  $("noscript").remove();

  $.fn.check_height = function()
  {
    $('.this_week').css('padding-bottom','20px');
    var left_col_bot = $('.left_col_wrapper').offset().top + $('.left_col_wrapper').height() +40;
    var multi_top = $('.lower_left').offset().top -20;


    if (multi_top < left_col_bot)
    {
      var my_pad = left_col_bot - multi_top + 20+'px';
      $('.this_week').css('padding-bottom',my_pad);
    }
  }

    $('.lower_right').load(function()
    {
      $(this).check_height();
    });


  /*------------------------------------*/
  /* fancy dropdown menu for navigation */
  /*------------------------------------*/
  $('ul.sf-menu').superfish();

  /*---------------------------------------------*/
  /* check_width                                 */
  /*                                             */
  /* check the width of the browser              */
  /* if less than than 1152 add the narrow class */
  /*---------------------------------------------*/
  $.fn.check_width = function()
  {
    window_width = $(window).width();

    if (window_width < 1152)
    {
      $('div#widthWrapper').addClass('narrow');
      $('.find_more img:gt(2)').hide();
    }
    else
    {
      $('div#widthWrapper').removeClass('narrow');
      $('.find_more img').show();
    }

    $(this).check_height();
  }

  /*---------------------------------------------*/
  /* check the width of the browser on page load */
  /*---------------------------------------------*/
  $(this).check_width();

  /*--------------------------------------------------*/
  /* check the width of the browser on browser resize */
  /*--------------------------------------------------*/
  $(window).resize(function()
  {
      $(this).check_width();
  });

  /*--------------------------------------------------*/
  /* display_next                                    */
  /*                                                  */
  /* get a list of divs with the class 'type'         */
  /* hide the current and show the next               */
  /* if at the last, loop around to the first         */
  /*--------------------------------------------------*/
  $.fn.display_next = function(type,event)
  {
    if ($('div.'+type+':last:visible').text() == '')
    {
      next = $('div.'+type+':visible').next('div.'+type);
    }
    else
    {
      next = $('div.'+type+':first');
    }

    $('div.'+type).hide();

    next.fadeIn('slow');

    if (event != '')
    {
      event.preventDefault();
    }
  }

  /*--------------------------------------------------*/
  /* display_prev                                     */
  /*                                                  */
  /* get a list of divs with the class 'type'         */
  /* hide the current and show the next               */
  /* if at the last, loop around to the first         */
  /*--------------------------------------------------*/
  $.fn.display_prev = function(type,event)
  {
    if ($('div.'+type+':first:visible').text() == '')
    {
      prev = $('div.'+type+':visible').prev('div.'+type);
    }
    else
    {
      prev = $('div.'+type+':last');
    }

    $('div.'+type).hide();

    prev.fadeIn('slow');

    event.preventDefault();

  }

  /*--------------------------------------------------*/
  /* get the next quote                               */
  /*--------------------------------------------------*/
  $('a.next_quote').click(function(event)
  {
    $(this).display_next('quote_text',event);
  });

  /*--------------------------------------------------*/
  /* get the next announcment                         */
  /*--------------------------------------------------*/
  $('a.next_announce').click(function(event)
  {
    $(this).display_next('announce_text',event);
    $(this).count_announce();
//    $(this).check_height();
  });

  /*--------------------------------------------------*/
  /* get the previous announcment                     */
  /*--------------------------------------------------*/
  $('a.prev_announce').click(function(event)
  {
    $(this).display_prev('announce_text',event);
    $(this).count_announce();
//    $(this).check_height();
  });


  /*--------------------------------------------------*/
  /* count_announce                                   */
  /*                                                  */
  /* display the current announcement index           */
  /* along with the total number of announcments      */
  /*--------------------------------------------------*/
  $.fn.count_announce = function()
  {
    an_count = $('div.announce_text').length;


    current = $('div.announce_text').index($('div.announce_text:visible')) + 1;

    $('span.announce_count').text(current+'/'+an_count);
  }

  $.fn.scroll_announce = function()
  {
    window.setTimeout(function() {
      $(this).display_next('announce_text','');
      $(this).count_announce();
      $(this).scroll_announce();
    }, 8000);
  }

  /*--------------------------------------------------*/
  /* display the announement count on page load       */
  /*--------------------------------------------------*/
  $(this).count_announce();

  $(this).scroll_announce();


  /*--------------------------------------------------*/
  /* initalize the modal window                       */
  /*--------------------------------------------------*/

  var $dialog_annc = $('#announce_all').dialog({
      modal: true,
      autoOpen: false,
      width: 800,
      maxHeight: 400,
      title: "Announcements"
  });
  $(".announce_text p").clone(false).prependTo("#announce_all");

  $('#annc_view_all').click(function(event)
  {
    $dialog_annc.dialog('open');
    event.preventDefault();
  });

  var $dialog_news = $('#dialog_news').dialog({
      modal: true,
      autoOpen: false,
      width: 800,
      maxHeight: 400,
      title: "News Article",
      beforeclose: function(event, ui) { $('#dialog_news').empty(); }
  });

  $('.news a').click(function(event)
  {
    if ($(this).next('div.news_text').length > 0)
    {
      $(this).next('div.news_text').contents().clone(false).prependTo("#dialog_news");
      $dialog_news.dialog('option', 'title', $(this).text());
      $dialog_news.dialog('open');
      event.preventDefault();
    }
  });

});
