(function( $ ){
  
  
  
  var category = '';
  
  var methods = {
    init : function( options ) { 
      $("#category_grid li",this).each(function() {
        var x = ($(this).index() % 13) * 33;
        var y = 0;
        if ($(this).index() > 12) {
          y = 33;
        }
        var z = 100;
        $(this).data('coordinates',{'x':x,'y':y,'z':z});
        $(this).css({
          'position' : 'absolute',
          'top' : $(this).data('coordinates').y,
          'left' : $(this).data('coordinates').x,
          'z-index' : $(this).data('coordinates').z
        });
        $(this).click(function() {
          category = $(this).attr('title');
          $('#category_display').html(category);
          $('input.category').attr('value',category);
          $("#category_grid li").removeClass('selected');
          $(this).addClass('selected');
          
        });
        $(this).hover(function() {
          $(this).css({
            'z-index' : 500,
            'top' : $(this).data('coordinates').y - 11,
            'left' : $(this).data('coordinates').x - 16,
            'height' : 65,
            'width' : 65
           }); /*.animate({
            'top' : $(this).data('coordinates').y - 11,
            'left' : $(this).data('coordinates').x - 16,
            'height' : 65,
            'width' : 65
          },100);*/
          $(this).addClass('hover');
          $('#category_display').html($(this).attr('title'));
        },
        function() {
          $('#category_display').html(category);
          $(this).css({
            'top' : $(this).data('coordinates').y,
            'left' : $(this).data('coordinates').x,
            'height' : 32,
            'width' : 32
          });
          /*$(this).animate({
            'top' : $(this).data('coordinates').y,
            'left' : $(this).data('coordinates').x,
            'height' : 32,
            'width' : 32
          },100);*/
          $(this).removeClass('hover').css({
            'z-index' : $(this).data('coordinates').z});
        });
      });
      
      $('#searchtool_entry li').each(function() {
        $(this).click(function() {
          var search_target = {'ARCHIVE':'/cgi-registry/wgbh/roadshow/archive_search.cgi',
          'SITE':'/wgbh/roadshow/search_results.html'};
          $('#searchtool_entry li').removeClass('active');
          $('#searchtool_form').attr('action',search_target[$(this).html()]);
          $(this).addClass('active');
        });
      });
      
      $('input[type="image"]').each(function() {
        $(this).hover(function() {
          $(this).attr('src',$(this).attr('src').replace(/(.png)/,'_ovr$1'));
        }, function() {
          $(this).attr('src',$(this).attr('src').replace(/_ovr/,''));
        });
      });
      
      $('input[type="text"]').each(function() {
        if ($(this).attr('value') == '') {
          $(this).addClass('empty');
        } else {
          $(this).removeClass('empty');
        }
        $(this).click(function() {
          $(this).removeClass('empty');  
        });
        $(this).blur(function() {
          if ($(this).attr('value') == '') {
            $(this).addClass('empty');
          } else {
            $(this).removeClass('empty');
          }
        });
      });      
      return this;
    },
    show : function( ) {    },
    hide : function( ) {  },
    update : function( content ) { 
      
    }
  };


  $.fn.archiveSearchtool = function(method) {
  
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.archiveSearchtool' );
    }    
  };

})( jQuery );

jQuery(document).ready(function() {
  $('#home_roadshow_toolbar').archiveSearchtool();
});

