(function() {
  function setAttributes(el, attributes) {
    for (var attr in attributes){
      el.setAttribute(attr, attributes[attr]);
    };
  }
  var
    cont = document.getElementById('rs_fwidg'),
    pro_nid = 54055,
    domain = 'http://www.realself.com',
    linkcolor = typeof rs_link_color != 'undefined' ? rs_link_color : '',
    bodycolor = typeof rs_body_color != 'undefined' ? rs_body_color : '',
    h = {
      src: domain + '/widget-frame/' + pro_nid + '?f=y&fp='+encodeURIComponent(window.location.href.replace(/#[^#]+$/, ''))+'&linkcolor='+linkcolor+'&bodycolor='+bodycolor,
      frameBorder: 0,
      border: 'none',
      width: '100%',
      height: '100%',
      scrolling: 'no',
      allowTransparency: true
    },
    d = document.createElement('DIV');
    i = document.createElement('IFRAME');
  
  setAttributes(i,h);
  d.appendChild(i);
  cont.appendChild(d);
  
  cont.style.width = typeof rs_widget_width != 'undefined' ? rs_widget_width + 'px' : '585px';
  
  if (window.location.hostname != domain.replace(/http:\/\//, '')) {
	  setInterval(
	    function() {
	      if (window.location.hash && window.location.hash != "#") {
	        var h = window.location.hash.substring(1);
	        if (h.substr(0,1) == 'f') {
	          h = h.substr(1);
	          var set_pos = false;
	        }
	        else {
	          var set_pos = true;
	        }
	        
	        if (parseInt(d.style.height) != h) {
	          d.style.height = h + 'px';
	          d.style.display='none';d.style.display='block';//IE6
	          if (set_pos) {
	            setTimeout(function() {
	              window.scrollTo(0,RSabsTop(cont) - 10);
	            }, 10);
	          }
	        }
	      }
	    },
	    10
	  );
  }
  
  function RSabsTop(el) {
    var t = 0;
    var isDiv = /^div$/i.test(el.tagName);
    if (isDiv && el.scrollTop) {
      t = el.scrollTop;
    }
    t = el.offsetTop - t;
    if (el.offsetParent) {
      t += RSabsTop(el.offsetParent);
    }
    return t;
  };
  
  var links = cont.getElementsByTagName('A');
  var s = 'pro_hdr=' + pro_nid;
  for (var i in links) {
    if (links[i].protocol + '//' + links[i].hostname == domain) {
      var h = links[i].hash.length == 1 ? '' : links[i].hash;
      var es = links[i].search.replace(/\?/, '');
      es += es.length ? '&' + s : s;
      links[i].href = domain + '/' + links[i].pathname.replace(/^\//, '') + '#' + es + h;
    }
  }
})();