// changer le contexte d'exécution d'une fonction
if(!Function.prototype.context) {
	Function.prototype.context = function(obj) {
		var fn = this;
		return function() {
			fn.apply(obj, arguments);
		};
	};
}

// namespace
var WN = {};

// dimensions du viewport
WN.dimensions = function() {
	var de = document.documentElement;
	return {
		'winW': window.innerWidth || (de && de.clientWidth) || document.body.clientWidth,
		'winH': window.innerHeight || (de && de.clientHeight) || document.body.clientHeight
	};
};

// centrer l'arrière-plan sur le <body> + dispatch
WN.center = {
	bgW: 1600,
	bgH: 1000,
	
	init: function() {
		WN.center.body = document.body;
		WN.center.site = document.getElementById('site-content');
		if(!WN.center.site) {return;}
		WN.center.siteH = WN.center.site.offsetHeight;
		WN.center.siteW = WN.center.site.offsetWidth;
		WN.center.position();
		addEvent(window, 'resize', WN.center.position.context(WN.center));
	},
	
	position: function() {
		this.dim = WN.dimensions();
		if(this.dim.winH < this.bgH) {
			if(this.dim.winH < this.siteH) {
				this.siteTop = '0';
				this.bgTop = '-203px';
			} else {
				this.siteTop = Math.round((this.dim.winH - this.siteH)/2) + 'px';
				this.bgTop = - Math.round((this.bgH - this.dim.winH)/2) + 'px';
			}
		} else {
			this.siteTop = '203px';
			this.bgTop = '0';
		}
		if(this.dim.winW < this.bgW) {
			if(this.dim.winW < this.siteW) {
				this.siteLeft = '0';
				this.bgLeft = '-321px';
			} else {
				this.siteLeft = Math.round((this.dim.winW - this.siteW)/2) + 'px';
				this.bgLeft = '50%';
			}
		} else {
			this.siteLeft = Math.round((this.dim.winW - this.siteW)/2) + 'px';
			this.bgLeft = '50%';
		}
		
		this.body.style.backgroundPosition = this.bgLeft +' '+ this.bgTop;
		this.site.style.top = this.siteTop;
		this.site.style.left = this.siteLeft;
	}
};

function rollOver(obj, rubOn){
	obj.src=(obj.src.lastIndexOf('-on')!=-1)? obj.src.replace(/-on/gi,"-off"):obj.src.replace(/-off/gi,"-on");
}

// chargement des fonctions
DomLoaded.load(WN.center.init);
