/*
  jQuery ugolok plugin
  This jQuery plugin can round corners of the div element
  and is absolutly cross browser, so it will work whith:
  Mozilla, Opera, Safary, Chrome, IE (from 6-th to current  shure)
  name jquery-ugolok-0.1.js
  author Zinovyev Ivan Dmitrievich - vanyazin@gmail.com
  version 0.1
  date Aug 30, 2011
  category jQuery plugin
  copyright (c) 2011 Zinovyev Ivan Dmitrievich (vanyazin@gmail.com) 
 */


(function($) {
        $.fn.ugolok = function(ugol1, ugol2, ugol3, ugol4){
          
                return this.each(function(){
                  
        var browser_ie='true_ie';
                  
                  
//for good browsers
        if($.browser.webkit || $.browser.mozilla || ($.browser.opera && $.browser.version>10.50)){                
//for HTML5
                if (($.browser.webkit && $.browser.version>4.0) || ($.browser.mozilla && $.browser.version>3.6) ||  $.browser.opera) {
                        $(this).css("border-radius", ""+ugol1+"px "+ugol2+"px "+ugol3+"px "+ugol4+"px ");       
                };
//if FF
                if ($.browser.mozilla && $.browser.version<3.6) {
                        $(this).css("-moz-border-radius", ""+ugol1+"px "+ugol2+"px "+ugol3+"px "+ugol4+"px ");   
                };
//if Opera || Chrome
                if ($.browser.webkit && $.browser.version<4.0) {
                        $(this).css("-webkit-border-radius", ""+ugol1+"px "+ugol2+"px "+ugol3+"px "+ugol4+"px ");         
                };
        }else{
        
//for IE and other shit-browsers
                  
                  
                  //$(this).css('background', 'red');
                        var height=$(this).height(); //current block height
                        var width=$(this).width(); //current block width
                        //now we'll count the max radius of the corner, that can be applied to this block
                        var maxrad=Math.max(height/2, width/2);
                        var motherdiv=$(this).attr("id");
                        
                        //now we check, if all parameters are given, if not - setting default parameters
                        if(!ugol4 || (ugol4>maxrad)){
                                if(!ugol3 || (ugol3>maxrad)){
                                        if(!ugol2 || (ugol2>maxrad)){
                                                if(!ugol1 || (ugol1>maxrad))
                                                ugol1=5;
                                                ugol2=5;
                                        };
                                        ugol3=5;  
                                };
                                ugol4=5;                          
                        };//end;
                        
                        //let's count how much div's do we need for the top and bottom corners
                        var top=Math.max(ugol1, ugol2);
                        var bottom=Math.max(ugol3, ugol4);
                        var middle=height-top-bottom;
                        
                        //saving the current content of the div:
                        var save_game=$(this).html();
                        
                        //copying styles from an old block
                        var background_image=$(this).css("background-image");
                        var background_color=$(this).css("background-color");
                        var height2=height-bottom;
                        
                        //clearing the current content of the div & creating all new blocks                     
                                var new_html='<div style="position: relative; width: 100%; height: 100%;">';
                                
                                var j='';
                                
                                //top part
                                for(var i=0; i<=top; i++){
                                        j=i+1;
                                        new_html+='<div id="'+motherdiv+'_top_round_div_'+i+'" style=\'background-image: '+background_image+'; background-color: '+background_color+'; height: '+height+'px; width: 100%; position: absolute; clip: rect('+i+', '+width+', '+j+', 0);\'></div>';
                                };
                        
                                //middle part
                                        new_html+='<div id="'+motherdiv+'_middle_round_div" style=\'background-image: '+background_image+'; background-color: '+background_color+'; width: 100%; height: '+height+'px; position: absolute; clip: rect('+top+', '+width+', '+height2+', 0)\'></div>';                    
                        
                                var bottom_offset='';
                                
                                //bottom part
                                for(var i=0; i<=bottom; i++){
                                        top_offset=middle+top+i;
                                        new_html+='<div id="'+motherdiv+'_bottom_round_div_'+i+'" style=\'background-image: '+background_image+'; background-color: '+background_color+'; height: '+height+'px; width: 100%; position: absolute; clip: rect('+top_offset+', '+width+', '+(top_offset+1)+', 0);\'></div>';
                                };
                                
                                
                                new_html+='<div style="position: absolute;">'+save_game+'</div></div>';
                
                        //accepting new content
                        $(this).html(new_html);
                        
                        //clearing old styles
                        $(this).css('background-image', 'none');
                        $(this).css('background-color', 'transparent');
                        
                        
                        //now the main part: we'll make the corners round
                        
                        var j='';
                        //ugol1                 
//                      j=ugol1; var p=''; var k='';
//                      var circle='';
//                      var real_width;
//                      for(var i=0; i<=ugol1; i++){
//                              real_width=width-ugol1+Math.floor(Math.sqrt(ugol1*ugol1-j*j)); //Math.floor(Math.sqrt(ugol1*ugol1-j*j));
//                              circle=width-real_width;        
//                              $('#'+motherdiv+'_top_round_div_'+i).css('clip', 'rect('+i+', '+width+', '+(i+1)+', '+circle+')');
//                              j--;                      
//                      };
                                        
                        
                        //ugol4
//                      j=0;
//                      for(var i=(bottom-ugol4); i<=ugol4; i++){
//                              top_offset=middle+top+i;
//                              real_width=width-ugol4+Math.floor(Math.sqrt(ugol4*ugol4-j*j)); //Math.floor(Math.sqrt(ugol1*ugol1-j*j));
//                              circle=width-real_width;
//                              $('#'+motherdiv+'_bottom_round_div_'+i).css('clip', 'rect('+top_offset+', '+width+', '+(top_offset+1)+', '+circle+')');
//                              j++;
//                      };
                        
                        
                        //top corners:
                        
                        
                        
                        function top_left(i,j1){
                        }
                        
//                      function top_right(i,j1, j2){
//                              if(ugol2=>i){
//                                real_width=width-top_left(i,j1);
//                                circle=real_width+Math.floor(Math.sqrt(ugol2*ugol2-j2*j2))-ugol2;
//                                return circle;
//                              }
//                              else{
//                                return width;
//                              }
//                      }
//                      
                        
//                      for(var i=0; i<=top; i++){
//                              j1=ugol1-i; j2=ugol2-i;
//                              $('#'+motherdiv+'_top_round_div_'+i).css('clip', 'rect('+i+', width, '+(i+1)+', 500)');
//                      };
                        
                        //function to count the radius of the top-left corner
                        function top_left(j1, i){
                                if(i<=ugol1){
                                        return ugol1-Math.floor(Math.sqrt(ugol1*ugol1-j1*j1));
                                }
                                else{
                                        return 0;
                                }
                        }
                        
                        //function to count the radius of the top-right corner                  
                        function top_right(j2, i){
                                if(i<=ugol2){
                                        return width-top_left(j2, i)+Math.floor(Math.sqrt(ugol2*ugol2-j2*j2))-ugol2;
                                }
                                else{
                                        return width;
                                }
                        }
                        
                        //function to count the radius of the bottom-right corner
                        function bottom_right(j1, i){
                                if(i>=(bottom-ugol3)){
                                        return width-ugol3+Math.floor(Math.sqrt(ugol3*ugol3-j1*j1));//ugol4-Math.floor(Math.sqrt(ugol4*ugol4-j2*j2)); //width-bottom_left(j1, i)+Math.floor(Math.sqrt(ugol3*ugol3-j1*j1))-ugol3;
                                }
                                else{
                                        return width;
                                }
                        }
                        
                        //function to count the radius of the bottom-left corner                        
                        function bottom_left(j2, i){
                                if(i>=(bottom-ugol4)){
                                        return ugol4-Math.floor(Math.sqrt(ugol4*ugol4-j2*j2));
                                }
                                else{
                                        return 0;                                 
                                }
                        }
                        
                        //...counting top corners
                        for(var i=0; i<=top; i++){
                                j1=ugol1-i; j2=ugol2-i;                         
                                $('#'+motherdiv+'_top_round_div_'+i).css('clip', 'rect('+i+', '+top_right(j2, i)+', '+(i+1)+', '+top_left(j1, i)+')');
                                if($.browser.msie && $.browser.version<8){$('#'+motherdiv+'_top_round_div_'+i).css('clip', 'rect('+i+' '+top_right(j2, i)+' '+(i+1)+' '+top_left(j1, i)+')');};
                        };
                        
                        //...counting bottom corners
                        for(var i=bottom; i>=0; i--){
                                top_offset=middle+top+i;
//                              j1=ugol3-bottom+i; j2=ugol4-bottom+i;   
                                j1=ugol3-bottom+i; j2=ugol4-bottom+i;
                                
                                $('#'+motherdiv+'_bottom_round_div_'+i).css('clip', 'rect('+top_offset+', '+bottom_right(j1, i)+', '+(top_offset+1)+', '+bottom_left(j2, i)+')');
                                if($.browser.msie && $.browser.version<8){$('#'+motherdiv+'_bottom_round_div_'+i).css('clip', 'rect('+top_offset+' '+bottom_right(j1, i)+' '+(top_offset+1)+' '+bottom_left(j2, i)+')');};
                        };                      
                        
                };});
        };
})(jQuery);

