$ = jQuery.noConflict();
$(function() {
	$('.nav').hover(function() {
	}, function() {
		$('.nav > li ul').hide();
		$('.current_page_item ul, .current_page_ancestor ul').show();
	}).children('li').hover(function() {
		$('.nav > li ul').hide();
		$(this).children('ul').css({ 'left':'-230px' }).show();
	}, function() {
		$(this).removeClass('hover');
	});
	
	$('#teamnav li.current_page_item').parent().css('background-position','0 -' + (($('#teamnav li').index( $('#teamnav li.current_page_item').get()[0] )+1)*158) + 'px');
	$('#teamnav li').hover(function() {
		$('#teamnav li a span').css('left','-9999px');
		$(this).find('span').css('left','0');
		$('#teamnav').css('background-position', '0 -' + (($('#teamnav li').index(this)+1)*158) + 'px');
	}, function() {
		$('#teamnav li a span').css('left','-9999px');
		$('#teamnav .current_page_item').find('span').css('left','0');
		if ($('#teamnav li.current_page_item').length) {
			$('#teamnav li.current_page_item').parent().css('background-position','0 -' + (($('#teamnav li').index( $('#teamnav li.current_page_item').get()[0] )+1)*158) + 'px');
		} else {
			$('#teamnav').css('background-position','0 0');
		}
	});
	
	
	if ($('.tour').is('div')) {
		
		imgCount = $('.tour img').length;
		if (imgCount) {
			setInterval('changeBg()', 3500);
		}
	}
	
	
	if ($('.twinkle').is('div')) {
		imgCount = $('.twinkle img').length;
		z = imgCount + 1;
		lastImg = new Object();
		if (imgCount) {
			twinkle();
		}
	}
});

function twinkle() {
	img = $('.twinkle img').get( Math.floor(imgCount * Math.random()) );
	if (img == lastImg) {
		twinkle();
		return;
	}	
	lastImg = img;
	
	z ++;
	
	$('.image-3').each(function() {
		$(this).css('z-index', $(this).css('z-index') + 1);				
	});
	
	if ($(img).css('z-index') == '1') {
		$(img).css({ 'opacity': '0', 'z-index': z }).animate({ 'opacity': 1 }, 3000, 'linear', function() {
			twinkle();
			return;
		});
	} else {
		$(img).animate({ 'opacity': 0 }, 3000, 'linear', function() {
			$('.twinkle .' + $(img).attr('class')).each(function() {
				if ($(this).css('z-index') == '1') {
					boostZ();
				}
			});
			$(this).css({ 'z-index': '1', 'opacity': '1' });
			twinkle();
			return;
		});
	}
}

function boostZ() {
	$('.twinkle img').each(function() {
		$(this).css('z-index', $(this).css('z-index') - 0 + 1);
	});
}

function changeBg() {
	nextImg = ( $('.tour img').index($('.active')) == imgCount - 1 ) ? $('.tour img:first') : $('.tour img.active').next();
	nextImg.css({ zIndex:'1', display:'block' });
	$('.tour img.active').fadeOut(1000, function() {
		$(this).removeClass('active');
		nextImg.addClass('active').css('z-index','2');
	});
}