$(function(){

	if(ltie7) {
		$('.resultat_thematique h4:first-child, .resultat_mot_cle h4:first-child').css({marginTop: 0});
		$('.resultat_thematique ul + p').css({marginTop: 0});
		$('.resultat_thematique h4 + p, .resultat_thematique h4 + ul, .resultat_thematique p + ul').css({margin: 0});
	}

	motCleConso.init();
	pngBgForIe.init();
	lisa.init();
	poll.init();
	nouscontacter.init();
	contactPlus.init();


	/* hack pour le service conso */
	var disabled = '';
	$('form.questionThematique').submit(function(){
		var select = $(this).find('select').attr('id');
		var option = '#' + select + ' option';
		$(option).each(function() {
			if ($(this).attr('selected')) {
				disabled = 0;
				if ($(this).attr('disabled')) {
					disabled = 1;
				}
			}
		});

		var val=$(this).find('select').val();
		val = val.replace(/ /mg, '_');
		val = val.split(":");
		if (disabled == 1) {
			return false;
		}
		else {
			if (val[3]) {
				location.href="/Service-Consommateurs/("+val[0]+")/"+val[1]+"/"+val[2]+"/(id)/"+val[3];
			}
			else {
				location.href="/Service-Consommateurs/("+val[0]+")/"+val[1]+"/(id)/"+val[2];
			}
		}
		return false;
	});
	
	
	if ($("#lancement_popup").text() != "0") {
		name = 'enqueteLP=';
		var i = document.cookie.indexOf( name);
		if (i == -1) {
			$('#div_enquete_sc').css({left: '25%'});
			$("#thematique select").hide(); // on cache les select pour ie6 
			$('#list_over').hide();
			$('#q_reclamation').hide();
			$('.select').hide();
			$('img[src$=.png]').iunfixpng();
		}
	}
	

	$('#commencer').click(function() {
		if ($("#lancement_popup").text() != "0") {
			window.open(	'/enquete_sc/repondre?questionnaire=1',
							'popup',
							'scrollbars=no, width=660, height=520, resizable=no, screenX=50, screenY=50'
			);
			$('#div_enquete_sc').css({left: '-9999px'});
			$('#list_over').show();
			$('#q_reclamation').show();
			$('.select').show();
			$("#thematique select").show(); // on reaffiche les select pour ie6 
		}
	});


	$('#enquete_sc #fermer').click(function() {
		$('#div_enquete_sc').css({left: '-9999px'});
		$("#thematique select").show(); // on reaffiche les select pour ie6 
		$('#list_over').show();
		$('.select').show();
		$('#q_reclamation').show();
		return false;
	});

	
});

var poll=function () {

	function _init() {
		$("#f_sondage").submit(function(){
			var identifiantPoll= $('#identifiantPoll').val();
			var answer=$(this).find("input[name='answer']:radio:checked");
			var idAnswer=answer.val();
			//xt_med('C',s2,xtpage+'::'+nettoyer_marqueur(answer.siblings('label:first').text()),'N');
			if (idAnswer>0) {
				$('#retourResult').load("/ServiceBarometre/addAnswer/(id)/1",{idAnswer:idAnswer},
				function(){
					$('#retourResultatResult').load("/ServiceBarometre/getResultat/(id)/1",{idPoll:identifiantPoll},function(){
						$('#retourResultatResult img[src$=.png]').ifixpng();
					});
					$("#retourResult").show();
					$("#f_sondage").find(":submit").css('visibility','hidden');
				});
			}
			return false;
		});
	};
return {init:_init}
}();

var motCleConso=function () {
	function _init() {
		$("#q_mot_cle").autocomplete({
			minChars : 1,
			delay : 400,
			matchContains : true,
		  	url : "/servicefaq/keywords"
		});

		$("#f_mot_cle").submit(function(){
			$("div.resultat_mot_cle").load("/servicefaq/results",{q:$("#q_mot_cle").val()},_show);
			return false;
		});
	};

	function _show() {
		$('div.resultat_mot_cle').show();
		if (ltie7) cleverDisplayIE6();
	};
	function _hide() {
		$('div.resultat_mot_cle').hide();
		if (ltie7) cleverDisplayIE6();
	};

	function cleverDisplayIE6(){
		if (!ltie7) return;
		$('#reponses, #produitsCaroussel').hide().show();
		$('#contactParTelephone .contactTelItem, #engagements').css({position: 'static'}).find('img').iunfixpng().end().css({position: 'relative'}).find('img').ifixpng();

	}

	return {init:_init,show:_show,hide:_hide}
}();

var pngBgForIe=function () {
	var _init = function () {
		if (($.browser.msie && $.browser.version < 7)) {
			var regxp = new  RegExp('url\(([a-zA-Z0-9:/.-_]{1,})\)');
			$('.bottom').each(BG2Img);
		}
	}

	var BG2Img = function () {
		var regxp = new  RegExp('.png');
		var src = $(this).css('background-image');
		var res;
		if (regxp.test(src)) {
			regxp.compile('url\((.+)\)');

			res = regxp.exec(src);
			src = res[1].replace('(','');
			src = src.replace(')','');
			src = src.replace('"','');
			src = src.replace('"','');

			var img = new Image();
			img.src=src

			$(this).css('background-image','none');
			$(this).css('paddingBottom',0);
			$(this).append(img);


			$(this).find('img:last').ifixpng();

		}
	}

	return {init:_init};
}();

var lisa = function () {
	var petiteLisa 			=	new SWFObject("/design/laposte/images/swf/anim_lisa_petite.swf", "Lisa", "530", "275", "8");
	var grandeLisa 			=	new SWFObject("/design/laposte/images/swf/anim_lisa_grande.swf", "Lisa", "1024", "838", "8");
	var petiteLisaWait 		=	new SWFObject("/design/laposte/images/swf/anim_lisa_attente.swf", "Lisa", "530", "246", "8");
//	var grandeLisa 			=	new SWFObject("/design/laposte/images/swf/anims_lisa.swf", "Lisa", "1024", "838", "8");
	var id_blockLisa		=	'lisa';
	var id_blockLisa_swf	=	'lisa_swf';
	var blockLisa 			= $('#'+id_blockLisa);
	var blockLisa_swf 		= $('#'+id_blockLisa_swf);

	var _init = function () {
		petiteLisa.addParam("wmode", "transparent");
		grandeLisa.addParam("wmode", "transparent");
		petiteLisaWait.addParam("wmode", "transparent");
		_aff_petite_lisa();
	};

	var _aff_grande_lisa = function (question) {
		grandeLisa.addVariable("question",question);
		blockLisa.removeClass('petite').addClass('grande');
		grandeLisa.write(id_blockLisa);
	};

	var _aff_petite_lisa_wait = function () {
		blockLisa.removeClass('grande').addClass('petite');
		petiteLisaWait.write(id_blockLisa);
	};

	var _aff_petite_lisa = function () {
		blockLisa.removeClass('grande').addClass('petite');
		petiteLisa.write(id_blockLisa);
	};

	return {init:_init,affGrandeLisa:_aff_grande_lisa,affPetiteLisa:_aff_petite_lisa,affPetiteLisaWait:_aff_petite_lisa_wait};
}();

var nouscontacter = function () {
	var _init =function () {
		var test = $('#q_reclamation');

		$('#f_reclamation').submit(function () {

			if ($('#q_reclamation option:selected').val() == "")
			{
				return false;
			}

			var page = nettoyer_marqueur($('#q_reclamation option:selected').text());
			xt_med('C',s2, xtpage+'::formulaire_reclamation::'+ page,'N');
			return nouscontacter.openform_popup(test)});
	};

	var _openform_popup = function (elt_select) {
		if (elt_select.val() == '')
			return false;
		var currentTime=new Date();
		window.open(elt_select.val(),"name"+currentTime.getHours()+currentTime.getMinutes()+currentTime.getSeconds());
		return false;
	};
	return {init:_init, openform_popup:_openform_popup};
}();

var contactPlus = function(){
	function init(){
		$('.SavoirPlusBas').before('<a href="#" class="fermer"><img src="/design/laposte/images/conso_v2/btn_fermer.gif" alt="Fermer le cadre d\'informations" width="14" height="14" /></a>');
		$('.plusMontrer').click(function(){
			var plus = getPlus($(this));
			if (!plus.is('.plusVisible')) {
				plusFixer(plus);
				plusMontrer(plus);
			}
			return false;
		}).mouseover(function(){
			var plus = getPlus($(this));
			plusMontrer(plus)
		});
		$('.fermer').click(function(){
			var plus = getPlus($(this));
			if (plus.is('.plusVisible')) {
				plusRelacher(plus);
				plusCacher(plus);
			}
			return false;
		}).mouseover(function(){
			var plus = getPlus($(this));
			plusCacher(plus);
		});
	}

	function getPlus(elmt){
		return elmt.parents('.contactTelItem').find('.contactSavoirPlus');
	}

	// gérer l'affichage
	function plusMontrer(elmt){
		elmt.addClass('plusVisible');
	}
	function plusCacher(elmt){
		if (!$.data(elmt.get(0),'fixe')) elmt.removeClass('plusVisible');
	}

	// pouroivr fixer l'affichage au clic
	function plusFixer(elmt){
		$.data(elmt.get(0),'fixe',true)
	}
	function plusRelacher(elmt){
		$.data(elmt.get(0),'fixe',false)
	}

	return {init:init};
}()