jQuery(function($){
  // Read More
  $('.textMore').hide();
  $('.fr .textMore').after('<p class="readmore"><a href="#">Suite &raquo;</a></p>');
  $('.en .textMore').after('<p class="readmore"><a href="#">Read More &raquo;</a></p>');

  $('div.text > p.readmore a').click(function(){
    $textMore = $(this).parents('.text').find('.textMore');

    if($textMore.length){
      if($('.ie').length){
        $textMore.show();
      } else {
        $textMore.css({opacity: '0'}).slideDown(250).fadeTo(500, 1);
      }

      $(this).parent().hide();
      return false;
    }
  });

  //Ie Fix .first .last
  if($('.ie').length){
    $('ul.customlist li:first-child, dl.customlist dd:first-child, .text p:first-child').addClass('first');
    $('ul.customlist li:last-child, dl.customlist dd:last-child, .text p:last-child').addClass('last');
  }

  //Clickable Anything
  $('.locationList li').css('cursor','pointer');
  $('.locationList li').click(function(){
    var $target = $(this).find('a').attr('target');
    var $location = $(this).find('a.ribbonBtn').attr('href');

    if ($target == '_blank') {
      window.open($location);
      return false;
    } else {
      location.href = $location;
    }
  });

  /* ************************ 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 Tabs Click
  $('.customTabs .title a').click(function(){
    if(!$(this).parents('.customTabs li').hasClass('customTabsLink')){
      $(this).blur();
      return false;
    }
  });

  //Custom Tabs Hover
  $('.customTabs .title a').hover(function(){
    var $parentItem = $(this).parents('.customTabs .item');
    var $trigger = $(this);

    customtabOpen = setTimeout(function(){
      if($parentItem.find('.customTabsContent').is(':hidden')){
        var $currentItem = $trigger.parents('.customTabs').find('.customTabsContent:visible');
        var $currentParentItem = $currentItem.parent();

        $parentItem.find('div.title').addClass('on');
        $trigger.parents('.customTabs').find('.item div.title span.arrow').addClass('up');
        $trigger.find('span.arrow').removeClass('up');

        $parentItem.find('.customTabsContent').show().fadeTo(250, 1);

        if($currentItem.length){
          $currentParentItem.find('div.title').removeClass('on');
          $currentItem.fadeTo(250, 0, function(){
            $(this).hide();
          });
        }
      }
    }, 250);
  }, function(){
    clearTimeout(customtabOpen);
  });

  //Custom Tabs Close
  $('.customTabs .closeBtn').click(function(){
    $(this).parents('.customTabs').find('.item .customTabsContent').fadeTo(250, 0, function(){
      $(this).hide();
      $(this).parents('.customTabs').find('.item div.title').removeClass('on');
      $(this).parents('.customTabs').find('.item div.title span.arrow').addClass('up');
    });

    $(this).blur();

    return false;
  });

  //Featured Pre-owned Slides
  usedFeatSlide = function(){
    var slidesQty = $('#homeUsed .usedFeatVeh').length;
    var slidesPos = 0;
    var singleSlideWidth = $('#homeUsed .usedFeatVeh').outerWidth(true);
    var SLIDES_PER_SCREEN = 6;
    var slidesLeft = slidesQty % SLIDES_PER_SCREEN;
    var SLIDES_MAX_MOVES = slidesLeft == 0 ? parseInt(slidesQty / SLIDES_PER_SCREEN)-1 : parseInt(slidesQty / SLIDES_PER_SCREEN);
    var SLIDES_MOVES_WIDTH = 898;
    var tempSlidesMovesWidth = SLIDES_MOVES_WIDTH;
    var wrapperWidth = $('.usedFeatContent').width();
    var slidesLeftMoveWidth = (singleSlideWidth*slidesLeft) - (wrapperWidth - SLIDES_MOVES_WIDTH);

    if(slidesQty > SLIDES_PER_SCREEN){
      $('#usedFeatNext').switchClass('off', 'active');
      $('#usedFeatNext').removeClass('end');
    }

    $('#usedFeatNext').click(function(){
      if(slidesPos != SLIDES_MAX_MOVES){
        if(slidesPos == SLIDES_MAX_MOVES-1){
          if(slidesLeft > 0){
            tempSlidesMovesWidth = slidesLeftMoveWidth;
          }
          else{
            tempSlidesMovesWidth = (singleSlideWidth*SLIDES_PER_SCREEN) - (wrapperWidth - SLIDES_MOVES_WIDTH);
          }
        }

        $('.usedFeatList').animate({
          left: '-=' + tempSlidesMovesWidth
        }, 1000);

        $('#usedFeatPrev').removeClass('end', 500, function(){
          $('#usedFeatPrev').removeClass('off', 500);
        });

        if(slidesPos == SLIDES_MAX_MOVES){
          $('.usedFeatContent').addClass('usedFeatContentEnd');
        }

        slidesPos++;
        tempSlidesMovesWidth = SLIDES_MOVES_WIDTH;

        if(slidesPos == SLIDES_MAX_MOVES){
          $('#usedFeatNext').addClass('off', 500, function(){
            $('#usedFeatNext').addClass('end', 500);
          });
        }
      }

      $(this).blur();

      return false;
    });

    $('#usedFeatPrev').click(function(){
      if(slidesPos != 0){
        $('.usedFeatContent').removeClass('usedFeatContentEnd');

        if(slidesPos == SLIDES_MAX_MOVES){
          if(slidesLeft > 0){
            tempSlidesMovesWidth = slidesLeftMoveWidth;
          }
          else{
            tempSlidesMovesWidth = (singleSlideWidth*SLIDES_PER_SCREEN) - (wrapperWidth - SLIDES_MOVES_WIDTH);
          }
        }

        $('.usedFeatList').animate({
          left: '+=' + tempSlidesMovesWidth
        }, 1000);

        $('#usedFeatNext').removeClass('end', 500, function(){
          $('#usedFeatNext').removeClass('off', 500);
        });

        slidesPos--;
        tempSlidesMovesWidth = SLIDES_MOVES_WIDTH;

        if(slidesPos == 0){
          $('#usedFeatPrev').addClass('off', 500, function(){
            $('#usedFeatPrev').addClass('end', 500);
          });
        }
      }

      $(this).blur();

      return false;
    });
  }

  /* ************************ Custom form validation ************************ */
  customForm = function(){
    var qmformSubmit = true;
    var couponformSubmit = true;

    var qmNameDefault = "Prénom et nom";
    var qmEmailDefault = "courriel@exemple.com";
    var qsSearchDefault = "Faire une recherche dans le site";

    if($('.en').length){
      qmNameDefault = "First and Last Name";
      qmEmailDefault = "email@example.com";
      qsSearchDefault = "Search our Site";
    }

    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);
    }

    /* #quickmailing validation */
    qmValidation = function(elem){
      var $formElem = elem;

      if(!qmformSubmit) return false;
      qmformSubmit = false;

      if($('#qmname').val() == qmNameDefault || !regexAlpha.test($('#qmname').val())){
        $('#qmname').parent().find('.quickformError').fadeIn(250);
      }
      else{
        $('#qmname').parent().find('.quickformError').hide();
      }

      if($('#qmemail').val() == qmEmailDefault || (!(!regexEmailNo.test($('#qmemail').val()) && regexEmailYes.test($('#qmemail').val())))){
        $('#qmemail').parent().find('.quickformError').fadeIn(250);
      }
      else{
        $('#qmemail').parent().find('.quickformError').hide();
      }

      if($formElem.find('.quickformError:visible').length){
        qmformSubmit = true;
        return false;
      }
      else{
        return true;
      }
    }

    /* #quickcoupon 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($('#quickmailing').length){
      $('#quickmailing form').submit(function(){
        return qmValidation($(this));
      });

      $('#quickmailing form .frmbtn a').click(function(){
        $(this).blur();

        $('#quickmailing form').submit();

        return false;
      });

      $('#quickmailing form input.textbox').click(function(){
        if($(this).attr('id') == 'qmname' && $('#qmname').val() == qmNameDefault){
          customFormClear($(this));
        }
        else if($(this).attr('id') == 'qmemail' && $('#qmemail').val() == qmEmailDefault){
          customFormClear($(this));
        }
      });

      $('#quickmailing form input.textbox').blur(function(){
        if($(this).attr('id') == 'qmname'){
          if(!regexNotEmpty.test($('#qmname').val()) || regexOnlySpace.test($('#qmname').val())){
            customFormReset($(this), qmNameDefault);
          }
        }
        else if($(this).attr('id') == 'qmemail'){
          if(!regexNotEmpty.test($('#qmemail').val()) || regexOnlySpace.test($('#qmemail').val())){
            customFormReset($(this), qmEmailDefault);
          }
        }
      });
    }

    if($('.quickformSearch').length){
      $('#qshSearch, #qsfSearch').click(function(){
        if($(this).val() == qsSearchDefault){
          customFormClear($(this));
        }
      });

      $('#qshSearch, #qsfSearch').blur(function(){
        if(!regexNotEmpty.test($(this).val()) || regexOnlySpace.test($(this).val())){
          customFormReset($(this), qsSearchDefault);
        }
      });
    }

    if($('#quickcontact').length){
      $('#quickcontact form .frmbtn a').click(function(){
        $(this).blur();

        $('#quickcontact form').submit();

        return false;
      });
    }

    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 ************************ */
  couponPopup = function(){
    $('body').prepend('<div id="dlr_couponBg"></div>');

    if($('.nocouponpopbody').length){
      doNotSetListener = 1;
    }

    var couponMain = new dlr_buildPopup('couponMain', 'noCoupon', 1, 10);

    $('#couponMain .closebtn a').click(function(){
      couponMain.setCookie(0, 0);

      $(this).blur();

      return false;
    });

    $('.couponRepop a').click(function(){
      couponMain.showBox();

      $(this).blur();

      return false;
    });
  }

  //Featured Pre-owned Tabs
  usedFeatTabs = function(){
    $('.usedFeatTabs').show();
  }

  if($('#homeUsed').length){
    usedFeatSlide();      //Activates the featured pre-owned slides
    usedFeatTabs();       //Activates the featured pre-owned tabs
  }

  if($('#couponMain').length){
    couponPopup();        //Activates the coupon popup
  }

  if($('.quickform').length){
    switchSubmitBtns();     //Submit btns - use js version
    customForm();         //Custom form validation
  }
});

