$( document ).ready( function() {
	animateMenu();
	highlightMenu();
	$( "a#join_newsletter" ).fancybox();
	$( "a.fancybox").fancybox( { 'titlePosition' : 'over' } );
	switchBanners();
	animateFooterLogos();	
} )

function switchBanners()
{
   var active = $('#banners img.active');
 
    if ( active.length == 0 ) active = $('#banners img:last');
 
    // use this to pull the images in the order they appear in the markup
    var next =  active.next().length ? active.next() : $('#banners img:first');
    
 	active.animate({opacity: 0.0}, 1000 );
    next.addClass('active').animate({opacity: 1.0}, 1000, function() {
		active.removeClass('active');
     });
    setTimeout( "switchBanners()", 4000 );
}

function preloadBanners( arrayOfImages )
{
	$( arrayOfImages).each(function(){
		var img = $( '<img />' ).attr( 'src', this );
       	$('#banners').append(img);
       	$('#banner').css( 'background', 'transparent' );
    });

}

function animateFooterLogos()
{
	$( '#footer .logos li a span' ).css( 'opacity', '0' );
	$("#footer .logos li a").hover(function(){
		$(this).find("span").stop( false, true ).animate({opacity: 1.0}, 600);
		}, function(){
		$(this).find("span").stop( false, true ).animate({opacity: 0.0}, 600);

	});
	
}

function animateMenu()
{
	$('#nav > li').hover(
	   function() {
		  $(this).addClass("active");
		  $(this).find('ul').stop(false, true).slideDown('fast');
	   },
	   function() {
		  $(this).removeClass("active");
		  $(this).find('ul').stop(false, true).slideUp('fast');
	   }
	);
}

function highlightMenu()
{
	var d = window.location.protocol + '//' + window.location.host;
	var uriparts = window.location.pathname;
	var nav = jQuery( '#nav' );
	jQuery( 'a', nav ).each( function() {
		var h = jQuery( this ).attr( 'href' );
		var h = h.replace( d, "" );
		if( uriparts.match( h ) )
		{
			jQuery( this ).parent().addClass( 'current' );
			return true;
		}
	} );
}

function bindPhotoAjaxLinks() {
	$( '.photos_ajax' ).each( function() {
		$( this ).click( function() {
			getPhotoAjaxContent( $( this ).attr( 'rel' ) );
			return false;
		} );
	} );
}

function getPhotoAjaxContent( url ) {
	$.ajax( {
	   type: "GET",
	   url: url,
	   success: function( data ) {
	     $( '#content_for_photo_gallery' ).html( data );
	   }
	 } );
}

$(function() {
	$( ".datepicker" ).datepicker({ dateFormat: 'DD, d MM, yy' });
});

