(function ($) {
  Drupal.behaviors.ictj_survey = {
    attach: function() {
      if (Drupal.settings.ictj_survey.active
        &&
        !($.cookie('ictj_survey_closed') || false)) {
        var ictj_survey = new Drupal.ictj_survey();
        ictj_survey.init();
      };
    }
  }

  // Trigger survey fly-in.
  Drupal.ictj_survey = function() {
    var _self = this;
    _self.content = $('#ictj-survey-wrapper');

    // Return unix timestamp.
    this.unix = function() {
      return Math.round(new Date().getTime() / 1000);
    }

    this.init = function() {
      if (_self.content.length > 0) {
        // Survey starts as being invisible.
        Drupal.settings.ictj_survey.visible = false;
        // Number of seconds to wait before invoking test.
        Drupal.settings.ictj_survey.timeout = 3;
        // Fraction of page loads for which to load the test.
        Drupal.settings.ictj_survey.frequency = 1;

        // See if this is a subsequent request.
        var initiated = ($.cookie('ictj_survey_init') || false);
        // Invoke for given frequency on the frontpage.
        if ((
            Math.random() <= Drupal.settings.ictj_survey.frequency
            ) || initiated) {
          // Start timer.
          if (!initiated) {
            $.cookie('ictj_survey_init', 1);
            $.cookie('ictj_survey_time', _self.unix());
            var survey_time = _self.unix();
          }
          else {
            var survey_time = $.cookie('ictj_survey_time');
          }
          setInterval(function() {
            // If the survey is not presently visible, invoke the slide-in.
            // after being on the site.
            if (!Drupal.settings.ictj_survey.visible
              &&
              _self.unix() - Drupal.settings.ictj_survey.timeout > survey_time) {
              _self.invoke1();
            };
          }, 1000);
        };

      };
    }

    // Bring in the survey.
    this.invoke1 = function() {
      var content = _self.content.html();
      var box = $('<div class="survey-container clearfix"><div class="survey-faux-wrapper"></div>' + content + '</div>');

      // Add close link.
      box.find('.survey-inner').append('<a id="survey-close" title="Close" href="/"><span class="text">Close</span><span class="x">&times;</span></a>');

      // Initialize display.
      var css_initial = {
        position: 'fixed',
        top: '200px',
        zIndex: 100
      }

      // Account for rtl.
      // if ($('body').hasClass('i18n-ar')) {
        // css_initial.right = '-800px';
        // var css_final = {right: '0px'};
      // }
      // else {
        css_initial.left = '-800px';
        var css_final = {left: '0px'};
      // }

      box.css(css_initial);

      // Place in DOM.
      box.appendTo($('#wrapper'));
      // Size.
      $('.survey-faux-wrapper, .survey-container').css({height: (box.find('.survey').height() + 24) + 'px'});

      // Use .survey to get both sidebar block and hover div.
      $('.survey').find('a').bind('click', function() {
        // Hide the box.
        box.fadeOut('fast');
        // Set cookie to not show again for 30 days (duration of survey).
        $.cookie('ictj_survey_closed', 1, {expires: 30});
        // Close link has dummy href.
        if (this.id == 'survey-close' || this.id == 'survey-close-button') {
          return false;
        };
      });

      // Slide in.
      box.show().animate(css_final);

      // Mark as visible to prevent further invocation.
      Drupal.settings.ictj_survey.visible = true;
    }
  }
})(jQuery);
;

/**
 * Cookie plugin 1.0
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!=""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}};
;

