function runNewsSearch( query ) {
  $('#news_search').toggleClass('spinner');
  $('#latest-news').fadeOut(400, function() {
    $.get('/pages/news_teasers', {'q':query}, 
      function(data) {
        $('#latest-news').html(data);
        $('#latest-news').jScrollPane({showArrows:true});
        $('#latest-news').fadeIn(400, function() {
          $('#news_search').toggleClass('spinner')
        });
      }
    );
  });
}

function logSponsorClick( id ) {
  $.get('/sponsors/log_access/'+id);
}

$(document).ready(function(){
  // Create proper height
  main_height = $('body').height() - 128 - 55 - 50;
  // $('#container').height(134+334+392+36);
  if(main_height < 535) {
    main_height = 535;
    // $('#container.exact-size').height(main_height + 128 + 55 + 50);
  }
  content_height = main_height - 20 - 10 - 15;
  $('.exact-size #main').height(main_height);
  $('.exact-size #main #content-container').height(content_height);
  $('.exact-size #main #content').height(content_height);
  $('.exact-size #main textarea.tinymce').height(content_height - 180);
  // $('#container').css('height', ''+(main_height+128+55+50)+'px');

  // Make scroll panes
  if($.fn.jScrollPane) {
    $('#latest-news').jScrollPane({showArrows:true});
    $('#sponsors').jScrollPane({showArrows:true});
    $('#content').jScrollPane({showArrows:true});
  }
  
  // Create drop down navs
  $('#header .nav').each(function() {
    $(this).children('ul').each(function() {
      new_top = -($(this).height()-40);
      $(this).css('top', new_top);
    })
  }).mouseenter(function() {
    $(this).children('ul:has(li)').eq(0).animate({top: 40}, 400);
  }).mouseleave(function() {
    new_top = -($(this).children('ul:has(li)').eq(0).height()-40);
    $(this).children('ul:has(li)').eq(0).animate({top: new_top}, 400)
  });
  
  // Setup search
  $('#news_search').newsSearchBox();
  $('#latest-news').ajaxError(function(e, req, opts) {
    if(opts.url.match('/pages/news_teasers') != null) {
      $(this).html('<p class="news no_news">Search Error</p>');
      $(this).fadeIn(400, function() {
        $('#news_search').removeClass('spinner');
      });
    }
  });
});

$.fn.newsSearchBox = function() {
  $(this).each(function() {
	  var timeout;
    $(this).data('default_text', $(this).attr('value'));
    $(this).click(function(event) {
      if($(this).hasClass('unused')) {
        $(this).attr('value', '')
        $(this).toggleClass('unused');
      }
    }).blur(function() {
      if($(this).attr('value') == '') {
        $(this).attr('value', $(this).data('default_text'));
        $(this).toggleClass('unused');
      }
    }).keyup(function(event) {
  		clearTimeout(timeout);
  		timeout = setTimeout("runNewsSearch('"+$(this).attr('value')+"')", 500);
    });
  });
};

$.fn.logSponsorClick = function( id ) {
  $(this).click(function(event) {
    $.get('/sponsors/log_access'+id);
  });
};