AICMedia.Backdrop = {

	dimension: null,
	minHeight: 600,
	postProcessTimeout: 100,

	initialize: function() {
		this.host = $('#current-backdrop');
		$(window).resize($.proxy(this.postProcess, this));
		$(window).resize($.proxy(this.resize, this));
	},

	postProcess: function() {
		if ( this.timer ) clearTimeout(this.timer);
		setTimeout($.proxy(function(){
			AICMedia.readWindowDimenstion();
			this.resize();
		}, this), this.postProcessTimeout);
	},

	resize: function() {


		var bodyWidth = AICMedia.frame.width;
		var bodyHeight = AICMedia.frame.height < this.minHeight ? this.minHeight : AICMedia.frame.height;

		this.host.find('.frame').css({width: bodyWidth});
        if (!this.host.find('.frame').hasClass('scroll')) {
            this.host.find('.frame').css({height: bodyHeight});
        }

		var images = this.host.find('.frame > .decorator');

		if ( images.length ) {
			jQuery.each(images, $.proxy(function(index, image) {
				var originalSize, newWidth, newHeight;
				if ( !$(image).data('originalSize') ) {
					originalSize = { width: $(image).width(), height: $(image).height() };
					$(image).data('originalSize', originalSize);
				} else originalSize = $(image).data('originalSize');

				newHeight = bodyHeight;
				newWidth = bodyHeight/originalSize.height * originalSize.width;

				if ( newWidth < bodyWidth ) {
					newWidth = bodyWidth;
					newHeight = bodyWidth/originalSize.width * originalSize.height
				}

				var left = bodyWidth/2 - newWidth/2;
				var top = bodyHeight/2 - newHeight/2;

				$(image).css({
					height: newHeight,
					width: newWidth,
					left: left,
					top: top
				});

                $('.b-scroller .left-fade, .b-scroller .right-fade').css({
                    //backgroundPosition: '0 ' + top + 'px',
                    height: newHeight + 'px'
                });
			}, this));
		}

        AICMedia.Index.resizeWelcome();

		this.resizeTitle();

	},

	resizeTitle: function() {
		var titles = this.host.find('.frame > div.backdrop-title');
		if (titles.length) {
			jQuery.each(titles, function(index, title) {
				$(title).css({
					fontSize: AICMedia.frame.width/5,
					left: $('#pointer').offset().left - 10
				});
			});
		}
	}

};

