(function( $ ){
  
  var findStyle=function (selector,prop) {
      var definition,value,result,rule;
       for(k=0;k<document.styleSheets.length;k++ ){
          regexp=new RegExp(selector+'[ ]+{[^{}]*}','gi');
           result=document.styleSheets[k].cssText.match(regexp);
           if(result){
               //last rule
               for(i=0;i<result.length;i++){
                   rule=result[i];
                   definition=rule.match(new RegExp(prop+'[ ]*:[^;]+[;}]','gi'));
                   if(definition){
                       value=definition[definition.length-1].match(new RegExp(':[^;]+[;}]','gi'));
                       if(value){
                           value=value[0].replace(/[:;}]+/gi,"");
                           if(value.length){
                              return value;
                           }
                       }
                       
                   }
               }
               
           }
            
            
       }
       return "";
    }
  
  methods = {
    init : function( options ) {
        if(typeof options.selector==='undefined'){
            return this;
        }else{
            var selectors=options.selector.split(',');
            for(z=0;z<selectors.length;z++){
                $(selectors[z]).each(function(){
                    $(this).borderprovider('makeborder',selectors[z])
                });
            }
            return this;
        }
    },
    makeborder:function(selector){   
            $this=jQuery(this);
            var classes,
                cssradius=findStyle(selector,'border-radius');
            if(!cssradius.length && this.id){                
                cssradius=findStyle('#'+this.id,'border-radius')
            }
            
            if(!cssradius.length){                
                classes=$this.attr('class').split(' ');
                
                for(y = classes.length-1;y > -1; y--){                
                    cssradius=findStyle('.'+classes[y],'border-radius');
                    if(cssradius.length){   
                        break;
                    }
                }
            }
            radius=parseInt(cssradius.replace("px",'')) || 5;
            
            var bwidth=0,
                bcolor=$this.css('border-color').replace('#','') || '000000';
            if($this.css('border-width').replace(/[a-zA-Z]*/,'').length){
                bwidth=parseInt($this.css('border-width').replace(/[a-zA-Z]*/,''));
            }
            bgurl="/typo3temp/comvosborders/"+($this.outerWidth()-3*bwidth)+"."+($this.outerHeight()-3*bwidth)+"."+radius+"."+bwidth+"."+bcolor+"."+$this.css('background-color').replace('#', '')+'.png';
            
            $this.css('background-image',"url("+bgurl+")");
            $this.css('border-style',"none");
            $this.css('background-color',"transparent");
            $this.css('background-repeat','no-repeat');
        }
  };

  $.fn.borderprovider = function( method ) {
    
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.borderprovider' );
    }    
  
  };

})( jQuery );
