qs.defineNS('app'); app.HeaderImage = qs.createObject(); app.HeaderImage.prototype = { container: null, options: {}, currentImageKey: false, resizeTimer: false, resizeTimeout: 200, initialize: function (options) { this.container = $('#header-image'); this.options = $.extend(options, {}); if (navigator.userAgent.match(/MSIE 8/)) { this.updateBackgound(960); } else { var obj = this; $(window).resize(function() { if(obj.resizeTimer !== false) { clearTimeout(obj.resizeTimer); obj.resizeTimer = false; } obj.resizeTimer = setTimeout(function () { obj.updateBackgound() }, obj.resizeTimeout); }); this.updateBackgound(); } }, getImageKey: function () { var windowWidth = $(window).width(); var imageKey = null; var lastKey = null; for (var key in this.options.images) { if (key < windowWidth) { imageKey = key; } lastKey = key; } if (null === imageKey) { imageKey = lastKey; } return imageKey; }, updateBackgound: function (imageKey) { if (typeof imageKey == 'undefined') { imageKey = this.getImageKey(); } if (imageKey !== this.currentImageKey) { this.currentImageKey = imageKey; this.container.css('background-image', 'url(' + this.options.images[imageKey] + ')'); } } };