(function ($) {


  Drupal.behaviors.exampleModule = {
    attach: function(context, settings) {
     //BEGIN ATTACH
      if (!thesite) {
        var thesite = {};
      }

      thesite.Site = {};
      thesite.Site.showBackground = function(index) {

        var backgroundFilePrefix = Drupal.settings.thetheme.path_to_theme +  "background-images/";

        var img = new Image();
        $(img)
          .load(function () {
            $(this).hide();
            $('#background').append(this);
            $(this).fadeIn();
            $(window).trigger('resize');
          })
          .error(function () {
            console.log('error');
          })
          .attr('src', backgroundFilePrefix + index + '.jpg')
          .css('marginLeft', $('#background img').css('marginLeft'))
          .css('height', 'auto !important');  
      };
 
 
      thesite.Site.initBackground = function() {
        $(window).resize(backgroundResizeHandler);
        $(window).trigger('resize');
      };

      // Private functions
      // -----------------
      var backgroundResizeHandler = function() {
        // If the inital image isn't cached, it'll report 0 dimensions.
        if ($('#background img').height() == 0 || $('#background img').width() == 0) {
          setTimeout(function() { $(window).trigger('resize'); }, 100);
          return;
        }

        if ($('#background').hasClass('horizontal')) {
          if ($('#background img').height() < $('#background').height()) {
            $('#background').addClass('vertical').removeClass('horizontal');
            //$('#background img').css('marginLeft', -1 * parseInt($('#background img').width() / 2));
          }
        } else {
          if ($('#background img').width() < $('#background').width()) {
            $('#background').addClass('horizontal').removeClass('vertical');
            $('#background img').css('marginLeft', 0);
          }
        }
      };
      
      $.fn.delay = function( time, name ) {
        return this.queue( ( name || "fx" ), function() {
          var self = this;
          setTimeout(function() { $.dequeue(self); } , time );
        });
      };


      thesite.Site.initBackground();
      if(!parent.tinyMCEPopup){
        if(Drupal.settings.thetheme.is_front){          //choose a specific image
          //thesite.Site.showBackground(2); //        } else {
          //choose random background image
          //thesite.Site.showBackground(Math.floor(Math.random()*4)+1);
        }
        thesite.Site.showBackground(1);
        //thesite.Site.showBackground(Math.floor(Math.random()*4)+1);
      }  
      // END ATTACH    
    }
  };

})(jQuery);





;

