var timer = null;
var headerImages = [];
var disableImageLinks;

function showHeader (elm, elmIdx) {

	var fadeDuration = 0;

	if (disableImageLinks == true) {
	
	} else {
	
		if (elm.hasClass('active') || elm.attr('rel') == '') {
		
			// DO NOTHING
		
		} else {
		
			disableImageLinks = true;

			var preloadElm = headerImages[elmIdx][0];
			var targetElm = $('#HeaderImage img');

			$(preloadElm).load(function(){
				headerImages[elmIdx][2] = true;
				elm.addClass('active').siblings().removeClass('active').blur();
				targetElm.replaceWith($(this)).animate({'opacity' : 1},{ duration: fadeDuration, queue: false, easing: 'easeInSine', complete: function(){ disableImageLinks = false; }}).parent().parent().removeClass('load');
			});
			
			targetElm.animate(
				{'opacity' : 0},
				{ duration: fadeDuration, queue: false, easing: 'easeOutSine', complete: function(){
					if (headerImages[elmIdx][2] != true) {
						$(preloadElm).attr({'src' : headerImages[elmIdx][1]});
					} else {
						elm.addClass('active').siblings().removeClass('active').blur();
						$(this).replaceWith($(preloadElm)).animate({'opacity' : 1},{ duration: fadeDuration, queue: true, easing: 'easeInSine', complete: function(){ disableImageLinks = false; }}).parent().parent().removeClass('load');					
					};
				}}
			);
		}
	}
};

function nextHeaderImage()
{
	var total = $('#ImageNav a').size();
	var current = -1;
	$('#ImageNav a').each(function(i) {
		if ($(this).hasClass("active")) current = i;
	});
	next_image = current + 1;
	if (next_image > total - 1) {
		next_image = 0;
	}
	showHeader($("#ImageNav a:eq(" + next_image + ")"), next_image);
	//clearInterval(timer);
}

function prepHeader () {
	
	if ($('#ImageNav a').size() > 1 ) {
		$('#ImageNav a').each(function(i) {
			var imgObj = $('<img>');
			var imgSrc = $(this).attr('rel');
			headerImages[i] = [imgObj, imgSrc, false];
			$(this).click(function(){
				showHeader($(this), i);
				return false;
			});
		});
		
		$('#ImageNav a:first').addClass('active');
		timer = setInterval("nextHeaderImage()", 10000);
	}
}

function setNav (targetElm) {
	
	// Add active state to target element
	var targetElm = 'div.default_menu ul li.' + targetElm;
	$(targetElm).addClass('active').find('a:first').addClass('active');

	// HOVER FUNCTION FOR EVERY LIST ITEM IN THE MENU
	
	}
	
function ieFix () {

	$('.default_menu li').hover(function(){
		$(this).addClass('hover').find('ul:first, a:first').addClass('show');
	},function(){
		$(this).removeClass('hover').find('ul:first, a:first').removeClass('show');
	});
	
}


$(document).ready(function(){
	prepHeader();
	ieFix();
});