//init page
$(function(){
	initOpenClose();
	initSlideShow();
});

//init open close
function initOpenClose(){
	var speed = 500;
	$('.info-box').each(function(){
		var hold = jQuery(this);
		var linkOpen = hold.find('a.book');
		var slideBox = hold.find('.info');
		var marginVal = -slideBox.outerHeight();
		var t1;
		slideBox.css({marginTop: marginVal});
		
		linkOpen.mouseenter(function(){
			if(t1) clearTimeout(t1);
			slideBox.stop().animate({marginTop: 0}, {duration: speed});
		});
		hold.mouseenter(function(){
			if(t1) clearTimeout(t1);
		}).mouseleave(function(){
			if(t1) clearTimeout(t1);
			t1 = setTimeout(function(){
				slideBox.stop().animate({marginTop: marginVal}, {duration: speed});
			}, 500);
		});
		$("#calendar").hover(function(){
			if(t1) clearTimeout(t1);
			slideBox.stop().animate({marginTop: 0}, {duration: speed});
		});
	});
}

//init slideshow
function initSlideShow(){
	var animSpeed = 1000;
	var switchSpeed = 5000;
	$('.gallery').each(function(){
		var gallery = $(this);
		var list = gallery.find('.mask');
		var prevButton = gallery.find('a.next');
		var nextButton = gallery.find('a.prev');
		var items = list.find('> ul > li');
		var currentIndex = 0, prevIndex, direction, animation, t1;
		var maxWidth = 0;
		
		items.each(function(){
			if($(this).outerWidth(true) > maxWidth) {
				maxWidth = $(this).outerWidth(true);
			}
		});
		list.css({height: items.eq(currentIndex).outerHeight(true)});
		
		items.css({left:-maxWidth});
		items.eq(currentIndex).css({left:0});
				
		function switchSlide(){
			if (direction == 0){
				items.eq(prevIndex).stop().animate({left:-maxWidth}, {
					duration: animSpeed,
					complete: function(){
						$(this).css({left:maxWidth});
						autoRotation();
						animation = false;
					}
				});
				items.eq(currentIndex).css({left:maxWidth}).stop().animate({left:0}, animSpeed);
				changeHeight();
			} else {
				items.eq(prevIndex).stop().animate({left:maxWidth}, {
					duration: animSpeed,
					complete: function(){
						$(this).css({left:-maxWidth});
						autoRotation();
						animation = false;
					}
				});
				items.eq(currentIndex).css({left:-maxWidth}).stop().animate({left:0}, animSpeed);
				changeHeight();
			}
		}
		
		function changeHeight() {
			var h =items.eq(currentIndex).outerHeight(true);
			list.stop().animate({height:h}, animSpeed);
		}

		function switchSlidePrev() {
			direction = 0;
			prevIndex = currentIndex;
			if(currentIndex > 0) currentIndex--;
			else currentIndex = items.length-1; 
			switchSlide();
		}
		
		function switchSlideNext() {
			direction = 1;
			prevIndex = currentIndex;
			if(currentIndex < items.length-1) currentIndex++;
			else currentIndex = 0; 
			switchSlide();
		}
		
		prevButton.click(function(){
			if(!animation){
				animation = true;
				if(t1) clearTimeout(t1);
				switchSlidePrev();
			}
			return false;
		});
		nextButton.click(function(){
			if(!animation){
				animation = true;
				if(t1) clearTimeout(t1);	
				switchSlideNext();
			}
			return false;
		});
		
		function autoRotation(){
			if(t1) clearTimeout(t1);
			t1 = setTimeout(switchSlidePrev, switchSpeed);
		}
		autoRotation();
	})
}
