/*var timer_running=false;
var timer_save=false;
function timer_start(){	
	if(!timer_running){
		timer_running=true;
		$('#progressmeter').stop().remove();
		$('div.gallery_thumbs a.current').append('<div id="progressmeter" style="width: 0"></div>').find('#progressmeter').hide().fadeIn('fast').animate({'width':'100%'},{
			duration: 3000,
			easing: 'linear',
			complete: function(){
				if($(this).length>0){
					$(this).remove();
					nextimg();
				}
			}
		});
		//console.log('timer started');
	}	
}
function timer_end(){
	if(timer_running){
		$('#progressmeter').stop().fadeOut('fast', function(){
			$(this).remove();
		});
		timer_running=false;
		//console.log('timer ended');
	} 
}*/
function nextimg(){
	var item=$('div.gallery_thumbs a.current').parent('div.gallery_thumb').next();
	if(item.hasClass('gallery_thumb')){
		item.find('a').click();
	} else {
		$('div.gallery_thumbs a').first().click();
	}
}
$(document).ready(function(){
$('#pagegallery div.gallery_thumbs').html('<div id=\"gallery_scroll\"><div id=\"gallery_wide\">'+$('div.gallery_thumbs').html()+'<\/div><\/div>');//<div id=\"gallery_pointer\"></div>
$('#gallery_scroll').each(function() {
	var totalheight=0;
	$(this).css({'overflow': 'hidden', 'position': 'relative', 'width': 100, 'height': 500});
	$('.gallery_thumb', this).each(function() {
		totalheight += $(this).outerHeight(true);
	});
	$('#gallery_wide', this).css({'width': 100, 'height': totalheight, 'position': 'absolute', 'left': 0, 'top': 0});
});
$('#pagegallery div.gallery_thumbs a').click(function(){
	//timer_save=timer_running;
	//timer_end();
	if(!$(this).hasClass('current')){
		$('div.gallery_thumbs a.current').removeClass('current').css({'opacity':0.5});
		$(this).addClass('current').animate({'opacity':1});
		// scroll thumbs
		var pos=$(this).position().top;
		var tot=$(this).closest('#gallery_wide').height(); 
		var vis=$(this).closest('#gallery_scroll').height(); 
		tot=tot-vis;
		var newpos=(vis/2)-pos-($(this).outerHeight()/2); 
		if(newpos>0){newpos=0;}
		if(newpos<-tot){newpos=-tot;}
		$('#gallery_wide').stop().animate({'top': newpos},{
			duration: 'slow'
		});	
		//$('#gallery_pointer').stop().animate({'top': pos+newpos+32});	
		// change image		
		var url=$(this).attr('href');
		var newimg = new Image(); // this new image will use the onload properly
		var txt = $(this).attr('title');
		newimg.onload = function() {
			$('img.gallery_mainimage').css({'opacity':0}).attr('src',url).animate({'opacity':1},{
				duration: 'slow',
				//complete: function(){if(timer_save){timer_start();}}
			});
			if(txt!=''){
				$('.gallery_wrap div.title').hide().text(txt).fadeIn();
			} else {
				$('.gallery_wrap div.title').hide();
			}
		}
		newimg.src = url;		
	}
	return false;
});
//$('div.gallery_thumbs a').hover(function(){timer_end();},function(){timer_start();});
// add ability to click main image
$('#pagegallery .gallery_frame').css({'cursor':'pointer', 'float': 'left'}).click(function(){nextimg();});
//$('img.gallery_mainimage').hover(function(){timer_end();},function(){timer_start();});
// first settings
$('#pagegallery div.gallery_thumbs a').css({'opacity':0.5}).first().addClass('current').css({'opacity':1});//.click();
//$('#pagegallery div.gallery_thumbs a').css({'opacity':0.5}).first().addClass('current').css({'opacity':1});
//var timer_init = setTimeout('timer_start()', 1000);
});
