var ltie7 = ($.browser.msie && $.browser.version < 7);

$.ifixpng('/design/cea/images/pixel.gif');


$(function(){

	$('img[src$=.png]').ifixpng();
	handleLang.init();
	handleSearch.init();
	handleThematiques.init();
	handleSousMenu.init();
	handleCadreItem.init();
	handlePrint.init();
	handlePopups.init();
	handleDocs.init();
	
	
	//handleListeAgenda.init();
	$('#dossiers .dossierHaut').adjustSize();
	$('#listeThematiques .listeThematiqueItem').adjustSize({periode: 2});
	$('#listeLabos .listeInstitutsItem').adjustSize({periode: 2});


	// Gestion des Zoom sur les images
	$('.zoomImage').click(function(){
		var img 	= $(this).find('a');
		var offset	= 20;
		if (ltie7) {
			var offset	= 40;
		}
		var width 	= parseInt(img.attr('width'))+offset;
		var height 	= parseInt(img.attr('height'))+offset;
		if(window.open(img.attr('href'),'zoom','width='+width+',height='+height+',left=600,top=0,resizable=1, scrollbars=1')){
			return false;
		}
	});


	if (ltie7) {
		$('img[@usemap]').css('border','none');
		$('input[type="radio"], input[type="checkbox"], input[type="file"]').css('border','none');

		$('#articleContenu >*:first-child').css('margin-top','0')
		$('#articleContenu >*:last').css('margin-bottom','0')

		$('#footer li:first-child').css('border-left','none');
	}
});

var handleLang = function(){
	var init=function(){
		if ($('#menuLangContenu li').size() > 0 && ltie7){
			$('#menuLangContenu li:first').css('border-right', '1px solid #FFF');
		}
	
	};
	return {init:init};
}();


var handleSearch = function(){
	var wrapper, title, liste;
	var init=function(){
		$('#headerRecherche').one('click',function(){
			$(this).val('');
		});
		wrapper = $('.synCoocTable, #collectionsListe');
		title = $('h2', wrapper);
		liste = $('#synLireAussi, >ul', wrapper);
		liste.hide();
		title.each(function(index){
			$(this).click(function(){
				var item=liste.eq(index);
				if (item.is(':visible')) item.hide();
				else item.show();
			});
		});
	};
	return {init:init};
}();

var handleThematiques = function(){
	var thematiques;
	var init = function(){
		thematiques=$('#thematiques');
		$('a',thematiques).fakeMiddleCenter();
	};
	return {init:init};
}();
var handleSousMenu = function(){
	var sousMenu;
	var init = function(){
		sousMenu=$('#sousMenu');
		$('a',sousMenu).fakeMiddleCenter({paddingHeight : 7});
	};
	return {init:init};
}();

var handleCadreItem = function(){
	var listeCadreItem;
	var init = function(){
		listeCadreItem=$('#listeCadres .cadre');
		listeCadreItem.adjustSize({periode: 2});
	};
	return {init:init};
}();

var handlePrint = function(){
	var textHeader, actuDate;
	var init = function(){
		if ($('body').is('#popup')) return;
		textHeader=$('#textHeader');
		actuDate=$('#ActuDate');
		if (actuDate.size() != 0) actuDate.prepend('<p id="print"><a href="#">'+_t.Imprimer+'</a></p>');
		else textHeader.prepend('<p id="print"><a href="#">'+_t.Imprimer+'</a></p>');
		$("#print").click(function(){
			window.print();
		});
	};
	return {init:init};
}();

var handlePopups = function(){
	var href, tabtemp, lang, reste;
	var init = function(){
		$('a.popup').click(function(){
			href=$(this).attr('href');
			tabtemp=$(this).attr('href').split('/');
			if (href.indexOf('/') === 0) tabtemp.shift(); // On retire le premier du tableau, qui est vide.
			lang=tabtemp.shift();
			reste=tabtemp.join('/');
			if (popup=window.open("/"+lang+"/layout/set/popup/"+reste,'contact','width=837,height=813,statusbar=no,toolbar=no,scrollbar=no,personalbar=no,locationbar=no'))
				return false;
			else {
				$(this).attr("target",'_blank');
				return true;
			}
		});
	};
	return {init:init};
}();

var handleDocs = function(){
	var dlDoc, dlDocHeight, dlDocPaddingTop;
	var init = function(){
		dlDoc=$('#dlDoc');
		if (dlDoc.size() == 0) return;
		dlDocHeight=parseInt(dlDoc.height());
		dlDocPaddingTop=40-dlDocHeight;
		if (!dlDocPaddingTop>20) dlDocPaddingTop=20
		dlDoc.css({paddingTop: dlDocPaddingTop+'px'})
	};
	return {init:init};
}();

var handleListeAgenda = function(){
	var listeAgenda;
	var maxHeight = 500;
	var init = function(){
		if (!ltie7) return;
		listeAgenda=$('#listeAgenda');
		if (parseInt(listeAgenda.height())>maxHeight) listeAgenda.height('500px');
	};
	return {init:init};
}();

/** fonctions et plugins **/

$.fn.fakeMiddleCenter=function(options) {
	if ($(this).size() == 0) return this;

	var ltie7 = ($.browser.msie && $.browser.version < 7);
	var minHeight, height;

	if (!ltie7 && (minHeight=parseInt($(this).css('min-height'))) != 0) {
		$(this).css('min-height','0');
	} else {
		height=parseInt($(this).height());
		$(this).height('auto');
	}
	$(this).each(function(){
		var thisElmtHeight = parseInt($(this).height());
		if(thisElmtHeight == (minHeight ? minHeight : height)) {
			if (!ltie7 && minHeight != 0) $(this).css('min-height',minHeight);
			else $(this).height(height);
			return;
		}
		var thisElmtPaddingTop=Math.ceil(((minHeight ? minHeight : height)-thisElmtHeight)/2);
		if (thisElmtPaddingTop<0) thisElmtPaddingTop=0;
		$(this).css({paddingTop :thisElmtPaddingTop+'px'});
		if (!ltie7 && minHeight != 0) $(this).css('min-height',(minHeight-thisElmtPaddingTop)+'px');
		else $(this).height((height-thisElmtPaddingTop)+'px');
	});
	return this;
}

$.fn.adjustSize=function(options) {
	var settings = {
		periode: 3,
		mode: 'height',
		minHeight: 0,
		minWidth: 0
	};
	var ltie7 = ($.browser.msie && $.browser.version < 7);
	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;
}
