JResize = function(gridItem){
	this.widthGridItem = parseInt($(gridItem).outerWidth());
	this.heightGridItem = parseInt($(gridItem).outerHeight());
	this.ratioItem = this.widthGridItem / this.heightGridItem;
	this.marginGridItem  = parseInt($(gridItem).css('margin-left')) + parseInt($(gridItem).css('margin-right'));
};

//Function which resize the item in a grid
JResize.prototype.resizeGrid = function(containerGrid, gridItem) {		
	
	var widthContainerGrid = $(containerGrid).width();
	
	if(widthContainerGrid % (this.widthGridItem + this.marginGridItem) != 0) {
		var nbItemPerLine = Math.ceil( widthContainerGrid / (this.widthGridItem + this.marginGridItem));
		var newWidth = ( widthContainerGrid / nbItemPerLine ) - this.marginGridItem;
		
		var newHeight = newWidth / this.ratioItem;
		$(gridItem).css('width', newWidth+'px');
		$(gridItem).css('height', newHeight+'px');
		
	} else {
		$(gridItem).css('width', this.widthGridItem+'px');
		$(gridItem).css('height', this.heightGridItem+'px');
	}
};

JResize.prototype.resizeBackground = function (bgImg, width) {
	
	var w_w	= (width != '') ? width : $(window).width(),
	w_h	= $(window).height(),
	r_w	= w_h / w_w,
	i_w	= $(bgImg).width(),
	i_h	= $(bgImg).height(),
	r_i	= i_h / i_w,
	new_w,new_h,
	new_left,new_top;

	if(r_w > r_i){
		new_h	= w_h;
		new_w	= w_h / r_i;
	}
	else{
		new_h	= w_w * r_i;
		new_w	= w_w;
	}
	
	$(bgImg).css({
		width	: new_w + 'px',
		height 	: new_h + 'px',
		left	: (w_w - new_w) / 2 + 'px',
		top		: (w_h - new_h) / 2 + 'px'
	});
};

JResize.prototype.slideWallpapper = function(bgImg) {
	$(bgImg+' img.active').fadeOut(50, function() { $(this).removeClass('active'); });
	var bg = $('#wallpapper img.active').next('img');
	if(bg.html() == null) {
		bg = $(bgImg+' img:eq(0)');
	}
	bg.fadeIn(50, function() {
		$(this).addClass('active'); 
		var theme = $(this).attr('rel');
		var classRemove = (theme == 'clair') ? 'sombre' : 'clair';
		$('body').removeClass(classRemove);
		$('body').addClass(theme); 
	});
	setTimeout('JResize.prototype.slideWallpapper("'+bgImg+'")', 5000);
	
}
