$(document).ready(function() {

// Navigation
	$('ul#navigation li').hover(function() {
		if($(this).attr('id')) $(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});

// Image lists
	$('.image-list li a').mouseover(function() {
		var ul = $(this).parent().parent().parent();
		ul.find('li').removeClass('selected');
		$(this).parent().addClass('selected');
		var idx = 0;
		ul.find('li').each(function(i) {
			if($(this).hasClass('selected')) idx = i;							  
		});
		var m = idx * ul.find('.thumbnail div').height();
		ul.find('img:first').css({ marginTop: - m });
	});

// Initialize when doc is ready:
	features.initialize();
	slides.initialize();
	gallery.initialize();
	
});

// Features
var features = {
	timer: null,
	initialize: function() {
		if($('#home-top')) {
			$('#features ul').before('<ol></ol>');
			$('#features ul li').each(function(i) {
				$('#features ol').append('<li><a href="#">' + (i + 1) + '</a></li>');									
			});
			$('#features ol li:first').addClass('selected');
			$('#features ol li a').click(function() {
				var index = parseInt($(this).html()) - 1;
				features.skip(index); 
				return false;
			});
			$('#features ul img').click(function() {						 
				var href = $(this).parent().find('a').attr('href') || '';
				if(href.indexOf('#gallery/') > -1 || href.indexOf('#diary/') > -1) {
					var id = href.substring(href.lastIndexOf('#') + 1);
					gallery.launch(id);
					return false;
				}
				else document.location = href;
			}).attr('galleryimg', 'no');
			$('#features ul li').hide().eq(0).show();
			$(window).load(function() {
				features.timer = setTimeout('features.rotate(0)', 2000);
			});
		}
	},
	rotate: function(index) {
		$('#features ul li').eq(index).fadeOut(function() {
			index = (index == $('#features ul li').length - 1) ? 0 : ++ index;
			features.indicator(index);
			$('#features ul li').eq(index).fadeIn(function() {
				features.timer = setTimeout('features.rotate(' + index + ')', 6000);
			});
		});
	},
	skip: function(index) {
		clearTimeout(features.timer);
		features.indicator(index);
		$('#features ul li').hide().eq(index).show();
		features.timer = setTimeout('features.rotate(' + index + ')', 6000);
	},
	indicator: function(index) {
		$('#features ol li').removeClass('selected').eq(index).addClass('selected');
	}
}


// Article Slideshows
var slides = {
	initialize: function() {
		var current = 1;
		var preload = new Array();
		var images = $('#slideshow .slideshow-image').length;
		if(images) {
			$.each(images, function(i) {
				preload[i] = new Image();
				preload[i].src = $(this).find('img:first').attr('src');
			});
			if(images > 1) {
				$('#slideshow-nav').html('<a href="#" class="back"></a>'
				+ '<a href="#" class="next"></a><span>1 of ' + images + '</span>');
			}
			else if(!$('#slideshow .credit:first').html().length) {
				$('#slideshow .credit').hide();
			}
			$('#slideshow-nav a').click(function() {
				var d = $(this).hasClass('back') ? current - 1 : current + 1;
				if(d < 1) d = images;
				else if(d > images) d = 1;
				$('#slideshow .slideshow-image').eq(current - 1).fadeOut(function() {
					$('#slideshow .slideshow-image').eq(d - 1).fadeIn();
					$('#slideshow-nav span:first').html(d + ' of ' + images);
					current = d;
				});
				return false;
			});
		}
	}
}


// Photo Galleries
var gallery = {
	initialize: function() {
		var links = false;
		$('#page a').each(function() {
			var href = $(this).attr('href') || '';
			if(/#(gallery|diary|contest)\//.test(href)) {
				links = true;
				$(this).click(function() {
					var id = $(this).attr('href');
					id = id.substring(id.lastIndexOf('#') + 1);
					gallery.launch(id);
					return false;
				});
			}
		});
		if(links) {
			var preload = new Image();
			preload.src = '/images/news/photos/gallery-bg.png';	
		}
	},
	launch: function(id) {
		gallery.current = 0;
		gallery.lock = false;
		$('#gallery').remove();
		$.getJSON('/_ajax/' + id, function(data) {
			if(!data.error) {
				$(document.body).append(data.template);
				$('#gallery h1').html(data.title);
				gallery.center();
				$(window).resize(gallery.center);
				$(window).scroll(gallery.center);
				$('#gallery').fadeIn('fast');
				$('#gallery-close').click(function() {
					$(window).unbind('resize', gallery.center);
					$(window).unbind('scroll', gallery.center);
					$('#gallery').fadeOut('fast', function() {
						$('#gallery').remove();
					});
					return false;
				});
				$('#gallery-nav-back').click(function() {
					gallery.navigate(-1);
					return false;
				});
				$('#gallery-nav-next').click(function() {
					gallery.navigate(1);
					return false;
				});
				gallery.photos = data.photos;
				if(data.intro) {
					var io = {intro: data.intro};
					gallery.photos.unshift(io);	
				}
				var loadImage = null;
				var imgPreload = new Image(); 
				var pl = data.intro ? 1 : 0;
				imgPreload.src = gallery.photos[pl].path;
				imgPreload.onload = function() {
					$('#photo-pad').removeClass('images-loading');
					clearInterval(loadImage);
					gallery.navigate(1);
					
				}
				var loadImage = setInterval(function() {
					if(imgPreload.complete) {
						$('#photo-pad').removeClass('images-loading');
						clearInterval(loadImage);
						gallery.navigate(1);
					}
				}, 250);
				
			}
			else alert('An error has occurred.');
		});
	},
	navigate: function(i) {
		if(!gallery.lock) {
			gallery.current = gallery.current + i;
			if(gallery.current < 1) {
				gallery.current = 1;
				return;
			}
			if(gallery.current > gallery.photos.length) {
				gallery.current = gallery.photos.length;
				return;
			}
			$('#gallery a span').removeClass('disabled');
			if(gallery.current == 1) {
				$('#gallery-nav-back span').addClass('disabled');
			}
			if(gallery.current == gallery.photos.length) {
				$('#gallery-nav-next span').addClass('disabled');
			}
			else {
				preload = new Image();
				preload.src = gallery.photos[gallery.current].path;	
			}
			var img = gallery.photos[gallery.current - 1];
			if(img.intro) {
				$('#photo').before('<div id="gallery-intro">' + img.intro + '</div>');
				$('#photo-container').css('marginTop', 210 - $('#gallery-intro').height() / 2);
				$('#gallery-nav-back span').addClass('disabled');
				$('#gallery-nav-next span').removeClass('disabled');
				$('#photo-caption').html('');
				$('#photo').hide();
			}
			else {
				$('#gallery-intro').remove();
				gallery.lock = true;
				$('#photo').fadeOut(function() {
					$('#photo').attr({
						'src': img.path,
						'width': img.width,
						'height': img.height
					});
					$('#photo-caption').html(img.caption);
					$('#photo-container').css('marginTop', 210 - $('#photo').height() / 2);
					$('#photo').fadeIn(function() {
						gallery.lock = false;							
					});
				});
			}
		}
	},
	center: function() {
		var w = $(window);
		var d = $(document);
		var g = $('#gallery');
		var l = (w.width() / 2) - (g.width() / 2) + d.scrollLeft();
		var t = (w.height() / 2) - (g.height() / 2) + d.scrollTop() - 30;
		if(t < d.scrollTop()) t = d.scrollTop();
		g.css({left: l, top: t});
	}
}




















