// Used to transform embedded image widgets into 
//   dynamic iframe ones

setTimeout(function() {
  
  // Not all browsers have implemented this yet
  function getElementsByClassName(classname, node, tag) {
    // Default options
    if (node == null) node = document;
    if (tag == null) tag = '*';
    var result;
    
    if (node.getElementsByClassName) {
      // If this is implemented by the broswer then use its
      //   probably faster and better implementation
      var elements = node.getElementsByClassName(classname);
      
      // Copy elements from a NodeList to an array 
      //  so that mutation of the document
      //  doesn't affect the elements list.
      var len = elements.length;
      result = [];
      for(var i=0; i<len; i++) { 
        result[i] = elements[i]; 
      }
    
    } else {
      // Otherwise find the elements ourselves
      
      // First retrieve all of the nodes to test
      var elements = node.getElementsByTagName(tag);
      var num_elements = elements.length;
      var pattern = new RegExp("(^|\\s)"+classname+"(\\s|$)");
      
      // Then, evaluate each node to see if it matches, if
      //  it does then add it to the results
      result = [];
      for (var i = 0, j = 0; i < num_elements; i++) {
        if (pattern.test(elements[i].className)) {
          result[j] = elements[i];
          j++;
        }
      }
    }
    
    return result;
  }
  
  var placeholders = getElementsByClassName("fb-widget-placeholder");
  
  var len=placeholders.length;
  for (var i=0; i<len; i++) {
    // Replace each placeholder div with an iframe
    var placeholder = placeholders[i];
    var parent = placeholder.parentNode;
    
    // Check that the placeholder has all the neccessary
    //  dynamic parameters in order to switch in the iframe
    if (!placeholder || !placeholder.elements) continue;
    if (!placeholder.elements['src']) continue;
    if (!placeholder.elements['height']) continue;
    if (!placeholder.elements['width']) continue;
    
    // Create the iframe
    var iframe = document.createElement("iframe");
    
    // Dynamic params
    var src = placeholder.elements["src"].getAttribute("value");
    var height = placeholder.elements["height"].getAttribute("value");
    var width = placeholder.elements["width"].getAttribute("value");
    
    // Make sure that we have valid values for the parameters
    if (!src || !height || !width) continue;
    
    iframe.setAttribute("src", src);
    iframe.style.height = height+"px";
    iframe.style.width = width+"px";
    
    // Static params
    iframe.setAttribute("className", "fb-widget-iframe");
    iframe.setAttribute("allowtransparency", "true");
    iframe.setAttribute("scrolling", "no");
    iframe.setAttribute("frameborder", "0");
    iframe.style.border = "0";
    iframe.style.outline = "0";
    iframe.style.padding = "0";
    iframe.style.margin = "0";
    
    // replace the placeholder with the new iframe
    parent.replaceChild(iframe, placeholder);
  }
}, 0);