Function.prototype.bind_inline = function( binded )
{ 
  var me = this; 
  var func = function() {
      return me.apply(binded, arguments); 
  }; 
  return func; 
};


var RBjs_Magic_Iframe = new function()
{
    var me = this;

    this.iframes = {};
    this.iframes_ini = 0;
    this.main_window = window.parent;
    this.iframe_delays = {};
    
    this.sw = 0;
    
    this.delay = function()
    {
        //this = here must be child_window object - see binded call
        this.setTimeout( me.resizeIframe.bind_inline( this ), me.iframe_delays[this.location.href] );    
    }
    
    this.resizeIframe = function()
    {
        var reg = /rb\.ru:?[0-9]*(\/__new_static\/abcd\/.*)/i;
        var reg2 = /(\/__new_static\/abcd\/.*)/i;
       
        // здесь this = window дочернего фрейма (так как запуск этого метода идет из его контекста - см. бинд)    
        var cur_window = this;
        var iframes = me.main_window.document.getElementsByTagName('iframe');
        var this_href = cur_window.document.location.href;
        var ttop = me.cumulativeOffset( cur_window.document.getElementById( 'banner_test' ) );
        ttop = ttop['top'];
        var this_iframe = 0;
            
        if( reg.exec( this_href ) ) {
            this_href = RegExp.$1
        }        
        
        for( var i=0; i < iframes.length; i++) { 
            i_url = me.readAttribute( iframes[i], 'src' );            
            if( reg2.exec( i_url ) ) {
                if( RegExp.$1 == this_href ) {
                    this_iframe = iframes[i];
                    break;
                }
            } 
        }

        if( this_iframe.id == 'abc_zone_1' ) {
        }

        if (ttop <= 0) {
            ttop = 0;
            this_iframe.style.display = 'none';
            
            //зануление дивов с отступами
            var div_br = me.main_window.document.getElementById( this_iframe.id + '_bottom_br' );
            if( div_br ) {
                div_br.style.display = 'none';
            }
            
        } else {
            var max_he = parseInt( this_iframe.style.height );
            if( !isNaN( max_he ) && (ttop>max_he) ) {
                ttop = max_he;
            }
            
            this_iframe.style.display = '';
        }

        this_iframe.setAttribute('height', ttop);
        this_iframe.style.height = ttop+'px';
        

    }

    /* from prototype 1.6 
       source: http://www.prototypejs.org/
    */
    this.cumulativeOffset = function(element)
    {
        var valueT = 0, valueL = 0;
        do {
          valueT += element.offsetTop  || 0;
          valueL += element.offsetLeft || 0;
          element = element.offsetParent;
        } while (element);
        return {'top': valueT, 'left': valueL};
    }
    
    /* from prototype 1.5 - 1.6 
       source: http://www.prototypejs.org/
    */
    this.readAttribute = function( dom_element, name )
    {
        var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
        var isIE    = !!window.attachEvent && !isOpera;
    
        if( isIE ) {
          if (!dom_element.attributes) return null;

          var t_names = {
            colspan:   "colSpan",
            rowspan:   "rowSpan",
            valign:    "vAlign",
            datetime:  "dateTime",
            accesskey: "accessKey",
            tabindex:  "tabIndex",
            enctype:   "encType",
            maxlength: "maxLength",
            readonly:  "readOnly",
            longdesc:  "longDesc"
          };
          
          if( t_names[name] ) name = t_names[name];
          var attribute = dom_element.attributes[name];
          return attribute ? attribute.nodeValue : null;
        }
        
        return dom_element.getAttribute( name );
    } 
    
    
    this.execMagicIframe = function( child_window, delay )
    {
        var delay = delay == null ? 0 : delay; 
        if( delay == -1 ) return; // не схлопывать баннер никогда
        
        child_window.document.write('<div id="banner_test" style="position: relative; clear:both; font-size:0;height:0;line-height:0;"></div>');

        if( delay ) {
            me.iframe_delays[child_window.location.href] = delay; 
            child_window.onload = me.delay.bind_inline( child_window );
        } else {
            child_window.onload = me.resizeIframe.bind_inline( child_window );
        }
    }
    
}