(function($) {
    $.fn.autoCSS = function(settings) {
        settings = jQuery.extend({
			name:                '',
			folder:              '',
			ext:                 'gif',
			width:               100,
			height:              100,
			top:                 5,
			right:               5,
			left:                5,
			bottom:              5,
			top_height:          1,
			bottom_height:       1,
			background:          false
        },settings);

		var save_content = this.html();
		var n = '';
		if (settings.name == ''){
			n = this.attr('class');
			if (!n) return false;
			settings.name = n;
		}
		n = settings.name;
		nc = n + '_';
        if (settings.folder == '') settings.folder = 'static/images/autoCSS/' + n + '/';

		var content = '<img class="' + nc + 'abs_img ' + nc + 'top_left" src="' + settings.folder + 'top_left.' + settings.ext + '" alt="" />';
        content = content + '<img class="' + nc + 'abs_img ' + nc + 'top_right" src="' + settings.folder + 'top_right.' + settings.ext + '" alt="" />';
		content = content + '<img class="' + nc + 'abs_img ' + nc + 'bottom_left" src="' + settings.folder + 'bottom_left.' + settings.ext + '" alt="" />';
		content = content + '<img class="' + nc + 'abs_img ' + nc + 'bottom_right" src="' + settings.folder + 'bottom_right.' + settings.ext + '" alt="" />';
		content = content + '<div class="' + nc + 'top"></div>';
		content = content + '<div class="' + nc + 'bottom"></div>';
		content = content + '<div class="' + nc + 'bg">' + save_content + '</div>';
		this.html(content);

		//Images dans les coins
		$('.' + nc + 'abs_img').css({
			position: 'absolute'
		});
		$('.' + nc + 'abs_img').css('z-index',3);
		$('.' + nc + 'top_right').css({ right : 0 });
		$('.' + nc + 'bottom_right').css({ right : 0, bottom : 0 });
		$('.' + nc + 'bottom_left').css({ bottom : 0 });

		//Div top et bottom
		$('.' + nc + 'top').css({ position : 'absolute', width : settings.width + 'px', height : settings.top_height + 'px', background : 'url(' + settings.folder + 'top.' + settings.ext + ') repeat-x' })
        $('.' + nc + 'top').css('z-index',2);
		$('.' + nc + 'bottom').css({ bottom : 0, position : 'absolute', width : settings.width + 'px', height : settings.bottom_height + 'px', background : 'url(' + settings.folder + 'bottom.' + settings.ext + ') repeat-x' })
        $('.' + nc + 'bottom').css('z-index',2);

		//div parent
		this.css({
			position: 'relative',
			width: settings.width + 'px'
			//height: settings.height + 'px'
		});
		//this.css('min-height', settings.height + 'px');

		//div conteneur du texte
		if (settings.background) var background = settings.background;
		else var background = 'bg.' + settings.ext;
		var padding = settings.top + 'px' + ' ' + settings.right + 'px' + ' ' + settings.bottom + 'px' + ' ' + settings.left + 'px';
		$('.' + nc + 'bg').css({
            position: 'relative',
			padding : padding,
			background : 'url(' + settings.folder + background + ') repeat-y'
        });
        $('.' + nc + 'bg').css('z-index',1);

    };
})(jQuery);
