/* CYBER-SALE IMG POPUP */
$(document).ready(function(){
  $('#cybersale .img').hover(function(){
    $thisElem = $(this);
      
    cyberOpen = setTimeout(function(){
      $thisItem = $thisElem.parents('#cybersale .item');
      $thisImgWrap = $thisElem.parent().find('.bigimg div');
      
      $('#cybersale .item').not($thisItem).css({'z-index': '0'});
      
      $thisItem.css({'z-index': '5'});
      $thisImgWrap.fadeTo(100, 1);
    }, 150);
  }, function(){
    clearTimeout(cyberOpen);
      
    $thisElem = $(this);
    $thisItem = $thisElem.parents('#cybersale .item');
    $thisImgWrap = $thisElem.parent().find('.bigimg div');
      
    $thisImgWrap.fadeTo(100, 0, function(){
      $thisImgWrap.hide();
      $thisItem.css({'z-index': '0'});
    });
  });
});

jQuery(function($){
  /* **********************
  FUNCTION DECLARATIONS
  ********************** */
  
  /* ************************ Submit btns - use js version (replace the default input by a "cooler" button) ************************ */
  switchSubmitBtns = function(){
    $('.quickform form .frmbtn input').remove();
    $('.quickform form .frmbtn div').show();
  }
  
  /* ************************ Custom form validation ************************ */
  customForm = function(){
    var couponformSubmit = true;
    var quoteformSubmit = true;
    
    var nameDefault = "Prénom et nom";
    var phoneDefault = "Téléphone";
    var emailDefault = "courriel@exemple.com";
    var email2Default = "Confirmez votre courriel";
    
    if($('.en').length){
      nameDefault = "First name and last name";
      phoneDefault = "Telephone";
      emailDefault = "email@exemple.com";
      email2Default = "Confirm your e-mail";

    }

    if(window.RegExp){
      var regexAlpha = /^[A-Za-zÀàÂâÉéÈèÊêËëÎîÏïÔôÖöÙùÛûÇç \-\'\.]+$/;
      var regexEmailNo = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
      var regexEmailYes = /^.+\@(\[?)[A-Za-z0-9\-\.]+\.([A-Za-z]{2,4}|[0-9]{1,3})(\]?)$/;
      
      var regexNotEmpty = /^.+$/;
      var regexOnlySpace = /^ +$/;
    }
    
    customFormClear = function($elem){
      $elem.val('');
    }
    
    customFormReset = function($elem, value){
      $elem.val(value);
    }
    
    /* #coupon popup validation */
    couponValidation = function(elem){
      var $formElem = elem;
    
      if(!couponformSubmit) return false;
      couponformSubmit = false;
      
      if(!regexNotEmpty.test($('#couponName').val()) || regexOnlySpace.test($('#couponName').val())){
        $('#couponName').parent().find('.quickformError').fadeIn(250);
      }
      else{
        $('#couponName').parent().find('.quickformError').hide();
      }
      
      if(!regexNotEmpty.test($('#couponPhone').val()) || regexOnlySpace.test($('#couponPhone').val())){
        $('#couponPhone').parent().find('.quickformError').fadeIn(250);
      }
      else{
        $('#couponPhone').parent().find('.quickformError').hide();
      }
      
      if(!(!regexEmailNo.test($('#couponEmail').val()) && regexEmailYes.test($('#couponEmail').val()))){
        $('#couponEmail').parent().find('.quickformError').fadeIn(250);
      }
      else{
        $('#couponEmail').parent().find('.quickformError').hide();
        
        if($('#couponEmail').val() != $('#couponEmail2').val()){
          $('#couponEmail2').parent().find('.quickformError').fadeIn(250);
        }
        else{
          $('#couponEmail2').parent().find('.quickformError').hide();
        }
      }
      
      if($formElem.find('.quickformError:visible').length){
        couponformSubmit = true;
        return false;
      }
      else{
        return true;
      }
    }
    
    if($('#quickcoupon').length){
      $('#quickcoupon form').submit(function(){
        return couponValidation($(this));
      });
    
      $('#quickcoupon form .frmbtn a').click(function(){
        $(this).blur();
      
        $('#quickcoupon form').submit();
      
        return false;
      });
    }
  }
  
  /* ************************ Coupon Popup New ************************ */
  couponPopupNew = function(){
    $('body').prepend('<div id="dlr_couponBg"></div>');
    
    if($('.noCouponPopupNew').length){
      doNotSetListener = 1;
    }
  
    var couponMain = new dlr_buildPopup('couponMain', 'noCouponPopupNew', 1, 0);
    
    $('#couponMain .closebtn a').click(function(){
      couponMain.setCookie(0, 0);
      
      $(this).blur();
      
      return false;
    });
    
    $('.couponRepop a').click(function(){
      couponMain.showBox();
      
      $(this).blur();
      
      return false;
    });
  }
  
  /* ************************ Coupon Popup Used ************************ */
  couponPopupUsed = function(){
    $('body').prepend('<div id="dlr_couponBg"></div>');
    
    if($('.noCouponPopupUsed').length){
      doNotSetListener = 1;
    }
  
    var couponMain = new dlr_buildPopup('couponMain', 'noCouponPopupUsed', 1, 0);
    
    $('#couponMain .closebtn a').click(function(){
      couponMain.setCookie(0, 0);
      
      $(this).blur();
      
      return false;
    });
    
    $('.couponRepop a').click(function(){
      couponMain.showBox();
      
      $(this).blur();
      
      return false;
    });
  }
  
  /* **********************
  FUNCTION CALLS
  ********************** */
  
  $('body').addClass('js');
  
  switchSubmitBtns();     //Submit btns - use js version
  
  if($('.quickform').length){
    customForm();         //Custom form validation
  }
  if($('#couponMain.couponNew').length){
    couponPopupNew();        //Activates the coupon popup for newcars
  } 
  if($('#couponMain.couponUsed').length){
    couponPopupUsed();        //Activates the coupon popup for usedcars
  }  
});
