﻿$(window).resize(function() {
	resizeLayerPopin();
})


function setPositionPopinProfil(id){
	var dim = getPageSize();
	var scroll = getPageScroll();
	var popin = $(id);
	
	popin.css({top: scroll[1]});
}

// Ouverture de Popin //
function openPopinLayer(urlPopin, newClass){
	$("#popinLayerDisplay").remove();
	$("#corps").after('<div id="popinLayerDisplay"><div id="bgPopinLayer" class="'+newClass+'"><div class="loading"></div></div></div>')
	$.ajax({
		type:"GET",
		url:urlPopin,
		success:function(msg) {
			$('#bgPopinLayer').find('.loading').remove();
			$("#bgPopinLayer").after(msg);
			
			if($('#popinLayerDisplay').find('#popin-profil-public').length){ // s il s'agit de la popin : Profil public ( popin longue .. )
				setPositionPopinProfil('#popin-profil-public');
			}
			
			closePopinLayer();
			backgroundPopin();
		}
	});
}

// Fermeture de popin //
function closePopinLayer(){
	$(".btClosePopinLayer").click(function() {
			$("#popinLayerDisplay").remove();
			if (typeof document.body.style.maxHeight === "undefined") { 
				$("select").show();
			}
			return false;
		});
}

// gestion taille background Popin //
function backgroundPopin(){
	var bodyHeight = $("body").height();
	$("#bgPopinLayer").height(bodyHeight);
	if (typeof document.body.style.maxHeight === "undefined") { // IE6
		var bodyWidth = $("body").width();
		$("#popinLayerDisplay").width(bodyWidth);
		$("#bgPopinLayer").width(bodyWidth);
		$("select").hide();
	}
}

// resize de la popin //
function resizeLayerPopin(){
	if (typeof document.body.style.maxHeight === "undefined") { // IE6
		var bodyWidth = $("body").width();
		$("#popinLayerDisplay").width(bodyWidth);
		$("#bgPopinLayer").width(bodyWidth);
	}
}

//appelable via le flash
function closeIntermediateScreen(){
    $("#popinLayerDisplay").remove();
    if (typeof document.body.style.maxHeight === "undefined") { 
        $("select").show();
	}    
}


$(document).ready(function()
 {
  $("#headerPlaySpiritLink").attr("href", 'http://www.playspirit.net/ramses/webclient/interface/index.php?cid=1abe322108b7f5bf95a3a8db88nestle&src=' + escape( location.href ));
  $("#headerPlaySpiritLink").click(
    function()
    {
    var x = screen.width - Math.round((screen.width * 300) / 800);
    var y = screen.height - Math.round((screen.height * 600) / 640);
 
    var  playWin = window.open( $("#headerPlaySpiritLink").attr("href"), 'PlaySpirit', 'directories=no,menubar=no,status=no,location=no,scrollbars=no,resizable=no,width=300,height=340,top=' + y + ',left=' + x);
    playWin.focus();
    return false;
    }
   )
   
   /* ! overture POPIN ---- */
  
   $('a.linkPopin').click(function(){
		var urlPopin = $(this).attr('href');
		
		openPopinLayer(urlPopin,'');
		return false;
   })
   
   if($('.list-avatars').length){
		$('.list-avatars ul li').bind('mouseover',function(){
			$(this).addClass('jshover');
		})
		
		$('.list-avatars ul li').bind('mouseout',function(){
			$(this).removeClass('jshover');
		})		
		
		$('.list-avatars ul li a').bind('click',function(){
			/*$('.list-avatars ul li.current').removeClass('current');
			$(this).parents('li').addClass('current');*/
			
			var srcImg = $('.avatar img',$(this).parents('li')).attr('src'); 
			var nameAvatar = $('.name-avatar',$(this).parents('li')).html();
			
			$('.aside .avatar img').attr('src',srcImg);
			$('.aside .avatar strong').html(nameAvatar);
			$('.srcAvatar').attr('value', srcImg);
			$('.nameAvatar').attr('value', nameAvatar);
		
			return false;
		})
	}
 });
 
 
 
 
 //PTS - move send to friend as it is now for all website
 $(document).ready(function(){
	openPopinSend();
	openPopinSendWithParameter();
});

$(window).resize(function() {
	
	resizeLayerPopin();
})

// Ouverture de Popin //
function openPopinSend(){
	$("a.sendToFriend").click(function() {
		$("#popinFriendDisplay").remove();
		$("#corps").after('<div id="popinFriendDisplay"><div id="bgPopinFriend"></div></div>')
		$.ajax({
			type:"GET",
			url:this.href,
			success:function(msg) {
				$("#bgPopinFriend").after(msg);
				closePopinSend1();   
				backgroundPopin1();
				checkFormFriend();
			}
		});
//		jQuery(this).load(this.href, function(responseText){
//		    $("#bgPopinFriend").after(responseText);
//			closePopinSend1();   
//			backgroundPopin1();
//			checkFormFriend();
//		});
		return false;
	});	
}


// Ouverture de Popin //
function openPopinSendWithParameter(){
	$("a.sendToFriendWithParameter").click(function() {
	    this.href+="?idRefStf=" + this.rel + "&CurrentUrl=" + escape(window.location.href);
	    window.scroll(0,0);
		$("#popinFriendDisplay").remove();
		$("#corps").after('<div id="popinFriendDisplay"><div id="bgPopinFriend"></div></div>')
		$.ajax({
			type:"GET",
			url:this.href,
			success:function(msg) {
				$("#bgPopinFriend").after(msg);
				closePopinSend1();   
				backgroundPopin1();
				checkFormFriend();
			}
		});
//		jQuery(this).load(this.href, function(responseText){
//		    $("#bgPopinFriend").after(responseText);
//			closePopinSend1();   
//			backgroundPopin1();
//			checkFormFriend();
//		});
		return false;
	});	
}


// Fermeture de popin //
function closePopinSend1(){
	$(".btClosePopinSend").click(function() {
			$("#popinFriendDisplay").remove();
			if (typeof document.body.style.maxHeight === "undefined") { 
				$("select").show();
			}
			return false;
		});
}

// gestion taille background Popin //
function backgroundPopin1(){
	var bodyHeight = $("body").height();
	var bodyWidth = $("body").width();
	
	$("#bgPopinFriend").height(bodyHeight);
	if(bodyWidth >= 954){
		$("#popinFriendDisplay").width(bodyWidth);
		$("#bgPopinFriend").width(bodyWidth);
	}
	else{
		$("#popinFriendDisplay").width(954);
		$("#bgPopinFriend").width(954);	
	}
	if (typeof document.body.style.maxHeight === "undefined") { // IE6
		$("select").hide();
	}
}

// resize de la popin //
function resizeLayerPopin(){
	var bodyWidth = $("body").width();
	if(bodyWidth >= 954){
		$("#popinFriendDisplay").width(bodyWidth);
		$("#bgPopinFriend").width(bodyWidth);
	}
	else{
		$("#popinFriendDisplay").width(954);
		$("#bgPopinFriend").width(954);	
	}
}

function checkFormFriend(){	
	$("form").submit(function(){					  
		var re= /^[a-z\d]+((\.|-|_)[a-z\d]+)*@((?![-\d])[a-z\d-]{0,62}[a-z\d]\.){1,4}[a-z]{2,6}$/gi;
		var re2 =/^[^<">]+$/gi;
		
		$("#insidePopinFriend fieldset label").removeClass("errorEmpty");
		$(".errorMsgTextareaFriend").removeClass("errorEmpty");
		$(".errorMsgFriend").hide();
		
		// test champs Expediteur //
		$(".blocFriendSender input").each(function(){
			if($(this).attr("value") != null){
				if($(this).attr("id") == "mailSender"){
					if($("#insidePopinFriend #mailSender").attr("value").match(re) == null){
						$("#insidePopinFriend #mailSender").siblings("label").addClass("errorEmpty");
						$(".errorMsgFriend").show();
					}
					
				}
				else{
					    if($(this).attr("value").match(re2) == null){
						    $(this).siblings("label").addClass("errorEmpty");
						    $(".errorMsgFriend").show();
					}
				}
			}
			else{
				$(this).siblings("label").addClass("errorEmpty");
				$(".errorMsgFriend").show();
			}
	   	});
		
		
		// test champs Destinataire obligatoire //
		$("#firstLineRecipient input").each(function(){
			if($(this).attr("value") != null){
				if($(this).attr("id") == "mailRecipient1"){
					if($("#firstLineRecipient #mailRecipient1").attr("value").match(re) == null){
						$("#insidePopinFriend #mailRecipient1").siblings("label").addClass("errorEmpty");
						$(".errorMsgFriend").show();
					}
					
				}
				else{
					    if($(this).attr("value").match(re2) == null){
						    $(this).siblings("label").addClass("errorEmpty");
						    $(".errorMsgFriend").show();
					}
				}
			}
			else{
				$(this).siblings("label").addClass("errorEmpty");
				$(".errorMsgFriend").show();
			}
		});
		
		// test champs Destinataire facultatif //
		$(".blocFriendRecipient .lineFormSendFriend").each(function(){
			if($(this).attr("id") != "firstLineRecipient"){
				$(this).each(function(){
						
					var resultTestLine = testLine($(this));
					
					if(resultTestLine == true){

						var inputLineTmp = $(this).children().children("input");
						
						inputLineTmp.each(function(){
							if($(this).attr("value") != null){
								if($(this).attr("class") == "mailInputRecipient"){
									if($(this).attr("value").match(re) == null){
										$(this).siblings("label").addClass("errorEmpty");
										$(".errorMsgFriend").show();
									}
								}
								else{
					                    if($(this).attr("value").match(re2) == null){
						                    $(this).siblings("label").addClass("errorEmpty");
						                    $(".errorMsgFriend").show();
					                }
				                }
							}
							else {
								$(this).siblings("label").addClass("errorEmpty");
								$(".errorMsgFriend").show();	
							}
						});
					}
				});
				
			}
		});
		
		// test champs message //
		if($(".blocMsgFriend #champMessageSendFriend").attr("value") == undefined){
		    $(".errorMsgTextareaFriend").addClass("errorEmpty");
			$(".errorMsgFriend").show();
		}
		 if($("#insidePopinFriend .errorEmpty").length){
		 return false;									   
		
		}
		else{
			SendToFriend();
			return false;									   
		
		}
		return false;
   });
	
}

function testLine(obj){
	var pathObj = obj.find("input");
	var resultSearchInput = false;
	pathObj.each(function(){
		if($(this).attr("value") != null){
			resultSearchInput = true;
		}
  });
	return resultSearchInput;
}

function openConfirmFriend(urlConfirm){

    var mailSender = encodeURIComponent($("#insidePopinFriend #mailSender").attr("value"));
    var nameSender = encodeURIComponent($("#insidePopinFriend #nameSender").attr("value"));
    var firstNameSender = encodeURIComponent($("#insidePopinFriend #firstNameSender").attr("value"));

    var mailRecipient1 = encodeURIComponent($("#firstLineRecipient #mailRecipient1").attr("value"));
    var nameRecipient1 = encodeURIComponent($("#firstLineRecipient #nameRecipient1").attr("value"));
    var firstNameRecipient1 = encodeURIComponent($("#firstLineRecipient #firstNameRecipient1").attr("value"));
  
    var message = encodeURIComponent($("#insidePopinFriend  #champMessageSendFriend").attr("value"));
    
    var isCopiedDesired =  $("#insidePopinFriend  #copySender").attr("checked");
               
    var mailRecipient2= "undefined";    
    if ($("#insidePopinFriend  #mailRecipient2").attr("value")!=null) 
       mailRecipient2 = encodeURIComponent($("#insidePopinFriend  #mailRecipient2").attr("value"));
    
    var nameRecipient2 = "undefined";
    if ($("#insidePopinFriend  #nameRecipient2").attr("value")!=null)
        nameRecipient2 =encodeURIComponent($("#insidePopinFriend  #nameRecipient2").attr("value"));
    
    var firstNameRecipient2 = "undefined";
    if ($("#insidePopinFriend  #firstNameRecipient2").attr("value")!=null)
        firstNameRecipient2 = encodeURIComponent($("#insidePopinFriend  #firstNameRecipient2").attr("value"));
    
    var mailRecipient3= "undefined";    
    if ($("#insidePopinFriend  #mailRecipient3").attr("value")!=null) 
       mailRecipient3 = encodeURIComponent($("#insidePopinFriend  #mailRecipient3").attr("value"));
    
    var nameRecipient3 = "undefined";
    if ($("#insidePopinFriend  #nameRecipient3").attr("value")!=null)
        nameRecipient3 = encodeURIComponent($("#insidePopinFriend  #nameRecipient3").attr("value"));
    
    var firstNameRecipient3 = "undefined";
    if ($("#insidePopinFriend  #firstNameRecipient3").attr("value")!=null)
        firstNameRecipient3 = encodeURIComponent($("#insidePopinFriend  #firstNameRecipient3").attr("value"));
    
    var mailRecipient4= "undefined";    
    if ($("#insidePopinFriend  #mailRecipient4").attr("value")!=null) 
       mailRecipient4 = encodeURIComponent($("#insidePopinFriend  #mailRecipient4").attr("value"));
    
    var nameRecipient4 = "undefined";
    if ($("#insidePopinFriend  #nameRecipient4").attr("value")!=null)
        nameRecipient4 = encodeURIComponent($("#insidePopinFriend  #nameRecipient4").attr("value"));
    
    var firstNameRecipient4 = "undefined";
    if ($("#insidePopinFriend  #firstNameRecipient4").attr("value")!=null)
        firstNameRecipient4 = encodeURIComponent($("#insidePopinFriend  #firstNameRecipient4").attr("value"));
    
    if($("#insidePopinFriend  #url").attr("value")!=null);
        url=escape($("#insidePopinFriend  #url").attr("value")); 

    var intIsCopiedDesired = 0;
    if ($("#insidePopinFriend  #copySender").attr("checked")==true)
        intIsCopiedDesired=1;
    
    var idRefStf = "";
    if($("#insidePopinFriend  #idRefStf").attr("value")!=null);
        idRefStf = encodeURIComponent($("#insidePopinFriend  #idRefStf").attr("value")); 
    
	$("#popinFriendDisplay").remove();
	$("#corps").after('<div id="popinFriendDisplay"><div id="bgPopinFriend"></div></div>')

	$.ajax({
		type:"POST",
		url:urlConfirm,	
		contentType:'application/x-www-form-urlencoded;charset=utf-8',
		data:"mailSender="+ mailSender + "&nameSender="+ nameSender + "&firstNameSender="+firstNameSender
		                  + "&mailRecipient1="+ mailRecipient1 + "&nameRecipient1="+ nameRecipient1 + "&firstNameRecipient1="+firstNameRecipient1
		                  + "&mailRecipient2="+ mailRecipient2 + "&nameRecipient2="+ nameRecipient2 + "&firstNameRecipient2="+firstNameRecipient2
                          + "&mailRecipient3="+ mailRecipient3 + "&nameRecipient3="+ nameRecipient3 + "&firstNameRecipient3="+firstNameRecipient3
                          + "&mailRecipient4="+ mailRecipient4 + "&nameRecipient4="+ nameRecipient4 + "&firstNameRecipient4="+firstNameRecipient4
                          + "&message="+ message
                          + "&intIsCopiedDesired=" + intIsCopiedDesired
                          + "&url="+ url
                          + "&idRefStf=" + idRefStf
		,
		beforeSend:function() {
		// Loader 
		$("#corps").after('<div id="popinFriendDisplay"><div id="bgPopinFriend"></div><img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." /></div>');
		backgroundPopin1();
		
	    }
	    ,
	    complete:function() {
		    $("#imgLoader").remove();
		
	    }
	    ,
    	success:function(msg1) {
    	    $("#imgLoader").remove();
			$("#bgPopinFriend").after(msg1);
			closePopinSend1();
			backgroundPopin1();		
		}
	});
}

function SendToFriend(){
         openConfirmFriend("/cuisine/recettes/confirmSendToFriend.aspx");
       }
	   
	  


/*************************************************/
// Fonctions générales
/*************************************************/

// Renvoie les dimensions de la page
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};


function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

/* layer nav IE6 */
$(document).ready(function(){
    displayNavLayer();

    function displayNavLayer(){
        if(typeof document.body.style.maxHeight === "undefined"){
            jQuery("#navigation ul .open-layer").hover(function(){
                jQuery(this).find(".hover-menu").toggle();
            },function(){
                jQuery(this).find(".hover-menu").toggle();
           })
        }
    }
})
