/** Préchargement des images **/
jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
  }
}

/*Vérification email*/
function isEmail(adresse){
	if (adresse.search(/^((\"[^\"f\n\r\t\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/) != -1) return true;
	else return false;
}

	/*Fonctions trim récupré sur http://www.webtoolkit.info/javascript-trim.html*/
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

jQuery.fn.extend({
	dmark:	function (oConfig){
		var color = "#FF0000";
		for(c in oConfig){	
			if(c == "color") color = oConfig[c]; 
		}
		var prefix = Math.round(Math.random()*1000000);
		//alert(prefix);
		return this.each(function (){
			var oldTheme = $(".partenerDevise").css("backgroundColor");
			var flag = false;
			$(this).css({cursor:"pointer"}).click(function (){
				if(!flag){
					box = "." + $(this).attr("id");//Les box partenaires sélectionnés
					//Première phase : Initialisation; on restaure tout le thème orignal avant toute nouvelle action
					$(".partener").css({border:"1px solid " + oldTheme});
					$(".partener .partenerDevise").css({backgroundColor:oldTheme, color:"#000000"}); 
				//Activer les box
					$(box).css({borderColor: color});
					$(box + " .partenerDevise").css({backgroundColor: color, color:"#FFFFFF"});
					//flag = true;
				}else{
					/* $(box).css({borderColor: oldTheme});
					$(box + " .partenerDevise").css({backgroundColor: oldTheme, color:"#FFFFFF"});
					flag = false; */
				}
			});
		});
	}
});

$.fn.regex = function(pattern) { 
  return ($(this[0]).val().match(pattern)) ? true : false; 
} 


$(document).ready(function (){	
	$('#dialog, #cfrm, #polloutput').dialog({
		autoOpen: false,
		bgiframe: true,
		dialogClass: 'alert',
		draggable: false,
		width: 400,
		//title: 'UTB Alerte > Erreur',
		ide: 'slide',
		modal: true,
		resizable: false,
		stack: false,
		loseOnEscape: false,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			}, 
			/* "Cancel": function() { 
				$(this).dialog("close"); 
			}  */
		}
	});
	//$("#out").html(lorem(1));
	//$(".produits-part-1").html(lorem(1));
	//$(".produits-part-2").html(lorem(1));
	
	$.preloadImages("image1.gif", "/path/to/image2.png",
"some/image3.jpg");

	$(".services-accueil-particulier ul li a:last").css("border","none");
	$(".services-accueil-professionnel ul li a:last").css("border","none");
	$(".service-accueil-entreprise ul li a:last").css("border","none");
	
	//Curving of the newsletter box
	$("#newsletter").rc({color:"#e8e8f3",img:"themes/2009a/images/rc-newsletter.jpg"});
	$(".presentation h3").rc({color:"#2776db",img:"themes/2009a/images/rc-bleu.jpg",lb:false, rt:true, rb:false});
	$("#contactform").parent().rc({color:"#f0f4fd",img:"themes/2009a/images/rc-bleu-clair.jpg"});
	
	//Mini plan du site
	$(".mps ul").each(function (i){
		$(this).children("li:first").css("listStyle","none");
	});
	
	//Transparence des bordures de la page
	$(".top").pngFix();
	
	
	//Marquee
	$("#marquee").marquee('pointer').mouseover(function () {
	  $(this).trigger('stop');
	}).mouseout(function () {
	  $(this).trigger('start');
	}).mousemove(function (event) {
	  if ($(this).data('drag') == true) {
		this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
	  }
	}).mousedown(function (event) {
	  $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
	}).mouseup(function () {
	  $(this).data('drag', false);
	});

	$(".tab-modele-1 tr").hover(function (){
			var oldBgImg = $(this).css("backgroundImage");
			var oldBgColor = $(this).css("backgroundColor");
			var oldBgPos = $(this).css("backgroundPosition");
			var oldBgRepetition = $(this).css("backgroundRepetition");
			//$(this).css("background-color","#FF0000")
		}, function (){
			//$(this).css({backgroundImage:oldBgImg, backgroundColor:oldBgColor, backgroundPosition:oldBgPos, backgroundRepetition:oldBgRepetition});
	});
	
	
	

	$("#XOF").dmark({color:"#2534bf"});
	$("#EUR").dmark({color:"#ff8200"});
	$("#USD").dmark({color:"#41bf25"});
	$("#ALL").dmark({color:"#e9f1f5"});
	$("#GBP").dmark({color:"#077980"});
	$("#XAF").dmark({color:"#bf00bd"});
	
	//$("#faq").css({padding:"6px"}).rc({color:"#e8e8f3",img:"themes/2009a/images/rc-newsletter.jpg"});
	
	$(".reliefbox").hover(function (){
			$(this).css({backgroundColor:"#e5ecf9"});
		}, function (){
				$(this).css({backgroundColor:"#eff3fc"});
		});
	
	/******* Contact form ********/
	$("#ctform input:[type='text'], #ctform select, #ctform textarea").css({border:"1px solid #2776db"});
	$('#ctform').submit(function (){
		//Réinitialisation
		$("#ctform input:[type='text'], #ctform select, #ctform textarea").css({border:"1px solid #2776db"});
		
		var error = false;
		$("#ctform .required").each(function (i){
				var val = trim($(this).attr('value'));
				if(val == ""){
					error = true;
					$(this).css({border:"1px solid red"}).attr('value',"");
					$('#dialog').html("Certaines informations sont obligatoires.<br /> Veuillez corriger les informations contenues dans les champs marqués en rouge.").dialog('option','title','UTB.TG - Alerte').dialog('open');
				}
		})
		if(error) return false;
			
	});
	
	//FAQ
	$("#accordion, #ouverturedecompteaccordeon").accordion({
		autoHeight: false
	});
	
	$("#accordeonCreditParticulier table tr").each(function (i){
		$(this).children('td').eq(0).css({fontWeight:"bold",textTransform:"capitalize"});
	});
	
	//Poll
	$('.poll table tr').each(function (){
		$(this).children("td").eq(0).css({width:'15px',textAlign:'center'});
	});
	
	//Les agences
	$("#agences .agence").hover(function (){
			/* $(this).children('.agenceImg').css({marginTop:"-70px"});*/
			$(this).children('.agenceImg').css({
												position:"absolute",
												marginTop:"20px",
												zIndex:"9999",
												border:"1px solidred;"
												}).show("slow"); 
		}, function (){
			$(this).children('.agenceImg').hide("slow");
		});
		
		//pour ajouter un autre compte bancaire
		$('#mores').click(function (){
			$('.compteini').eq(0).clone(true).appendTo('#touscompte');
		});
		//pour supprimer un compte bancaire
		$('#diminuer').click(function (){
			if($('.compteini').size()==1) return false;
			$('.compteini:last').remove();
		});	
		//controle du type de compte bancaire
		$('.myselect').change(function (){
			var	i =$(this).children('option:selected').attr("value");
			if(i!=1){
					$(this).parent().parent().children("input").eq(0).attr("disabled","disabled").css("background","#cccccc");
					$(this).parent().parent().children("input").eq(1).attr("disabled","disabled").css("background","#cccccc");
					$(this).parent().parent().children("input").eq(8).attr("disabled","disabled").css("background","#cccccc");
					$(this).parent().parent().children("input").eq(14).attr("disabled","disabled").css("background","#cccccc");
					$(this).parent().parent().children("input").eq(0).removeClass('required');
					$(this).parent().parent().children("input").eq(1).removeClass('required');
					$(this).parent().parent().children("input").eq(8).removeClass('required');
					$(this).parent().parent().children("input").eq(14).removeClass('required');
			}
			else{
					$(this).parent().parent().children("input").attr("disabled",false).css("background","#FFFFFF");
			}	
		});
		//controle du type des données saisies
		$('.compteini input').keypress(function (e){
				if(e.which == 9) return false;//Ne rien faire quand c'est une tabulation
				if(e.which == 8){
					if($(this).attr("name")!= "")
						$(this).val(null);
					else
						return false;
				}
				else if((e.which < 48 || e.which > 57)){ 
					return false;//$(this).val(null);
				}
				else{
					$(this).next().focus();
				}
		});
		$('#toutcocher').click(function (){
			$("#abonnes .jforms-ctl-destinataires input:checkbox").attr("checked","checked");
		})
		$('#toutdecocher').click(function (){
			$("#abonnes .jforms-ctl-destinataires input:checkbox").attr("checked",false);
		})		
});


