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

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

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


  Ethnio.should_display = function() {
    return true;
  }


Ethnio.base_url = location.protocol + '//ethn.io';
Ethnio.dnd_enabled = true;
Ethnio.inlinePreview = false;

Ethnio.show = function() {
  if (Ethnio.should_display()) {
    Ethnio.screener = document.createElement('div');
    if (Ethnio.dnd_enabled && !Ethnio.inlinePreview) Ethnio.screener.setAttribute('onmousedown', "startdrag(this, event);");
    Ethnio.screener.setAttribute('id', "ethnio-screener-63162");
    
      Ethnio.screener.style.cssText = 'z-index:16777271;line-height:normal;letter-spacing:-0.4px;font-family: \'Helvetica Neue\', Helvetica, Arial, sans-serif;padding:50px 50px 28px;     width:500px;                   position:absolute;background:#fff;border:1px solid #d1d1d1;-moz-border-radius:3px;-webkit-border-radius:3px;-o-border-radius:3px;border-radius:3px;-moz-box-shadow:0 4px 15px #9a9a9a;-webkit-box-shadow:0 4px 15px #9a9a9a;-o-box-shadow:0 4px 15px #9a9a9a;box-shadow:0 4px 15px #9a9a9a;top:100px;';
      if (Ethnio.dnd_enabled && !Ethnio.inlinePreview){
        Ethnio.screener.style.top  = "21%";
        Ethnio.screener.style.left = "30%";
      } else {
        Ethnio.screener.style.left = "50%";
        Ethnio.screener.style.margin = "0 0 0 -300px";
      }
      var close_button = document.createElement('div');
      close_button.style.cssText ="background:url('" + Ethnio.base_url + "/images/close_default-trans.png') no-repeat;cursor:pointer;background:;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src = '" + Ethnio.base_url + "/images/close_default-trans.png');width:36px;height:36px;position:absolute;top:-17px;left:-13px;";
      close_button.onclick = function(){ Ethnio.close();}
      Ethnio.screener.appendChild(close_button);

      var text_block_1 = document.createElement('div');
      text_block_1.innerHTML = "Earn $75 at Amazon.com!" + "<div style='display:block;color:#808080;font-size:20px;'>" + "Have 1 hr for an interview about Wikipedia?" + "</div>";
      text_block_1.style.cssText = 'font-size:32px;color:#000;text-align:center;margin:0;';
      Ethnio.screener.appendChild(text_block_1);

      var options = ["#", "Continue", "", ""];
      var text_block_2 = document.createElement('div');
      text_block_2.innerHTML = "We are looking for visitors to Wikipedia to participate in a usability interview by phone. To qualify, just answer a few short questions. If you are selected, you will receive a phone call within 30 min with further details. Not everyone will be selected. If you don\'t hear from us, you have not been selected.";
      text_block_2.style.cssText = 'font-size:16px;color:#000;padding-top:28px;margin-top:40px;border-top:1px solid #aaaaaa;';
      Ethnio.screener.appendChild(text_block_2);

      var continue_button = document.createElement('a');
      continue_button.style.cssText = 'font-size:22px;float:right;max-width:190px;overflow:hidden;color:#fff;background:-moz-linear-gradient(top, #848484, #525252);background:-webkit-gradient(linear,0 0,0% 100%,from(#848484),to(#525252));background-color:#696969;-moz-border-radius:5px;-webkit-border-radius:5px;-o-border-radius:5px;border-radius:5px;display:block;padding:5px 15px 7px;text-decoration:none;margin-top:35px;' + options[2];
      continue_button.innerHTML = options[1];
      if (options[0] == '#'){
        continue_button.setAttribute('href', 'javascript:void(0)');
        continue_button.onclick = function(){ Ethnio.insertIframe(); /*Ethnio.ajax();*/};
      } else {
        continue_button.setAttribute('href', options[0]);
        continue_button.setAttribute('target', '_blank');
      }
      Ethnio.screener.appendChild(continue_button);

      
        var bottom = document.createElement('div');
        bottom.innerHTML = '<a href="http://ethn.io/" target="_blank" style="color:#666;text-decoration:none;padding-left:23px;height:18px;display:block;">powered by ethn.io</a>';
        bottom.style.cssText = "background:url('" + Ethnio.base_url + "/images/mini-logo.png') no-repeat 0 50%;color:#666;height:18px;float:left;margin-top:45px;font-size:10px;line-height:18px;";
        Ethnio.screener.appendChild(bottom);
      
          
      var image_wraper = document.createElement('div');
      image_wraper.style.cssText = 'overflow:hidden;text-align:center;margin-top:40px';
      var image_icon = document.createElement('img');
      image_icon.setAttribute('src', Ethnio.base_url + "/assets/screener_number_1244/wikipedia-logo.png");
      image_icon.style.cssText = "max-height:50px;";
      image_wraper.appendChild(image_icon);
      Ethnio.screener.appendChild(image_wraper);
      
    

    
      document.body.appendChild(Ethnio.screener);
      
    
    
  }
}

Ethnio.animateScreener = function(direction) {
  var animInterval = 20; //milliseconds
  var stepsCount = 15; //smoothness
  var step = (Ethnio.screener.clientHeight-5) / stepsCount;
  if (direction == 'up') step = step*(-1);
  var startY = Ethnio.screener.offsetTop;
  var move = function(element, newY, interval) {
    window.setTimeout(function() { element.style.top = newY; }, interval);
  }
  for (var i = 1; i < stepsCount + 1; i++) {
    var newY = (startY + i*step) + 'px';
    move(Ethnio.screener, newY, animInterval*i);
  }
  if (direction == 'down') window.setTimeout(function() { Ethnio.close(); }, animInterval*(stepsCount + 1));
}

Ethnio.close = function() {
  try { if (Ethnio.screener.parentNode) document.body.removeChild(Ethnio.screener); } catch(e) {};
}

var onIframeLoad = function(id){
  var iframe = document.getElementById(id);
  iframe.style.visibility = "";
  Ethnio.close();
};

Ethnio.windowSize = function(){
  var myWidth = 0, myHeight = 0;
  if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth  = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth  = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

Ethnio.insertIframe = function(){
  var iframe = document.createElement('iframe');
  iframe.frameBorder = '0';
  iframe.allowTransparency = 'true';
  iframe.id     = 'ethnio-screener-id-' + '63162';
  iframe.className  = 'ethnio-screener-name';
  iframe.src    = Ethnio.base_url + '/remotes/63162/edit?next=2;'  + 'theme=0;' + 'mode=preview;' + 'referrer_url=' + document.location.href;
  iframe.style.cssText = "z-index:16777271;position: absolute; visibility:hidden; border:0; frameBorder:0; width:810px; height: 800px; allowTransparency:true; scrolling:no; top:100px; left:" + (Ethnio.windowSize()[0] - 800)/2 + 'px;';
  Ethnio.observe(iframe, 'load', function(){onIframeLoad(iframe.id);});
  Ethnio.screener.parentNode.appendChild(iframe);

  XD.receiveMessage(function(message){
    var data = message.data.split('-');
    var iframe = document.getElementById('ethnio-screener-id-' + '63162');
    if (data[0] == 'size') {
      iframe.style.height = data[1];
    } else {
      document.body.removeChild(iframe);
    }
  }, Ethnio.base_url);
}

Ethnio.observe(window, 'load', function() {
  Ethnio.show();
  if (Ethnio.screener && Ethnio.dnd_enabled) Drag.init(Ethnio.screener);
});

/**************************************************
 * Based on dom-drag.js
 **************************************************/
var Drag = {
	obj : null,

	init : function(o)
	{
		o.onmousedown	= Drag.start;
		o.root = o;
		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.root.offsetTop);
		var x = parseInt(o.root.offsetLeft);
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.root.offsetTop);
		var x = parseInt(o.root.offsetLeft);
		var nx, ny;

		nx = x + ((ex - o.lastMouseX) * 1);
		ny = y + ((ey - o.lastMouseY) * 1);

		Drag.obj.root.style["left"] = nx + "px";
		Drag.obj.root.style["top"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style["left"]), parseInt(Drag.obj.root.style["top"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

/* 
 * a backwards compatable implementation of postMessage
 * by Josh Fraser (joshfraser.com)
 * released under the Apache 2.0 license.  
 */
var XD = function(){

    var interval_id,
    last_hash,
    cache_bust = 1,
    attached_callback,
    window = this;

    return {
        // method postMessage was deleted as useless
        receiveMessage : function(callback, source_origin) {
            
            // browser supports window.postMessage
            if (window['postMessage']) {
                // bind the callback to the actual event associated with window.postMessage
                if (callback) {
                    attached_callback = function(e) {
                        if ((typeof source_origin === 'string' && e.origin !== source_origin)
                        || (Object.prototype.toString.call(source_origin) === "[object Function]" && source_origin(e.origin) === !1)) {
                            return !1;
                        }
                        callback(e);
                    };
                }
                if (window['addEventListener']) {
                    window[callback ? 'addEventListener' : 'removeEventListener']('message', attached_callback, !1);
                } else {
                    window[callback ? 'attachEvent' : 'detachEvent']('onmessage', attached_callback);
                }
            } else {
                // a polling loop is started & callback is called whenever the location.hash changes
                interval_id && clearInterval(interval_id);
                interval_id = null;

                if (callback) {
                    interval_id = setInterval(function(){
                        var hash = document.location.hash,
                        re = /^#?\d+&/;
                        if (hash !== last_hash && re.test(hash)) {
                            last_hash = hash;
                            callback({data: hash.replace(re, '')});
                        }
                    }, 100);
                }
            }
        }
    };
}();

