var DCBackground = {

	initialize:function (data) {
		var _self = this;
		
		this.window = $(window);
		this.parent = $('#background');

		if (data) {
			this.data = data;
			var html = '';
			_.each(data, function (num) {
				html += "<img src='"+num.image+"' id='bg-"+num.id+"' class='bg-img' alt=' Image Description '/><div class='hidden'><h1> Image Title </h1><p> Image Description </p></div>";
				return true;
			});
			this.parent.html(html);
		} else {

			// This will load static background images based on certain category's (skate, moto etc) article and gallery pages
				// check to see if the body has a class of article or gallery
				// if true, then grab the id nav item with the class of "current"
				// split the value of that id on the "-" to give us a file name for the new BG image
 				// load in the new bg image

			var isArticle = $('body').hasClass('article'),
				isGalleryLandingPg = $('body').hasClass('gallery_landing_page'),
				isGallery = $('body').hasClass('gallery');
				
			if($('#secondaryNav').length > 0 && (isArticle || isGalleryLandingPg || isGallery)) {
				var currentPage = $('#nav-list').find('.current'),
					currentCategory = currentPage.attr('id').split('-');

				this.parent.html('<img src="/extension/qsdcshoes/design/dcshoes/images/bg-' + currentCategory[1] + '.jpg" alt="' + currentCategory[1] + ' Background" class="bg-img" />');
			}

			this.parent.find('img:first').bind('load', function() {
				$('#background').find('img').css('display', 'block');
			});
		}
		
	},
	
	min: {
		width: 1001
	},
	
	update: function (id) {
		var list = this.parent.find('img:not(#bg-'+id+')');
		list.hide();
		var n = this.parent.find('#bg-'+id);

		if(n[0].src.indexOf('Rediscover-DC') >= 0){
			$('#fixed-container').css('position', 'static');
			$('html').css('background','#FFFFFF');
		}
		else if(n[0].src.indexOf('SCROLLBLACK') >= 0){
			$('#fixed-container').css('position', 'static');
			$('html').css('background','#000000');	
		}		
		else{
			$('html').css('background','#000000');
			$('#fixed-container').css('position', 'fixed');			
		}

		n.fadeIn();		
		this.reposition(n);
	},
	
	reposition: function (selected) {
		
		var _self = this;
		
		if(!_self.window) return;
		
		var ww = this.window.width();
		var wh = this.window.height();
		
		if(ww < _self.min.width){
			ww = _self.min.width;
			$('body').css('overflow-x','visible');		
		}
		else{
			$('body').css('overflow-x','hidden');		
		}
		
		function resize(img) {
						
			var oiw = 1024; //img.width();
			var oih = 600; //img.height();
			var sx = ww / oiw;
			var sy = wh / oih;
						
			var src = img[0].src;	

			//if the image is the DC Rediscover
			if (src.indexOf('Rediscover-DC') >= 0){
				var nw = 1400;
				var nh = 778;
			}
			else if(src.indexOf('SCROLLBLACK') >= 0){
				var nw = 1400;
				var nh = 778;
			}					
			else{
				var scale = (sx > sy) ? sx : sy;			
				var nw = img.width((oiw * scale)).width();
				var nh = img.height((oih * scale)).height();	
			}
															
			var l = (ww/2) - (nw/2);
			l = l + Number($('#faux-background').offset().left);
			
			img.offset({left:l});
			
		}
		
		if(selected) {
			if(selected.attr('loaded')) {
				resize(selected);
			} else {
				selected.bind('load', function() {
					var loaded = $(this);
					loaded.attr('loaded', true);
					resize($(this));
				});
			}
		} else {
			_self.parent.find('img').each(function () {
				var img = $(this);
				if(img.attr('loaded')) {
					$(this).bind('load', function() {
						var loaded = $(this);
						loaded.attr('loaded', true);
						resize($(this));
					});
				} else {
					resize(img);
				}
			});
		}
		

	}
};

$(function () {
	
	$(window).resize (function() {
		DCBackground.reposition();
	});
	
	var data;
	
	if(typeof HeroModel !== 'undefined' && HeroModel.getBackgrounds()) {
		data = HeroModel.getBackgrounds();
	}
	
	DCBackground.initialize(data);
	DCBackground.reposition ();
});


