/*
-----------------------------------------------------
Feature Gallery-related javascript
Requires: JQuery; Grayscale.js

Expects UL, with LI featuring first an image (used for navigation) and
a DIV. Do not give the DIV a class.

Each slide must have a unique ID

Author: Andrew White, a@awhite.info
-----------------------------------------------------
*/

function featureGallery(targetDiv,currentSlide) {
	if ($.browser.safari || ($.browser.msie&&parseInt($.browser.version)<7)) {
		doGrayscale=false;
	} else {
		doGrayscale=true;
	}
	
	// Create the UI
	$(targetDiv).append('<div id="hero-gallery-indicator"><img src="/_media/images/global/img_slide_indicator.png" alt="" width="9" height="17"></div>');
	$(targetDiv).append('<div id="hero-gallery-content"></div>');
	$(targetDiv).append('<div id="hero-gallery-navigation"></div>');
	
	var numSlides = $(targetDiv+' ul#gallery>li').length;

	$('#hero-gallery-indicator').height(parseInt(480/numSlides)-4);
	$('#hero-gallery-indicator img').css('top',($('#hero-gallery-indicator').height()-17)/2);
		
	// Move the elements of the list into place
	$(targetDiv+' ul#gallery>li>img').each( function() {
		$(this).appendTo('#hero-gallery-navigation').wrap('<div>');
	});
	$(targetDiv+' ul#gallery>li>div').each( function() {
		$(this).appendTo('#hero-gallery-content').addClass('slide').hide();
	});
	
	// Hide the UL
	$(targetDiv+' ul:eq(0)').hide();
	
	$('div.slide:eq('+currentSlide+')').show();
	
	// Build the navigation
	$('#hero-gallery-navigation div:eq('+currentSlide+')').addClass('current');
	
	$('#hero-gallery-navigation > div').height(parseInt(480/numSlides));
	
	indicatorPosition = $('#hero-gallery-navigation div:eq('+currentSlide+')').position();
	$('#hero-gallery-indicator').css('top',(indicatorPosition.top)+'px');
	
	$('#hero-gallery-navigation div img').hover(
		function () {
			if (doGrayscale) {
				grayscale.reset($(this));
			}
		}, 
		function () {
			imagePosition = $(this).position();
			indicatorPosition = $('#hero-gallery-indicator').position();
			
			if ((imagePosition.top != indicatorPosition.top) && (doGrayscale)) {
				grayscale($(this));
			}
		}
	);
	
	var images = $('#hero-gallery-navigation div').find('img');
	var slides = $('#hero-gallery-content').find('div.slide');
	
	images.click(function() {
		links = new Array();
		
		if ($(this).attr('rel')){links = $(this).attr('rel').split(',');}

		if (links[0]&&$(this).hasClass('linkout')) {
			window.location=links[0];
		} else {
	
			slides.hide().eq( images.index(this) ).show();
			if (doGrayscale) {grayscale($('#hero-gallery-navigation div.current img'));}
			$('#hero-gallery-navigation div.current').removeClass('current');
			indicatorPosition = $(this).position();
			$('#hero-gallery-indicator').css('top',indicatorPosition.top+'px');
			$(this).parent().addClass('current');
			if (doGrayscale) {grayscale.reset($(this));}
			
			if (links[0]) {
				// all slides should have this rel/group
				$('div.hiddenGroup').hide();
				$('div#'+links[0]).show();
			}
			if (links[1]) {
				window.location='#'+links[1];
			}
		}
		
	});
	if (doGrayscale) {
		grayscale($('#hero-gallery-navigation div img'));
		grayscale.reset($('#hero-gallery-navigation div img:eq('+currentSlide+')'));
	}
}
