
// IE6 PNG fix, checks for IE6 only IEFix include file/object
pngFix = function(image) {
	if (typeof IEFix != "undefined") {
			var img = {
				'src':image.attr('src'),
				'width':image.width(),
				'height':image.height()
			};
			image.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + img.src + '", sizingMethod="crop")')
			image.attr('src', '/media/images/transparent.gif');
			image.css('width', img.width);
			image.css('height', img.height);
			image.css('visibility', 'visible');
	}
	};

/*
// Not used
pngFixRemove = function(image, src) {
		var img = {
			'src':image.attr('src'),
			'width':image.width(),
			'height':image.height()
		};
		image.css('filter', '')
		image.attr('src', src);
		image.css('visibility', 'visible');
	};
*/


// Wire up mouse-over and active state images for nav and subnav
navigationSprites = function(containerid){
	$('li', containerid).each(function(index) {
		$(this).width($('img',$(this)).width());

		var imgSpriteUrl = $('img',$(this)).attr('src');
		imgSpriteUrl = imgSpriteUrl.replace('.jpg','-s.jpg');
		imgSpriteUrl = imgSpriteUrl.replace('.png','-s.png');
		imgSpriteUrl = imgSpriteUrl.replace('.gif','-s.gif');
		
		$('img',$(this)).css('visibility','hidden');
		$(this).css('background-color','transparent');
		$(this).css('background-image','url('+imgSpriteUrl+')');
		$(this).css('background-repeat','no-repeat')
		$(this).css('background-position','0 0');
		
	});
	
	$('li.selected', containerid).each(function(index) {
		$(this).css('background-position','0 -'+$(this).height()*2+'px');
	});
	
	$('li:not(".selected")', containerid).hover(
		function () {
			$(this).css('background-position','0 -'+$(this).height()+'px');
		}, 
		function () {
			$(this).css('background-position','0 0');
		}
	);
	
	
	$('li.selected', containerid).hover(
		function () {
			$(this).css('background-position','0 -'+$(this).height()+'px');
		}, 
		function () {
			$(this).css('background-position','0 -'+$(this).height()*2+'px');
		}
	);
	
};






// Wire up all image and actions for the gallery navigation
loadGallery = function(){
	var imagesperpage = 16;
	var pagewidth = parseInt($('#list-container').css('width').replace('px',''));
	var imagecount = $('li','ul.thumbnail-list').size();
	var pagecount = Math.ceil(imagecount / imagesperpage);
	var stagewidth = pagecount * pagewidth;

	$('#list-stage').width(stagewidth);
	$('#list-stage').css('display','block');
	
	
	$('#gallery-contents #detail-view-photo').click(function(event){
		$('#gallery-contents #pagination').fadeIn("slow");
		$('#gallery-contents #detail-view-photo').fadeOut("fast");
	});
	$('#gallery-contents #detail-view-video').click(function(event){
		$('#gallery-contents #pagination').fadeIn("slow");
		$('#gallery-contents #detail-view-video').fadeOut("fast");
	});
	
		
	showGalleryPage = function(pageref){
		$('a.page-link').removeClass('selected');
		$('a#page-link-'+pageref).addClass('selected');
		//Cufon.refresh('div#pagination');
		
		var stageoffset = pagewidth * (parseInt(pageref) - 1);
		$('#list-stage', $('#gallery-contents')).animate({"left": "-"+stageoffset+"px"}, "slow", function () {
			$('img:visible:not(".png-fixed")', $('ul.thumbnail-list')).each(function() {
				pngFix($(this));
				$(this).addClass('png-fixed');
			});
		});
		
		$('#gallery-contents #pagination').css('display','block');
		
	};

	addPagination = function(){
		$('#pagination').html('<a href="#" id="page-link-back" class="page-link">&lt;</a> \n');
		for (i=1;i<=pagecount;i++)
		{
			$('#pagination').append('<a href="#" id="page-link-' + i + '" class="page-link">' + i + '</a> \n');
		}		
		$('#pagination').append('<a href="#" id="page-link-forward" class="page-link">&gt;</a> \n');

		
		$('a.page-link').each(function(index) {
			$(this).click(function(event) {
				event.preventDefault();
				//showGalleryPage($(this).attr('href'));
				var newpage = $(this).attr('id').replace('page-link-','');
				if (newpage == 'back') {
					newpage = parseInt($('#pagination a.selected').attr('id').replace('page-link-','')) - 1;
					if (newpage == 0) {
						newpage = 1;
					}
				} else if (newpage == 'forward') {
					newpage = parseInt($('#pagination a.selected').attr('id').replace('page-link-','')) + 1;
					if (newpage > pagecount) {
						newpage = pagecount;
					}
				}
				showGalleryPage(newpage);
			});
		});
		
	};
	
	$('li.video', 'ul.thumbnail-list').each(function(index) {
		$('a', $(this)).html("<img src='/media/images/racing/common/gallery-video-overlay.png' />");
		$(this).mouseenter(function(){
			$('img', $(this)).attr('src','/media/images/racing/common/gallery-video-overlay-off.png');
			pngFix($('img', $(this)));
		}).mouseleave(function(){
			$('img', $(this)).attr('src','/media/images/racing/common/gallery-video-overlay.png');
			pngFix($('img', $(this)));
		});
		$(this).click(function(event){
			event.preventDefault();
			
			$('#gallery-contents #pagination').fadeOut("fast");
			$('#gallery-contents #detail-view-video').fadeIn("slow");
			
		});
		
	});
	
	$('li.photo', 'ul.thumbnail-list').each(function(index) {
		$('a', $(this)).html("<img src='/media/images/racing/common/gallery-photo-overlay.png' class='png' />");
		$(this).mouseenter(function(){
			$('img', $(this)).attr('src','/media/images/racing/common/gallery-photo-overlay-off.png');
			pngFix($('img', $(this)));
		}).mouseleave(function(){
			$('img', $(this)).attr('src','/media/images/racing/common/gallery-photo-overlay.png');
			pngFix($('img', $(this)));
		});
		$(this).click(function(event){
			event.preventDefault();
			
			$('#gallery-contents #detail-view-photo').css('background-image','url('+$(this).attr('data-media')+')');
			$('#gallery-contents #pagination').fadeOut("fast");
			$('#gallery-contents #detail-view-photo').fadeIn("slow");
			
		});
	});
	
	$('li', 'ul.thumbnail-list').each(function(index) {
	
		$(this).css('background-image','url('+$(this).attr('data-thumbnail')+')');
		$(this).css('background-repeat','no-repeat')
		$(this).css('background-position','0 0');


	});
	


	addPagination();
	$('#pagination').css('display','block');
	
};


// Document loaded
$(document).ready(function(){

	// Remove some unneeded dynamic styles from the Container Controller
	$('#content').css('left', '');
	$(window).unbind('resize', Container.adjust);
	
	navigationSprites('#navigation-container');
	navigationSprites('#subnavigation-container');


	$('#galleryWindow').jqm({
		trigger:'#gallery-link',
		onShow: function(h) {
			/* callback executed when a trigger click. Show notice */
			h.w.slideDown(function() {			
				showGalleryPage(1);
				
			}); 
		},
		onHide: function(h) {
			/* callback executed on window hide. Hide notice, overlay. */
			h.w.slideUp("slow",function() { if(h.o) h.o.remove(); });
			$('#gallery-contents #detail-view-photo').css('display','none');
			$('#gallery-contents #detail-view-video').css('display','none');
		}
	});


	$('a#gallery-link').click(function(event) {
		event.preventDefault();
	});
	
	Cufon.now();
	$('.font-set-1').css('visibility','visible');
	
	loadGallery();

});


	
