$(function(){
	//add in navigation controls (hidden when JS is not available)
	$('.textlinks').css({'display':'block'});
	$('.textlinks .previous').click(function(){gotoThumb("previous");return false;});
	$('.textlinks .next').click(function(){gotoThumb("next");return false;});
	
	//remove scrollbar from thumbnails
	$('.thumbcontainer,.thumbviewport').css({'height':'100px'});
	$('.thumbviewport').css({'overflow':'hidden'});
	
	//swap iframe for img
	$(".largeview img").css({'display':'block'});
	$(".largeview iframe").css({'display':'none'});
	
	//add some padding to each side of the slider
	//$('.thumbslider').css({'padding-left':'600px'});
	//$('.thumbslider a:last-child').css({'margin-right':'600px'});
	
	//activate the first thumb
	$('.thumbslider a:first-child').addClass('active');

	$(".thumbslider a").click(function(e){
		e.preventDefault();
		gotoThumb($(this).index());
		return false;
	});
	setTimeout("gotoThumb(0)",1000);
});

function gotoThumb(thumbnum){
	if(thumbnum=="next"){
		if($(".thumbslider a.active").index()>=$(".thumbslider a").length-1){
			return false;
		}
		thumbnum=$(".thumbslider a.active").index()+1;
	}
	if(thumbnum=="previous"){
		if($(".thumbslider a.active").index()<=0){
			return false;
		}
		thumbnum=$(".thumbslider a.active").index()-1;
	}

	//calculate the left offset. start with slider padding
	var left=parseInt($('.thumbslider').css('padding-left'));
	for(i=0;i<thumbnum;i++){
		left+=20+parseInt($('.thumbslider a').eq(i).width());
	}
	left+=(0.5*parseInt($('.thumbslider a').eq(thumbnum).width()))-($(".thumbviewport").width()/2);
	$('.thumbviewport').animate({scrollLeft:left + 'px'},500,'swing');
	
	//now make sure the right thumbnail has the active class
	$('.thumbslider a.active').removeClass('active')
	$('.thumbslider a').eq(thumbnum).addClass('active');
	
	//load in the large picture
	$(".largeview img").attr('src',$(".thumbslider a.active").attr('href'));
	
	//fix dimensions in IE
	if($.browser.msie){
		$(".largeview img").css({'width':$(".thumbslider a.active").attr("data-width")});
	}
	
	//finally, see if the next/previous buttons need hiding/showing
	//enablers
	if($(".thumbslider a.active").index()<$(".thumbslider a").length-1)
		$(".textlinks .next").fadeIn();
	if($(".thumbslider a.active").index()>0)
		$(".textlinks .previous").fadeIn();
	//disablers
	if($(".thumbslider a.active").index()>=$(".thumbslider a").length-1)
		$(".textlinks .next").fadeOut();
	if($(".thumbslider a.active").index()<=0)
		$(".textlinks .previous").fadeOut();
	
		
		
}
