﻿/// Rounded corner
jQuery(function ($) {
	var template =
		$('<span class=\"rounded_border\">' +
			'<div class=\"p_tl\"></div>' +
			'<div class=\"p_tr\"></div>' +
			'<div class=\"p_c\">' +
				'<div class=\"p_ci\"></div>' +
			'</div>' +
			'<div class=\"p_bl\"></div>' +
			'<div class=\"p_br\"></div>' +
		'</span>');

	var copyStyle = function (names, elem) {
		var styles = $.map(names, function (n) {
			return { name: n, value: elem.css(n) };
		});
		return styles;
	}

	var pasteStyle = function (styles, elem) {
		$.each(styles, function (i, n) {
			elem.css(n.name, n.value);
		});
	}

	$.fn.rounded = function () {
		return this.each(function () {
			var self = $(this);

			var styles = copyStyle([
				'margin-left', 'margin-right', 'margin-top', 'margin-bottom', 'float'
			], self);

			var rounded = template.clone();
			rounded.insertAfter(self);

			// Move image to template
			rounded.find('.p_ci').append(self);

			pasteStyle(styles, rounded);

			self.css({ 'margin': 0, 'float': 'none' });
		});
	}
});
