(function(bool) {
  if (bool) return;
  //window.sharedcopy_embed_script_loaded = true;
  var http_hostname = "http://sharedcopy.com";
  
  function set_onclick(ele, fn) {
    if ((ele.tagName || '').match(/embed|object/i)) return;
    if (ele.href && ele.href.match(http_hostname)) return ele.target = '_blank';
    if (ele.tagName) ele.onclick = fn;
    if (ele.childNodes && ele.childNodes[0]) {
      for (var x = 0; ele.childNodes[x]; x++) {
        set_onclick(ele.childNodes[x], fn);
      }
    }
  }
  
  function get_blockquote(evt, forced) {
    if (! evt) evt = window.event;
    var blockquote = evt.target || evt.srcElement;
    if (!forced && blockquote.href && blockquote.target) return null;
    while (blockquote && blockquote.parentNode && (! blockquote.parentNode.id)) {
      blockquote = blockquote.parentNode;
    }
    return blockquote;
  }
  
  function on_mouseover(evt) {
    if (window.shcp) return true;
    var blockquote = get_blockquote(evt, true);
    if (!blockquote) return true;
    // if (navigator.userAgent && navigator.userAgent.match(/MSIE/)) return true;
    blockquote.style.borderTopColor = blockquote.style.borderRightColor = blockquote.style.borderBottomColor = "#BBBBBB";
    blockquote.style.borderLeftColor = "#BBBBBB";
    var ldquo = blockquote.firstChild;
    ldquo.style.opacity = "0.45";
    ldquo.style.filter = "alpha(opacity=45)";
  }
  
  function on_mouseout(evt) {
    if (window.shcp) return true;
    var blockquote = get_blockquote(evt, true);
    if (!blockquote) return true;
    // if (navigator.userAgent && navigator.userAgent.match(/MSIE/)) return true;
    blockquote.style.borderTopColor = blockquote.style.borderRightColor = blockquote.style.borderBottomColor = "#FFFFFF";
    blockquote.style.borderLeftColor = "#EEEEEE";
    var ldquo = blockquote.firstChild;
    ldquo.style.opacity = "0.15";
    ldquo.style.filter = "alpha(opacity=15)";
  }
  
  function on_click(evt) {
    if (window.shcp) return true;
    var now = (new Date()).getTime();
    var blockquote = get_blockquote(evt);
    if (!blockquote) return true;
    var div_id = blockquote.parentNode.id;
    blockquote.parentNode.name = div_id;
    
    var config = eval(div_id.replace('shcp_', 'json_'));
    var child_div_id = div_id + "_child";
    var dom = document.getElementById(child_div_id);
    if (dom) {
      if ((dom.last_evt + 1000) > now) return false;
      window.location.hash = "#" + div_id;
      if (dom.style.marginLeft == "0px") {
        dom.style.marginLeft = "-9" + config.width;
        blockquote.style.height = blockquote.old_height;
      } else {
        dom.style.marginLeft = "0px";
        blockquote.style.height = config.height;
      }
    } else {
      var old_dimensions = parseInt(config.width) + "." + parseInt(config.height);
      config.max_width = (window.innerWidth || (window.document.documentElement && window.document.documentElement.clientWidth) || document.body.offsetWidth);
      config.width = parseInt(config.max_width * 0.95) + 'px';
      config.max_height = (window.innerHeight || (window.document.documentElement && window.document.documentElement.clientHeight) || document.body.offsetHeight);
      config.height = (config.max_height * 0.8) + 'px';
      config.src = config.src.replace(old_dimensions, parseInt(config.width) + "." + (parseInt(config.height) - 8));
      if (window.console) console.log("config", config);
      var match = config.src.match(/([0-9a-f]{6})\.([0-9a-f]{6})\.([0-9a-f]{6})/);
      var colorfg   = match ? match[1] : 'ffffff';
      var colorbg   = match ? match[2] : 'ffffff';
      var colorlink = match ? match[3] : 'cc0500';
      var link_colorstyle = "color: #" + colorlink;
      dom = document.createElement('div');
      dom.id = child_div_id;
      dom.className = "k_deleteme";
      dom.innerHTML = "" +
          "<iframe src='" + config.src + "?t=" + Math.random() + "' " +
          "class='sharedcopy_embed' " +
          "style='width: " + config.width + "; height: " + (parseInt(config.height) - 8) + "px; " + 
          "border: 0px; padding: 0px; margin 0px; background: #" + config.background + "' " +
          "frameborder='0' scrolling='no'></iframe>";
      dom.style.left = parseInt((config.max_width - parseInt(config.width)) / 4) + 'px';
      document.body.appendChild(dom);
      window.location.hash = "#" + div_id;
      dom.style.top  = (window.scrollY || document.documentElement.scrollTop) + 'px';
      window.scrollBy(0, -1 * parseInt(config.max_height * 0.1));

      var action_div = document.createElement("div");
      action_div.style.height = "0px";
      action_div.title = "Hide this embed";
      action_div.innerHTML = '' +  
        '<div style=\'font-family: "Candara", "Lucida Grande", "Trebuchet MS", sans-serif; padding: 1px 5px; ' +  
        'font-size: 11px; font-decoration: underline; border: 0px #ccc outset; position: absolute; top: ' + 2 + 'px' + '; left: ' + 0 +  
        'px; width: 80px; background: ' + config.bgcolor + '; text-align: left; \'>' +  
        '<img alt="Hide this embed" title="Hide this view port in your window" style="position: absolute; top: -19px; left: -19px; " ' +  
        'src="http:\/\/' + config.host + '\/static\/embed\/close.png" \/><\/div>' + 
        '</div>' +
        '';        

      action_div.onclick = function(evt) {
        dom.style.marginLeft = "-9" + config.width;
        blockquote.style.height = blockquote.old_height;
        return false;
      };
      dom.appendChild(action_div);
      
      dom.style.position = 'absolute';
      dom.style.color = '#' + colorlink;
      dom.style.background = '#' + colorbg;
      dom.style.margin = "0px";
      dom.style.border = "8px #BBBBBB solid";
      // dom.style.borderLeft = "15px #EEEEEE solid"; 
      // dom.style.borderBottom = "10px #EEEEEE solid";
      dom.style.padding = "0px";
      blockquote.old_height = blockquote.style.height;
      blockquote.style.height = config.height;
    }
    
    if (evt && evt.preventDefault) {
        evt.preventDefault();
        evt.stopPropagation();
    } else if (typeof(evt) != 'undefined') {
        evt.cancelBubble = false;
        evt.returnValue = false;
    }
    dom.last_evt = now;
    return false;
  }
  
  setTimeout(function() {
  var blockquotes = document.getElementsByTagName('blockquote');
  for (var x = 0; blockquotes[x]; x++) {
    var blockquote = blockquotes[x];
    if (blockquote.parentNode && blockquote.parentNode.id && blockquote.parentNode.id.match(/^shcp_/)) {
      if (window.console) console.log("blockquotes", x, blockquote.processed, blockquote, blockquote.parentNode.id);
      var div_id = blockquote.parentNode.id;
      var config = eval(div_id.replace('shcp_', 'json_'));
      if (! blockquote.processed) {
        blockquote.processed = true;
        blockquote.title = "Click to view embedded website!";
        blockquote.style.overflow = 'hidden';
        blockquote.style.background = '#ffffff';
        blockquote.style.color = '#000000';
        var ldquo = document.createElement('div');
        ldquo.style.background = "url(" + http_hostname + "/static/embed/ldquo.gif) #FFFFFF top left no-repeat";
        ldquo.style.height = "33px"; 
        ldquo.style.opacity = "0.15";
        ldquo.style.filter = "alpha(opacity=15)";
        ldquo.style.position = "relative";
        ldquo.style.top = "0px";
        ldquo.style.left = "0px";
        blockquote.insertBefore(ldquo, blockquote.firstChild);
        blockquote.onmouseover = on_mouseover;
        blockquote.onmouseout = on_mouseout;
        set_onclick(blockquote, on_click);
      }
    } else if (blockquote.className && blockquote.className.match("comment_body")) {
      blockquote.style.textAlign = 'justify';
    }
  }
  
  }, 500);
})(window.sharedcopy_embed_script_loaded || window.shcp);
