/**
 * JQuery gallery fade and button rollover background position
 */
$(function(){
	$('#gallery-nav a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(0px -160px)"}, {duration:150})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:150, complete:function(){
				$(this).css({backgroundPosition: "0 0"})
			}})
		})
		
	$('#page-gallery-link-1 a#gallery-link-1')
	.css( {backgroundPosition: "0 -160px"} )
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(0px -160px)"}, {duration:150})
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(0px -160px)"}, {duration:150, complete:function(){
			$(this).css({backgroundPosition: "0px -160px"})
		}})
	})

$('#page-gallery-link-2 a#gallery-link-2')
	.css( {backgroundPosition: "0 -160px"} )
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(0px -160px)"}, {duration:150})
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(0px -160px)"}, {duration:150, complete:function(){
			$(this).css({backgroundPosition: "0px -160px"})
		}})
	})
	
$('#page-gallery-link-3 a#gallery-link-3')
	.css( {backgroundPosition: "0 -160px"} )
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(0px -160px)"}, {duration:150})
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(0px -160px)"}, {duration:150, complete:function(){
			$(this).css({backgroundPosition: "0px -160px"})
		}})
	})
});

function slideSwitch() {
    var $active = $('#gallery-mainpics IMG.active');

    if ( $active.length == 0 ) $active = $('#gallery-mainpics IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#gallery-mainpics IMG:first');
    
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});
