var windowRef;
var listOpen;
var clickedList;

$(function() {

  windowRef = $( window );

  $( window ).resize( positionFooter )
  positionFooter();
  
  $( "#filter_list_header" ).click( openList )
  $( "#filter_list li:not(#filter_list_header)" ).click( function(){ 
  
    $( "#filter_list li" ).show();
    $( this ).hide();
    changeCategory( $( this ).text() )
  
  } );
  
  $( document ).click( checkCloseList );
    
});

function positionFooter()
{
  $( "#footer" ).width( $( window ).width() + 5 );
  $( "#footer" ).css({ "top": ( $(window).height()-65 ) + "px" });
}

function openList()
{
  
  if( listOpen )
  {
    closeList();
    return;
  }
  
  listOpen = true;

  var newHeight = $( "#filter_list" ).height();
  var oldHeight = $( "#filter_mask" ).height();
  
  $( "#filter_mask" ).animate({
    height: newHeight,
    "margin-top": "-" + ( newHeight - oldHeight ) + "px"
  });
  
  event.stopPropagation();

}

function changeCategory( new_caption )
{
  $( "#filter_list_header_caption" ).text( new_caption );

  closeList();
}

function closeList()
{
  listOpen = false;
  
  $( "#filter_mask" ).animate({
    height: 26,
    "margin-top": 0
  });
}

function checkCloseList()
{
  if( listOpen && !clickedList )
  {
    closeList();
  }
}
