
//Force IE6 to cache Images 
(function(){  var m = document.uniqueID /*IE*/ && document.compatMode /*>=IE6*/ && !window.XMLHttpRequest /*<=IE6*/ && document.execCommand ; try{ if(!!m){ m("BackgroundImageCache", false, true) /* = IE6 only */ } }catch(oh){}; })();



var RoundImage =  new function(){
    
    var isOpera = !!window.opera ;
    var isSafari = (window.navigator.userAgent.indexOf('Safari')>=0);        
    var isIE = !!( document.expando && document.uniqueID  && document.compatMode );
    
    if(isIE){
       document.namespaces.add("v", "urn:schemas-microsoft-com:vml"); 
    };
    
    /***********
     * Warning!
     * Using VML may create extra HttpRequest to request image to fill the VML element     
     * For the consideration of the performance of your application, please talk with your visual designer 
     * and see if they can cook images for you first.
     **/
    var p_renderVML = function(dIMG , nRadius){
        
        var nW = dIMG.width  ;
        var nH = dIMG.height ;
        
        nRadius = isNaN( nRadius )?0:nRadius;
        if( !nRadius ){ return false;};        
        
        nRadius = nRadius*0.5;
        
        var nRadiusW = Math.min( nRadius / nW );
        var nRadiusH = Math.min( nRadius / nH );
        
        
        nRadius  = Math.floor(  Math.min( nRadiusW ,nRadiusH ) * 100 );
        nRadius = Math.max( 0 , nRadius );
        
        
        nRadius = Math.min( nRadius , 50 );
        nRadius = nRadius + '%';
        var sDisplay =  ( dIMG.currentStyle.display.toLowerCase() == 'block' )?'block':'inline-block';        
       
        //v:rect
        var dVML = document.createElement( [ '<var style="zoom:1;overflow:hidden;;display:',sDisplay,';width:',nW  ,'px;height:',nH ,'px;padding:0;">' ].join('') );
        var sFloat =  dIMG.currentStyle.styleFloat.toLowerCase();
        sDisplay = (sFloat == 'left' || sFloat == 'right')?'inline':sDisplay;
     
         
        
        dVML.innerHTML =[ 
        '<v:roundrect arcsize="',nRadius,'"  strokeweight="0" filled="t" stroked="f" fillcolor="#ffffff" style="zoom:1;margin:-1px 0 0 -1px;behavior:url(#default#VML);display:',sDisplay ,';width:',nW,'px;height:',nH,'px;">' ,
        '<v:fill style="behavior:url(#default#VML);" src="', dIMG.src ,'" type="tile" />',
        '</v:roundrect>'].join('');
       
        //dVML.innerHTML = 'foo';
        
        dVML.className =  dIMG.className ;
        dVML.title = dIMG.getAttribute('alt') || dIMG.getAttribute('title') || '';
                
        dIMG.insertAdjacentElement('BeforeBegin' , dVML); 
        dIMG.style.display = 'none'; 
      
    };
    
    
    
    var p_render = function( dIMG , nRadius , oIMG  ){
        
        
       
        var dCanvas = document.createElement('canvas');
        if(!dCanvas.getContext){ return };
        
        var nW = dIMG.width  ;
        var nH = dIMG.height 
        
        nRadius = nRadius || 0 ;
        
        //console.log( nRadius )
       
        nRadius = Math.min( nW/1.4 ,  nRadius );
        nRadius = Math.min( nH/1.4 ,  nRadius );        
        nRadius  = Math.floor(nRadius);
        
      
        var nRadius_2 = nRadius/2 ;
    
        if( isSafari ){
            
            dIMG.width  = 0;
            dIMG.height = 0;
            dIMG.style.position = 'absolute';
            dIMG.style.visibility = 'hidden';
            
        }else{
            dIMG.style.display = 'none';
        }
       
       
       
        dCanvas.setAttribute('width',nW);
        dCanvas.setAttribute('height',nH);
        
        dCanvas.width = nW;
        dCanvas.height = nH;
        
        dCanvas.className =  dIMG.className ;
        dCanvas.title = dIMG.getAttribute('alt') || dIMG.getAttribute('title') || '';
        dIMG.parentNode.insertBefore( dCanvas , dIMG );
       
        
        var ctx = dCanvas.getContext("2d");
        ctx.clearRect(0, 0, nW, nH );
        ctx.fillStyle =ctx.createPattern(oIMG || dIMG, "repeat"),
        ctx.strokeStyle = ctx.createPattern(oIMG || dIMG, "repeat"),
        ctx.lineWidth = nRadius;
        ctx.lineJoin = "round";
        
        if( isOpera ){
              //Opera tile background from the center, which is really annoying!!!!              
              ctx.translate( -nW/2 ,  -nH/2 );
              ctx.fillRect( nRadius_2  + nW/2 , nRadius_2 + nH/2 , nW -  nRadius  , nH - nRadius );
              ctx.strokeRect( nRadius_2  + nW/2 , nRadius_2  + nH/2 , nW -  nRadius  , nH - nRadius  );
              ctx.translate( nW/2 ,  nH/2 );
        
        }else{
            ctx.fillRect( nRadius_2, nRadius_2 , nW -  nRadius  , nH - nRadius );
            ctx.strokeRect( nRadius_2 , nRadius_2 , nW -  nRadius  , nH - nRadius  );
        };        
    };
    
    
    var p_nBufferedTimer;
    var p_aBufferedImages = [];
    var p_processBuffer = function(){
        
        var dIMG = p_aBufferedImages.shift(); 
        if( dIMG ){
         
              //This is really bad, since Safari only accept Image Object instead of Image Element here
              //To Avoid potential extra Http:request foo Safari, maybe we should disable this feature for Safari;
              var oIMG = new Image(dIMG.width , dIMG.height ); // Safari need Object instead of DomElement to createPattern;                       
              
              oIMG.src = dIMG.src ;
              p_render( dIMG , dIMG.__radius ,  oIMG  );
              dIMG = undefined; 
              oIMG = undefined;            
                                    
             if( p_aBufferedImages.length){
                 p_nBufferedTimer = setTimeout( p_processBuffer ,0 );
             };
        };          
    };
    
    var p_befferedRender = function(dIMG , nRadius){
    
        if(p_nBufferedTimer ){
           clearTimeout( p_nBufferedTimer);
        }
          
        //Poor safari, we need to do it slowly as possible as Safari does.
        p_aBufferedImages.push(dIMG);
        dIMG.__radius = nRadius ;          
        p_nBufferedTimer = setTimeout( p_processBuffer , 100 );
          
       
    }
    
    
    /*
    Optional  Methods
    */
    this.toVisualDesigners = function(){      
      var n = 10;
      while( n ){
        if( n ){
            var c = confirm("Don't abuse it! Please"); 
            if(c){  n-- ;};
        };    
      }; 
    }; 
    
    
    /*
    Public Methods
    */
    
    this.render = function( dIMG , nRadius  ){
         
         if(dIMG.__hasCanvasCorner){return};
         dIMG.__hasCanvasCorner = 1;
         
         if(isIE){
           
            p_renderVML(dIMG , nRadius);
            p_befferedRender = undefined;
         
         }else if( isSafari ){  
            
            p_renderVML =  undefined;
            p_befferedRender(dIMG , nRadius );  
         
         }else{
            //Opera , Gecko
            p_befferedRender = undefined;
            p_renderVML   = undefined;
            p_render(dIMG , nRadius );  
         
         };          
    } 
}

