var ResizeIframe = {    
  wrapperId : "tinc_content",
                
  resize : function ( oDoc, sId ) 
  {
    try
    {
      var iframe = oDoc.getElementById( sId );
      var iframeDoc = iframe.contentWindow.document;
    } catch ( ex )
    {
      return; 
    }
    
    // Switch off scrollbars, 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 )
    {
      // Search for wrapper element with width being set and set including iframe's width accordingly
      var wrapper = iframeDoc.getElementById( this.wrapperId );
      if ( wrapper )
      {
        var width = parseInt( iframeDoc.defaultView.getComputedStyle( wrapper, "" ).getPropertyValue( "width" ) );  
        var iframeWidth = parseInt( iframe.ownerDocument.defaultView.getComputedStyle( iframe, "" ).getPropertyValue( "width" ) );
        if ( width > iframeWidth && iframe.getAttribute( "shrinkToHost" ) == "true" )
        {
           wrapper.style.width = iframeWidth + "px";
        } else {
          iframe.style.width = width + "px";
        }
      }                    
      
      // Get height and set including iframe's height
      var height = iframeDoc.body.clientHeight;
      iframe.style.height = height + "px";
      iframe.style.display = "block"; // fixes a gecko display problem
    } else if ( window.attachEvent ) 
    {
      var wrapper = iframeDoc.getElementById( this.wrapperId );
      if ( wrapper )
      {
        var width = wrapper.offsetWidth;  
        if ( width > iframe.offsetWidth && iframe.getAttribute( "shrinkToHost" ) == "true" )
        {
          wrapper.style.width = iframe.offsetWidth + "px";           
        } else {
          iframe.style.width = width + "px";
        }
        
        // Fix weird behavior
        wrapper.style.height = "0px"; 
        var height = wrapper.offsetHeight;
        iframe.style.height = height + "px";
      }
    }
    
    // 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";
    }
  }
};
