﻿var manag = "";
if(window.location.href.indexOf("ContentManager")!=-1){
	manag = "/ContentManagerData/Sites/aspnet";
}

$(document).ready(function(){
	preloadPage();
	PngFixImg();
	//initScrollLayer ();
	// Zone texte
	$(".zone_txt_titre a").click(function(event)
	{
		event.preventDefault();
		$(this).siblings().removeClass("zone_txt_titre_infos_active zone_txt_titre_resultats_active");
		if ($(this).hasClass("zone_txt_titre_infos"))
		{
			$(this).addClass("zone_txt_titre_infos_active");
			$(this).parent().parent().children(".zone_txt_contenu_infos").removeClass("off2");
			$(this).parent().parent().children(".zone_txt_contenu_resultats").addClass("off2");
		}
		else
		{
			$(this).addClass("zone_txt_titre_resultats_active");
			$(this).parent().parent().children(".zone_txt_contenu_resultats").removeClass("off2");
			$(this).parent().parent().children(".zone_txt_contenu_infos").addClass("off2");
		}
		//initScrollLayer ();
	});

	// Carrousel
	$(".liste_produits_01 > li").each(function()
	{
		var titre = $(this).children("span")[0];
		var liste = $(this).children("ul")[0];
		var titre_width = $(titre).width();
		var liste_width = $(liste).width();
		if (titre_width > liste_width)
		{
			var padLeft = Math.floor((titre_width - liste_width) / 2);
			var padRight = titre_width - liste_width - padLeft;
			$(liste).css({paddingLeft: padLeft + "px", paddingRight: padRight + "px"});
		}
		else
		{
			var padLeft = Math.floor((liste_width - titre_width) / 2);
			var padRight = liste_width - titre_width - padLeft;
			$(titre).css({paddingLeft: padLeft + "px", paddingRight: padRight + "px"});
		}
	});
	
	// Gestion curseur
	jQuery.fn.extend({
	   findPos : function() {
	       obj = $(this).get(0);
	       var curleft = obj.offsetLeft || 0;
	       var curtop = obj.offsetTop || 0;
	       while (obj = obj.offsetParent) {
	                curleft += obj.offsetLeft
	                curtop += obj.offsetTop
	       }
	       return {x:curleft,y:curtop};
	   }
	});

	var offsetX = $("#TakeCareCenter").offset().left;
	var margeFlecheProduitX = 11;
	var largeurColGauche = 180;
	var posCurrentY = 21;
	if ($(".liste_produits_01 .current").length != 0){
	var posCurrent = $(".liste_produits_01 .current").findPos();
	var posCurrentX = posCurrent.x - offsetX - largeurColGauche + margeFlecheProduitX;
	/*
	var leftMain = $("#TakeCareCenter").css("left");
	var marginLeftMain = $("#TakeCareCenter").css ("margin-left");
	leftMain = parseFloat(leftMain.replace ("px", ""));
	marginLeftMain = parseFloat (marginLeftMain.replace ("px", ""));
	*/
	$(".liste_produits_01_curseur_current").css({left: posCurrentX + "px", top: posCurrentY + "px"});
	}
  $(".liste_produits_01_curseur_hover").hide();  	
	$(".liste_produits_01 a").hover(function()
	{
		posCurrent = $(this).findPos();
		posCurrentX = posCurrent.x - offsetX - largeurColGauche + margeFlecheProduitX;
		$(".liste_produits_01_curseur_hover").show();
		/*
		var leftMain = $("#TakeCareCenter").css("left");
		var marginLeftMain = $("#TakeCareCenter").css ("margin-left");
		leftMain = parseFloat(leftMain.replace ("px", ""));
		marginLeftMain = parseFloat (marginLeftMain.replace ("px", ""));
		*/
		/*posCurrentX = posCurrentX - (leftMain + marginLeftMain);*/
		$(".liste_produits_01_curseur_hover").css({left: posCurrentX + "px", top: posCurrentY + "px", marginLeft : 0, marginRight : 0});
	}, function ()
	{
		$(".liste_produits_01_curseur_hover").css({left: "-20px", top: "-20px"});
		$(".liste_produits_01_curseur_hover").hide();

	});
	$(window).resize(function()
	{
		/*
		leftMain = $("#TakeCareCenter").css("left");
		marginLeftMain = $("#TakeCareCenter").css ("margin-left");
		leftMain = parseFloat(leftMain.replace ("px", ""));
		marginLeftMain = parseFloat (marginLeftMain.replace ("px", ""));
		*/
		//alert (leftMain + " | " + marginLeftMain);
		offsetX = $("#TakeCareCenter").offset().left;
		
	});
	
	/******************************************************************************************************************************************************************/
	/**************************************************************** CRM 2010 COMMENT PRODUCTS ***********************************************************************/
	/******************************************************************************************************************************************************************/
	// >> code for select comment
	$('#newest').click(function(){
		$('#newest .sortbyangle img').attr({src: "/img/our_products/sortbygaucheon.png"});
		$('#newest a').removeClass('off');
		$('#newest a').addClass('on');
		$('#moreuseful .sortbyangle img').attr({src: "/img/our_products/sortbydroiteoff.png"});
		$('#moreuseful a').removeClass('on');
		$('#moreuseful a').addClass('off');
		return false;
	});
	$('#moreuseful').click(function(){
		$('#newest .sortbyangle img').attr({src: "/img/our_products/sortbygaucheoff.png"});
		$('#newest a').removeClass('on');
		$('#newest a').addClass('off');
		$('#moreuseful .sortbyangle img').attr({src: "/img/our_products/sortbydroiteon.png"});
		$('#moreuseful a').removeClass('off');
		$('#moreuseful a').addClass('on');
		return false;		
	});
	
	// >> display popin login if i click in the but of push a comment
	$('.addcommentnolog').click(function(){
	  var gyourl_product = window.location.href;
	  gyourl_product = gyourl_product.replace("#","");
	  wslogin.popIn.signIn(gyourl_product);
	});
	
	// >> votes
	gyo_vote();
	
	// >> num post
	display_numerotation(1);
	
	// >> mask select if no post
	nopost();
	
	// >> load numerotation on the first load page
	updatepostforum_forprdnumeratation($('input.prdcodeforpagination').val(),'DATE_CREATED','1','3','0');
	
	// >> votes on product
	$('a',$('.encartilikethisproduct')).ilikeit({code:$('input.ilikeit_onproduct').val(),rewrite:true,cookie:true,cookieName:$('#pcode').val()});
	var cookie_name = 'produit'+($('#pcode').val())+'_ilike';
	var getCookies = $.cookie(cookie_name);
	if(getCookies == "ilike"){$('.encartilikethisproductmiddle a span').text('LIKED');}
	$('a',$('.tvadshowtouse')).popin({width:597,height:700,loaderImg : '/img/register/ajax-loader.gif',opacity: .5,callbackpopin:function(){$("#diapo").slideproduct()}});
		$('a',$('.tvadshowtousefirst')).popin({width:491,height:700,loaderImg : '/img/register/ajax-loader.gif',opacity: .5,callbackpopin:function(){$("#diapo").slideproduct()}});
	$('a',$('.howtorecycle')).popin({width:597,height:700,loaderImg : '/img/register/ajax-loader.gif',opacity: .5,callbackpopin:function(){$("#diapo").slideproduct()}});

  //vérifie que le textarea et la capcha sont bien remplis
  $('.btn_retour_01').click(function(e){
  var comment = $("[id$=f1message]");
  var captcha = $("[id$=captcha]");
  if(comment.val() == ""){
  e.preventDefault();
    $('#adcom').addClass('error'); 
  }else{
    $('#adcom').removeClass('error'); 
  }
  if(captcha.val() == ""){
  e.preventDefault();
    $('#caplab').addClass('error'); 
  }else{
    $('#caplab').removeClass('error');
  } 
  });

	
	
	
	// >> left nav
	$('.scroll-pane').jScrollPane({scrollbarWidth : 12, showArrows : true, arrowSize : 10, animateTo : true});  
	setTimeout("$('.navDeGauche').height($('.partieCentrale').height())",1000);
	
	// >> display terms & conditions
	$('.gyo_terms').click(function() {
		 Openhwscroll('terms-commentonproduct.htm','Terms','600','500');
		 return false;
	});
	
	// >> change display date
	change_display_date();
	
	// >> change backgound comment & text "Comments on this product (0) :"
	if(window.location.href.indexOf("product-struct") > 0 || window.location.href.indexOf("product") > 0){
		if($('.background_comment').val() != ""){
			background_comment = $('.background_comment').val();
			$('.fondpageproduct').css("background-color",background_comment);
		}
		if($('.numbertotalmasque').text() == 0){
			$('.cadreblanctitre').remove();
			//$('.cadreblanctitre').text("Be the first to leave a comment");
			$('.addcomment .btn_retour_01_centre_inner').text('Be the first to leave a comment');
			$('.titrepartiecommenter').text("Dodaj swoją opinię na temat tego produktu:");
			var testlongeur_titparcom = $('.titrepartiecommenter').text();
			if (testlongeur_titparcom.length > 0){
				$('.cadreblanctitre').remove();
				$('.cadreblanccommentairescommenter .partieCommenter').css('margin','-12px 0 0');
			}
		}
	}
	/*faux formulaire quand pas loggué*/
	$('.fake').focus(function(){
    var gyourl_product = window.location.href;
	  gyourl_product = gyourl_product.replace("#","");
	  wslogin.popIn.signIn(gyourl_product);
	  /*var pos = $('.partieCommenter').offset();
	  $('#popin').css('top',pos.top+'px'); */
	  setTimeout("window.scrollTo(0,0);",1500);//a remplacer par un call back
  });
  $('.fakeSub').click(function(){
    var gyourl_product = window.location.href;
	  gyourl_product = gyourl_product.replace("#","");
	  wslogin.popIn.signIn(gyourl_product);
	  setTimeout("window.scrollTo(0,0);",1500);//a remplacer par un call back
  });
	/**/
	/**********************************************************************************************************************************************************************/
	/**************************************************************** END CRM 2010 COMMENT PRODUCTS ***********************************************************************/	
	/**********************************************************************************************************************************************************************/
	/*si pas de boutons howto*/
	if(($('.tvadshowtouse').length == 0) && ($('.howtorecycle').length == 0)){
    $('.zone_txt').after('<div style="height:76px"><\/div>')
  }
  $('.pop-top').click(function(){
    window.scrollTo(0,0);
});
});
//gestion du text ilke it
function rewrite(){
  var nb = $('.encartilikethisproductmiddle a p nobr').text();
  if(nb == 1){
     $('.encartilikethisproductmiddle a p').html('<nobr>1<\/nobr> person<br \/> likes this product!');
     $('.encartilikethisproduct').css('height','59px');
  }else if(nb== 0){
      $('.encartilikethisproduct').css({'height':'29px','overflow':'hidden'});    
  }else if(nb > 1){
      $('.encartilikethisproductmiddle a p').html('<nobr>'+nb+'<\/nobr> people<br \/> like this product!');
      $('.encartilikethisproduct').css('height','59px');    
  }
}	

/******************************************************************************************************************************************************************/
/**************************************************************** CRM 2010 COMMENT PRODUCTS ***********************************************************************/
/******************************************************************************************************************************************************************/
// >> go to bottom
function scrolldown() {
 var h=0;
 if (window.innerHeight) h = window.innerHeight;
 else if (document.body && document.body.offsetHeight) h = window.document.body.offsetHeight;
 else if (document.documentElement && document.documentElement.clientHeight) h = document.documentElement.clientHeight;    
 this.scroll(1,h+150);
}  
// >> display Success Post On Product(SPOP)
function displaySPOP(){
	var gyourl    = location.href,
	gyoquery      = gyourl.substring(gyourl.lastIndexOf("=")),
	gyocleanQuery = gyoquery.replace(/=/,"");
	if(gyocleanQuery == "1"){
	  $('.titrepartiecommenter, .formulairepartiecommenter').hide();
	  $('.partieCommenter').html('<span class="commentpostsuccess">Twoja opinia czeka na moderację administratora.<br />Zostanie opublikowana po akceptacji.</span>');
	  $(document).ready(function(){
		setTimeout("scrolldown();",1200);						 
	  });						 
	}
}

// >> display number total of posts
function displayNumberTotal(){
var gyonumbertotalmasque  =  $('.numbertotalmasque').text();
$('.numbertotal').text(gyonumbertotalmasque);
}

// >> change selection
function updatepostforum_forprd(prdcode,tri,p,nbaffichage,successcomm){
	//var heightcommentaire = $(".partieCommentaires").innerHeight();
	//$(".partieCommentaires").css('min-height',heightcommentaire);
	$(".partieCommentaires").empty();
	$(".partieCommentaires").html("<img src='/img/common/ajax-loader-b-tr.gif' alt='loader' />");
	$.ajax({
		url: manag + "/services/update/updatepostforum_forprd.aspx",
		data: "prdcode="+prdcode+"&tri="+tri+"&p="+p+"&nbaffichage="+nbaffichage+"&successcomm="+successcomm,
		cache: false,
		type : "GET",
		error: function (xhr, desc, exceptionobj){
		},
		success: function(content){
			var contenu = $(content).html(); 
			$(".partieCommentaires").html(contenu);
			//$(".partieCommentaires").css('height','auto');
			$(".commentpostsuccess").remove();
			$('.scroll-pane').jScrollPane({scrollbarWidth : 12, showArrows : true, arrowSize : 10, animateTo : true});  
			setTimeout("$('.navDeGauche').height($('.partieCentrale').height())",1000);
			//votes
			gyo_vote();
			display_numerotation(p);
			change_display_date();
		}
	});
}

// >> load numerotation on the first load page
function updatepostforum_forprdnumeratation(prdcode,tri,p,nbaffichage,successcomm){
	$(".pagination").empty();
	//$(".pagination").html("<img src='/img/common/ajax-loader-b-tr.gif' alt='loader' />");
	$.ajax({
		url: manag + "/services/update/updatepostforum_forprdnum.aspx",
		data: "prdcode="+prdcode+"&tri="+tri+"&p="+p+"&nbaffichage="+nbaffichage+"&successcomm="+successcomm,
		cache: false,
		type : "GET",
		error: function (xhr, desc, exceptionobj){
		},
		success: function(content){
			var contenu = $(content).html(); 
			$(".pagination").html(contenu);
			$('.scroll-pane').jScrollPane({scrollbarWidth : 12, showArrows : true, arrowSize : 10, animateTo : true});  
			setTimeout("$('.navDeGauche').height($('.partieCentrale').height())",1000);
		}
	});
}

// >> function for vote
function gyo_vote(){
	$('.blockcommentaires').each(function(){
			  var code = $('input[type=hidden]',$(this)).val();
			  var divresult = $('.blockfoundmiddle .lik',$(this));
			  //alert(divresult);
			  divtotal =  $('.blockfoundmiddle .total',$(this));
			  //alert("divtotal :"+divtotal);
			  $('a.ilike',$(this)).ilikeit({code:code,forumName :"GIVE_YOUR_OPINION_PRODUCTS",divresult :divresult,peCode:"codeIlike",divtotal:divtotal,showtotal:true});
			  $('a.dontlike',$(this)).ilikeit({code:code,peLike:"Idontlike",forumName:"GIVE_YOUR_OPINION_PRODUCTS",divresult:divresult,peCode:"codeIlike",divtotal:divtotal,showtotal:true}); 
	});
	var cookieIlike = {
	  eachpost: function(){
		  $('.ilike').each(function(){
			$(this).click(function(){
			  var cookie_name = 'post'+($(this).parent().find('.fid').val())+'_ilike';
			  $.cookie(cookie_name, "ilike", { path: '/', expires: 365 });
				$(this).parent().find('.thank').animate({float:"left"},500,
				function(){$(this).parent().parent().find('.qEnd').remove();$(this).parent().parent().find('a.ilike').remove();$(this).parent().parent().find('a.dontlike').remove();$(this).parent().find('.thank').html('<span class="ilike"><img src="/img/our_products/pictoyesitisvert.png" alt="" \/><span>Podoba Ci się ten komentarz<\/span><\/span>');});
				});
		  });
		  $('.dontlike').each(function(){
			$(this).click(function(){
			  var cookie_name = 'post'+($(this).parent().find('.fid').val())+'_ilike';
			  $.cookie(cookie_name, "dontlike", { path: '/', expires: 365 });
			  $(this).parent().find('.thank').animate({float:"left"},500,
				function(){$(this).parent().parent().find('.qEnd').remove();$(this).parent().parent().find('a.ilike').remove();$(this).parent().parent().find('a.dontlike').remove();$(this).parent().find('.thank').html('<span class="dontlike"><img src="/img/our_products/pictonoitsnotvert.png" alt="" \/><span>Nie podoba Ci się ten komentarz<\/span><\/span>');});
			});
		  });
		  this.replace();
	  },
	  replace:function(){    
		  $('.like').each(function(){
			var cookie_name = 'post'+($('.fid',$(this)).val())+'_ilike';
			var getCookies = $.cookie(cookie_name);
			if(getCookies == "ilike"){$('a',$(this)).remove();$(this).parent().find('.qEnd').remove();$('.thank',$(this)).html('<span class="ilike"><img src="/img/our_products/pictoyesitisvert.png" alt="" \/><span>Podoba Ci się ten komentarz<\/span><\/span>');}
			if(getCookies == "dontlike"){$('a',$(this)).remove();$(this).parent().find('.qEnd').remove();$('.thank',$(this)).html('<span class="dontlike"><img src="/img/our_products/pictonoitsnotvert.png" alt="" \/><span>Nie podoba Ci się ten komentarz<\/span><\/span>');}
		  });
	  }
	}
	var hideIlikeIfUser={
	   init:function(){    
		 	$.ajax({
    		url: getRootPathFromUrl()+"/ws/Customer.asmx/InitSession",
    		cache: false,
    		type : "GET",
    		error: function (xhr, desc, exceptionobj){
    		},
    		success: function(content){
           var currentUserId = $(content).find("SessionID").text();
      		 $('.blockfoundmiddle').each(function(){
        			var posterId = $('.posterId',$(this)).val();
        			if(currentUserId == posterId){
        			  $('.like',$(this)).remove();
        			  $('.qEnd',$(this)).remove();
        			} 
      			}); 
    		}
    	});	
		 
	  }
	}
	cookieIlike.eachpost();
	hideIlikeIfUser.init();
}

// >> function change display date
function change_display_date(){
	$('span.date').each(function(){
	  var recupDate = $(this).text();
	  var recupDate = recupDate.replace(RegExp("-","g"),"/");
	  var newDate= Date.parse(recupDate);
	  $(this).text(newDate.toString('d MMMM yyyy, HH:mm'));
	});
}
// >> function change num
function display_numerotation(page){
	var num_post = page*3-3;
	$('span.number').each(function(){
		num_post++;
		$(this).text(num_post);
	});
}
// >> mask select if no post
function nopost(){
	if($('.numbertotalmasque').text() == 0) $('.sortby').remove();
}
// >> fonction report content
function send_reportcontent(createur,postid){
	$.ajax({
		url: manag + "/services/update/report-content.aspx",
		data: "postid="+postid+"&forum=GIVE_YOUR_OPINION_PRODUCTS",
		cache: false,
		type : "GET",
		error: function (xhr, desc, exceptionobj){
		},
		success: function(content){
			var sendurl = escape(document.location.href); //mandatory of escape
			var idrecupcontenu = 'input.recupcontenu'+postid;
			var recupcontenu = $(idrecupcontenu).val();
			sendurl = sendurl.replace("&","&amp;");
			var content_mail = {Url:"URL : "+sendurl+", EMAIL-CREATOR : "+createur+", POSTID : "+postid+", CONTENT : "+recupcontenu};
			reportContent.start(content_mail);
			
			return false;
		}
	});	
}
/**********************************************************************************************************************************************************************/
/**************************************************************** END CRM 2010 COMMENT PRODUCTS ***********************************************************************/	
/**********************************************************************************************************************************************************************/

