$(function(){
	setupMenu();
  setupSearch();
	findTabs();
	findFoldOut(80);
	findBigViewer();
	findListViewer();
  //findCoverflow();
	findToggleBlocks();
	findImageViewer();
	findUserImages();
  findModal();
  showtip();
  datepopup();
});
// 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 ($('.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() {
  var bigSlider = $('#bigviewer').bxSlider({
    auto: true,
    pause: 4000,
    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: 3,
      scaleFactorPortrait: 1.65,
      reflectionHeight: 0.35,
      maxItemHeight: 120,
      reflectionGap: 0.02
    });
  }
}

// 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 doResizeModal(height) {
    $('.nyroModalCont').css('height', height + 'px');
    $('.nyroModalIframe').css('height', height + 'px');
    $('.nyroModalCont iframe').css('height', height + 'px');
    $.nmTop().resize(true);
}
