// this plugin allows you to create drop down lists when a text input field is
// clicked or a div. If using a div you MUST specify a TABINDEX otherwise
// certain events like pressing the esc key or clicking outside of the drop down
// will not register.   


(function($)
{

  $.fn.context_window = function(options)
  {

    // Go through the matched elements and return the jQuery object.
    return this.each(function()
    {
      // create function to bind to the correct events
      var click = function(e) {
        show_window(e, options);
      };
      var focus = function(e) {
        show_window(e, options);
      };

      var blur = function(e) {
        hide_window(e, options);
      };

      var keyup = function(e){
        if (e.keyCode == 27) {
          hide_window(e, options);  //Hide the menus if visible
        }
      };

      //bind the events to the object
      $(this).click(click);
      //$(this).focus(focus);
      $(this).blur(blur);
      $(this).keyup(keyup);
    });
  };
 

    // funciton to show the selection box and highlight the currently selected
    // item
   function show_window(event, options) {
      var trigger = $(event.target); 
      
      var parent_div = trigger.parents(".promo_list_item")
      //figure out the position of the drop down
      var offset = trigger.offset({relativeTo: "body"});
      var context_window = $(options.content_id);
      //var left = parseInt(offset.left) - context_window.outerWidth()/2 + trigger.width()/2;
      var left = parseInt(parent_div.offset().left) +
parent_div.outerWidth()+12 - context_window.outerWidth();
      context_window.css('left' , left+'px');
      
      var top = parseInt(parent_div.offset().top) + parent_div.outerHeight()-1;
      context_window.css('top' ,  top+'px');

      context_window.show("normal");
  
    };
    
    function hide_window(event, options) {
      //need ed to add the delay b/c the click events on objects inside the
      //context window weren't triggering sometime
      setTimeout("$('"+options.content_id+"').hide('normal');", 500);
    };

      
})(jQuery);
