/* background image rotator */
function rotator() {
        $('#homeBanner div').css('opacity', 0)
                            .eq(0).css('opacity', 1).addClass('active');
        
        setInterval('rotateIt()', 5000);
}

function rotateIt() {
    // Get the first image
    var currentImg = $('#homeBanner div.active') ? $('#homeBanner div.active') : $('#homeBanner 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')) ? $('#homeBanner div:first') : currentImg.next() : $('#homeBanner 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();
    
/* dropdown menu */                 
    $('#nav li').hover(function(){
        $(this).addClass('hover');
        $('ul:first', this).css('visibility', 'visible');
    }, function(){
        $(this).removeClass('hover');
        $('ul:first', this).css('visibility', 'hidden');
    });
    
/* clickable(big) feature buttons */
    $('ul.quickMenu li').click(function() {
	var theLink = $(this).find('a')[0].href;
	window.location = theLink;
    }).css('cursor', 'pointer');
    
/* Newsletter placeholder text (HOMEPAGE ONLY) */
    $('#email').addClass('placeholder');
    $('#email').bind('focusin', function(event){
	var obj = $(event.target);
	obj.removeClass('placeholder');
	if(obj.val() == obj.attr('title')) obj.val('');
    });
    $('#email').bind('focusout', function(event){
	var obj = $(event.target);
	if((obj.val() == '') || (obj.val() == obj.attr('title'))) {
	    obj.val(obj.attr('title'));
	    obj.addClass('placeholder');
	}
    });
    
/* menu styling adjustment */
    $('#nav > ul > li:last').find('ul:first').css({'left': 'auto', 'right': '0', 'width': '100px'});
        
    pageName = $('#subNav > h2').text().toLowerCase().replace(/ /g,'').replace(/&/g,'');
    //$('#nav > ul > li').removeClass('current').filter('#'+ pageName).addClass('current');
            
    if(pageName === 'corporate') {$('#nav li#corporate').css({'background-position': '0 -32px'});}
    if(pageName === 'projects') {$('#nav li#projects').css({'background-position': '-145px -32px'});}
    if(pageName === 'investors') {$('#nav li#investors').css({'background-position': '-283px -32px'});}
    if(pageName === 'news') {$('#nav li#news').css({'background-position': '-430px -32px'});}
      
});
