(function($) {
  $.fn.dropdownize = function(options) {
    // build main options before element iteration
    var opts = $.extend({}, $.fn.dropdownize.defaults, options);
    $.fn.dropdownize.options = opts;
    // iterate and reformat each matched element
    var childs = $(this);
    var menu = childs.closest('ul');
      
    childs
      .bind('mouseover', function(e){
        var target = $(this),
        submenu = $(opts.sub_element, target),
        side = parseInt($(document).width()-opts.content_width)/2,
        dist = target.position().left-side;
        target.addClass(opts.hover_class);
        
        if (submenu.width()+dist >= opts.sensitive_width) submenu.css({left: 'auto', right: opts.offset_x});
        submenu.removeClass('accessible');
      })
      .bind('reset', function(e){
        $(opts.sub_element, menu).addClass('accessible');
        $(this).removeClass(opts.hover_class);
      });
      
      $('#container').add($('> li > a', menu)).bind('mouseover', function(){
        childs.trigger('reset');
      });
      
      $('#mainContent').add($('> li > a', menu)).bind('mouseover', function(){
          childs.trigger('reset');
        });
  };
  // plugin defaults
  $.fn.dropdownize.defaults = {sub_element: "ul", content_width: 950, hover_class: 'over', sensitive_width: 700, offset_x: '0px'};
  

  // Playing with classes for overlay labels
  $.fn.overlayfield = function() {
    return this.each(function (i) {
      var _fadetime = 170,
          _self = $(this),
          _input = new Array,
          _label = new Array;
  
      _input[i] = $("textarea, input", _self),
      _label[i] = $("label", _self);
  
      if(_input[i].val() != "") _label[i].animate({"opacity": "0"}, 0);
  
      _input[i].focus(function(){
        if($(this).val() == "") _label[i].animate({"opacity": "0.5"}, _fadetime);
      });
  
      _input[i].blur(function(){
        if($(this).val() == "") _label[i].animate({"opacity": "1"}, _fadetime);
      });
  
      _input[i].keydown(function(){
        _label[i].animate({"opacity": "0"}, _fadetime);
      });
    });
  };
  
  
  
})(jQuery);
