$(function() {
	var iDisplayDelay = 3000;
	var iFadeSpeed = 1750;
	
	var sCurrentLink = '';
					 
	var iCurrent = 0;
	var jImages = $('#logoimages .fluximg-al')

	var oLink = $(jImages[iCurrent]).children('a');
	
	if (jImages.length) {
		jImages.css({ opacity: 0, visibility: 'visible' })
		$(jImages[0]).css({ opacity: 1.0 });
		if (jImages.length > 1) {
			setInterval(function() {
				$(jImages[iCurrent]).animate({ opacity: 0.0 }, iFadeSpeed);
				++iCurrent;
				if (iCurrent >= jImages.length) {
					iCurrent = 0;
				}
				oLink = $(jImages[iCurrent]).children('a');
				$(jImages[iCurrent]).animate({ opacity: 1.0 }, iFadeSpeed);

			}, iDisplayDelay);
		}
	}
	
	$('#clickshield').click(function() {
		oLink.length && (location.href = oLink[0].href);
		return false;
	});
	
	function ClearOpacity(jQueryElement) {
		if (jQueryElement.length) {
			var oStyle = jQueryElement[0].style;
			// clear opacity attribute
			oStyle.opacity = '';
			// clear custom mozilla attribute
			oStyle['-moz-opacity'] = '';
			// clear custom khtml attribute
			oStyle['-khtml-opacity'] = '';
			// if an alpha filter is in place, clear it
			if (typeof(oStyle.filter) == 'string') {
				oStyle.filter = oStyle.filter.replace(rAlphaFilter, '');
			}
		}
	}	
});		

// highlights crossfader
$(function() {
// are there any numbered photos?
	var jFeatured = $('.highlight-photo');
	if (!jFeatured.length) {
		return;		
	}

	// create the main element + numbering panel + click cover
	$(jFeatured[0]).before('<div id="highlight-photo-cover"></div><div id="highlight-photo-numbers"><div></div></div><div id="highlight-photo-wrapper"></div>');
	
	// move photos inside it
	jContainer = $('#highlight-photo-wrapper');
	jNumbers = $('#highlight-photo-numbers div');
	
	jContainer.append(jFeatured);

	// create numbered items for highlight photos
	var iClickTicker = null;
	var aoFeatured = [];
	var iCurrent = -1;
	// find and store the featured images and links	
	var iCount = 1;
	jFeatured.css({ opacity: 0 }).show().each(function() {
		jNumbers.append('<div class="number">' + (iCount++) + '</div>');
		aoFeatured.push({
			highlight: $(this),
			image: $(this).find('img'),
			link: $(this).find('a'),
			preload: null,
			loaded: false
		});
	});
	// setup the click cover
	$('#highlight-photo-cover').click(function() {
		clearInterval(iClickTicker);
		if (aoFeatured[iCurrent] && aoFeatured[iCurrent].link) {
			location.href = aoFeatured[iCurrent].link.attr('href');
		}
	});
	// setup the transition controls, and initiate the first one
	var iCount = 0;
	$($('#highlight-photo-numbers .number').each(function() {
		this.iFeaturedIndex = iCount++;
		$(this).click(ControlClicked) 
	})[0]).click();
		
	function ControlClicked(eEvent) {
		// setup the auto-click
		clearTimeout(iClickTicker);
		iClickTicker = setTimeout(ImageNext, 4000);
		
		var iIndex = eEvent.target.iFeaturedIndex;
		Preload(aoFeatured[iIndex], true);
		Preload(aoFeatured[iIndex+1], false);
		ImageTransition();
		return false;
		
		function Preload(oFeatured, bLoadingAnim) {
			// create the image if it has not already been
			if (oFeatured && !oFeatured.preload) {
				// set the loading anim display if it was newly created
				if (bLoadingAnim) {
					$('#highlight-photo-cover').addClass('loading');
				}
				var preload = oFeatured.preload = new Image;
				preload.onload = function() {
					oFeatured.loaded = true;
					this.onload = null;
				}
				preload.src = oFeatured.image.attr('src');
			}
		}
		
		function ImageTransition() {
			if (!aoFeatured[iIndex].loaded) {
				return setTimeout(ImageTransition, 50);
			}
			// clear the loading display
			$('#highlight-photo-cover').removeClass('loading');
			// reset animations, and fade it in and all the others out
			jFeatured.stop().animate( { opacity: 0 }).show();
			aoFeatured[iIndex].highlight.stop().animate({ opacity: 1.0 });
			// update the current image state to the new index
			iCurrent = iIndex;
			$('#highlight-photo-numbers .number').removeClass('current');
			$(eEvent.target).addClass('current')
		}
	}
	
	function ImageNext() {
		iCurrent++;
		if (iCurrent >= aoFeatured.length) {
			iCurrent = 0;
		}
		$($('#highlight-photo-numbers .number')[iCurrent]).click();
	}
});




