// JavaScript Document

Date.prototype.getDOY = function() {
	var onejan = new Date(this.getFullYear(),0,1);
	return Math.ceil((this - onejan) / 86400000);
} 

var days = {};
var day = 0;

function onThisDay(xml) { 
			
	var today = new Date();
	day = today.getDOY(); 
	
	days = xml;
	 
	node = $("date",xml).get(day);
	
	$("#xmlDateMonth").html( $(node).attr('m') + " " + $(node).attr('d') );
	
	$("#xmlRecord").html( $(node).find('record').text() );
			
}

function dayNext() {
	
	day++;
	
	node = $("date",days).get(day);
	
	$("#xmlDateMonth").html( $(node).attr('m') + " " + $(node).attr('d') );
	
	$("#xmlRecord").html( $(node).find('record').text() );

}

function dayPrevious() {
	
	day--;
	
	node = $("date",days).get(day);
	
	$("#xmlDateMonth").html( $(node).attr('m') + " " + $(node).attr('d') );
	
	$("#xmlRecord").html( $(node).find('record').text() );

}

$(function() {
	var h = 430 - $("#imageGallery div.current .text:first").height();
	$("#imageGallery div.current .text").animate({top:h}, 500);
	
	resizeNews();
	
	setInterval(featureSlide, "10000");
	
	currentLeft = 0;
	direction = 1;
	$('.slider').everyTime(5000,function() {
			
			if ( currentLeft < -300 || currentLeft >= 0 ) {
				direction *= -1;
			}
			currentLeft += (300 * direction);
			$('.slider').animate( { left: currentLeft }, 1500, 'easeOutBounce' );
			
	});
	
	//Caption Slider
	$('.boxgrid').hover(function(){
		$(".cover", this).stop().animate({top:'130px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'210px'},{queue:false,duration:160});
	});

	
	$('.zoom').lightBox();
	
	
	$.get("onThisDay.xml", function(xml) {  
	   onThisDay(xml);  
	});   

	
	
});

function featureSlide() {
	
	var currentSlide = $("#imageGallery div.current");
	var nextSlide = currentSlide.next();
	
	if ( nextSlide.length == 0 ) { 
		nextSlide = $("#imageGallery div:first");
	}
	
	$(".text",nextSlide).css({top:"430px"});
	
	nextSlide.addClass('next').animate({opacity:1.0});
		
	currentSlide.animate( { 	
		opacity: 0 }, 
		1500, 'easeOutCubic', 
		function() { 
			
			currentSlide.removeClass('current'); 
			nextSlide.removeClass('next').addClass('current'); 
			h = 430 - $("#imageGallery div.current .text:first").height();
			$("#imageGallery div.current .text").animate({top:h}, 1000, 'easeOutQuint');
		} 
	);
	
}

function resizeNews() {
	var maxWidth = 80; 
	var maxHeight = 70;   
    $('.imgholder img').each(
		function() {
			var ratio = 0;  
			var width = $(this).width();   
			var height = $(this).height(); 
	
			if(height > maxHeight){
				ratio = maxHeight / height; 
				$(this).css("height", maxHeight);   
				$(this).css("width", width * ratio);   
				width = width * ratio;   
			}
		}
	);
	
}

