$(function(){
	setupMenu();
  setupSearch();
	findTabs();
	findBigViewer();
  findFoldOut(118);
	findListViewer();
  findCoverflow();
	findToggleBlocks();
	findImageViewer();
	findUserImages();
  findModal();
  showtip();
  datepopup();
  findFinance();
});
// search
function setupSearch() {
  var defaultVal = "zoek";
  if ($("#search input:text;").attr("value") == defaultVal) {
    $("#search input:text;").removeClass('focus');
  } else {
     $("#search input:text;").addClass('focus');
  }
  $("#search input:text;").focus(function() {
    if ($("#search input:text;").attr("value") == defaultVal) {
      $(this).attr("value","");
      $(this).addClass('focus');
    }
  });
  $("#search input:text;").blur(function() {
    if ($("#search input:text;").attr("value") == "") {
      $(this).attr("value",defaultVal);
      $(this).removeClass('focus');
    }
  });
}

// dropdown menu
var dir;
function setupMenu() {
  $("#menu").superfish({
    hoverClass:    'hover',
    speed:         'fast',
    autoArrows:    false,
    dropShadows:   false
  });
}

// tabs
function findTabs() {
   $(".carsearch").tabs();
   $(".search").tabs();
}
// foldout list (dealers)
function findFoldOut(fh) {
  if ( $.browser.msie && $('#bigviewer').length!=0 ) {
    if (parseInt($.browser.version, 10) < 8) {
      $('.foldoutholder').parent().clone().appendTo('body > form');
      $('#wrapper .foldoutholder').parent().remove();
    }
  }
  if ($('.foldoutcontent li').length > 2) {
    $(".foldout .pull").click(function() {
      $(this).toggleClass("on");
      var th = parseInt($(".foldoutcontent").height());
      var hh = parseInt($(".foldout").height());
      if (hh == fh) {	$(".foldout").animate({"height": "+="+(th-fh)+"px"}, "slow",function() { $(this).stop(); }); }
      else { $(".foldout").animate({"height": "-="+(th-fh)+"px"}, "slow",function() { $(this).stop(); }); }
    });
  } else {
    $(".foldout .pull").hide();
  }
}
// big image viewer on home page
function findBigViewer() {
  if($('#bigviewer').length!=0) $('body').addClass('hasBigViewer');
  var bigSlider = $('#bigviewer').bxSlider({
    auto: true,
    pause: 12000,
    mode: 'fade',
    autoHover: true,
    wrapperClass: 'big-wrapper',
    controls: false,
    displaySlideQty: 1
  });
  $('#bigviewernav .prev').click(function(){
    bigSlider.goToPreviousSlide();
    return false;
  });

  $('#bigviewernav .next').click(function(){
    bigSlider.goToNextSlide();
    return false;
  });
}

// list of occasions viewer on home page
function findListViewer() {
  var slider = $("#filmstrip .listviewer").bxSlider({
    auto: false,
    pause: 4000,
    autoHover: true,
    controls: false,
    displaySlideQty: 4
  });
  $('#filmstripnav .prev').click(function(){
    slider.goToPreviousSlide();
    return false;
  });

  $('#filmstripnav .next').click(function(){
    slider.goToNextSlide();
    return false;
  });
}

// list of occasions viewer on home page
function findCoverflow() {
  if ($('#coverflow').length !=0) {
    var coverflow = new ContentFlow('coverflow',{
      visibleItems: 2,
      maxItemHeight: 85,
      scaleFactorLandscape: 1.7
    });
  }
}

// toggle blocks on search page
function findToggleBlocks() {
  var ani = true;
  if ( $.browser.msie ) {
    if (parseInt($.browser.version, 10) < 9) {
      ani = false;
    }
  }
	$(".collapsible").coolfieldset({collapsed:true, animation: ani});
}
// tooltip
function showtip() {
  $(".tooltip[title]").tooltip({
  offset: [10, 2],
  effect: 'slide',
  position: 'center right',
  tipClass: 'tooltipBox'
  }).dynamic({ 
    right: { direction: 'left', bounce: true }
  });
}

// detail page image viewer
function findImageViewer() {
  var fotoviewer = $('#photoviewer').bxSlider({
    wrapperClass: 'photo-wrapper',
    controls: false
  });
  
  $('.thumbs a').click(function(){
   var thumbIndex = $('.thumbs a').index(this);
    // call the "goToSlide" public function
    fotoviewer.goToSlide(thumbIndex);
 
    // remove all active classes
    $('.thumbs a').removeClass('pager-active');
    // assisgn "pager-active" to clicked thumb
    $(this).addClass('pager-active');
    // very important! you must kill the links default behavior
    return false;
  });

  // assign "pager-active" class to the first thumb
  $('.thumbs a:first').addClass('pager-active');
}
// align all user added images inside free text blocks to the right
function findUserImages() {
	$(".wysiwyg img").attr("align","right");
  /*
  if ( $.browser.msie ) {
    if (parseInt($.browser.version, 10) < 9) {
      $('legend').html('<span>' + $('legend').html() + '</span>');
    }
  }
  */
}

function findModal() {
  $('.nyroModal').nyroModal();
}

function datepopup() {
  $("input.date").datepicker();
}

function findFinance() {
  $('.financelist').prepend('<div class="close"></div>');
  $('.financelist .close').click(function() {
    $(this).parent().hide();
    $('a.priceMonth img').removeClass('up');
  });
  $('.financelist').click(function(e) {
    e.stopPropagation();
  });
  $('a.priceMonth').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    $('a.priceMonth img').not($('img',$(this))).removeClass('up');
    $('.financelist').not($('.financelist', $(this).parent())).hide();
    $('.financelist .pointer').css('left',$(this).position().left + $(this).width()/2 - 10 + 'px');
    $('.financelist', $(this).parent()).css('top', $(this).position().top + $(this).height() + 10 + 'px');
    $('.financelist', $(this).parent()).toggle();
    $('img',$(this)).toggleClass('up');
  });
  $('body').click(function() {
    $('.financelist:visible').hide();
    $('a.priceMonth img').removeClass('up');
  });
}

function doResizeModal(height) {
  $('.nyroModalCont').css('height', height + 'px');
  $('.nyroModalIframe').css('height', height + 'px');
  $('.nyroModalCont iframe').css('height', height + 'px');
  $.nmTop().resize(true);
}
