var imgRotateAmt = 0;
var imgCur = 1;

var galleryRotateAmt = 0;
var galleryCur = 1;
var thumbImgGrp = 1;
//Timer scripts
dropImgTimers = function(){
	clearTimeout(imgInt);
}
initateImgTimer = function(){
	imgInt = setTimeout(imgSwitch,3000);
}

imgSwitch = function(){
	dropImgTimers();
	$('#ir-'+imgCur).animate({
		opacity: 'toggle'
	}, 1000, function() {
	});
	imgCur++;
	if(imgCur > imgRotateAmt){
		imgCur =1;
	}
	$('#ir-'+imgCur).animate({
		opacity: 'toggle'
	}, 1000, function() {
		initateImgTimer();
	});
}

shiftThumbs = function(pos) {
	var newPos = -((pos-1)*6*(156+3));
	$('#thumbinner').stop().animate({
				left: newPos
			}, 600, function() {
				//
	});
};

$(function() {
	//shift middle
	var windowH = $(this).height();
	if(windowH > 680){
		//shift
		var changeHt = (windowH-680)/2;
		//changeHt = 30;
		$('#header').css('margin-top',changeHt);
	}
	//scrolling
	if($('#pagescroll').size() > 0){
		$('#pagescroll').jScrollPane();
	}
	
	
	imgRotateAmt = $('.imgrotate').size();
	galleryRotateAmt = $('.galleryimgclip').size();
	$('#ir-1').fadeIn(1000,function(){
		if(imgRotateAmt > 1){
			initateImgTimer();
		}
	});	
	
	//gallery
	var galleryCount = 1;
	
	if(galleryRotateAmt > 1) {
		var thpanels = '<div id="thumbshiftleft">&nbsp;</div><div id="thumbouter"><div id="thumbinner">';
		$('.galleryimgclip').each(function() {
			var thumbimg = $(this).children('img').attr('src');
			thumbimg = thumbimg.replace('gallery/','gallery/th');
			var thumbdes = $(this).children('img').attr('alt');
			var thumbpanel = '<div id="th-'+galleryCount+'" class="nthumb"><div class="thumbimginner"><img src="'+thumbimg+'"/></div><p>'+thumbdes+'</p></div>';
			thpanels = thpanels + thumbpanel;
			$(this).attr('id','galimg-'+galleryCount);
			galleryCount++;
		});
		thpanels = thpanels + '</div></div><div id="thumbshiftright">&nbsp;</div><div class="clear"><!-- clear --></div>';
		$('#gallerythumbs').html(thpanels);
		$('#thumbinner').css('width',galleryCount*(156+3));
		
		//thumb hovers
		$('.nthumb').find('img').css('opacity',0.5);
		$('.nthumb').hover(function() {
			var nCur = parseInt($(this).attr('id').split('-')[1]);
			if(nCur != galleryCur) {
				$(this).find('img').stop().animate({
					opacity: 1
				}, 300, function() {
					//
				});
			}
		},function(){
			var nCur = parseInt($(this).attr('id').split('-')[1]);
			if(nCur != galleryCur) {
				$(this).find('img').stop().animate({
					opacity: 0.5
				}, 300, function() {
					//
				});
			}
		});
		$('.nthumb').click(function(){
			var nCur = parseInt($(this).attr('id').split('-')[1]);
			if(nCur != galleryCur) {
				//old thumb
				$('#th-'+galleryCur).find('img').stop().animate({
					opacity: 0.5
				}, 300, function() {
					//
				});
				$('#galimg-'+galleryCur).stop().fadeOut();
				$('#galimg-'+nCur).stop().fadeIn();
				galleryCur = nCur;
			}
		});
		if(galleryRotateAmt > 6) {
			$('#thumbshiftleft').click(function(){
				var nClick = thumbImgGrp -1;
				if(nClick < 1){
					nClick = 1;
				}
				if(nClick != thumbImgGrp) {
					shiftThumbs(nClick);
					thumbImgGrp = nClick;
				}
			});
			$('#thumbshiftright').click(function(){
				var nClick = thumbImgGrp +1;
				var mClick = Math.ceil(galleryRotateAmt/6);
				if(nClick > mClick){
					nClick = mClick;
				}
				if(nClick != thumbImgGrp) {
					shiftThumbs(nClick);
					thumbImgGrp = nClick;
				}
			});
			$('#thumbshiftleft').css('cursor','pointer');
			$('#thumbshiftright').css('cursor','pointer');
		} else {
			$('#thumbshiftleft').css('background','none');
			$('#thumbshiftright').css('background','none');
		}
	}
	$('#galimg-1').stop().fadeIn();
	$('#th-1 img').css('opacity',1);
});