jQuery(document).ready(function($) {

  // Initialize the slideshow on start page
  var slideshow = $('#slides .slides_container');
  if( slideshow.length > 0 && slideshowImages.length > 0 ) {

    for(i=0;i<slideshowImages.length;i++) {
      var image = slideshowImages[i];
      var imageTag = image['image'] + "<span>" + image['title'] + "</span>";

      if ( image['url'] != '' ) {
        var slide = "<a href=\"" + image['url'] + "\">" + imageTag + "<div class=\"search\"></div></a>";
      } else {
        var slide = imageTag;
      }

      if (i == 0) {
        slideshow.append('<div class="active">' + slide + '</div>');
      } else {
        slideshow.append('<div>' + slide + '</div>');
      }
    }

    function resize() {
      $(window).resize( function() {
        var el = $('#slides div.active');
        var newHeight = el.find('img').first().height();

        if( newHeight > 1 ) {
          $('.slides_container').height(newHeight);
        }
      });
    }

    $("#slides").slides({
      effect: 'fade',
      generatePagination:false,
      autoHeight: true,
      play:3000,
      pause:2500,
      animationComplete: resize,
      hoverPause: true,
      preload: true,
      preloadImage: '/wp-content/themes/cbpictures/images/ajax-loader.gif',
      slidesLoaded: function () { $(".slides_control").height($("img:first").height()); }
    });

  }

  // Activate SlideJS on Content Footer (start page)
  $("#content_footer").slides({
    prependPagination: true
  });

  // Activate SlideJS on Portfolio Section
  $('#slides_portfolio').slides({
    effect: 'fade',
  	preload: true,
  	preloadImage: '/wp-content/themes/cbpictures/images/ajax-loader.gif',
  	play: 3000,
  	pause: 1500,
  	slideSpeed: 600,
  	hoverPause: true,
    autoHeight: true,
    prependPagination: true,
    slidesLoaded: function () { $(".slides_control").height($(".slide:first").height()); }
  });

  var slidesController = $('.controller');
  slidesController.hide();
  $('.slides_container,.controller').hover( function() {
    slidesController.show();
  }, function() {
    slidesController.hide();
  });

  // Help IE with placeholder in input elements
  $('input[type="search"]').each(function() {
    if( $(this).attr('placeholder') ) {
      var v = $(this).attr('placeholder');
      if( $(this).val() == '' ) { $(this).attr('value', v); }

      $(this).focus(function() {
        if( $(this).val() == v ) {
          $(this).attr('value', '');
        }
      });
      $(this).blur(function() {
        if( $(this).val() == '' ) {
          $(this).attr('value', v);
        }
      });
    }
  })

});
