(function($) {
    $.fn.shadow2 = function(over, gDir, options){
        var elem = this;
        var overflow = over * 2;
        var imgPixel = 8;
        var total = overflow - imgPixel * 2;
        var width = elem.outerWidth() + total;
        var height = elem.outerHeight() + total;
        var diff = imgPixel - over;

        var options = $.extend({
          position: 'absolute',
          width: width,
          height: height,
          backgroundColor: '#474747',
          top: elem.css('position') == 'static'? 0: elem.position().top + diff,
          left: elem.css('position') == 'absolute'? elem.position().left + diff: 0,
          marginLeft: elem.css('marginLeft'),
          marginRight: elem.css('marginRight'),
          marginTop: elem.css('marginTop'),
          marginBottom: parseInt(elem.css('marginBottom')) - total
        }, options);

        var shadowParts = [options,
            {
                width: imgPixel,
                height: imgPixel,
                top: -imgPixel,
                left: -imgPixel,
                backgroundImage: 'url(' + gDir + 'shadow_tl.png)'
            },
            {
                width: width,
                height: imgPixel,
                top: -imgPixel,
                left: 0,
                backgroundImage: 'url(' + gDir + 'shadow_t.png)',
                backgroundRepeat: 'repeat-x'
            },
            {
                width: imgPixel,
                height: imgPixel,
                top: -imgPixel,
                right: -imgPixel,
                backgroundImage: 'url(' + gDir + 'shadow_tr.png)'
            },
            {
                width: imgPixel,
                height: height,
                top: 0,
                left: -imgPixel,
                backgroundImage: 'url(' + gDir + 'shadow_l.png)',
                backgroundRepeat: 'repeat-y'
            },
            {
                width: imgPixel,
                height: height,
                top: 0,
                right: -imgPixel,
                backgroundImage: 'url(' + gDir + 'shadow_r.png)',
                backgroundRepeat: 'repeat-y'
            },
            {
                width: imgPixel,
                height: imgPixel,
                top: height,
                left: -imgPixel,
                backgroundImage: 'url(' + gDir + 'shadow_bl.png)'
            },
            {
                width: width,
                height: imgPixel,
                top: height,
                left: 0,
                backgroundImage: 'url(' + gDir + 'shadow_b.png)',
                backgroundRepeat: 'repeat-x'
            },
            {
                width: imgPixel,
                height: imgPixel,
                top: height,
                right: -imgPixel,
                backgroundImage: 'url(' + gDir + 'shadow_br.png)'
        }];
        return this.each(function(){
            var container = $('<div class="shadow">').css(shadowParts.shift()).appendTo(elem.parent());
            $(shadowParts).each(function() {$('<div class="iepngfix">').appendTo(container).css($.extend({position: 'absolute', backgroundRepeat: 'no-repeat'}, this));});
            container.append(elem.css({position: 'absolute', margin: 0, top: -diff, left: -diff}));
        });
    };
})(jQuery);
