/**
 *This handles the rounded corners fix on IE (or any browser that does'nt
 *support rounded corners
 */
//=============================================================================>
function IEFixer(){
  
  this.auto_detect = false;
  
  this.map = {
    'BorderRadius04': {
      'paintBottom': 'yes',
      'paintTop':    'yes',
      'radius':      '4'
    },
    'BorderRadius05': {
      'paintBottom': 'yes',
      'paintTop':    'yes',
      'radius':      '5'
    },
    'BorderRadius09': {
      'paintBottom': 'yes',
      'paintTop':    'yes',
      'radius':      '9'
    },
    
    'BorderRadius_Bottom_04': {
      'paintBottom': 'yes',
      'paintTop':    'no',
      'radius':      '4'
    },
    'BorderRadius_Bottom_05': {
      'paintBottom': 'yes',
      'paintTop':    'no',
      'radius':      '5'
    },
    'BorderRadius_Bottom_09': {
      'paintBottom': 'yes',
      'paintTop':    'no',
      'radius':      '9'
    },
    /*
    'BorderRadius_Left_04': {
    },
    'BorderRadius_Left_05': {
    },
    'BorderRadius_Left_09': {
    },
    
    'BorderRadius_Right_04': {
    },
    'BorderRadius_Right_05': {
    },
    'BorderRadius_Right_09': {
    },
    */
    'BorderRadius_Top_04': {
      'paintBottom': 'no',
      'paintTop':    'yes',
      'radius':      '4'
    },
    'BorderRadius_Top_05': {
      'paintBottom': 'no',
      'paintTop':    'yes',
      'radius':      '5'
    },
    'BorderRadius_Top_09': {
      'paintBottom': 'no',
      'paintTop':    'yes',
      'radius':      '9'
    }
  };
  
  this.holder = [];
  
  this.queue = [];
  
  this.timeout = 2000;
};
//#############################################################################>
/**
 *Are rounded corners supported?
 */
IEFixer.prototype.autodetect = function(){
  
  var body = dojo.query( 'html body' )[0];
          
  dojo.addClass( body, 'BorderRadius09' );
  
  var css3    = dojo.style( body, 'borderRadius'       );
  var mozilla = dojo.style( body, 'MozBorderRadius'    );
  var webkit  = dojo.style( body, 'WebkitBorderRadius' );
  
  if( (typeof css3    != 'undefined') ||
      (typeof mozilla != 'undefined') ||
      (typeof webkit  != 'undefined')   ){
    this.auto_detect = true;
  }else{
    this.auto_detect = false;
  }
  
  dojo.removeClass( body, 'BorderRadius09' );
  
  return this.auto_detect;
};
//#############################################################################>
/**
 *Allow for delayed instantiation and loading of a widget
 *Possible long runing memory leak here
 */
IEFixer.prototype.delayedLoad = function(){
  
  var temp_queue = [];
  
  //Instantiate the visible elements
  while( this.queue.length > 0 ){
    
    var item = this.queue.shift();
    
    if( (dojo.hasClass(item.element, 'NoShow'    )            ) ||
        (dojo.style(   item.element, 'visibility') == 'hidden')   ){
      
      temp_queue[ temp_queue.length ] = item;
      
    }else{
      
      this.replacer( item.element, this.map[item.className], item.className );
    }
  }
  
  //The remaining elements not visible
  this.queue = temp_queue;
  
  //Poll again later
  window.setTimeout(
    dojo.hitch( this, 'delayedLoad'),
    this.timeout
  );
};
//#############################################################################>
/**
 *Get the ball rolling
 */
IEFixer.prototype.initialiser = function(){
  
  if( this.autodetect() ){ return; }
  
  //Extract all the possible replacements
  for( var k in this.map ){
    
    var elements = dojo.query( ('.' + k) );
    
    for( p = 0; p < elements.length; p++ ){
      this.queue[ this.queue.length ] = { 'className': k, 'element': elements[p] };
    }
  }
  
  //Initial load delayed
  window.setTimeout(
    dojo.hitch( this, 'delayedLoad'),
    this.timeout
  );
};
//#############################################################################>
/**
 *Replace an element with the appropriate rounded corner widget
 */
IEFixer.prototype.replacer = function( element, settings, cssClass ){
  
  //Detect the IE version
  var ie_version = dojo.isIE;
  ie_version = ((typeof ie_version == 'undefined') ? 0 : ie_version);
  
  //Handle special classes
  if( dojo.hasClass( element, 'Xenomorph_RoundedCorners') )
    return;
  
  if( dojo.hasClass( element, 'Xenomorph_InputAndPrompt') )
    return;
  
  if( dojo.hasClass( element, 'IEFixer_BorderRadiusInhibit') )
    return;
  
  //IE 6 only
  if( (ie_version > 0) && (ie_version <= 6) && dojo.hasClass( element, 'IEFixer_BorderRadiusInhibit_IE6') )
    return;
  
  //IE 7 only
  if( (ie_version > 0) && (ie_version <= 7) && dojo.hasClass( element, 'IEFixer_BorderRadiusInhibit_IE7') )
    return;
  
  //IE 8 only
  if( (ie_version > 0) && (ie_version <= 8) && dojo.hasClass( element, 'IEFixer_BorderRadiusInhibit_IE8') )
    return;
  
  //Extract classes
  var classes = element.className;
  
  //Extract styles
  var styles  = {};
  styles.backgroundColor = dojo.style( element, 'backgroundColor' );
  styles.color           = dojo.style( element, 'color'  );
  
  styles.borderColor = dojo.style( element, 'borderTopColor' );
  styles.borderWidth = dojo.style( element, 'borderTopWidth' );
  
  styles.clear   = dojo.style( element, 'clear'   );
  styles.display = dojo.style( element, 'display' );
  styles.float   = dojo.style( element, 'float'   );
  
  styles.marginBottom = dojo.style( element, 'marginBottom' );
  styles.marginLeft   = dojo.style( element, 'marginLeft'   );
  styles.marginRight  = dojo.style( element, 'marginRight'  );
  styles.marginTop    = dojo.style( element, 'marginTop'    );
  
  styles.height = dojo.style( element, 'height' );
  styles.width  = dojo.style( element, 'width'  );
  
  styles.paddingBottom = dojo.style( element, 'paddingBottom' );
  styles.paddingLeft   = dojo.style( element, 'paddingLeft'   );
  styles.paddingRight  = dojo.style( element, 'paddingRight'  );
  styles.paddingTop    = dojo.style( element, 'paddingTop'    );
  
  //console.log( '======>' );
  //console.log( element );
  //console.log( classes );
  //console.log( styles.borderColor );
  //console.log( dojo.style( element, 'borderColor' ) );
  //console.log( dojo.style( element, 'borderBottomColor' ) );
  //console.log( dojo.style( element, 'borderLeftColor' ) );
  //console.log( dojo.style( element, 'borderRightColor' ) );
  
  //Box
  var content_box  = dojo.doc.createElement( 'div' );
  var position_box = dojo.doc.createElement( 'div' );
  
  //Swap out the current element
  var parent = element.parentNode;
  parent.replaceChild( position_box, element );
  
  position_box.appendChild( content_box );
  content_box.appendChild(  element     );
  
  //Fix styles
  dojo.style(
    position_box,
    { 'clear':   styles.clear,
      'display': styles.display,
      'float':   styles.float,
  
      'marginBottom': styles.marginBottom + 'px',
      'marginLeft':   styles.marginLeft   + 'px',
      'marginRight':  styles.marginRight  + 'px',
      'marginTop':    styles.marginTop    + 'px',
      
      'padding': '0',
  
      'width':   styles.width + styles.paddingLeft + styles.paddingRight + 'px',
      'backgroundColor': 'transparent'
    }
  );
  dojo.style( content_box,
              { 'width':  styles.width + 'px',
                
                'margin': 0,
                
                'paddingBottom': styles.paddingBottom  + 'px',
                'paddingLeft':   styles.paddingLeft    + 'px',
                'paddingRight':  styles.paddingRight   + 'px',
                'paddingTop':    styles.paddingTop     + 'px',
                
                'backgroundColor': styles.backgroundColor,
                'color':           styles.color
              }
             );
  
  dojo.style( element,
              { 
                'border':  'none',
                'margin':  '0',
                'padding': '0'
                /*
                'backgroundColor': 'transparent',
                'borderBottom': 'none',
                'borderTop':    'none'*/
              }
             );
  
  if( styles.paddingBottom ){
    dojo.style( content_box, 'paddingBottom', '1px' );
    dojo.style( element,     'paddingBottom', '1px' );
  }
  
  if( styles.paddingTop ){
    dojo.style( content_box, 'paddingTop', '1px' );
    dojo.style( element,     'paddingTop', '1px' );
  }
  
  
  dojo.addClass( content_box, classes );
  
  dojo.removeClass( element, cssClass );
  
  this.holder[ this.holder.length ] = 
    new xenomorph.RoundedCorners( { 'cssOnly': 'no',
                                    
                                    'backgroundColour': 'transparent',
                                    'foregroundColour': styles.backgroundColor,
                                    
                                    'paintBottom': settings['paintBottom'],
                                    'paintTop':    settings['paintTop'   ],
                                    'radius':      settings['radius'     ]/* */,
                                    
                                    'borderColour': ( (styles.borderColor) ? styles.borderColor : ''),
                                    'borderWidth':  ( (styles.borderWidth) ? styles.borderWidth :  0)
                                    /* */
                                  },
                                  content_box
                                 );
  
  this.holder[ this.holder.length - 1].startup();
  
  /*
  //Fix element styles
  dojo.style( this.holder[ this.holder.length - 1].domNode,
              { 'margin':        '0',
                'paddingBottom': '0',
                'paddingTop':    '0'
              }
           );
  */
  
};
//#############################################################################>
//=============================================================================>
IEFixer.instance = new IEFixer();
//=============================================================================>
dojo.addOnLoad( dojo.hitch(IEFixer.instance, 'initialiser') );
//=============================================================================>

