// JavaScript Document

$(document).ready(function() {
	/*Homepage branding*/
	$('ul#brandingNav li span').css({opacity: 0.66});
	
	$('ul#brandingNav li a').click(function() {
		var curChildIndex = $(this).parent().prevAll().length + 1;
		$(this).parent().parent().children('.current').removeClass('current');
		$(this).parent().addClass('current');
		$('div#homeBranding a.current').fadeOut('slow',function() {
			$(this).removeClass('current');	
		});
		$('div#homeBranding').children('a.branding:nth-child('+curChildIndex+')').fadeIn('slow',function() {
			$(this).addClass('current');
		});
		return false;
	});
	/*End homepage branding*/
	
	/**Share link**/
	$('li#share > a').click(function() {
		$(this).toggleClass('open');
		$('li#share ul').slideToggle();
		return false;
	});
	/**End Share link**/
	
	/*Subpage Branding*/
	$('div#branding').innerfade({ speed: 1500, timeout: 7000, type: 'random_start', containerheight: '178px' });
	/*End subpage branding*/
	
	/* Link to Timeline */
	$('.timelineLink').click(function() {
		window.open(this.href,'myWindow','width=812,height=690,toolbar=no,scrollbars=yes');
        return false;					  
	});
	/* End Timeline */
	
	/**Ornaments Modal window**/
	$('ul.ornaments a').click(function() {	
									   
		//Setup the modal window
		$('body').append('<div id="overlay"></div><div id="modal"><div id="modalInner"><a href="#" id="close" title="Close">Close</a><div id="modalImageColumn"></div><div id="modalTextColumn"></div><div class="clearing"></div></div></div>');
		
		if($.browser.msie && $.browser.version <= 6.0) {
			var windowHeight = $(document).height();
			$('#overlay').height(windowHeight);
		}
		
		$('#overlay').css('opacity', 0.5).fadeIn('fast');
		$('#modal').fadeIn('slow');
		
		//Position it in the viewport
		var scrollPos = $(window).scrollTop() + 50;		
		$('div#modal').css('top',scrollPos+'px');
		
		//Handle the clicking of close button or clicking the background
		$('div#modal a#close,div#overlay').bind('click', function() {
			$('div#modal').fadeOut('fast',function() {
				$('div#overlay').fadeOut('fast',function() {
					$(this).remove();							   
				});
				$(this).remove();							   
			});
			return false;
		});	
		
		//Get the image
		var imgUrl = $(this).attr('href');
		$('div#modalImageColumn').append('<img src="'+imgUrl+'" alt="" class="frame" height="400" id="ornament" width="400" />');
		$('img#ornament').load(function() {
			$(this).fadeIn();								
		});
		
		var clonedText = $(this).siblings('.stateDescription').clone().appendTo('div#modalTextColumn');
		
		return false;
	});
	/**End ornaments modal window**/

	/* Striping */
	$(".striped li:odd").addClass("odd");
	$(".striped li:even").addClass("even");
	$("table.striped tr:odd").addClass("odd");
	$("table.striped tr:even").addClass("even");
	$("div.striped div:odd").addClass("odd");
	$("div.striped div:even").addClass("even");
	/* End Striping */

});