var is_rotating = true;
var interval = 30000;

$(document).ready(function(){

	if($('.secondary-promo-nav').length > 0) {
		// Scroll to next promo
		setInterval(function(){
			if(!is_rotating){
				return;
			}
			
			// Store the selected one
			var previous_selected = $('.secondary-promo-nav .selected');
			var previous_selected_image = $('.secondary-images .selected');
			
			// Figure out what the last button is, so we know if we need to start next at #1.
			if($('.secondary-promo-nav :last-child img').attr('rel') == $('.secondary-promo-nav .selected').attr('rel')) {
				$('.secondary-promo-nav :first-child img').addClass('selected');
				$('.secondary-promo-nav :first-child img').attr('src', '/hr/resources/images/dot-selected.gif');	
				
				// Remove the selected
				$('.secondary-images .selected').removeClass('selected');
				
				// Add the new one
				$('.secondary-images :first-child').addClass('selected');
			}else{
				// For the next button state
				$('.secondary-promo-nav .selected').parent().next().children().addClass('selected');
				$('.secondary-promo-nav .selected').attr('src', '/hr/resources/images/dot-selected.gif');
				
				$('.secondary-images .selected').next().addClass('selected');
				
				$(previous_selected_image).removeClass('selected');
			}
			
			// Remove the previous button state
			$(previous_selected).removeClass('selected');
			$(previous_selected).attr('src', '/hr/resources/images/dot.gif');
							
		}, interval);
		
		// Homepage Box
		$('.secondary-promo-nav').css('display', 'block');
		
		 $('.secondary-promo-nav a').each(function() {
			$(this).attr('href', 'javascript:void(0);');
		});
		
		$('.secondary-promo-nav a img').click(function(e){
			var cur_promo = $(this).attr('rel').substring(6);
			var prev_promo = $('.secondary-promo-nav .selected').attr('rel').substring(6);
			
			
			if(cur_promo != prev_promo) {
				if($(this).attr('class') != 'selected'){
					$('#rotating-' + prev_promo).parent().css('display', 'none');
					$('.secondary-promo-nav .promo-' + prev_promo).attr('src', '/hr/resources/images/dot.gif');
					$('.secondary-promo-nav .selected').toggleClass('selected');
					
					
					$('#rotating-' + cur_promo).parent().css('display', 'block');
					$('.secondary-promo-nav .promo-' + cur_promo).attr('src', '/hr/resources/images/dot-selected.gif');
					$(this).toggleClass('selected');
				}
			}
			
			is_rotating = false;
			
			e.preventDefault();
			return false;
		});
	}
});
