/* background image rotator */
function rotator() {
        $('#backgroundImages div').css('opacity', 0)
                                  .eq(0).css('opacity', 1).addClass('active');

        $('#Taglines 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');
    var currentTag = $('#Taglines div.active') ? $('#Taglines div.active') : $('#Taglines 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');	
    var nextTag = (currentTag.next().length) ? (currentTag.next().hasClass('active')) ? $('#Taglines div:first') : currentTag.next() : $('#Taglines 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);

    nextTag.css('opacity', 0)
	   .addClass('active')
           .animate({opacity: 1}, 1500);

    //Hide the current image
    currentImg.animate({opacity: 0}, 1500)
	      .removeClass('active');

    currentTag.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);
    
	});
    }    
/* dropdown menu */                 
    $('#topNav li').hover(function(){
        $(this).addClass('hover');
        $('ul:first', this).css('visibility', 'visible');
    }, function(){
        $(this).removeClass('hover');
        $('ul:first', this).css('visibility', 'hidden');
    });
    
/* menu styling adjestment */
    $('ul.submenu, ul.submenu li ul').find('li:last a').addClass('noborder');
    
});
