$(document).ready(function(){
	// start coda slider
	$('#coda-slider-1').codaSlider({
		autoHeight: false,
		autoSlide: false,
		crossLinking:true,
		dynamicArrows:false,
		dynamicTabs:false
	});
		
	//for each panel, get height of tallest img in panel, set top margin
	$('.panel').each(function() {
		this_panel = $(this);
		var max_height = 0;
		this_panel.find('.slide-box img').each(function() {
			max_height = Math.max(max_height, $(this).attr("height"));
		});
		var marg_top = Math.round((500 - max_height)/3);
		this_panel.css('padding-top',marg_top);
	});
	 
	//mark external links
	$('.text a[href^="http"]').not('a[href*=norabrowndesign]').not('a:has(img)').addClass('external');
	
	//create prev next buttons within panels
	$('ul.slides').each(function(){
		var this_slides = $(this);
		var new_slide = 0;
		var slide_count = this_slides.find('li').hide().size();
		this_slides.find('li:first').fadeIn(2000);//show first
		this_panel = this_slides.closest('.panel');
		this_panel.find(".text").append('<div class="prev-next"><div class="prev">previous</div><div class="next">next</div></div>');
		this_panel.find('.next').click(function(){
			//alert('next!');
			new_slide = new_slide + 1;
			if(new_slide >= slide_count){//if at end, go to first img
				new_slide=0;
			}
			this_slides.find('li:visible').fadeOut(500,function(){
				this_slides.find('li:eq(' + new_slide + ')').fadeIn(500);
			});
		});
		this_panel.find('.prev').click(function(){
			new_slide = new_slide - 1;
			if(new_slide < 0){//if at beginning go to last img
				new_slide = slide_count - 1;
			}
			this_slides.find('li:visible').fadeOut(500,function(){
				this_slides.find('li:eq(' + new_slide + ')').fadeIn(500);
			});
		});
	});
	
	$('.prev, .next').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
});

// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		//alert("pageload: " + hash);
		// hash doesn't contain the first # character.
		if(hash) {
			// restore ajax loaded state
			if($.browser.msie) {
				// jquery's $.load() function does't work when hash include special characters like aao.
				hash = encodeURIComponent(hash);
			}
			$('#trick').attr("href","#"+hash).trigger('click');
		} else {
			// start page
			$('#trick').attr("href","#1").trigger('click');
		}
	}