/**
* jQuery corner plugin
*
* Dual licensed under the MIT and GPL licenses:
* 	http://www.opensource.org/licenses/mit-license.php
* 	http://www.gnu.org/licenses/gpl.html
* @author Jonathan Gotti  < jgotti at jgotti dot org >
*/

(function($){
	$.fn.corner = function(options){
		return this.each(function() {
				CORNER($(this),options);
		});
	}
	function CORNER(elmt, options ){
		return this instanceof CORNER ? this.init(elmt,options): new CORNER(elmt, options);
	}
	$.extend(CORNER.prototype,{
		elmt:      null,
		opts:      {},
		padTop:    0,
		padBottom: 0,
		bgColor:   '#fff',
		containers:[null,null],

		init: function(elmt,opts){
			//this.opts      = $.extend({}, $.fn.corner.defaults, opts); //<-- on verra ca quand on ajoutera des options
			this.elmt      = elmt;
			this.padTop    = parseInt(elmt.css('padding-top'));
			this.padBottom = parseInt(elmt.css('padding-bottom'));
			this.bgColor   = elmt.css('background-color');
			elmt.css({paddingTop:0,paddingBottom:0});
			this.addCorners();
			this.containers[0].css('margin-top',this.elmt.css('margin-top'));
			this.containers[1].css('margin-bottom',this.elmt.css('margin-bottom'));
			this.elmt.css({marginTop:0,marginBottom:0});
		},

		addCorners: function(){
			var width = parseInt(this.elmt.width()) + parseInt(this.elmt.css('padding-left')) + parseInt(this.elmt.css('padding-right'));
			var style = {
				overflow: 'hidden',
				height:'1px',
				background:this.bgColor,
				border:'none',
				margin: 'auto'
			}
			this.containers[0] = $('<div style="text-align:center;border:none;width:'+width+'px;height:'+this.padTop+'px;" ></div>');
			this.containers[1] = $('<div style="text-align:center;border:none;width:'+width+'px;height:'+this.padBottom+'px;" ></div>');
			this.elmt.before(this.containers[0])
				.after(this.containers[1]);

			for(var i= this.padTop; i>0; i--){
				var w = width - Math.round(this.padTop*(1-Math.cos(Math.asin(i/this.padTop))));
				var div = $('<div></div>');
				style.width = w+'px';
				div.css(style);
				this.containers[0].append(div);
			}
			for(var i=1; i<=this.padBottom; i++){
				var w = width - Math.round(this.padBottom*(1-Math.cos(Math.asin(i/this.padBottom))));
				var div = $('<div></div>');
				style.width = w+'px';
				div.css(style);
				this.containers[1].append(div);
			}
		}
	});
})(jQuery);
