jQuery.my_debug = function(text){
  $('#my_debug').append(text+'<br />\r\n');
}

$(document).ready(function(){

  var animation_speed = 800;
  var $categories_select = $('select#categories');
  var all_categories_option = '';
  var form_height = 0;
  var centerCover_height = 0;
  $('.advanced_search_button').click(function(){
    option_height = $('select#categories option:first').height()+8;
    var $all_categories_option = $categories_select.find('#all_categories');
    if($categories_select.attr('multiple') == ''){//is advanced search not displayed?
      form_height = $('#jobsearch form').height();
      $('#jobsearch form').css('height', 'auto');
      $categories_select.find('#all_categories').remove();
      $categories_select.css('height', option_height+'px').attr('multiple','multiple').attr('size','10').animate({height: '160px'}, animation_speed, function(){
        $('div#advanced_search').slideToggle(animation_speed, function(){
          if($('#page_coverCover').hasClass('slide_job_search')){//if the job search is slided out, check if there is enought space in the content cover for the job search
            centerCover_height = $('#page_coverCover .centerCover').height();
            new_height = centerCover_height + parseInt($('#page_coverCover .centerCover').css('padding-bottom'));
            ideal_height = $('#jobsearch').height();
            if(new_height < ideal_height){
              new_height = ideal_height;
              $('#page_coverCover .centerCover').addClass('height_changed').animate({ height:  new_height+ 'px'}, 2000);
            }
            $('#jobsearch a.tab').css('padding-bottom', parseInt($('#jobsearch').height() - $('#jobsearch a.tab').height() - 15)+'px' );
          }
        });
        $categories_select.find('option').each(function(){
          $(this).removeAttr('selected');
        });
      });
    } else {
      $('div#advanced_search').slideToggle(animation_speed, function(){
        $categories_select.animate({height: (option_height-4)+'px'}, animation_speed, function(){
          $(this).html('<option value="all_categories" id="all_categories" selected="selected">-- Select --</option>'+$(this).html()).removeAttr('style').removeAttr('multiple').attr('size', '1');
          $('#jobsearch form').css('height', form_height);
          if($('#page_coverCover').hasClass('slide_job_search') && $('#page_coverCover .centerCover').hasClass('height_changed')){//if content cover was resized, update it's height again
            $('#page_coverCover .centerCover').removeClass('height_changed').animate({ height:  centerCover_height+'px'}, 2000);
          }
        });
      });
    }
    return false;
  });
  
  $('a#job_alert_forgot_link').click(function(){
    $('#job_alert_login_form').slideToggle(animation_speed, function(){
      $('#job_alert_forgot_form').slideToggle(animation_speed);
    });
    return false;
  });

  $('#jobsearch a.tab').click(function(){
    jobsearch_box = $('#jobsearch');
    tab_link = $(this);
    if(!jobsearch_box.hasClass('shown')){//show the search
      jobsearch_box.addClass('shown');
      jobsearch_box.find('#search_keyword').focus();
      jobsearch_box.animate({
        'width': '321px'
      }, animation_speed);
      $('#job_search_cover form').animate({'width': '100%'}, animation_speed);
    } else {// hide the search
      if($('#advanced_search:visible').length > 0){
        $('input.advanced_search_button').click();
      }
      $('#job_search_cover form').animate({'width': '0'}, animation_speed-200);
      jobsearch_box.animate({
        'width': tab_link.width()
      }, animation_speed, function(){
        jobsearch_box.removeClass('shown');
        tab_link.removeAttr('style');
      });
    }
    return false;
  });
  
  $('table.job_list tbody tr').mouseover(function(){
    $(this).addClass('hover');
  }).mouseout(function(){
    $(this).removeClass('hover');
  }).click(function(){
    if(!$(this).hasClass('no_link'))
      document.location.href = $(this).find('a').attr('href');
  });
  
  //resize Featured Jobs box to max possible height and run the marquee
  var max_height = $('#page_cover_grey_bottom').height() - $('#job_alerts').height() - $('#featured_jobs h2.heading').height() - parseInt($('#featured_jobs div.content').css('padding-bottom')) - parseInt($('#featured_jobs').css('margin-top')) - 2;
  if(max_height > 600)
    max_height = 600;
  initializemarquee();
  $('#featured_jobs #marqueecontainer').animate({
    height: max_height + 'px'
  }, animation_speed*2);
  
});
