$(document).ready(function() { 
	
	$('#accordion').accordion({ autoHeight: false, clearStyle: true, collapsible: true }); 
	 	
	$('#multi_images IMG').each(function() {
		
			$(this).css('float', 'left');
	        var maxWidth = 200; // Max width for the image
	        var maxHeight = 200;    // Max height for the image
	        var ratio = 0;  // Used for aspect ratio
	        var width = $(this).width();    // Current image width
	        var height = $(this).height();  // Current image height
	 
	        // Check if the current width is larger than the max
	        if(width > maxWidth){
	            ratio = maxWidth / width;   // get ratio for scaling image
	            $(this).css("width", maxWidth); // Set new width
	            $(this).css("height", height * ratio);  // Scale height based on ratio
	            height = height * ratio;    // Reset height to match scaled image
	            width = width * ratio;    // Reset width to match scaled image
	        }
	 
	        // Check if current height is larger than max
	        if(height > maxHeight){
	            ratio = maxHeight / height; // get ratio for scaling image
	            $(this).css("height", maxHeight);   // Set new height
	            $(this).css("width", width * ratio);    // Scale width based on ratio
	            width = width * ratio;    // Reset width to match scaled image
	        }
	    });
	 $('#multi_images IMG').show(); 
	 
	 $('#email').bind("change", function(){ window.validEmail('email');});
	 $('#nom').bind("change", function(){ window.validRequire('nom', 2);});
	 $('#comment').bind("change", function(){ window.validRequire('comment', 2);});
	 
	 
	 if($('#perpage')) {
		 $('#perpage').bind("change", function(){ 
			 $('#nbperpageform').submit(); 
		 });
	 }
	 
});

/** Submit form validation */
function submitForm(){
	var valid = true;
	if( ! validEmail('email') ) valid = false; 
	if( ! window.validRequire('nom', 2)  )  valid = false;  
	if(!valid) return;
	$("#comment_form").submit();
}

/** Email validation */
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

/** Require validation */
function validRequire(inputId, len) { 
	var val = $("#" + inputId ).val();  
	var ptest = true;
	if(!val) val=''; 
	if(! val.length  || val.length < len || ptest == false ) {   
		$("#" + inputId +"_err").show();   
		return false; 
	} else { 
		$("#" + inputId +"_err").hide();  
	}
	return true;
}

/** Email validation */
function validEmail(inputId) {
	var val = $("#" + inputId ).val();
	if( !isValidEmailAddress(val ) )  { 
		$("#" + inputId +"_err").show(); 
		 
		return false; 
	} else {
		$("#" + inputId +"_err").hide();  
	}
	return true;
}
