var ResizeIframe = {    
  wrapperId : "tinc_content",
                
  resize : function ( oDoc, sId ) 
  {
    try
    {
      if ( typeof oDoc == "undefined" ) return;
  
      if ( typeof sId != "undefined" ) 
      {
        var iframe = oDoc.getElementById( sId );
        var iframeDoc = iframe.contentWindow.document;
      } else {
        var iframeDoc = oDoc;
        var iframeWindow = iframeDoc.parentWindow || iframeDoc.defaultView;
        var iframe = iframeWindow.frameElement;
      }
      
      // Resize only once
      if ( iframeDoc.isResized == true ) return;
      iframeDoc.isIntegrated = true;  
      
      // Remove scrollbar. Set padding and margin of the included document to zero. 
      iframe.scrolling = "no";
      iframeDoc.body.style.margin = 0;
      iframeDoc.body.style.padding = 0;
            
      if ( window.addEventListener )
      {
        var wrapper = iframeDoc.getElementById( this.wrapperId );
        if ( wrapper.clientWidth > iframe.clientWidth && iframe.getAttribute( "wfx:resizeContentWidth" ) == "true" )
        {
          wrapper.style.width = ( iframe.clientWidth - 1 ) + "px";
        } else {
          iframe.style.width = ( wrapper.clientWidth + 1 ) + "px";
        }
        
        // Setting the top padding of the wrapper avoids wrong calculation of the height due to collapsing margins behavior
        wrapper.style.paddingTop = "1px";
        iframe.style.height = wrapper.clientHeight + "px";
      } else if ( window.attachEvent ) 
      {
        var wrapper = iframeDoc.getElementById( this.wrapperId );
        if ( wrapper.offsetWidth > iframe.offsetWidth && iframe.getAttribute( "wfx:resizeContentWidth" ) == "true" )
        {
          wrapper.style.width = iframe.offsetWidth + "px";           
        } else {
          iframe.style.width = wrapper.offsetWidth + "px";
        }
        
        // Fix weird behavior
        wrapper.style.height = "0px"; 
        var height = wrapper.offsetHeight;
        
        iframe.style.height = height + "px";
      }
    } catch ( ex )
    {
      return; 
    }
    
    // let non-tinc links load in the parent document. this might need some adjustment
    var links = iframeDoc.getElementsByTagName( "a" );
    for ( var i = 0; i < links.length; i++ )
    {
      if ( links[i].href.indexOf( "/tinc" ) == -1 ) links[i].target = "_parent";
    }
    
    // Special case: WebElements forms might do redirection to another page after submission. That's indicated by a non-empty
    // 'redirectionEnabled' attribute of a submit button. 
    var inputs = iframeDoc.getElementsByTagName( "input" );
    for ( var i = 0; i < inputs.length; i++ )
    {
      var input = inputs[i];
      if ( input.getAttribute("redirection") != null && input.getAttribute("redirection") != '' ) 
        input.form.target = "_parent";
    }
  }
};
