/* background image rotator */
function rotator() {
        $('#backgroundImages div').css('opacity', 0)
                                  .eq(0).css('opacity', 1).addClass('active');
        
        setInterval('rotateIt()', 8000);
}

function rotateIt() {
    //Get the first image
    var currentImg = $('#backgroundImages div.active') ? $('#backgroundImages div.active') : $('#backgroundImages div:first');

    //Get next image, when it reaches the end, rotate it back to the first image
    var nextImg = (currentImg.next().length) ? (currentImg.next().hasClass('active')) ? $('#backgroundImages div:first') : currentImg.next() : $('#backgroundImages div:first');	
	
    //Set the fade in effect for the next image, the active class has higher z-index
    nextImg.css('opacity', 0)
	   .addClass('active')
           .animate({opacity: 1}, 1500);

    //Hide the current image
    currentImg.animate({opacity: 0}, 1500)
	      .removeClass('active');
                  
}

$(function() {
/* run rotator */
    rotator();
    
/* sticky footer */
    if($('body').hasClass('home')) {
	
	$(window).bind('load', function() { 
    
	   var footerHeight = 0,
	       footerTop = 0,
	       $footer = $('#footer_home, #footer');
    
	   positionFooter();
    
	   function positionFooter() {
    
		    footerHeight = $footer.height();
		    footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+'px';
    
		   if (($(document.body).height()+footerHeight) < $(window).height()) {
		       $footer.css({
			    position: 'absolute',
			    top: footerTop
		       });
		   } else {
		       $footer.css({
			    position: 'static'
		       });
		   }
		   
	   }
    
	   $(window).scroll(positionFooter)
		    .resize(positionFooter);
    
	});
    }    
    
});
