// ATLAS METHODS
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
				phone_number = phone_number.replace(/\s+/g, "");
					return this.optional(element) || phone_number.length > 9 &&
					phone_number.match(/^00336[0-9]{8}$/);
				}, "Ko");
	
				
	function ajaxCallForm(element,dwl){

	    //if(dwl){
		/*	var currentLocation = document.location.href;
			var array_url = currentLocation.split('#');
			var param = array_url[1];
			alert(param);
			var url = $(element).attr("action")+param;*/
		//}
	//	else var url = $(element).attr("action");
		var s = $(element).serialize(); 
		if($(element).valid()){
			$.ajax({ 
				type: "POST", 
				data: s, 
				url: $(element).attr("action"), 
				success: function(retour){ 				   
					if(retour!='false'){
						if(dwl) {
							location.href="pdf/recettes.zip";	
						}
						$("#fancy_ajax").html(retour);
					}
				}
			}); 
		}
	}

// ATLAS EVENTS
	$(document).ready(function() {
	

		//url index.htm#&email=traimbault@next-idea.fr&optin=1

		
		//Activate FancyBox
		$("a.download, a.download2, a.download_bottom, a.mail, .jFlowSlideContainer a, .homelist a, #dliphone").fancybox({
			'hideOnContentClick': false,
			'frameWidth': 690,
			'frameHeight': 421,
			'overlayOpacity': 0.7,
			//'overlayShow':	true
			'overlayColor': '#000'
		});
		
		 // #### Form Validation ####
		
		 // POP1 form Validation (download fiche)
		 $("#pop1").livequery(function(){
			$(this).validate({
				debug: true,
				errorElement: "em",
				success: function(label) {
					label.text("ok").addClass("success");
				},
				rules: {
					civility: { required: true },
					lastname: { required: true },
					firstname: { required: true },
					email: { required: true, email:true }
					},
				messages: {
					civility: "ko",
					lastname: "ko",
					firstname: "ko",
					email: "ko"
					}
			});	 
		 });
		 
		// POP2 form validation (iphone number)
	    $("#pop2").livequery(function(){
			$(this).validate({
				debug: true,
				errorElement: "em",
				success: function(label) {
					label.text("ok").addClass("success");
				},
				rules: {
					iphone: { required: true, phoneUS: true }
					},
				messages: {
					iphone: "ko"
					}
			});
		});
		
		// POP3 form validation (email parrainage)
		$("#pop3").livequery(function(){
			$(this).validate({
				debug: true,
				errorElement: "em",
				success: function(label) {
					label.text("").addClass("success");
				},
				rules: {
					mail1: { email:true, required: true },
					mail2: { email:true },
					mail3: { email:true }
					},
				messages: {
					mail1: "a",
					mail2: "b",
					mail3: "c"
					}
			});
		});
		
		// POP3 form validation (email parrainage)
		$("#pop3bis").livequery(function(){
			$(this).validate({
				debug: true,
				errorElement: "em",
				success: function(label) {
					label.text("").addClass("success");
				},
				rules: {
					mail1: { email:true, required: true },
					mail2: { email:true },
					mail3: { email:true }
					},
				messages: {
				    parrain: "d",
					mail1: "a",
					mail2: "b",
					mail3: "c"
					}
			});
		});
		
		// POP2bis form validation (Iphone)
		$("#pop2b").livequery(function(){
		   $(this).validate({
			debug: true,
			errorElement: "em",
			success: function(label) {
				label.text("ok").addClass("success");
			},
			rules: {
				civility: { required: true },
				lastname: { required: true },
				lastname: { required: true },
          		firstname: { required: true },
				email: { required: true, email:true },
				iphone: { required: true, phoneUS: true }
				},
			messages: {
				civility: "ko",
				lastname: "ko",
				firstname: "ko",
				email: "ko",
				iphone: "ko"
				}
		  });
		 });
		 
		// #### Form Ajax ####
		 
		// form binding pop1 (download recette)
		$("#pop1").livequery('submit', function(e) {    	
			e.preventDefault(); 
			ajaxCallForm(this,true);
		});
			
	    // form binding pop2 (iphone number)
		$("#pop2").livequery('submit', function(e) {    
			e.preventDefault(); 		
			ajaxCallForm(this);
		});
		 
	    // form binding pop3 (email parrainage)
		$("#pop3").livequery('submit', function(e) {    	
			e.preventDefault(); 		
			ajaxCallForm(this);
		});
		
		// form binding pop3bis (email parrainage)
		$("#pop3bis").livequery('submit', function(e) {    	
			e.preventDefault(); 		
			ajaxCallForm(this);
		});
			
	    // form binding pop2bis (iphone complet)
		$("#pop2b").livequery('submit', function(e) {    	
			e.preventDefault(); 
			ajaxCallForm(this);
		});
		 
		

		//Fade image
		$("a img").fadeTo("fast", 1.0); // This sets the opacity of the thumbs to fade down to 60% when the page loads
		$("a img").hover(function(){
			$(this).fadeTo("fast", 0.7); // This should set the opacity to 100% on hover
		},function(){
			$(this).fadeTo("fast", 1.0); // This should set the opacity back to 60% on mouseout
		});
		
		//Display bigTarget
		$("div.bigTarget a").bigTarget({
    		hoverClass: 'over', // CSS class applied to the click zone onHover
    		clickZone : 'div:eq(0)' // jQuery parent selector
  		});
		
		// Animate Images into bigTarget
		$("div.bigTarget").hover(function(){
			$("img",this).fadeTo("fast", 0.7);
			},function(){
			$("img",this).fadeTo("fast", 1.0);
		});	
		
		// jflow carroussel
		$( "#myController ").jFlow({
			slides: "#mySlides",
			width: "631px",
			height: "261px",
			duration: 400,
			timeout: 5000,
			enable_opacity_change: true 
		});
	
	});
	
	function get(name) {
		if(param_url != "") {
			var tab = param_url.split("&");
			for(var i = 0 ; i < tab.length ; i++) {
				var tmp = 	tab[i].split("=");
				if(tmp[0] == name) {
					return tmp[1];
				}
			}
		}
		else
			return "";
	}