﻿$.fn.corners = function(options) {
    var opts = $.extend({}, $.fn.corners.defaults, options);
    return this.each(function() {
        var $this = $(this);
        $this.addClass('corners').wrapInner($('<div class="boxContent"></div>')).prepend($('<div class="tl corner"></div><div class="tr corner"></div><div class="bl corner"></div><div class="br corner"></div>'));

        if (opts.border == 'auto') {
            var topOffset = $this.css("border-top-width");
            var rightOffset = $this.css("border-right-width");
            var bottomOffset = $this.css("border-bottom-width");
            var leftOffset = $this.css("border-left-width");

            var patt =/px/;

            if (!patt.test(topOffset)) {
                topOffset = 0;
            }
            if (!patt.test(rightOffset)) {
                rightOffset = 0;
            }
            if (!patt.test(bottomOffset)) {
                bottomOffset = 0;
            }
            if (!patt.test(leftOffset)) {
                leftOffset = 0;
            }

            $this.children('div.tl, div.tr').css({ 'top': '-' + topOffset });
            $this.children('div.tr, div.br').css({ 'right': '-' + rightOffset });
            $this.children('div.bl, div.br').css({ 'bottom': '-' + bottomOffset });
            $this.children('div.tl, div.bl').css({ 'left': '-' + leftOffset });
        } else {
            $this.children('div.tl, div.tr').css({ 'top': opts.border });
            $this.children('div.tr, div.br').css({ 'right': opts.border });
            $this.children('div.bl, div.br').css({ 'bottom': opts.border });
            $this.children('div.tl, div.bl').css({ 'left': opts.border });
        }


        /*if (IE6) {
        $.fn.corners.fixCorners($this);
        }*/
    });
};

$.fn.corners.defaults = {
    border: 'auto'
};

$.fn.corners.fixCorners = function(e) {
    if (e.innerHeight() % 2 != 0) {
        // Get the bottom coordinate of the bottom corners and subtract 1px
        var currentBtm = e.find('div.bl').css('bottom');
        e.children('div.bl').css('bottom', parseInt(currentBtm) - 1 + "px");
        e.children('div.br').css('bottom', parseInt(currentBtm) - 1 + "px");
    }
    if (e.innerWidth() % 2 != 0) {
        // Get the bottom coordinate of the bottom corners and subtract 1px
        var currentRight = e.find('div.tr').css('right');
        e.children('div.tr').css('right', parseInt(currentRight) - 1 + "px");
        e.children('div.br').css('right', parseInt(currentRight) - 1 + "px");
    }
}