if('undefined' == typeof(Ethnio)) {
  Ethnio = {};
}

Ethnio.head = function(element) {
  document.getElementsByTagName('head')[0].appendChild(element);
}

Ethnio.load_css = function(file) {
  var link = document.createElement('link');
      link.href = file;
      link.rel  = 'stylesheet';
      link.type = 'text/css';
  Ethnio.head(link);
}

Ethnio.observe = function(element, eventName, handler) {
  if (element.addEventListener) {
    element.addEventListener(eventName, handler, false);
  }
  else {
    element.attachEvent("on" + eventName, handler);
  }
}


  Ethnio.get_cookie = function( cookie_name ) {
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
    if ( results ) {
      return ( unescape ( results[2] ) );
    }
    else {
      return null;
    }
  }

  Ethnio.set_cookie = function() {
    var domain = document.domain;
    var domainString = (domain == 'localhost' ? '' : 'domain=' + domain + ';');
    var today = new Date();
    var cookieexpiration = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000);

    document.cookie = "ethnio_displayed=yes; expires=" + cookieexpiration.toGMTString() + ";path=/;" + domainString;
  }

  Ethnio.currently_displayed = function() {
    return (document.getElementById('ethnio-screener') != null);
  }

  Ethnio.previously_displayed = function() {
    return (Ethnio.get_cookie('ethnio_displayed') == 'yes');
  }

  Ethnio.wheel_of_fortune = function() { 
    var wedge = Math.floor(Math.random() * 1);
    return (wedge == 0);
  }

  Ethnio.should_display = function() {
    var active = false;
    return (active && Ethnio.wheel_of_fortune() && !Ethnio.currently_displayed() && !Ethnio.previously_displayed());
  }


// Ethnio.base_url = location.protocol + '//192.168.1.107:3000';
Ethnio.base_url = location.protocol + '//ethnio.com';

Ethnio.show = function() {
  if(Ethnio.should_display()) {
    Ethnio.load_css(Ethnio.base_url + "/stylesheets/static/remote_reset.css");

    Ethnio.screener = document.createElement('div');
    Ethnio.screener.setAttribute('id', 'ethnio-screener');
    Ethnio.screener.style.top      = "5px";
    Ethnio.screener.style.left     = "376px";
  
    var close_button = document.createElement('a');
        close_button.innerHTML = '<img style="border: 0px" alt="close" src="' + Ethnio.base_url + '/images/screener-header.gif" />';
        close_button.href      = "#";
        close_button.onclick        = function() { Ethnio.close(); }
    Ethnio.screener.appendChild(close_button);
  
    var iframe = document.createElement('iframe');
        iframe.id     = 'ethnio-screener-id';
        iframe.src    = Ethnio.base_url + '/remotes/31762/edit?';
        iframe.style.border = 0;
        iframe.frameBorder  = 0;
        iframe.setAttribute('allowTransparency', 'true');
        iframe.setAttribute('scrolling', 'no');
    Ethnio.screener.appendChild(iframe);

    document.body.appendChild(Ethnio.screener);

    Ethnio.set_cookie();
  }
}

Ethnio.close = function() {
  document.body.removeChild(Ethnio.screener);
}

Ethnio.observe(window, 'load', function() { Ethnio.show(); });
