var SendToFriend = {
	
	/* initialisation */
	init: function() {
		if(jQuery('#sendToFriend').length) {
			
			SendToFriend.close();
			
			var errors,
				status;
	
			jQuery('#sendToFriend :submit').click(function(event){
				
				errors = SendToFriend.checkInputs();			
				
				status = jQuery.grep(errors, function(n, i){
					return n == false;
				});
			
				if(status.length < 1) {
					
				} else {
					event.preventDefault();
					jQuery('#sendToFriend .error').addClass('showed');
				}
			});
		}
		
	},
	close: function() {
		jQuery('.closeIt').bind("click", function() {
			window.top.Popin.closeIFrame();		
		});
	},
	
	 reloadCaptcha : function(){ 
      var image_src = document.getElementById('captcha-sticky-img').src.split("?");
      document.getElementById('captcha-sticky-img').src = image_src[0] + "?img=" + Math.random();;;
 },
	/* verification des champs */
	checkInputs: function() {
		
		jQuery('#sendToFriend .error').empty().removeClass('showed');
		
		var errorMsg = {
			'email-invalid': 'Veuillez renseigner un email valide',
			'friendEmail-invalid': 'Veuillez renseigner un email valide', 
			'captcha-invalid': 'Veuillez renseigner la valeur'
		}
		
		
		var mandatoryInputs = jQuery('.mandatory input'),
			errorStatus = [],
			currentItem,
			regMail = new RegExp(/^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$/);
		
		
		
		for (var i = 0; i < mandatoryInputs.length; i++) {
			
			currentItem = mandatoryInputs[i];
						
			if (currentItem.value != '') {
				
				
				if (currentItem.name!='captcha' && !currentItem.value.match(regMail)) {
					errorStatus.push(false);
					
					jQuery(currentItem).parent().find('.error').html(errorMsg[jQuery(currentItem).attr('name')+'-invalid']);
					continue;
				}
        /*

				 				else if(currentItem.value ==null) {
				 					jQuery(currentItem).parent().find('.error').html(errorMsg[jQuery(currentItem).attr('name')+'-invalid']);
				 					continue;
				 				}
				 */
				
				
				
				errorStatus.push(true);
				
			} else { // empty
				errorStatus.push(false);
				jQuery(currentItem).parent().find('.error').html(errorMsg[jQuery(currentItem).attr('name')+'-invalid']);
			}
		}
		
		// retour status champs
		return errorStatus;
	}
	
}



var Popin = {
	
	init: function() {

		if(jQuery('#iFrameSendToFriend').length) {
			Popin.setLayer();
			Popin.open();
		}
	},
	setLayer: function() {
		if(!jQuery('.bgPopin').length) {
			jQuery("body").append('<div class="bgPopin"></div>');
		}
		jQuery('.bgPopin').css('height', jQuery('body').height() + 60);
		
	},
	closeIFrame: function() {  
		jQuery('#iFrameSendToFriend').hide();
		jQuery('.bgPopin').unbind().hide();
		if(jQuery.browser.msie && jQuery.browser.version == 6) {
			jQuery('#globalContent select').css('visibility', 'visible');	
		}
	},
	open: function() {

		jQuery('.sendToFriend a:first').click(function(event){
		  event.preventDefault();
	    Popin.write_src();
			jQuery('.bgPopin').bind("click", Popin.closeIFrame).show();
			jQuery('#iFrameSendToFriend').show();
			if (jQuery.browser.msie && jQuery.browser.version == 6) {
				jQuery('#globalContent select').css('visibility', 'hidden');
			}
		});
	},
  write_src: function(){
   var langue = jQuery("html").attr("lang");
     if(langue == 'en'){
        langue = "gb";
     }
     jQuery('#iFrameSendToFriend').attr("src","/"+langue+"/sendtofriend/send_to_friend.html");
  }
};


jQuery(document).ready(function(e) {
	SendToFriend.init();
	Popin.init();
}); 
