$(function(){
	$('#liste_phototheque .item_sous_rubrique').adjustSize({periode: 3});
	centerVignette.init();

	if($("#voeux_2011").is('div')){
	$('body').append('<div id="TB_overlay" class="TB_overlayBG"></div>');
	$("#voeux_2011").show();
	$("#voeux_2011").vAlign();
	$("#voeux_2011").hAlign();
	}
     var agent=navigator.userAgent.toLowerCase();
	if (agent.indexOf('ipad') != -1) {
        $('#voeux_2011').hide();
        $('#TB_overlay').hide();
	}
    if (agent.indexOf('iphone') != -1) {
        $('#voeux_2011').hide();
        $('#TB_overlay').hide();
       
	}
    if (agent.indexOf('iPod') != -1) {
        $('#voeux_2011').hide();
        $('#TB_overlay').hide();
	}

	$('#agenda table.tableau tbody tr:odd').addClass('odd');
	$('#agenda table.tableau tbody tr:even').addClass('even');
});


// Nouvelle fonction jquery de centrage horizontal et vertical
// du container dans la fenetre

$.fn.vAlign = function() {
	return this.each(function(i){
	var h = $(this).height();
	var oh = $(this).outerHeight();
	var mt = (h + (oh - h)) / 2;
	$(this).css("margin-top", "-" + mt + "px");
	$(this).css("top", "50%");
	$(this).css("position", "absolute");
	});
};

$.fn.hAlign = function() {
	return this.each(function(i){
	var w = $(this).width();
	var ow = $(this).outerWidth();
	var ml = (w + (ow - w)) / 2;
	$(this).css("margin-left", "-" + ml + "px");
	$(this).css("left", "50%");
	$(this).css("position", "absolute");
	});
};
function CloseDivOutOfBox(){
	$('#voeux_2011').hide();
	$('#TB_overlay').hide();
};

// Nouvelle fonction jquery de retaillage de blocks
$.fn.adjustSize=function(options) {
	var settings = {
		periode: 2,
		mode: 'height',
		minHeight: 0,
		minWidth: 0
	};
	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
	if (options) {
		jQuery.extend(settings, options);
	}
	var totalSize = $(this).size();
	var nbPeriodes = Math.ceil(totalSize/settings.periode);
	var index = 0;
	if (settings.mode == 'height' || settings.mode == 'both') {
		var maxTotalHeightArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalHeightArray[blockId]) maxTotalHeightArray[blockId]=0;
			var blockTotalHeight = $(this).height();
			if (blockTotalHeight > maxTotalHeightArray[blockId]) {
				if (settings.minHeight && settings.minHeight > blockTotalHeight) blockTotalHeight = settings.minHeight;
				maxTotalHeightArray[blockId] = blockTotalHeight;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedHeight = maxTotalHeightArray[blockId];
			if (ltie7) $(this).height(blockComputedHeight+'px');
			else $(this).css('minHeight',blockComputedHeight+'px');
			index++;
		});
	}
	if (settings.mode == 'width' || settings.mode == 'both') {
		var maxTotalWidthArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalWidthArray[blockId]) maxTotalWidthArray[blockId]=0;
			var blockTotalWidth = $(this).width() ? $(this).width() : $(this).attr('offsetWidth') ;
			if (blockTotalWidth > maxTotalWidthArray[blockId]) {
				if (settings.minWidth && settings.minWidth > blockTotalHeight) blockTotalWidth = settings.minWidth;
				maxTotalWidthArray[blockId] = blockTotalWidth;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedWidth = maxTotalWidthArray[blockId];
			if (ltie7) $(this).width(blockComputedWidth+'px');
			else $(this).css('minWidth',blockComputedWidth+'px');
			index++;
		});
	}

	return this;
}

var centerVignette = function(){
	var conteneurs;
	var elmtPaddingBottom = 0;
	function _init(){
		conteneurs =  $('.cadre_sous_rubrique');
		conteneurs.each(function(){
			doCenter($(this));
		});
	}

	function doCenter(elmt){
		var content = elmt.find('.spip_logos');
		if (!content.size()) return;
		elmtHeight = elmt.height();
		contentHeight = content.height();
		if (contentHeight+elmtPaddingBottom >= elmtHeight) return;
		calcMarginTop = Math.ceil((elmtHeight-contentHeight-elmtPaddingBottom)/2)+'px';
		content.css({marginTop: calcMarginTop});

	}

	return {init:_init}
}();
