// Cufon JS custom font replacement
Cufon.replace('h2');
Cufon.replace('h3');

// random array sort from http://javascript.about.com/library/blsort2.htm
$.fn.reorder = function() {
  function randOrd() { return(Math.round(Math.random())-0.5); }
  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;
    if (childCount > 1) {
      $children.remove();
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });
    }
  }));
}


$(document).ready(function() {

  // Home page: Main header carousel
  $('#home-intro-carousel').jcarousel({scroll: 1, auto: 5, wrap: 'last', itemLoadCallback: initCarousel });

	function initCarousel(carousel, state) {

		 $('.carousel-item').show();
	}
  // Home page: Our clients slideshow
  $('body#home ul#clients').reorder().find("li:gt(7)").remove(); // Randomise and limit to 8.
  $('body#home ul#clients').after('<ol id="pager">').cycle({
      speed:  2000,
      timeout: 3000,
      pager:  '#pager',
      pagerAnchorBuilder: function(idx, slide) {
        return '<li><a href="' + $(slide).find('a')[0].href + '"><img src="' + $(slide).find('img')[0].src + '" width="70" height="39" /></a></li>';
      }
  });

});

