$(document).ready(function(){

  // COMMON: places a video in the selector div.
  // parameters are: the width you want the video to be, and chrome height
  $('#video_placeholder').place_video( 400, 24 );
  
  // COMMON: removes the submit button form the locations filter on the openings page
  // and sets the form to auto sybmit
  $('div#filter input[type="image"]').remove();
    $('select[name="locations"]').auto_submit($('div#filter form'));
    $('select#filter_expertise, select#filter_service').auto_submit($('div#filter form'));
    
    // slider in the horizons archive page
    $('p.horizon_slider a').slider();
    
    // COMMON: insert the 'keywords' text into the search text input field
    $('input.keywords').search_text_input();
    
    // remove border when hovering over navigation items
    $('div#navigation_pri ul li').tidy_nav_hover();
    
    // validate forms using jquery.validate
    $('form#newsletter_header_form').validate();
    $('div#jobs_email form').validate();
    $('div#news_updates_signup form').validate();
    $('#freeform').validate();

    // slideshow on the homepage
    $('div#home_slideshow').load('/home/slideshow_home div.ss:eq(0)').slideshow('/home/slideshow_home');
  
  // hide/show the jobs updates sidebar form
  $('div#jobs_email form').hide();
  $('div#jobs_email').sidebar_slider( );
  
  // show/hide the newa updates sidebar signup form
  $('div#news_updates_signup form').hide();
  $('div#news_updates_signup').sidebar_slider();
  
  // show/hide the main signup form
  $('ul#tab_subscribe li a').show_hide( 'div#email_panel', 'Close', $('ul#tab_subscribe li a').html() );        
    
    // Projects on the 'Our Work' page sidebar
    $('ul#projects_widget').accordion({ 
      active: false, 
      header: 'h3',
        autoHeight: false,
        clearStyle: true,
        alwaysOpen: false
    });

});


/// BEING FUNCTIONS SPECIFIC TO H&S --------------------------


/**
*
* Show/Hide
* -----------------
* Toggle slides and item, and chnages the html() of
* the clicked item to the specified value
*
* @return $(this) obj
*/
$.fn.sidebar_slider = function()
{
  container = $(this);
  $('h2,h3', this).click( function() {
    $(this).next('form').slideToggle(500);
  } );
  return $(this);
}


/**
*
* Show/Hide
* -----------------
* Toggle slides and item, and chnages the html() of
* the clicked item to the specified value
*
* @return $(this) obj
*/
$.fn.show_hide = function( the_item, althtml, orightml )
{
  $(this).click(function(){
    $(the_item).slideToggle('fast');
    $(this).toggleClass('open').toggleClass('closed');
    if( $(this).html() != althtml) {
      $(this).html(althtml);
    } else {
      $(this).html(orightml);
    }
    return false;
  });
  return $(this);
}


/**
*
* Slideshow
* -----------------
*
* @return $(this) obj
*/
$.fn.slideshow = function( path )
{ 
  var slideshow_div = $(this);
  $('li.ss_next a, li.ss_prev a', this).live('click', function(){
      trgts = $(this).attr('href').split('/');
      trgt = trgts[trgts.length - 1];         
      $('img',slideshow_div).fadeOut('slow', function(){
          slideshow_div.load(path+' div.ss:eq('+trgt+')', function(){
          });
      });
      return false;
  });
  return $(this);
}


/**
*
* Tidy Navigation Hover States
* -----------------
* Adds a class to the next element to 'this' parent
* the css for the class will remove the unsighlty border
* on the next element.
*
* @return $(this) obj
*/
$.fn.tidy_nav_hover = function()
{
  var element_to_change = $(this).parent().next();
  $('a', this).hover(
    function(){
      element_to_change.addClass('hover_next');
    },
    function(){
      element_to_change.removeClass('hover_next');
    }
  );
  return $(this);
}


/**
*
* Slider
* ------
* This function is quite markup specific (currently used in the horizons section)
* I'm not sure if it's worth making it more re-usable. Probably not.
*
* @return $(this) obj
*/
$.fn.slider = function()
{
  $(this).next('ul').hide();
  $('a', this).click(function(){
      $(this).parent().parent().next('ul').slideToggle('slow');
      return false;
  });
  return $(this);
}