/************************/
/**** DOCUMENT READY ****/
/************************/
$(document).ready(function(){

	// Auto Empty
	$(".autoempty")
		.blur(function(event){
			if(!$(this).attr('value'))
				{$(this).css('color','#999'); $(this).attr('value',$(this).attr('alt')); }
		})
	    .focus(function(event){
			if($(this).attr('value')==$(this).attr('alt'))
				{ $(this).attr('value',''); $(this).css('color',''); }
		})
		.each(function(){
			if(!$(this).attr('value') || $(this).attr('value')==$(this).attr('alt') )
				{$(this).css('color','#999'); $(this).attr('value',$(this).attr('alt'));  }
		})
	;

	$('.jsOnly')
		.show();
	$('.notjsOnly')
		.hide();

	if ( $('.doEdit').length )
		tableauOptLongToggle();
	

	xOffset = 10;
	yOffset = 30;
	$(".autoenlarge").hover(function(e){

		$("body").append("<div id='autoenlargeDiv' style='display:none'><img src='"+ $(this).attr('longdesc')+"' alt='Image preview' />"+"</div>");								 
		$("#autoenlargeDiv")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		$("#autoenlargeDiv").remove();
    });	
	$(".autoenlarge").mousemove(function(e){
		$("#autoenlargeDiv")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});	

	// autoradiocheck
	$('.autoradiocheck')
		.change( function(){ $(this).parent().find('input[type=radio]').attr('checked','true'); });

	// Select autosubmit
	$('.autosubmit')
		.change( function(){ submitTop(this); })
		.parents('form').find('input[type=submit]').not('.disableautosubmit').hide()
	;
	$('input[type=radio].autosubmit')
		.click( function(){ submitTop(this); })
	;
	$('.autosubmittop')
		.click( function(){ submitTop(this); return false; })
	;
	
	$('.autosubmittargetform')
		.click(function() { $('.targetform').submit(); return false; } )
	;
	
	$('.autochecksameparent')
		.keyup( function(){ if($(this).val()){ $(this).parents().find('input[type=checkbox]').attr('checked','true'); } } )
	;

	// Onglets
	$('#onglets li.withsousmenu')
		.mouseover(function(event){
			$(this).find('ul').show();
		})
		.bind('mouseleave',function(event){
			$(this).find('ul').hide();
		})
	;
		
	$("span.addcart .add")
		.css({display:''})
		.click(function(){ fieldAdd(this,'1'); });
	$("span.addcart .sub")
		.css({display:''})
		.click(function(){ fieldAdd(this,'-1'); });
	$("span.addcart")
		.find('form')
		.append('<a href="#" onclick="submitTop(this)">Ajouter<br/>au panier</a>')
		.find('input[type=submit]').hide();
	$("span.addcart.noform")
		.append('<a href="#" onclick="submitTop(this)">Ajouter<br/>au panier</a>')
		.find('input[type=submit]').hide();
	$("span.addlist")
		.find('form')
		.append('<a href="#" onclick="submitTop(this)">Ajouter<br/>Ã  la liste</a>')
		.find('input[type=submit]').hide();
	$("span.dellist")
		.find('form')
		.append('<a href="#" onclick="if (confirm(\'Etes vous sur de vouloir enlever le produit de la liste ?\')) submitTop(this)">Enlever de<br/>cette liste</a>')
		.find('input[type=submit]').hide();

	// Carrousel
	if( $('#colCcarrousel') &&  $('#car_content ul li').size()>1)
	{
		$('#colCcarrousel #car_head').html('<a class="carPrec" href="#" onclick="carrousel(\'p\',\'c\',this);"><span>p</span></a><a class="carSuiv" href="#" onclick="carrousel(\'s\',\'c\',this);"><span>s</span></a>');
		for(var i=1;i<=$('#car_content ul li').size();i++)
		{
			$('#colCcarrousel #car_head').append('<a class="carLienItem'+i+'" href="#" onclick="carrousel('+i+',\'c\',this);"><span>'+i+'</span></a>');
		}
		carrouselAutoNext();
	}
	
	// Panier
	$('.achats tr td.col3 .btn_refresh').hide();
	$('.achats tr td.col3 input').keyup(function(){
		$(this).siblings('.btn_refresh').show();
	});
	
	
	/*********POP UP*********/

	//UPLOAD IDtimbre
	initFileUploads();
	
	//LOADING POPUP
	$("#OpenPopUp").click(function(){
		centerPopup();
		loadPopup();
	});
		
	$("#OpenPopUpTop").click(function(){
		centerPopupTop();
		loadPopup();
	});
	
	$("#OpenPopUpSecondaireTop").click(function(){
		centerPopupSecondaireTop();
		loadPopupSecondaire();
	});
	
	$("#OpenPopUpTertiaireTop").click(function(){
		centerPopupTertiaireTop();
		loadPopupTertiaire();
	});

	//CLOSING POPUP
	//Click the Button "Fermer" event!
	$("#PopUpFermer").click(function(){
		disableOKPopup();
	});	
	//Click the x event!  
	$("#popupClose").click(function(){  
		disableOKPopup();  
	});
	//Click the x event!  
	$("#popupCloseSecondaire").click(function(){  
		disableOKPopup();  
	});
	//Click the x event!  
	$("#popupCloseTertiaire").click(function(){  
		disableOKPopup();  
	});
	//Click out event!
	$("#popupBackground").click(function(){
		disableOKPopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
		disableOKPopup();
		}
	});		
	//Click IDtimbreLP
	$("#IDtimbreLPPopUp").click(function(){
		disableOKPopup();
		utiliseVisuel('IDtimbreL');
	});
	//Click IDtimbreMD
	$("#IDtimbreMDPopUp").click(function(){
		disableOKPopup();
		utiliseVisuel('IDtimbreD');
	});	
		
	//REDIRECT POP UP
	//Click the button "Non, je modifie mon profil" on the Mandat Paiement PopUp
	$("#ModifyProfil").click(function(){
		disableOKPopup();
		redirectModifyProfil();		
	});	
	
	//Click the button "OK, je continue" on the Mandat Paiement PopUp
	$("#PopUpAccepter").click(function(){
		disableOKPopup();
	});
	
	/********* FIN POP UP*********/
	
	
	/***************FORMULAIRE INSCRIPTION/MODIFICATION CLIENT ************/
	//CHARGEMENT DE LA PAGE
	// Inscription client : Secteur Public : Siret coché ou pas (Chargement de la page)
	if($('#checkNumSiret').is(':checked')){
		$('#siret').show();
		$('#codeEntreprise').hide();
	} else {
		$('#siret').hide();
		$('#codeEntreprise').show();
	}
	
	if($('#typEntr').attr("value") == '08'){
		$('#typeSecteurPublic').hide();
		$('#checkNumSiretDiv').hide();
		$('#siret').hide();
		$('#typeAutre').hide();
		$('#codeEntreprise').show();
	} else if($('#typEntr').attr("value") == '09'){
			$('#typeSecteurPublic').hide();
			$('#checkNumSiretDiv').hide();
			$('#typeAutre').show();
			$('#codeEntreprise').show();
			$('#siret').hide();
	} else if($('#typEntr').attr("value") == '10'){
			$('#typeSecteurPublic').show();
			$('#typeAutre').hide();
			$('#checkNumSiretDiv').show();
			if($('#checkNumSiret').is(':checked')){
				$('#siret').show();
				$('#codeEntreprise').hide();
			} else {
				$('#siret').hide();
				$('#codeEntreprise').show();
			}				
	} else {
		$('#typeSecteurPublic').hide();
		$('#typeAutre').hide();
		$('#codeEntreprise').hide();
				$('#checkNumSiretDiv').hide();
		$('#siret').show();				
	}
	
	// Inscription client : Secteur Public : Siret coché ou pas (Click sur la case à coché)
	$("#checkNumSiret").click(function(){
		if($('#checkNumSiret').is(':checked')){
			$('#siret').show();
			$('#codeEntreprise').hide();
		} else {
			$('#siret').hide();
			$('#codeEntreprise').show();
		}
	});
	/***************FIN FORMULAIRE INSCRIPTION/MODIFICATION CLIENT ************/
	
	/**************FORMULAIRE ADDRESSE LIVRAISON/FACTURATION processus de vente ************/
	//CHARGEMENT DE LA PAGE
	if($('#radioLivraisonL').is(':checked')){
		$('#livraisonAdresse').css({"opacity": "1.0"});
		$('#facturationAdresse').css({"opacity": "0.3"});
		$('#facturationAdresse').css({"z-index":"25"});
	}
	if($('#radioLivraisonF').is(':checked')){
		$('#facturationAdresse').css({"opacity": "1.0"});
		$('#livraisonAdresse').css({"opacity": "0.3"});
		$('#facturationAdresse').css({"z-index":"25"});
	}
		//Click sur Adresse de livraison
	$('#radioLivraisonL').click(function(){
		$('#livraisonAdresse').css({"opacity": "1.0"});
		$('#facturationAdresse').css({"opacity": "0.3"});
		$('#facturationAdresse').css({"z-index":"25"});
	});
	
	//Click sur Adresse de livraison
	$('#radioLivraisonF').click(function(){
		$('#facturationAdresse').css({"opacity": "1.0"});
		$('#livraisonAdresse').css({"opacity": "0.3"});
		$('#livraisonAdresse').css({"z-index":"25"});
	});		
	/**************FORMULAIRE ADDRESSE LIVRAISON/FACTURATION processus de vente ************/
});	

/************************/
/** FIN DOCUMENT READY **/
/************************/

	
	
$(window).load(function(){
	if($('#panierbulle').length>0)
	{
		alertePanier();
	}
});

$(window).load(function(){
	if($('#listebulle').length>0)
	{
		alerteListe();
	}
});

function fieldAdd(objLien,diff)
{
	var val=parseFloat($(objLien).parents('span').find('input.text').attr('value'))+parseFloat(diff);
	var minimum = 1;
	if( $(objLien).hasClass('min4') ) { minimum = 4; }
	if(val<minimum){ val=minimum;}
	$(objLien).parents('span').find('input.text').attr('value',(val).toString());
	objLien.blur();
}

function enlargeImage(obj,state)
{
	if(state)
	{
		$('.enlargeTmpZoomDiv').remove();
		var par=$(obj).parent().get(0);
		$(par).append('<div class="enlargeTmpZoomDiv" style="display:none" ><img src="'+$(obj).attr('src')+'" class="enlargeTmpZoom" /></div>');
		$('.enlargeTmpZoomDiv img')
			.css({zIndex:5, border:'1px solid #f90' })
		;
		$('.enlargeTmpZoomDiv')
			.css({position:'absolute', border:'1px solid #aaa', background:'#ffffff',padding:'10px',zIndex:10 })
			.mouseout(function(){enlargeImage($(this),false);})
			.show();
		;
	}
	else
	{
		$('.enlargeTmpZoomDiv').remove();
	}
}

function submitTop(obj)
{
	$(obj).parents("form").submit();
}

function carrousel(num,auteur,lienfrom)
{
	if(lienfrom)
	{
		lienfrom.blur();
	}
	if(auteur=='c')
	{
		clearTimeout(carrouselAutoNextTimer);
	}

	if( num> $('#car_content ul li').size() )
	{
		num=1;
	}
	else if( num<=0 )
	{
		num=$('#car_content ul li').size();
	}

	if(num=='s')
	{
		carrousel(parseInt($('#car_head').attr('class').substr(3))+1);
	}
	else if(num=='p')
	{
		carrousel(parseInt($('#car_head').attr('class').substr(3))-1);
	}
	else if(num>0)
	{
		$('#car_head').attr('class','sel'+num);
		var temp=navigator.appVersion.split('MSIE');
		var ieVer=parseInt(temp[1]);
		if(ieVer==6)
		{
			$('#car_content ul li').hide();
			var elt=$('#car_content ul li').get(num-1)
			$(elt).show();
		}
		else
		{
			carrousel();
		}
	}
	else
	{
		var pos= (parseInt($('#car_head').attr('class').substr(3))-1)*300;
		if( Math.abs($('#car_content').scrollLeft() - pos)<50)
		{
			$('#car_content').scrollLeft( pos );
		}
		else if( $('#car_content').scrollLeft() > pos)
		{
			$('#car_content').scrollLeft( $('#car_content').scrollLeft()-50 );
			var t=setTimeout('carrousel()',10);
		}
		else if( $('#car_content').scrollLeft() < pos)
		{
			$('#car_content').scrollLeft( $('#car_content').scrollLeft()+50 );
			var t=setTimeout('carrousel()',10);
		}
	}
}

var carrouselAutoNextTimer=0;

function carrouselAutoNext()
{
	if(carrouselAutoNextTimer!=0)
	{
		carrousel('s','timer');
	}
	carrouselAutoNextTimer=setTimeout('carrouselAutoNext()',4500);
}

function tableauOptLongToggle()
{
	$('#tableauOptShort').slideUp();
	$('#tableauOptLong').slideDown();
}

function tableListePost()
{
	$('#addAllToCart').submit();
}

function alertePanier(val)
{
	if(val)
	{
		$('#panierbulle').fadeOut();
	}
	else
	{
		$('#colDpanier').prepend('<div id="panierbulletmp">'+$('#panierbulle').html()+'</div>');
		$('#panierbulle').remove();
		$('#panierbulletmp').attr('id','panierbulle');
		var t=setTimeout("$('#panierbulle').fadeIn()",500);
	}
}

function alerteListe(val)
{
	if(val)
	{
		$('#listebulle').fadeOut();
	}
	else
	{
		$('#colDpanier').prepend('<div id="listebulletmp">'+$('#listebulle').html()+'</div>');
		$('#listebulle').remove();
		$('#listebulletmp').attr('id','listebulle');
		var t=setTimeout("$('#listebulle').fadeIn()",500);
	}
}


/************DEBUT IDTIMBRE LP&DESTINEO**************/

//UPLOAD IDtimbre
var W3CDOM = (document.createElement && document.getElementsByTagName);
function initFileUploads() {
	$('.fileinputs input[type="file"]')
		.parents('.fileinputs')
		.each(function(){
			var clone=$(this).html();
			$(this).find('input[type="file"]')
				.addClass('file hidden')
				.change( function(){
					$(this).siblings('.fakefile').find('input').val(this.value);
					} )
				.mouseout( function(){
					$(this).siblings('.fakefile').find('input').val(this.value);
					} )
				;
			$(this).append('<div class="fakefile"><input/><img src="../../../xp/images/philaposte/inputfile-btn.png"/></div>');
		})
	;
}

//BIBLIOTHEQUE IDTIMBRE
var nameGlobal="";

//View PopUp & GetImageName
function viewPopUp(name)
{
	nameGlobal = name;
	centerPopupTop();
	loadPopup();
}

//Destineo Inactif
function useVisuelNotMD(name,idtimbreType)
{
	nameGlobal = name;
	utiliseVisuel(idtimbreType);
}

//Suppression	
function suppressionVisuel(name)
{
if (!confirm("Etes-vous sûr de vouloir supprimer ce visuel ?")) {
	return false;
	}
	$('#imagename').attr('value', name);
	$('#next').submit();
return true;
}

//Use visuel Destineo Actif
function utiliseVisuel(idtimbreType) 
{
	$('#imagename').attr('value', nameGlobal);	
	$('#IDtimbre').attr('value', idtimbreType);
	$('#event').attr('value', 'use');
	$('#next').submit();	
}	
	
/************FIN IDTIMBRE LP&DESTINEO**************/


/**********************POPUPS**********************/

//0 means disabled; 1 means enabled;  
var popupStatus = 0; 

//loading popup
function loadPopup(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#popupBackground").css({"opacity": "0.7"});  
		$("#popupBackground").fadeIn("slow");  
		$("#popupPrincipal").fadeIn("slow");  
		popupStatus = 1;  
	}  
 }
 
function loadPopupSecondaire(){  
//loads popup only if it is disabled  
if(popupStatus==0){  
	$("#popupBackground").css({"opacity": "0.7"});  
	$("#popupBackground").fadeIn("slow");  
	$("#popupSecondaire").fadeIn("slow");  
	popupStatus = 1;  
}  
}

function loadPopupTertiaire(){  
//loads popup only if it is disabled  
if(popupStatus==0){  
	$("#popupBackground").css({"opacity": "0.7"});  
	$("#popupBackground").fadeIn("slow");  
	$("#popupTertiaire").fadeIn("slow");  
	popupStatus = 1;  
}  
}
 
//disabling popup 
function disableOKPopup(){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#popupBackground").fadeOut("slow");  
		$("#popupPrincipal").fadeOut("slow");
		$("#popupSecondaire").fadeOut("slow");
		$("#popupTertiaire").fadeOut("slow");  
		popupStatus = 0;  
	}  	
} 

//Not Use (disabling popup and checked other checkbox
function disableKOPopup(){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#popupBackground").fadeOut("slow");  
		$("#popupPrincipal").fadeOut("slow"); 
		$("#IDtimbreP").attr("checked", "checked"); 
		popupStatus = 0;  
	}  	
}

function centerPopup(){  
	//request data for centering  
	var windowWidth = $(window).width();
	var windowHeight = $(document).height();
	var popupHeight = $("#popupPrincipal").height();  
	var popupWidth = $("#popupPrincipal").width();  
	//centering  
	$("#popupPrincipal").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  
   //Set height and width to mask to fill up the whole screen  
  $('#popupBackground').css({'width':windowWidth,'height':windowHeight}); 
}  

//center on the window (not document)
function centerPopupTop(){  
	//request data for centering  
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var windowHeightBackGround = $(document).height();
	var popupHeight = $("#popupPrincipal").height();  
	var popupWidth = $("#popupPrincipal").width();  
	//centering  
	$("#popupPrincipal").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  
   //Set height and width to mask to fill up the whole screen  
  $('#popupBackground').css({'width':windowWidth,'height':windowHeightBackGround}); 
} 

//center on the window (not document)
function centerPopupSecondaireTop(){  
	//request data for centering  
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var windowHeightBackGround = $(document).height();
	var popupHeight = $("#popupSecondaire").height();  
	var popupWidth = $("#popupSecondaire").width();  
	//centering  
	$("#popupSecondaire").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  
   //Set height and width to mask to fill up the whole screen  
  $('#popupBackground').css({'width':windowWidth,'height':windowHeightBackGround}); 
} 

//center on the window (not document)
function centerPopupTertiaireTop(){  
	//request data for centering  
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var windowHeightBackGround = $(document).height();
	var popupHeight = $("#popupTertiaire").height();  
	var popupWidth = $("#popupTertiaire").width();  
	//centering  
	$("#popupTertiaire").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  
   //Set height and width to mask to fill up the whole screen  
  $('#popupBackground').css({'width':windowWidth,'height':windowHeightBackGround}); 
} 

function redirectModifyProfil(){  
	window.location = "/webentreprise/af/webentreprise/visitor/entrepriseProfile.do"
}

/********************FIN POPUPS*********************/

/***************FORMULAIRE INSCRIPTION/MODIFICATION CLIENT ************/
//MODIFICATION UTILISATEUR	
function switchTypeEntreprise(e) {
	if (e.value == '08') {
		$('#typeSecteurPublic').hide();
		$('#checkNumSiretDiv').hide();
		$('#siret').hide();
		$('#typeAutre').hide();
		$('#codeEntreprise').show();
	} else if (e.value == '09') {
		$('#typeSecteurPublic').hide();
		$('#checkNumSiretDiv').hide();
		$('#typeAutre').show();
		$('#codeEntreprise').show();
		$('#siret').hide();
	} else if (e.value == '10') {
		$('#typeSecteurPublic').show();
		$('#typeAutre').hide();
		$('#checkNumSiretDiv').show();
		if($('#checkNumSiret').is(':checked')){
			$('#siret').show();
			$('#codeEntreprise').hide();
		} else {
			$('#siret').hide();
			$('#codeEntreprise').show();
		}				
	} else {
		$('#typeSecteurPublic').hide();
		$('#typeAutre').hide();
		$('#codeEntreprise').hide();
		$('#checkNumSiretDiv').hide();
		$('#siret').show();				
	}
}
		
//***************FIN FORMULAIRE INSCRIPTION/MODIFICATION CLIENT ************	
