﻿/*
author:		Alessandro Parodi a.parodi@axware.net
version:	0.1
date:		2011-01-14
required:	resizeManager.js
*/
var bgManager = {
	settings:{
		fadeIn:1500,
		step:100,
		margin:{top:0,right:0,bottom:0,left:0}
	},
	base:null,
	initialize:function()
	{
		if(this.base != null) return;
		this.base = $('<div></div>');
		$('*:first', document.body).before(this.base);
		//
		this.base.css('position', 'absolute').css('top', this.settings.margin.top).css('left', this.settings.margin.left).css('z-index', 0).css('overflow','hidden');
		this.base.css('position', 'fixed');	// carroponte specific
		//
		resizeManager.addListener(this);
		resizeManager.onresize();
	},
	show:function(cmsImage, canCrop)
	{
		//
		if(canCrop == null) canCrop = true;
		//
		var w = resizeManager.width() - this.settings.margin.left - this.settings.margin.right;
		var h = resizeManager.height() - this.settings.margin.top - this.settings.margin.bottom;
		//
		w = Math.floor(w / this.settings.step) * this.settings.step;
		h = Math.floor(h / this.settings.step) * this.settings.step;
		//
		var container = $('<div></div>');
		//container.css('position','absolute').css('top',0).css('left',0).css('overflow','hidden').width(0).height(resizeManager.getSize().height);
		var size = resizeManager.getSize();
		container.css('position','absolute').css('top',0).css('left',0).css('overflow','hidden').width(size.width).height(size.height);
		container.css('opacity', 0.0);
		//
		var image = $('<img src="' + cmsImage.getBySize(w, h, canCrop, true) + '" />');
		image.css('position', 'absolute');
		image.data('cmsImage', cmsImage);
		image.data('canCrop', canCrop);
		image.load(function(){
			var j = $(this);
			var imageSize = {width:j.width(), height:j.height()};
			j.data('size', imageSize);
			//
			bgManager.onresize(resizeManager.getSize());
			//
			j.parent().css('opacity',1.0);
			bgManager.cleanUp();
			j.parent().toggleClass('removable', true);
		});
		//
		this.base.append(container);
		container.append(image);
	},
	onresize:function(size)
	{
		var baseWidth = size.width - this.settings.margin.left - this.settings.margin.right;
		var baseHeight = size.height - this.settings.margin.top - this.settings.margin.bottom;
		this.base.width(baseWidth).height(baseHeight);
		//
		this.base.find('div').each(function(){$(this).width(baseWidth).height(baseHeight);});
		//
		$('img', this.base).each(function(){
			var j = $(this);
			var imageSize = j.data('size');
			if(!imageSize) return;
			var canCrop = j.data('canCrop');
			// fix size
			var factorX = imageSize.width / baseWidth;
			var factorY = imageSize.height / baseHeight;
			var factor = null;
			if(canCrop)
				factor = Math.min(factorX, factorY);
			else
				factor = Math.max(factorX, factorY);
			//
			j.width(Math.ceil(imageSize.width / factor));
			j.height(Math.ceil(imageSize.height / factor));
			// fix position
			if(baseWidth != null && baseWidth != 0)
				j.css('left', Math.round((baseWidth - j.width()) / 2));
			if(baseHeight != null && baseHeight != 0)
				j.css('top', Math.round((baseHeight - j.height()) / 2));
		});
	},
	cleanUp:function()
	{
		// remove hidden old images
		this.base.find('div.removable').remove();
	}
};

//
$(function(){bgManager.initialize()});
