/***<script>***/

  
  $(document).ready(function(){

    ///image fade in with random durations
    
    $('#opening').find('img').each(function(){
      var rno=Math.random()*2000;
      $(this).hide();
      $(this).fadeIn(rno);
    });
    ///herobar animation in
    var siloimg = $('p.silo img');
    var silotxt = $('p.silo_text');
    var bar = $('.herobar');
    var barexp = $('.gallery_intro').height();
    var barin = $('#opening').width();
   
    silotxt.each(function(){
      var silosrc = $(this).children('a').children('img:first').attr('src');
    
      silosrc = silosrc.substring(0,silosrc.lastIndexOf('.'));
      $(this).children('a').children('img:first').before('<img class="txt_shad" src="'+silosrc+'_shad.png"/>');
    });
    silotxt.children('a').children('img').css('top', -200);
    silotxt.find('.txt_shad').css('top', -200);
    bar.children('p').hide();
    
    siloimg.css('top', -200);
    bar.css('width', 0)
      .delay(1500)
      .animate({'width': barin-2}, 500, function(){
	
	//fade in herobar text
	bar.children('p').
	fadeIn(600, function(){
	  bar.delay(3000)
	    .animate({
	      height:barexp-4, 
	      top:2}, 1000, function(){
		///once paused for delay, expand herobar, hide images, fade out hero bar expand, then fade in gallery intro
				   $('#opening').hide();
		$('.gallery_intro').show();
		  //.fadeIn(200);
		    //bring in gallery buttons
		    
		$(this).fadeOut(800, function(){
		  siloimg.animate({'top' : 0}, 1000); 
		  silotxt.children('a').children('img').not('.txt_shad').animate({'top' : 0}, { queue: false, duration: 2200 });
		  silotxt.find('.txt_shad').animate({'top' : 0},{ queue: false, duration: 2000, complete: function(){$(this).hide()}});
		});
		    
	      
				    
				     });
		
	  //fade out text right before bar expansion
	  $(this).delay(3000).fadeOut('fast');
	});
	///end animation queue
	///Hide the contents of the gallery prior to loading the first image
	$('.galleriffic').children().hide();
      });
    ///rollover and click functions for the Homepage Gallery selector
    $('.gall_cont p').children('a').children('img').hover(function(){
      //turn on or off the rolled over item. The hover image is placed on top of the live image
      $(this).closest('div').find('img').not('.txt_shad').toggle();
      
    }, function(){
      //change back to normal on rollout
      $(this).closest('div').find('img').not('.txt_shad').toggle();
    })
      .end()
      ///Start the click events for the initial gallery selectors
      .click(function(event){
	event.preventDefault();//kill the normal a tag function
	var src = $(this).attr('href').slice(0, $(this).attr('href').indexOf('-'));//find out which gallery we are targeting 
	var first = $('.navigation[rel="'+src+'"] .thumbs li:first a').attr('title');//Find the first image in the thumbnail list
	$.galleriffic.gotoImage(first);//load the first image using the Galleriffic gotoImage function
	
	$('.galleriffic #gal-gaff-gallery, .gal-gaff-switch, .galleriffic>.navigation[rel="'+src+'"]').show();//show the gallery viewer and the targeted navigation
	$('.gal-gaff-switch div[rel="'+src+'"]').children().toggle();//turn on the switch item
	$('.gallery_intro').hide();//hide the gallery intro screen
	
      });   
    ///Gallery switching functions
    $('.gal-gaff-switch img').each(function(){
      $(this)
	.hover(function(){
	  $(this).siblings().andSelf().toggle();
	}, function(){
	  $(this).siblings().andSelf().toggle();
	})
	.click(function(e){
	  //find out what gallery is clicked on, then load the first image and toggle navigations
	  if($(this).parent().attr('rel')== $('.galleriffic>.navigation:hidden').attr('rel')){
	    //Find the first image in the thumbnail list
	    var first = $('.galleriffic>.navigation:hidden')
	    .find('.thumbs li:first a')
	    .attr('title');
	  $.galleriffic.gotoImage(first);
	  $('.galleriffic').children('.navigation').toggle();
	  $(this).closest('.gal-gaff-switch').find('img').toggle();
	  e.preventDefault();
	}
      });
      
    });
			     });
