
jQuery.preloadImages = function() {
  for (var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function() {

  // external links
  $('a[rel=external]').attr("target", "_blank");

  // more pages - hide all but first
  $('div.text:gt(0)').hide();

  // show nav
  if ($('div.text').size() > 1) {
    $('div.textnav').show();
    $('#forward').click( function() { page(1); } );
    $('#back').click( function() { page(-1); } );
  } else {
    $('div.textnav').hide();
  }

  // preload images
  //$('div.picnav img').each( function(){
  //  var src = $(this).attr('src');
  //  var hrc = src.replace(/_pre/, "");
  //  $.preloadImages(hrc);
  //  $(this).click(function(){
  //    $('#largepic').attr('src', hrc);
  //  })
  //   .css('cursor','pointer');
  //});

  // logos
  $('#logos img').click(function(){
    var s = $(this).attr('src');
    s = s.replace(/.+\//, '');
    s = s.replace(/_pre\.png/, '');
    if (s)
      {
        $('div.awards').hide();
        $('#' + s).show();
      }
  })
    .css('cursor','pointer');;

});  


function page (p) {

  if (p < 0) p = $('div.text').size() - 1;
  if (p > $('div.text').size() - 1) p = 0;

  //alert(p);
  $('div.text').each( function(i) { 
    if (i == p ) { $(this).show(); } else { $(this).hide();  }
  });

  $('#forward').unbind('click').click( function() { page(p+1); } );
  $('#back').unbind('click').click( function() { page(p-1); } );

}


$(window).load(function() {
	$('#slider').nivoSlider({
		effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
		slices:15,
		animSpeed:500,
		pauseTime:5000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:false, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:true, //1,2,3...
		controlNavThumbs:true, //Use thumbnails for Control Nav
                controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: 'full', //Replace this with...
		controlNavThumbsReplace: 'thumb', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:true, //Force manual transitions
		captionOpacity:0.8, //Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});

	var total = $('#autoslider img').length;
	var rand = Math.floor(Math.random()*total);
        $('#autoslider').nivoSlider({
		effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
		slices:15,
		animSpeed:1000,
		pauseTime:5000,
		startSlide:rand, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:false, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.8, //Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});
});

