var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);

$(function() {
	$('img[@src$=.png]').ifixpng();
	// au chargement de la page
	$('#menu ul li ul').css("display","none");
	$('#menu ul li').mouseover(
			function() { 
					$(this).find('>ul').css("display","block");
			} 
		).mouseout(
			function() { 
					$(this).find('>ul').css("display","none");
			}
		);
	
	$("#btn_hello").click(function() {
		annuairevert_alertHelloWorld();
	});

/** Affichage des cat�gorie Niveau 1 **/
	$('#CategorieNiv1').change(function() {
        location.replace("?aff=produits&catniv1="+$(this).val());
	});
	/** **/

/** Affichage des cat�gorie Niveau 2 **/
	$('#CategorieNiv2').change(function() {
        location.replace("?aff=produits&catniv1="+$('#CategorieNiv1').val()+"&catniv2="+$(this).val());
	});
	/** **/

/** Affichage des cat�gorie Niveau 3 **/
	$('#CategorieNiv3').change(function() {
        location.replace("?aff=produits&catniv1="+$('#CategorieNiv1').val()+"&catniv2="+$('#CategorieNiv2').val()+"&catniv3="+$(this).val());
	});
	/** **/

/** Rechercher un produit  **/
	$('#liste_produit').change(function() {
        location.replace(""+$(this).val()+"&aff=produits");
	});
	$('#select_produit').change(function() {
        location.replace($(this).val());
	});
	/** **/
	
	$('#formContact > ul > li').adjustSize();
	
	gestionCommandes.init();

});

$.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 gestionCommandes = function(){
	var champQuantite, champTarifApplique, champtarifTotal;
	function init() {
		champQuantite=$('#quantite');
		champTarifApplique=$('#tarifApplique');
		champtarifTotal=$('#tarifTotal');
		
		if (!ltie7) {
			champTarifApplique.attr('disabled','disabled');
			champtarifTotal.attr('disabled','disabled');
		}
		
		initTarifApplique();
		initChampQuantite();
		initPrintButton();
	}
	function recalculPrix() {
		var prix=champQuantite.val()*champTarifApplique.val();
		champtarifTotal.val(prix);
	}
	
	function initTarifApplique(){
		$('#tarifs input[name=tarif]').click(function(){
			var tarifApplique=$(this).val();
			champTarifApplique.val(tarifApplique);
			recalculPrix();
		});
	}
	function initChampQuantite(){
		champQuantite.keyup(function(){
			var q=champQuantite.val();
			if(isNaN(q)) {
				champtarifTotal.val('err.').css('background','red');
			}
			else {
				champtarifTotal.css('background','');
				recalculPrix();
			}
		});
	}
	function initPrintButton(){
		$('#imprimerBon').html('					<input id="imprimer" name="button" value="Imprimer votre bon de commande" type="button" />');
		$('#imprimer').click(function(){
			window.print();
			return false;
		});
	}
	
	return {init: init, recalculPrix: recalculPrix};
}();