// jquery photo rotator

var current;
var next;
var myTimer;
var direction = 1;

function rotateDivs(){
	clearTimeout(myTimer);

	current = $('#galleryPictureInterior img.current');
	next = $(current).next().length > 0 ? $(current).next() : $('#galleryPictureInterior img:first');
	
	$(current).trigger('fadeBack');
	$(next).trigger('fadeFront');
	
	myTimer = setTimeout('rotateDivs()',6000);
}

function moveIt(obj) {
			var newWidth = parseInt($(obj).width()*1.08);
			var newHeight = parseInt($(obj).height()*1.08);
			var oldWidth = $(obj).width();
			var oldHeight = $(obj).height();

			if (direction) {
				$(obj).css('left',0).css('top',0);
				$(obj).animate({width: newWidth + 'px' , height: newHeight + 'px'},6000);
//				direction = 0;
			}
			else {
				$(obj).css('left',0).css('top',0).css('height',newHeight+'px').css('width',newWidth+'px');
				$(obj).animate({width: oldWidth + 'px' , height: oldHeight + 'px'},6000);
				direction = 1;
			}
}

$(document).ready( function(){
	moveIt($('#galleryPictureInterior img.current'));
	myTimer = setTimeout('rotateDivs()',6000);
	
 	$('#galleryPictureInterior img').bind( 'fadeBack', function(){
		
		var id = $(this).attr('id').replace('pho','');
		$('#inf'+id).removeClass('current').addClass('next');
		
		$(this).fadeOut(1000, function(){
			$(this).removeClass('current').addClass('next');
			$(this).attr('style','');
		});
		
 	}).bind( 'fadeFront', function(){
 	
 	  var id = $(this).attr('id').replace('pho','');
		$('#inf'+id).removeClass('next').addClass('current');
		
		$('.galleryButton').attr('src', 'images/galleryOff.jpg');
		$('#bul'+id).attr('src', 'images/galleryOn.jpg');
		
		$(this).fadeIn(1000, function(){
			$(this).removeClass('next').addClass('current');
			moveIt($(this));
		}); 	
		
 	});
});

