var homegarnierclub_member = (document.location.href.indexOf("/member/signed/")>-1);

Date.prototype.getWeek = function() {
  var onejan = new Date(this.getFullYear(),0,1);
  return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
    
var currentDate = new Date();
var weekno = currentDate.getWeek();
var monthno = currentDate.getMonth()+1;
var yearno = currentDate.getFullYear();     
var dgcode = "EXPRESS_SURVEY_"+monthno+"_"+yearno;
var dgcode = "EXPRESS_SURVEY_4_2010";
                     
$(document).ready(function(){
  $(document).rememberme({
      loginClass : "loginrememberme",
      passwordClass : "passwordrememberme",
      submitClass : "submitrememberme",
      checkboxClass : "checkboxrememberme",
      expiration : 365
  });
  $(document).randomdisplay({
      class_name : "div.randomreco",
      hideAllBeforeDisplay : false
  });
  $(document).randomdisplay({
      class_name: "div.randomcoupons",
      hideAllBeforeDisplay: false,
      cycle: true
  });
  $('#product img').each(function(){
      $(this).css('margin-left','-40px');
  });
  $(document).crunch({
      class_name : "p.crunch_bloc1",
      numChar : 300
  });
  $(document).crunch({
      class_name : "p.crunch_bloc2",
      numChar : 200
  });
  $('.submitrememberme').attr("href","javascript:__doPostBackHome('[id$=submit]','');");
  if(homegarnierclub_member) {
    var id_submit = $("[id$=submitlogout]").attr("id").substring(1);
		id_submit  = id_submit.replace(/_/g,"$").replace(/\$\$/g,"$_");
		id_submit = $("[id$=submitlogout]").attr("id").substring(0,1) + id_submit;
		$("[id$=submitlogout]").attr("href","javascript:__doPostBackHome('"+id_submit+"','');");
  } 
  
  $.ajax({
      url: (homegarnierclub_member?"../../../garnier-club/":"")+"express-survey.aspx",
      data: "diagcode="+dgcode,
      success: function(html){          
          $("#form_survey").html(html);
          $("#intro-survey .title_survey").html($("#form_survey .sondage-minute strong:first").text());
          if($.cookies.get(dgcode) != 'avote'){
              $('.submitAjax').attr("href","#");
              expandSurvey();
              options = {
                  target:        '.list-arrows',   // target element(s) to be updated with server response
                  beforeSubmit:  showLoading,  // pre-submit callback
                  success:       showResponse  // post-submit callback
              };
              // bind to the form's submit event
              $('.submitAjax').click(function() {
                  if ($(".sondage-minute input:checked").length != 0){
                      $('.sondage-minute label, .sondage-minute input').unbind();
                      
                      var id_submit = $(this).attr('id').substring(1);
          						id_submit  = id_submit.replace(/_/g,"$").replace(/\$\$/g,"$_");
          						id_submit = $(this).attr('id').substring(0,1) + id_submit;
                      __doPostBackSurvey(id_submit,'');
                      $.cookies.set(dgcode, 'avote');
                      $("#aspnetForm_ajax").ajaxSubmit(options);
                  } else {}
                  return false;
              });
              
          } else {
              $.ajax({
                  url: (homegarnierclub_member?"../../../garnier-club/":"")+"express-survey-statistic.aspx",
                  data: "diagcode="+dgcode,
                  success: function(html){
                      $("#form_survey").html(html);
                  }
              });
              $("#form_survey").hide();
          }
      }
  });
  
  if($("#left_col").height() > $("#main_col").height()) {
      $("#main_col").height($("#left_col").height()+"px");
  } else {
      $("#left_col").height($("#main_col").height()+"px");
  }
   var currentTips = $('#coaching>h4').text();
  if(currentTips == "BEAUTY TIPS")
  {
  $('#coaching>h4').css({'background':'url(/img/garnier-club/home_titrepush-1bis.png)'});
  $('#coaching>.under').css({'background':'url(/img/garnier-club/itemPushUnder1bis.jpg)'});
  }else{
  $('#coaching>h4').css({'background':'url(/img/garnier-club/home_titrepush-1.png)'});
  $('#coaching>.under').css({'background':'url(/img/garnier-club/itemPushUnder1.jpg)'});  
  };
  
  $(document).crunch({
   class_name : "div.tipTexte",
   numChar : 50
    });
  $('.tipTexte').css({'color':'#464646'});
  $("#coaching>.under>.pop").popin({
   width:540,
   height:260,
   loaderImg : '/img/register/ajax-loader.gif',
   opacity: .5
 	});
  $('.fullLink').hover(function()
    {
      $('.eco').css({'text-decoration':'underline'});
    },function()
    {
      $('.eco').css({'text-decoration':'none'});
    }
  );	         	
});

function showLoading(formData, jqForm, options){
  $("#form_survey").html("Загрузка...");
  return true;
}  


function showResponse(responseText, statusText){
  $.ajax({
  	url: (homegarnierclub_member?"../../../garnier-club/":"")+"express-survey-statistic.aspx",
  	cache:false,
  	data: "diagcode="+dgcode,
  	success: function(html){
  		$("#form_survey").html(html);
  	  expandSurveyResults();
  	}
  });
}	

function ReplaceAll(Source,stringToFind,stringToReplace){
var temp = Source;
var index = temp.indexOf(stringToFind);
while(index != -1){
  temp = temp.replace(stringToFind,stringToReplace);
  index = temp.indexOf(stringToFind);
}
return temp;
}

function stop(){
$('#form_survey').stop();
$('#intro-survey').stop();  
}

function expandSurvey(){
  $('#survey').hover(
    function(){
     var ddW = "";
     $(this).css({'width':'auto'});
      $('#form_survey').show();
     
      if($('label',$('#form_survey')).length > 0){
        $('label',$('#form_survey')).each(function(){
          var textbr = $(this).html();
          textbr = textbr.replace(/<br>/,"").replace(/<br\/>/,"");
          $(this).html(textbr);
        });
        var textbr = $('.sondage-minute p span',$('#form_survey')).html();
        textbr = textbr.replace(/<br>/,"").replace(/<br\/>/,"");
        $('.sondage-minute p span',$('#form_survey')).html(textbr);
      }else{
        $('dt',$('#form_survey')).each(function(){
          var textbr = $(this).html();
          textbr = textbr.replace(/<br>/,"").replace(/<br\/>/,"");
          $(this).html(textbr);
        });
      }


      
    
      ddW = $('#form_survey').width()+'px';
      $('#form_survey').css('visibility','hidden');
      $('.sondage-minute dd').css('width',ddW);
      var formW = $('#form_survey').outerWidth();
      $(this).css({'width':'180px'});
      $('#intro-survey').hide().css('border-right','0px');
      $(this).stop().animate({'width':formW},300,function(){
        $('#form_survey').css('visibility','visible');  
      });

    },
    function(){
      //$(this).css('width','180px');
      $('#form_survey').hide();
      $('#intro-survey').show();
      $(this).stop().animate({'width':'180px'},300, function(){
        $('#intro-survey').css('border-right','1px solid #B3B3B3')
      });
    }
  )
}

function expandSurveyResults(){
     var ddW = "";
     $('#survey').css({'width':'auto'});
     $('#form_survey').show(); 
      $('dt',$('#form_survey')).each(function(){
          var textbr = $(this).html();
          textbr = textbr.replace(/<br>/,"").replace(/<br\/>/,"");
          $(this).html(textbr);
      });
      ddW = $('#form_survey').width()+'px';
      $('#form_survey').css('visibility','hidden');
      $('.sondage-minute dd').css('width',ddW);
      var formW = $('#form_survey').outerWidth();
      $('#survey').css({'width':'180px'});
      $('#intro-survey').hide().css('border-right','0px');
      $('#survey').stop().animate({'width':formW},300,function(){
        $('#form_survey').css('visibility','visible');  
      });
}

